Exemplo n.º 1
0
        /// <summary>
        /// Does the express checkout.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="authorizeOnly">if set to <c>true</c> [authorize only].</param>
        /// <returns></returns>
        internal Transaction DoExpressCheckout(Order order, bool authorizeOnly)
        {
            Transaction transaction = null;

              DoExpressCheckoutPaymentReq expressCheckoutPayment = new DoExpressCheckoutPaymentReq();
              DoExpressCheckoutPaymentRequestType expressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType();
              DoExpressCheckoutPaymentRequestDetailsType expressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType();

              PaymentDetailsType paymentDetails = new PaymentDetailsType();
              if(authorizeOnly) {
            expressCheckoutPaymentRequestDetails.PaymentAction = PaymentActionCodeType.Authorization;
              }
              else {
            expressCheckoutPaymentRequestDetails.PaymentAction = PaymentActionCodeType.Sale;
              }

              expressCheckoutPaymentRequestDetails.Token = order.ExtendedProperties[PAYPAL_TOKEN].ToString();
              expressCheckoutPaymentRequestDetails.PayerID = order.ExtendedProperties[PAYPAL_PAYER_ID].ToString();

              expressCheckoutPaymentRequest.Version = PayPalServiceUtility.PayPalVersionNumber;

              #region Basic Payment Information

              Currency currency = new Currency();
              decimal total = GetOrderTotal(currency, order);
              //Required
              paymentDetails.OrderTotal = GetBasicAmount(total);

              //Optional (see DoDirectPayment for additional notes)
              paymentDetails.ItemTotal =
            GetBasicAmount(decimal.Round(order.SubTotal, currency.CurrencyDecimals));

              paymentDetails.ShippingTotal =
            GetBasicAmount(decimal.Round(order.ShippingAmount, currency.CurrencyDecimals));

              paymentDetails.TaxTotal =
            GetBasicAmount(decimal.Round(order.TaxTotal, currency.CurrencyDecimals));

              paymentDetails.HandlingTotal =
            GetBasicAmount(decimal.Round(order.HandlingAmount, currency.CurrencyDecimals));

              //Note: You can use this value for whatever purpose, such as an accounting tracking number
              //or additional data needed by your program.
              paymentDetails.Custom = order.OrderNumber;
              paymentDetails.ButtonSource = Constants.EC_BN_ID;

              #endregion

              #region ShipTo Address

              //Load up The ShipTo Address
              paymentDetails.ShipToAddress = new PayPalSvc.AddressType();
              paymentDetails.ShipToAddress.Name =
            order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName;
              paymentDetails.ShipToAddress.Street1 =
            order.ShippingAddress.Address1;
              paymentDetails.ShipToAddress.Street2 =
            order.ShippingAddress.Address2;
              paymentDetails.ShipToAddress.CityName =
            order.ShippingAddress.City;
              paymentDetails.ShipToAddress.StateOrProvince =
            order.ShippingAddress.StateOrRegion;
              paymentDetails.ShipToAddress.Country =
            (CountryCodeType)Enum.Parse(typeof(CountryCodeType), order.ShippingAddress.Country);
              paymentDetails.ShipToAddress.CountrySpecified = true;
              paymentDetails.ShipToAddress.PostalCode =
            order.ShippingAddress.PostalCode;

              #endregion

              #region Load Individual Items

              //Add the individual items if they are available
              //This will also add nice line item entries to the PayPal invoice - good for customers!
              OrderItemCollection orderItemCollection = order.OrderItemRecords();
              if(orderItemCollection != null) {
            if(orderItemCollection.Count > 0) {
              paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[orderItemCollection.Count];
              PaymentDetailsItemType[] paymentDetailsItems = new PaymentDetailsItemType[orderItemCollection.Count];
              PaymentDetailsItemType item = null;
              for(int i = 0;i < orderItemCollection.Count;i++) {
            item = new PaymentDetailsItemType();
            string sku = orderItemCollection[i].Sku.Trim();
            if(sku.Length > 127) {
              sku = sku.Substring(0, 126);
            }
            item.Name = orderItemCollection[i].Name;
            item.Number = sku;
            item.Amount = GetBasicAmount(decimal.Round(orderItemCollection[i].PricePaid, currency.CurrencyDecimals));
            item.Quantity = orderItemCollection[i].Quantity.ToString();
            item.Tax = GetBasicAmount(decimal.Round(orderItemCollection[i].ItemTax, currency.CurrencyDecimals));
            paymentDetailsItems[i] = item;
              }
              paymentDetails.PaymentDetailsItem = paymentDetailsItems;
            }
              }

              #endregion

              //Now load it all up
              expressCheckoutPayment.DoExpressCheckoutPaymentRequest = expressCheckoutPaymentRequest;
              expressCheckoutPayment.DoExpressCheckoutPaymentRequest.DoExpressCheckoutPaymentRequestDetails = expressCheckoutPaymentRequestDetails;
              expressCheckoutPayment.DoExpressCheckoutPaymentRequest.DoExpressCheckoutPaymentRequestDetails.PaymentDetails[0] = paymentDetails;

              #region Charge and Validation
              //Finally, send it
              DoExpressCheckoutPaymentResponseType expressCheckoutPaymentResponse =
            payPalAPIAASoapBinding.DoExpressCheckoutPayment(expressCheckoutPayment);
              //Validate the response
              string errorList = ValidateResponse(expressCheckoutPaymentResponse);
            //      if(expressCheckoutPaymentResponse.Ack != AckCodeType.Success && expressCheckoutPaymentResponse.Ack != AckCodeType.SuccessWithWarning) {
              if(IsValidResponse(expressCheckoutPaymentResponse.Ack)){
            if(!string.IsNullOrEmpty(errorList)) {
              throw new PayPalServiceException(errorList);
            }
              }
              #endregion

              #region Transaction

              transaction = new Transaction();
              transaction.OrderId = order.OrderId;
              if(!authorizeOnly) {
            transaction.TransactionTypeDescriptorId = (int)TransactionType.Charge;
              }
              else {
            transaction.TransactionTypeDescriptorId = (int)TransactionType.Authorization;
              }
              transaction.PaymentMethod = order.CreditCardType.ToString();
              transaction.GatewayName = PAYPAL_EXPRESSCHECKOUT;
              string gatewayResponse = expressCheckoutPaymentResponse.Ack.ToString();
              if(expressCheckoutPaymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus == PaymentStatusCodeType.Pending) {
            gatewayResponse += "::Pending";
              }
              transaction.GatewayResponse = gatewayResponse;
              transaction.GatewayTransactionId = expressCheckoutPaymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID;
              transaction.GrossAmount = Convert.ToDecimal(expressCheckoutPaymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].GrossAmount.Value);
              transaction.FeeAmount = expressCheckoutPaymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].FeeAmount != null ? Convert.ToDecimal(
            expressCheckoutPaymentResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].FeeAmount.Value) : 0M;
              transaction.TransactionDate = DateTime.Now;
              transaction.GatewayErrors = errorList;
              transaction.Save("System");

              #endregion

              return transaction;
        }