Exemple #1
0
        protected void Submit_Click(object sender, EventArgs e)
        {
            // Create request object
            DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
            DoDirectPaymentRequestDetailsType requestDetails = new DoDirectPaymentRequestDetailsType();
            request.DoDirectPaymentRequestDetails = requestDetails;

            requestDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), paymentType.SelectedValue);

            // Populate card requestDetails
            CreditCardDetailsType creditCard = new CreditCardDetailsType();
            requestDetails.CreditCard = creditCard;
            PayerInfoType payer = new PayerInfoType();
            PersonNameType name = new PersonNameType();
            name.FirstName = firstName.Value;
            name.LastName = lastName.Value;
            payer.PayerName = name;
            creditCard.CardOwner = payer;

            creditCard.CreditCardNumber = creditCardNumber.Value;
            creditCard.CreditCardType = (CreditCardTypeType)
                Enum.Parse(typeof(CreditCardTypeType), creditCardType.SelectedValue);
            creditCard.CVV2 = cvv2Number.Value;
            string[] cardExpiryDetails = cardExpiryDate.Text.Split(new char[] { '/' });
            if (cardExpiryDetails.Length == 2)
            {
                creditCard.ExpMonth = Int32.Parse(cardExpiryDetails[0]);
                creditCard.ExpYear = Int32.Parse(cardExpiryDetails[1]);
            }

            requestDetails.PaymentDetails = new PaymentDetailsType();
            AddressType billingAddr = new AddressType();
            if (firstName.Value != "" && lastName.Value != ""
                && street1.Value != "" && country.Value != "")
            {
                billingAddr.Name = payerName.Value;
                billingAddr.Street1 = street1.Value;
                billingAddr.Street2 = street2.Value;
                billingAddr.CityName = city.Value;
                billingAddr.StateOrProvince = state.Value;
                billingAddr.Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), country.Value);
                billingAddr.PostalCode = postalCode.Value;

                //Fix for release
                billingAddr.Phone = phone.Value;

                payer.Address = billingAddr;
            }

            // Populate payment requestDetails
            CurrencyCodeType currency = (CurrencyCodeType)
                Enum.Parse(typeof(CurrencyCodeType), currencyCode.SelectedValue);
            BasicAmountType paymentAmount = new BasicAmountType(currency, amount.Value);
            requestDetails.PaymentDetails.OrderTotal = paymentAmount;

            // Invoke the API
            DoDirectPaymentReq wrapper = new DoDirectPaymentReq();
            wrapper.DoDirectPaymentRequest = request;
            PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();
            DoDirectPaymentResponseType response = service.DoDirectPayment(wrapper);

            // Check for API return status
            setKeyResponseObjects(service, response);
        }
        protected void Submit_Click(object sender, EventArgs e)
        {
            // Create request object
            DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
            DoDirectPaymentRequestDetailsType requestDetails = new DoDirectPaymentRequestDetailsType();
            request.DoDirectPaymentRequestDetails = requestDetails;

            // (Optional) How you want to obtain payment. It is one of the following values:
            // * Authorization – This payment is a basic authorization subject to settlement with PayPal Authorization and Capture.
            // * Sale – This is a final sale for which you are requesting payment (default).
            // Note: Order is not allowed for Direct Payment.
            requestDetails.PaymentAction = (PaymentActionCodeType)
                Enum.Parse(typeof(PaymentActionCodeType), paymentType.SelectedValue);

            // (Required) Information about the credit card to be charged.
            CreditCardDetailsType creditCard = new CreditCardDetailsType();
            requestDetails.CreditCard = creditCard;
            PayerInfoType payer = new PayerInfoType();
            // (Optional) First and last name of buyer.
            PersonNameType name = new PersonNameType();
            name.FirstName = firstName.Value;
            name.LastName = lastName.Value;
            payer.PayerName = name;
            // (Required) Details about the owner of the credit card.
            creditCard.CardOwner = payer;

            // (Required) Credit card number.
            creditCard.CreditCardNumber = creditCardNumber.Value;
            // (Optional) Type of credit card. For UK, only Maestro, MasterCard, Discover, and Visa are allowable. For Canada, only MasterCard and Visa are allowable and Interac debit cards are not supported. It is one of the following values:
            // * Visa
            // * MasterCard
            // * Discover
            // * Amex
            // * Maestro: See note.
            // Note: If the credit card type is Maestro, you must set currencyId to GBP. In addition, you must specify either StartMonth and StartYear or IssueNumber.
            creditCard.CreditCardType = (CreditCardTypeType)
                Enum.Parse(typeof(CreditCardTypeType), creditCardType.SelectedValue);
            // Card Verification Value, version 2. Your Merchant Account settings determine whether this field is required. To comply with credit card processing regulations, you must not store this value after a transaction has been completed.
            // Character length and limitations: For Visa, MasterCard, and Discover, the value is exactly 3 digits. For American Express, the value is exactly 4 digits.
            creditCard.CVV2 = cvv2Number.Value;
            string[] cardExpiryDetails = cardExpiryDate.Text.Split(new char[] { '/' });
            if (cardExpiryDetails.Length == 2)
            {
                // (Required) Credit card expiration month.
                creditCard.ExpMonth = Convert.ToInt32(cardExpiryDetails[0]);
                // (Required) Credit card expiration year.
                creditCard.ExpYear = Convert.ToInt32(cardExpiryDetails[1]);
            }

            requestDetails.PaymentDetails = new PaymentDetailsType();
            // (Optional) Your URL for receiving Instant Payment Notification (IPN) about this transaction. If you do not specify this value in the request, the notification URL from your Merchant Profile is used, if one exists.
            // Important: The notify URL applies only to DoExpressCheckoutPayment. This value is ignored when set in SetExpressCheckout or GetExpressCheckoutDetails.
            requestDetails.PaymentDetails.NotifyURL = ipnNotificationUrl.Value.Trim();

            // (Optional) Buyer's shipping address information.
            AddressType billingAddr = new AddressType();
            if (firstName.Value != string.Empty && lastName.Value != string.Empty
                && street1.Value != string.Empty && country.Value != string.Empty)
            {
                billingAddr.Name = payerName.Value;
                // (Required) First street address.
                billingAddr.Street1 = street1.Value;
                // (Optional) Second street address.
                billingAddr.Street2 = street2.Value;
                // (Required) Name of city.
                billingAddr.CityName = city.Value;
                // (Required) State or province.
                billingAddr.StateOrProvince = state.Value;
                // (Required) Country code.
                billingAddr.Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), country.Value);
                // (Required) U.S. ZIP code or other country-specific postal code.
                billingAddr.PostalCode = postalCode.Value;

                // (Optional) Phone number.
                billingAddr.Phone = phone.Value;

                payer.Address = billingAddr;
            }

            // (Required) The total cost of the transaction to the buyer. If shipping cost and tax charges are known, include them in this value. If not, this value should be the current subtotal of the order. If the transaction includes one or more one-time purchases, this field must be equal to the sum of the purchases. This field must be set to a value greater than 0.
            // Note: You must set the currencyID attribute to one of the 3-character currency codes for any of the supported PayPal currencies.
            CurrencyCodeType currency = (CurrencyCodeType)
                Enum.Parse(typeof(CurrencyCodeType), currencyCode.SelectedValue);
            BasicAmountType paymentAmount = new BasicAmountType(currency, amount.Value);
            requestDetails.PaymentDetails.OrderTotal = paymentAmount;

            // Invoke the API
            DoDirectPaymentReq wrapper = new DoDirectPaymentReq();
            wrapper.DoDirectPaymentRequest = request;

            // Configuration map containing signature credentials and other required configuration.
            // For a full list of configuration parameters refer in wiki page
            // [https://github.com/paypal/sdk-core-dotnet/wiki/SDK-Configuration-Parameters]
            Dictionary<string, string> configurationMap = Configuration.GetAcctAndConfig();

            // Create the PayPalAPIInterfaceServiceService service object to make the API call
            PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(configurationMap);

            // # API call
            // Invoke the DoDirectPayment method in service wrapper object
            DoDirectPaymentResponseType response = service.DoDirectPayment(wrapper);

            // Check for API return status
            setKeyResponseObjects(service, response);
        }
        public ActionResult AddressAndPayment(FormCollection values)
        {
            // fill out basic info
            var cart = ShoppingCartHelper.GetCart(HttpContext);
            ViewData["cart"] = cart;
            var deliveryLocation = LocationHelper.GetDeliveryLocation(HttpContext);
            if (deliveryLocation == null)
            {
                deliveryLocation = db.University_Delivery.First(i => i.UniversityId == 1);
                LocationHelper.SetDeliveryLocation(HttpContext, deliveryLocation);
            }
            ViewData["deliveryLocation"] = deliveryLocation;
            DateTime deliveryTime;
            if (deliveryLocation.DeliveryTime.Hour < DateTime.Now.Hour)
            {
                deliveryTime = DateTime.Now.Date.AddDays(1).AddHours(deliveryLocation.DeliveryTime.Hour);
            }
            else
            {
                deliveryTime = DateTime.Now.Date.AddHours(deliveryLocation.DeliveryTime.Hour);
            }
            ViewData["deliveryTime"] = deliveryTime;
            var totalRewardPoints = db.Rewards.Sum(i => i.Amount);
            ViewData["totalRewardPoints"] = totalRewardPoints;

            //fillout order
            var order = new Order();
            var userId = MembershipHelper.GetUserIdByEmail(HttpContext.User.Identity.Name);
            order.Gross = cart.Gross;
            if (cart.CanUserRewardPoint)
            {
                order.rewardPoints = Convert.ToInt32(values["rewardPoints"]);
            }
            else
            {
                order.rewardPoints = 0;
            }
            order.Savings = order.rewardPoints / 100.0m;
            order.Tax = 0.0m;
            order.Fee = 0.0m;
            order.billingFirstName = values["billingFirstName"];
            order.billingLastName = values["billingLastName"];
            order.billingAddress1 = values["billingAddress1"];
            order.billingAddress2 = values["billingAddress2"];
            order.billingCity = values["billingCity"];
            order.billingState = values["billingState"];
            order.billingZipCode = values["billingZipCode"];
            order.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), values["paymentType"]);
            order.cardNumber = values["cardNumber"];
            order.cardExpYear = int.Parse(values["cardExpYear"]);
            order.cardExpMonth = int.Parse(values["cardExpMonth"]);
            order.CSV = values["CSV"];
            order.NeedDeliveryInfo = cart.NeedDeliveryInfo;
            if (cart.NeedDeliveryInfo)
            {
                order.ReceiverFirstName = values["ReceiverFirstName"];
                order.ReceiverLastName = values["ReceiverLastName"];
                order.ReceiverPhoneNumber = values["ReceiverPhoneNumber"];
                order.DeliveryTime = deliveryTime;
            }
            order.PayerEmail = HttpContext.User.Identity.Name;
            order.UserId = userId.Value;
            order.PayerEmail = User.Identity.Name;
            order.OrderDescription = string.Join(",", cart.ShoppingCartItems.Select(i => i.Description).ToList());
            order.DeliveryLocationId = deliveryLocation.LocationId;
            PaymentStatusLevel paymentStatus = PaymentStatusLevel.WaitingForPayment;
            DeliveryInfo deliveryInfo = new DeliveryInfo() { ReceiverFirstName = order.ReceiverFirstName, ReceiverLastName = order.ReceiverLastName };

            if (!TryValidateModel(order))
            {
                return View("AddressAndPayment", "_CheckoutMaster", order);
            }
            else
            {

                if (order.PaymentType == PaymentType.CreditCard)
                {

                    DoDirectPaymentRequestType request = new DoDirectPaymentRequestType();
                    DoDirectPaymentRequestDetailsType requestDetails = new DoDirectPaymentRequestDetailsType();
                    request.DoDirectPaymentRequestDetails = requestDetails;
                    requestDetails.PaymentAction = PaymentActionCodeType.SALE;

                    // Populate card requestDetails
                    CreditCardDetailsType creditCard = new CreditCardDetailsType();
                    requestDetails.CreditCard = creditCard;
                    PayerInfoType payer = new PayerInfoType();
                    PersonNameType name = new PersonNameType();
                    name.FirstName = order.billingFirstName;
                    name.LastName = order.billingLastName;
                    payer.PayerName = name;
                    creditCard.CardOwner = payer;

                    creditCard.CreditCardNumber = order.cardNumber;
                    creditCard.CreditCardType = (CreditCardTypeType)
                        Enum.Parse(typeof(CreditCardTypeType), values["creditCardType"]);
                    creditCard.CVV2 = order.CSV;
                    creditCard.ExpMonth = order.cardExpMonth;
                    creditCard.ExpYear = order.cardExpYear;
                    requestDetails.PaymentDetails = new PaymentDetailsType();
                    AddressType billingAddr = new AddressType();
                    billingAddr.Name = order.billingFirstName + " " + order.billingLastName;
                    billingAddr.Street1 = order.billingAddress1;
                    billingAddr.Street2 = order.billingAddress2;
                    billingAddr.CityName = order.billingCity;
                    billingAddr.StateOrProvince = order.billingState;
                    billingAddr.Country = CountryCodeType.US;
                    billingAddr.PostalCode = order.billingZipCode;
                    payer.Address = billingAddr;
                    // Populate payment requestDetails
                    CurrencyCodeType currency = (CurrencyCodeType)
                        Enum.Parse(typeof(CurrencyCodeType), "USD");
                    BasicAmountType paymentAmount = new BasicAmountType(currency, order.FinalAmount.ToString());
                    requestDetails.PaymentDetails.OrderTotal = paymentAmount;

                    // Invoke the API
                    DoDirectPaymentReq wrapper = new DoDirectPaymentReq();
                    wrapper.DoDirectPaymentRequest = request;

                    PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();

                    DoDirectPaymentResponseType response = service.DoDirectPayment(wrapper);

                    BillingInfo billingInfo = new BillingInfo()
                    {
                        BillingFirstName = order.billingFirstName,
                        BillingLastName = order.billingLastName,
                        BillingAddress1 = order.billingAddress1,
                        BillingAddress2 = order.billingAddress2,
                        City = order.billingCity,
                        State = order.billingState,
                        ZipCode = order.billingZipCode,
                        CountryCode = CountryCodeType.US
                    };

                    if (response.Ack.Equals(AckCodeType.FAILURE) ||
                        (response.Errors != null && response.Errors.Count > 0))
                    {
                        paymentStatus = PaymentStatusLevel.WaitingForPayment;
                    }
                    else
                    {
                        paymentStatus = PaymentStatusLevel.Paid;
                    }

                    OnPaymentComplete(order.PaymentType, paymentStatus, billingInfo, deliveryInfo, order);

                    if (order.OrderId == 0)
                    {
                        ViewBag.Errors = string.Join(",", response.Errors.Select(i => i.LongMessage).ToList());
                        ViewData["Errors"] = string.Join(",", response.Errors.Select(i => i.LongMessage).ToList());
                        return View("AddressAndPayment", "_CheckoutMaster", order);
                    }
                    return RedirectToAction("PaymentComplete", new { orderId = order.OrderId });
                }
                else if(order.PaymentType == PaymentType.Cash) {
                    paymentStatus = PaymentStatusLevel.WaitingForPayment;
                    OnPaymentComplete(order.PaymentType, paymentStatus, null, deliveryInfo, order);
                    return RedirectToAction("PaymentComplete", new { orderId = order.OrderId });
                }
                else
                {
                    OnPaymentComplete(order.PaymentType, PaymentStatusLevel.Paid, null, deliveryInfo, order);

                    return RedirectToAction("PaymentComplete", new { orderId = order.OrderId });
                }
            }
        }