Beispiel #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            _portalLanguage = PortalSettings.DefaultLanguage;
            string subject          = string.Empty;
            string reason           = string.Empty;
            bool   sendEmail        = false;
            PayPalIPNParameters ipn = new PayPalIPNParameters(Request.Form, Request.BinaryRead(Request.ContentLength));

            _settings        = new PayPalSettings(StoreSettings.GatewaySettings);
            _verificationURL = _settings.UseSandbox ? SandboxVerificationURL : _settings.VerificationURL;
            // Verify payment with PayPal
            PaymentStatus status = VerifyPayment(ipn);

            // What's the user language?
            _userLanguage = Request.QueryString["language"];
            switch (status)
            {
            case PaymentStatus.Payed:
                int portalId = PortalSettings.PortalId;
                // Set order status to "Paid"...
                OrderInfo order = UpdateOrderStatus(ipn.invoice, OrderInfo.OrderStatusList.Paid, _userLanguage);
                // Add User to Product Roles
                OrderController orderController = new OrderController();
                orderController.AddUserToRoles(portalId, order);
                // Add User to Order Role
                StoreInfo storeSetting = StoreController.GetStoreInfo(portalId);
                if (storeSetting.OnOrderPaidRoleID != Null.NullInteger)
                {
                    orderController.AddUserToPaidOrderRole(portalId, order.CustomerID, storeSetting.OnOrderPaidRoleID);
                }
                break;

            case PaymentStatus.Pending:
                // Inform Store Admin
                subject   = Localization.GetString("StorePayPalGateway", LocalResourceFile, _portalLanguage) + Localization.GetString("IPNInfo", LocalResourceFile, _portalLanguage);
                reason    = Localization.GetString("PendingReason_" + ipn.pending_reason, LocalResourceFile, _portalLanguage);
                sendEmail = true;
                break;

            case PaymentStatus.Refunded:
                // Inform Store Admin
                subject   = Localization.GetString("StorePayPalGateway", LocalResourceFile, _portalLanguage) + Localization.GetString("IPNInfo", LocalResourceFile, _portalLanguage);
                reason    = Localization.GetString("ReasonCode_" + ipn.reason_code, LocalResourceFile, _portalLanguage);
                sendEmail = true;
                break;

            case PaymentStatus.Reversed:
                // Inform Store Admin
                subject   = Localization.GetString("StorePayPalGateway", LocalResourceFile, _portalLanguage) + Localization.GetString("IPNInfo", LocalResourceFile, _portalLanguage);
                reason    = Localization.GetString("ReasonCode_" + ipn.reason_code, LocalResourceFile, _portalLanguage);
                sendEmail = true;
                break;

            case PaymentStatus.Unattended:
                // Alert Store Admin
                subject   = Localization.GetString("StorePayPalGateway", LocalResourceFile, _portalLanguage) + Localization.GetString("IPNAlert", LocalResourceFile, _portalLanguage);
                reason    = Localization.GetString("ReasonCode_" + ipn.reason_code, LocalResourceFile, _portalLanguage);
                sendEmail = true;
                break;

            default:
                break;
            }
            // Do we need to send an email to the store admin?
            if (sendEmail)
            {
                if (string.IsNullOrEmpty(reason))
                {
                    reason = ipn.reason_code;
                }
                string paymentType = Localization.GetString("PaymentType_" + ipn.payment_type, LocalResourceFile, _portalLanguage);
                if (string.IsNullOrEmpty(paymentType))
                {
                    paymentType = ipn.payment_type;
                }
                string paymentStatus = Localization.GetString("PaymentStatus_" + ipn.payment_status, LocalResourceFile, _portalLanguage);
                if (string.IsNullOrEmpty(paymentStatus))
                {
                    paymentStatus = ipn.payment_status;
                }
                string emailIPN = Localization.GetString("EmailIPN", LocalResourceFile, _portalLanguage);
                string body     = string.Format(emailIPN, ipn.invoice, ipn.txn_id, paymentType, paymentStatus, reason);
                SendEmailToAdmin(subject, body);
            }
        }
Beispiel #2
0
        public void ProcessTransaction(IAddressInfo billing, OrderInfo orderInfo, TransactionDetails transaction)
        {
            PayPalSettings settings = new PayPalSettings(_gatewaySettings);

            if (transaction.IsValid())
            {
                CultureInfo ciEnUs = new CultureInfo("en-US");
                _paymentURL = settings.UseSandbox ? SandboxPaymentURL : settings.PaymentURL;
                RemoteForm paypal = new RemoteForm("paypalform", _paymentURL);
                // Main fields
                paypal.Fields.Add("cmd", "_cart");
                paypal.Fields.Add("upload", "1");
                paypal.Fields.Add("business", settings.PayPalID.ToLower());
                paypal.Fields.Add("charset", settings.Charset);
                paypal.Fields.Add("currency_code", settings.Currency);
                paypal.Fields.Add("invoice", orderInfo.OrderID.ToString());
                paypal.Fields.Add("return", transaction.ReturnURL);
                paypal.Fields.Add("cancel_return", transaction.CancelURL);
                paypal.Fields.Add("notify_url", transaction.NotifyURL);
                paypal.Fields.Add("rm", "2");
                paypal.Fields.Add("lc", settings.Lc);
                paypal.Fields.Add("cbt", transaction.Cbt);
                paypal.Fields.Add("custom", orderInfo.CustomerID.ToString());
                paypal.Fields.Add("email", transaction.Email);
                paypal.Fields.Add("first_name", billing.FirstName);
                paypal.Fields.Add("last_name", billing.LastName);
                if (!string.IsNullOrEmpty(billing.Address1))
                {
                    paypal.Fields.Add("address1", billing.Address1);
                }
                if (!string.IsNullOrEmpty(billing.Address2))
                {
                    paypal.Fields.Add("address2", billing.Address2);
                }
                if (!string.IsNullOrEmpty(billing.City))
                {
                    paypal.Fields.Add("city", billing.City);
                }
                if (!string.IsNullOrEmpty(billing.PostalCode))
                {
                    paypal.Fields.Add("zip", billing.PostalCode);
                }
                // Get ISO country code for specified country name
                string country = GetISOCountryCode(billing.CountryCode);
                if (!string.IsNullOrEmpty(country))
                {
                    paypal.Fields.Add("country", country);
                }
                if (!string.IsNullOrEmpty(billing.Phone1))
                {
                    // Remove all chars but numbers from phone number
                    string phonenumber = Regex.Replace(billing.Phone1, "[^\\d]", "", RegexOptions.Compiled);
                    // If the buyer live in the USA
                    if (country == "US")
                    {
                        // Get US postal code for specified region code and add it to the form
                        paypal.Fields.Add("state", GetUSPostalRegionCode(country, billing.RegionCode));
                        // If the phone number is valid
                        int phoneLength = phonenumber.Length;
                        if (phoneLength > 7)
                        {
                            // Extract area code, three digits prefix and four digits phone number
                            paypal.Fields.Add("night_phone_a", phonenumber.Substring(0, phoneLength - 7));
                            paypal.Fields.Add("night_phone_b", phonenumber.Substring(phoneLength - 7, 3));
                            paypal.Fields.Add("night_phone_c", phonenumber.Substring(phoneLength - 4));
                        }
                    }
                    else
                    {
                        // For International buyers, set country code and phone number
                        //paypal.Fields.Add("night_phone_a", country); HERE PHONE country code is required!
                        paypal.Fields.Add("night_phone_b", phonenumber);
                    }
                }
                // Order details
                OrderController        orderController = new OrderController();
                List <OrderDetailInfo> orderDetails    = orderController.GetOrderDetails(orderInfo.OrderID);
                int itemNumber = 1;
                foreach (OrderDetailInfo detail in orderDetails)
                {
                    paypal.Fields.Add("item_number_" + itemNumber, detail.ProductID.ToString());
                    paypal.Fields.Add("item_name_" + itemNumber, detail.ProductTitle);
                    paypal.Fields.Add("quantity_" + itemNumber, detail.Quantity.ToString());
                    paypal.Fields.Add("amount_" + itemNumber, detail.UnitCost.ToString("0.00", ciEnUs));
                    itemNumber++;
                }
                // If a valid coupon exists
                if (orderInfo.CouponID != Null.NullInteger)
                {
                    decimal discount = Math.Abs(orderInfo.Discount);
                    paypal.Fields.Add("discount_amount_cart", discount.ToString("0.00", ciEnUs));
                }
                // Shipping
                if (orderInfo.ShippingCost > 0)
                {
                    paypal.Fields.Add("handling_cart", orderInfo.ShippingCost.ToString("0.00", ciEnUs));
                }
                // Tax
                if (orderInfo.TaxTotal > 0)
                {
                    paypal.Fields.Add("tax_cart", orderInfo.TaxTotal.ToString("0.00", ciEnUs));
                }
                // Post the form to the client browser then submit it to PayPal using JavaScript
                paypal.Post();
            }
        }
Beispiel #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Do we have any special handling?
            PayPalNavigation nav = new PayPalNavigation(Request.QueryString);

            switch (nav.GatewayExit.ToUpper())
            {
            case "CANCEL":
            {
                InvokePaymentCancelled();
                CheckoutControl.Hide();
                pnlProceedToPayPal.Visible = false;
                return;
            }

            case "RETURN":
            {
                PayPalIPNParameters ipn = new PayPalIPNParameters(Request.Form, Request.BinaryRead(Request.ContentLength));
                // Here there is no check about the validity of the PayPal response (IPN),
                // because it's just a message displayed to the customer.
                // Everything is checked in the NOTIFY case received from PayPal in the PayPalIPN.aspx page.
                switch (ipn.payment_status.ToLower())
                {
                case "completed":
                    InvokePaymentSucceeded();
                    break;

                default:
                    InvokePaymentRequiresConfirmation();
                    break;
                }
                CheckoutControl.Hide();
                pnlProceedToPayPal.Visible = false;
                return;
            }
            }

            if (nav.GatewayExit.Length > 0)
            {
                //If the PayPalExit is anything else with length > 0, then don't do any processing
                HttpContext.Current.Response.Redirect(Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID), false);
                return;
            }

            // Continue with display of payment control...
            if (Page.IsPostBack == false)
            {
                PayPalSettings settings = new PayPalSettings(StoreSettings.GatewaySettings);
                if (!settings.IsValid())
                {
                    lblError.Text              = Localization.GetString("GatewayNotConfigured", LocalResourceFile);
                    lblError.Visible           = true;
                    pnlProceedToPayPal.Visible = false;
                    return;
                }

                SurchargePercent = settings.SurchargePercent;
                SurchargeFixed   = settings.SurchargeFixed;

                btnConfirmOrder.Attributes.Add("OnClick", ScriptAvoidDoubleClick(btnConfirmOrder, Localization.GetString("Processing", this.LocalResourceFile)));
                string message = Localization.GetString("lblConfirmMessage", LocalResourceFile);
                lblConfirmMessage.Text = string.Format(message, PortalSettings.PortalName);
                message = Localization.GetString("paypalimage", LocalResourceFile);
                paypalimage.AlternateText = message;

                lblError.Text        = string.Empty;
                lblError.Visible     = false;
                paypalimage.ImageUrl = settings.ButtonURL;
            }
        }