PayPal API Confusion - Which one to use for ExpressCheckout

此生再无相见时 提交于 2019-12-21 21:38:37

问题


I'm looking at the various integrations PayPal offer and as I just want to offer PayPal as a payment options, I'm going with Express Checkout. That being said, there are numerous ways this can be integrated it seems:

  1. Name Value Pairs
  2. Classic API (SDKs)
  3. Restful API.

The issue I have is that the .NET SDK is part of the classic API set which is apparently being deprecated (source: https://github.com/paypal/merchant-sdk-dotnet). The RESTful API doesn't mention anything about ExpressCheckout so I am assuming NVP is the way to go.

Can someone clarify the best route to go down as I'd rather not have to rewrite it if things are being deprecated.

For info: I'm using ASP.NET MVC with C#.


回答1:


First off, there are only two APIs:

  1. Classic API
    • Request and response payloads are formatted as NVP and SOAP.
    • Official PayPal SDK support for Express Checkout available with the PayPal Merchant SDK for .NET (GitHub | NuGet)
  2. REST API
    • Request and response payloads are formatted as JSON
    • Official PayPal SDK support available with the PayPal .NET SDK (GitHub | NuGet)

The PayPal Name-Value Pair API (NVP API) enables you to leverage the functionality of the PayPal API by simply sending an HTTP request to PayPal and specifying request parameters using name-value pairs. The NVP API is a lightweight alternative to the PayPal SOAP API and provides access to the same set of functionality as the SOAP API.

A few points to consider with respect to Classic API vs. REST API:

  • Classic API is not going to be deprecated any time soon. You can use Classic API.
  • Moreover REST API does not support many variables/parameters that work in Classic API.
  • REST API can be used for both credit card and PayPal payments (express checkout architecture).

Since you are using .NET SDK, you can continue using it as it uses Classic API+NVP.




回答2:


I prefer SOAP because is a clean code, with classes.

Look adapted C# code:

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;
}


来源:https://stackoverflow.com/questions/28426249/paypal-api-confusion-which-one-to-use-for-expresscheckout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!