簡體   English   中英

PayPal API混淆-用於ExpressCheckout的哪一個

[英]PayPal API Confusion - Which one to use for ExpressCheckout

我正在查看PayPal提供的各種集成,而我只是想提供PayPal作為付款方式,因此我打算使用Express Checkout。 話雖這么說,但似乎可以通過多種方式進行整合:

  1. 名稱值對
  2. 經典API(SDK)
  3. 寧靜的API。

我遇到的問題是.NET SDK是經典API集的一部分,而該API集顯然已被棄用(來源: https//github.com/paypal/merchant-sdk-dotnet )。 RESTful API沒有提及ExpressCheckout,因此我假設 NVP是可行的方法。

有人可以說出最好的解決方法嗎,因為如果事情被棄用,我不想重寫它。

有關信息:我正在將ASP.NET MVC與C#一起使用。

首先,只有兩個API:

  1. 經典API
    • 請求和響應有效載荷的格式為NVP and SOAP
    • 適用於.NETPayPal Merchant SDK提供了對Express Checkout的官方PayPal SDK支持( GitHub | NuGet
  2. REST API
    • 請求和響應有效載荷格式為JSON
    • PayPal .NET SDK提供官方的PayPal SDK支持( GitHub | NuGet

使用PayPal名稱-值對API (NVP API),您只需將HTTP請求發送到PayPal並使用名稱-值對指定請求參數,即可利用PayPal API的功能。 NVP API是PayPal SOAP API的輕型替代方案,並提供對與SOAP API相同功能集的訪問。

關於經典API與REST API的幾點注意事項:

  • Classic API不會很快被棄用。 您可以使用經典API。
  • 此外,REST API不支持在Classic API中工作的許多變量/參數。
  • REST API可用於信用卡和PayPal付款(快速結帳架構)。

由於您使用的是.NET SDK,因此可以繼續使用它,因為它使用的是Classic API + NVP。

我更喜歡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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM