我正在查看PayPal提供的各种集成,因为我只想提供PayPal作为付款选项,我将使用Express Checkout。话虽如此,似乎可以通过多种方式进行整合:
我遇到的问题是.NET SDK是经典API集的一部分,显然已被弃用(来源:https://github.com/paypal/merchant-sdk-dotnet)。 RESTful API没有提及ExpressCheckout的任何内容,所以我假设 NVP是要走的路。
有人可以澄清最佳路线,因为我不想在事情被弃用时重写它。
有关信息:我使用ASP.NET MVC和C#。
答案 0 :(得分:5)
首先,只有两个API:
PayPal Name-Value Pair API(NVP API)使您可以通过向PayPal发送HTTP请求并使用名称 - 值对指定请求参数来利用PayPal API的功能。 NVP API是PayPal SOAP API的轻量级替代品,可以访问与SOAP API相同的一组功能。
有关Classic API与REST API的几点要考虑:
由于您使用的是.NET SDK,因此可以继续使用它,因为它使用的是Classic API + NVP。
答案 1 :(得分:1)
我更喜欢SOAP,因为它是一个干净的代码,带有类。
看看改编的C#代码:
private paypalProxy.CustomSecurityHeaderType getCredentials()
{
paypalProxy.CustomSecurityHeaderType header = new paypalProxy.CustomSecurityHeaderType();
header.Credentials = new paypalProxy.UserIdPasswordType();
header.Credentials.Username = "";
header.Credentials.Password = "";
header.Credentials.Signature = "";
header.Credentials.Subject = "";
return header;
}
private paypalProxy.BasicAmountType getAmountValue(Decimal valor)
{
paypalProxy.BasicAmountType amt = new paypalProxy.BasicAmountType();
amt.currencyID = paypalProxy.CurrencyCodeType.BRL;
amt.Value = valor.ToString("N2").Replace(",", ".");
return amt;
}
public paypalProxy.SetExpressCheckoutResponseType ExpressCheckout(Venda venda, string emailComprador, Decimal valor, Decimal valorFrete, string shipToName, string shipToStreet, string shipToStreet2, string shipToCity, string shipToState, string shipToZip, string shipToCountryCode, string billingToName, string billingToStreet, string billingToStreet2, string billingToCity, string billingToState, string billingToZip, string billingToCountryCode)
{
#region Header/Identification
paypalProxy.CustomSecurityHeaderType header = getCredentials();
#endregion
#region Body
paypalProxy.SetExpressCheckoutReq requisicao = new paypalProxy.SetExpressCheckoutReq();
#region Body configuration
requisicao.SetExpressCheckoutRequest = new paypalProxy.SetExpressCheckoutRequestType();
requisicao.SetExpressCheckoutRequest.Version = "109.0";
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails = new paypalProxy.SetExpressCheckoutRequestDetailsType();
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.OrderTotal = getAmountValue(valor + valorFrete - venda.valorFreteDesconto);
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BuyerEmail = emailComprador;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.ReturnURL = ""
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.CancelURL = ""
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.PaymentAction = paypalProxy.PaymentActionCodeType.Sale;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.ReqConfirmShipping = "0";
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.AddressOverride = "0";
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.AllowNote = "0";
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BrandName = ""
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.ShippingMethod = ShippingServiceCodeType.CustomCode;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.cppheaderimage = "http://.../logo.jpg"
#endregion
#region Shipping Details
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address = new paypalProxy.AddressType();
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.Name = shipToName;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.Street1 = shipToStreet;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.Street2 = shipToStreet2;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.CityName = shipToCity;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.StateOrProvince = shipToState;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.PostalCode = shipToZip;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.Address.Country = paypalProxy.CountryCodeType.BR;
#endregion
#region Billing Address
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress = new paypalProxy.AddressType();
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.Name = billingToName;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.Street1 = billingToStreet;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.Street2 = billingToStreet2;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.CityName = billingToCity;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.StateOrProvince = billingToState;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.PostalCode = billingToZip;
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.BillingAddress.Country = paypalProxy.CountryCodeType.BR;
#endregion
#region Payment detail
PaymentDetailsType paymentDetail = new PaymentDetailsType();
#region ITEMS
List<PaymentDetailsItemType> paymentItems = new List<PaymentDetailsItemType>();
foreach (var item in venda.Product)
{
PaymentDetailsItemType itemPaypal = new PaymentDetailsItemType();
itemPaypal.Name = item.Produto;
//itemPaypal.Description = item.ProductName;
itemPaypal.Amount = getAmountValue(item.value);
itemPaypal.Quantity = item.qtd.ToString();
itemPaypal.ItemURL = item.UrlImage;
paymentItems.Add(itemPaypal);
}
#region DISCOUNT
if (venda.isDesconto)
{
PaymentDetailsItemType itemPaypal = new PaymentDetailsItemType();
itemPaypal.Name = "Discount";
itemPaypal.Description = "Discount";
itemPaypal.Amount = getAmountValue(venda.Discount * (-1));
itemPaypal.Quantity = "1";
paymentItems.Add(itemPaypal);
}
#endregion
paymentDetail.ItemTotal = getAmountValue(valor);
#endregion
#region Shipping
if (ShippingValue > 0)
{
paymentDetail.ShippingTotal = getAmountValue(ShippingValue);
}
if (venda.ShippingDiscount > 0)
{
paymentDetail.ShippingDiscount = getAmountValue(venda.ShippingDiscount);
}
#endregion
paymentDetail.OrderDescription = string.Format("SALE {0} Nº {1}", "XPTO eCommerce", "Sale ID");
paymentDetail.PaymentDetailsItem = paymentItems.ToArray();
paymentDetail.AllowedPaymentMethod = AllowedPaymentMethodType.AnyFundingSource;
paymentDetail.InvoiceID = venda.IdVenda.ToString();
List<PaymentDetailsType> paymentDetails = new List<PaymentDetailsType>();
paymentDetails.Add(paymentDetail);
requisicao.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails.PaymentDetails = paymentDetails.ToArray();
#endregion
#endregion
#region Sending to Paypal and waiting for token in var TIPO
paypalProxy.SetExpressCheckoutResponseType tipo = null;
paypalProxy.PayPalAPIAAInterfaceClient cliente = new paypalProxy.PayPalAPIAAInterfaceClient();
tipo = cliente.SetExpressCheckout(ref header, requisicao);
#endregion
return tipo;
}