public PayPalPaymentStatus DoExpressCheckoutPayment(tbl_Orders order, string token, string payerId, string payPalCurrencyCode, string payPalReturnUrl, string payPalCancelUrl, string payPalUser, string payPalPassword, string payPalSignature, string payPalNvpSetExpressUrl, string invoiceID) { string resultToLog; NVPCodec encoder = InitializeEncoder(order); encoder[PayPalConsts.Method] = "DoExpressCheckoutPayment"; encoder[PayPalConsts.Token] = token; encoder[PayPalConsts.PaymentAction] = "Sale"; encoder[PayPalConsts.PayerId] = payerId; encoder[PayPalConsts.CurrencyCode] = payPalCurrencyCode; encoder[PayPalConsts.ReturnUrl] = payPalReturnUrl; encoder[PayPalConsts.CancelUrl] = payPalCancelUrl; encoder[PayPalConsts.User] = payPalUser; encoder[PayPalConsts.Pwd] = payPalPassword; encoder[PayPalConsts.Signature] = payPalSignature; encoder[PayPalConsts.InvoiceID] = invoiceID; string encoded = encoder.Encode(); string result = resultToLog = HttpPost(payPalNvpSetExpressUrl, encoded, 30000); NVPCodec decoder = new NVPCodec(); decoder.Decode(result); PayPalPaymentStatus payPalStatus = new PayPalPaymentStatus(); payPalStatus.TransactionID = decoder[PayPalConsts.TransactionID]; payPalStatus.Token = decoder[PayPalConsts.Token]; payPalStatus.ACK = decoder[PayPalConsts.ACK]; payPalStatus.PaymentStatus = decoder[PayPalConsts.PaymentStatus]; payPalStatus.PendingReason = decoder[PayPalConsts.PendingReason]; payPalStatus.ErrorCode = decoder[PayPalConsts.ErrorCode]; payPalStatus.ErrorMessage = decoder[PayPalConsts.ErrorMessage]; if (payPalStatus.ACK.ToLower() != "success") { ECommerceService.UpdateOrderPaymentStatus(order.OrderID, BL.PaymentStatus.PayPal_DoExpressCheckout_Failure); resultToLog = Server.UrlDecode(resultToLog).Replace("&", Environment.NewLine); Log.Error(String.Format("PayPal payment - DoExpressCheckoutPayment failed: {0}", resultToLog)); } return payPalStatus; }
private NVPCodec InitializeEncoder(tbl_Orders order) { decimal totalItemsPrice = order.tbl_OrderContent.Sum(oc => oc.OC_TotalPrice) + order.DependentOrders.Sum(o => o.TotalAmount); string totalAmount = Math.Round(order.TotalAmountToPay, 2).ToString().Replace(',', '.'); string totalTaxAmount = Math.Round(order.TotalTaxAmount, 2).ToString().Replace(',', '.'); string totalItemsAmount = order.O_IsCustomAmount ? order.TotalAmountToPay.ToString().Replace(',', '.') : Math.Round(totalItemsPrice, 2).ToString().Replace(',', '.'); string totalShippingAmount = Math.Round(order.TotalDeliveryAmount, 2).ToString().Replace(',', '.'); NVPCodec encoder = new NVPCodec(); encoder[PayPalConsts.Version] = "98.0"; encoder[PayPalConsts.TotalAmount] = totalAmount; encoder[PayPalConsts.DeliveryAmount] = totalShippingAmount; encoder[PayPalConsts.ItemsTotalAmount] = totalItemsAmount; if (order.DiscountAmount != 0) { decimal discount = order.DiscountAmount > totalItemsPrice ? totalItemsPrice : order.DiscountAmount; encoder[PayPalConsts.DiscountAmount] = Math.Round(-Math.Abs(discount), 2).ToString().Replace(',', '.'); } // if order items should be listed in order details displayed on PayPal account if (DomainService.GetSettingsValueAsBool(BL.SettingsKey.payPalSendOrderItems, this.DomainID) && !order.O_IsCustomAmount) { int i = 0; foreach (var oc in order.tbl_OrderContent) { encoder[PayPalConsts.GetItemNumberKey(i)] = oc.tbl_Products==null ? oc.tbl_ProductPrice.PR_ProductID.ToString() : oc.tbl_Products.P_ProductCode; encoder[PayPalConsts.GetItemNameKey(i)] = oc.OC_Title ?? ""; encoder[PayPalConsts.GetItemDescriptionKey(i)] = oc.OC_Description ?? ""; encoder[PayPalConsts.GetItemQuantityKey(i)] = oc.OC_Quantity.ToString(); encoder[PayPalConsts.GetItemsTotalAmountKey(i)] = Math.Round(oc.OC_TotalPrice / oc.OC_Quantity.GetValueOrDefault(1), 2).ToString().Replace(',', '.'); i++; } if (order.DependentOrders.Any()) { foreach (var donation in order.DependentOrders) { encoder[PayPalConsts.GetItemNumberKey(i)] = donation.OrderID.ToString(); encoder[PayPalConsts.GetItemNameKey(i)] = String.Format("Donation to order {0}", order.OrderID); encoder[PayPalConsts.GetItemDescriptionKey(i)] = "donation"; encoder[PayPalConsts.GetItemQuantityKey(i)] = "1"; encoder[PayPalConsts.GetItemsTotalAmountKey(i)] = Math.Round(donation.TotalAmount, 2).ToString().Replace(',', '.'); i++; } } } return encoder; }
private string SetExpressCheckout(tbl_Orders order, string payPalReturnUrl, string payPalCancelUrl, string payPalUser, string payPalPassword, string payPalSignature, string payPalCurrencyCode, string payPalNvpSetExpressUrl, string payPalHost, string invoiceID, string languageCode) { string resultToLog; NVPCodec encoder = InitializeEncoder(order); encoder[PayPalConsts.Method] = "SetExpressCheckout"; encoder[PayPalConsts.NoShipping] = "1"; encoder[PayPalConsts.ReturnUrl] = payPalReturnUrl; encoder[PayPalConsts.CancelUrl] = payPalCancelUrl; encoder[PayPalConsts.User] = payPalUser; encoder[PayPalConsts.Pwd] = payPalPassword; encoder[PayPalConsts.Signature] = payPalSignature; encoder[PayPalConsts.CurrencyCode] = payPalCurrencyCode; encoder[PayPalConsts.InvoiceID] = invoiceID; if (!string.IsNullOrEmpty(languageCode)) { encoder[PayPalConsts.LocaleCode] = languageCode; } string settings = encoder.GetSettings(); Log.Info(settings); string encoded = encoder.Encode(); string result = resultToLog = HttpPost(payPalNvpSetExpressUrl, encoded, 30000); NVPCodec decoder = new NVPCodec(); decoder.Decode(result); string token = decoder[PayPalConsts.Token]; if (decoder[PayPalConsts.ACK].ToLower() == "success" || decoder[PayPalConsts.ACK].ToLower() == "successwithwarning") { ECommerceService.UpdateOrderPaymentStatus(order.OrderID, BL.PaymentStatus.PayPal_SetExpressCheckout_Success); ECommerceService.UpdateOrderSecurityKey(token, order.OrderID); return String.Format("{0}?cmd=_express-checkout&{1}={2}&{3}={4}", payPalHost, PayPalConsts.Token.ToLower(), token, PayPalConsts.Useraction, PayPalConsts.Commit); } else { ECommerceService.UpdateOrderPaymentStatus(order.OrderID, BL.PaymentStatus.PayPal_SetExpressCheckout_Failure); resultToLog = Server.UrlDecode(resultToLog).Replace("&", Environment.NewLine); Log.Error(String.Format("PayPal payment - SetExpressCheckout failed: {0}", resultToLog)); } return String.Empty; }
private NVPCodec InitializeEncoder(tbl_Orders order, string currencyCode) { NVPCodec encoder = new NVPCodec(); encoder[SecureTradingConsts.SiteReference] = GetReferenceSite(order.O_DomainID); encoder[SecureTradingConsts.CurrencyIso3a] = currencyCode; encoder[SecureTradingConsts.MainAmount] = order.TotalAmountToPay.ToString("C"); encoder[SecureTradingConsts.Version] = "1"; encoder[SecureTradingConsts.OrderReference] = order.OrderID.ToString(); //encoder[SecureTradingConsts.BillingPremise] if (order.BillingAddress1 != null) encoder[SecureTradingConsts.BillingStreet] = order.BillingAddress1; if (order.BillingCity != null) encoder[SecureTradingConsts.BillingTown] = order.BillingCity; if (order.BillingState != null) encoder[SecureTradingConsts.BillingCounty] = order.BillingState; if (order.BillingPostCode != null) encoder[SecureTradingConsts.BillingPostCode] = order.BillingPostCode; if (order.BillingFirstnames != null) encoder[SecureTradingConsts.BillingFirstName] = order.BillingFirstnames; if (order.BillingSurname != null) encoder[SecureTradingConsts.BillingLastName] = order.BillingSurname; if (order.BillingCountry != null) encoder[SecureTradingConsts.BillingCountryIso2a] = order.BillingCountry; if (order.CustomerEMail != null) encoder[SecureTradingConsts.BillingEmail] = order.CustomerEMail; if (order.DeliveryAddress1 != null) encoder[SecureTradingConsts.CustomerStreet] = order.IsDeliverable ? order.DeliveryAddress1 : order.BillingAddress1; if (order.DeliveryCity != null) encoder[SecureTradingConsts.CustomerTown] = order.IsDeliverable ? order.DeliveryCity : order.BillingCity; if (order.DeliveryState != null) encoder[SecureTradingConsts.CustomerCounty] = order.IsDeliverable ? order.DeliveryState : order.BillingState; if (order.DeliveryPostCode != null) encoder[SecureTradingConsts.CustomerPostCode] = order.IsDeliverable ? order.DeliveryPostCode : order.BillingPostCode; if (order.DeliveryCountry != null) encoder[SecureTradingConsts.CustomerCountryIso2a] = order.IsDeliverable ? order.DeliveryCountry : order.BillingCountry; encoder[SecureTradingConsts.SiteSecurity] = CreateRequestSecurityCode(order, currencyCode); return encoder; }