使用 CreateRecurringPaymentsProfile API 时如何指定 PayPal 买家账户?

Posted

技术标签:

【中文标题】使用 CreateRecurringPaymentsProfile API 时如何指定 PayPal 买家账户?【英文标题】:How to specify a PayPal buyer account when using CreateRecurringPaymentsProfile API? 【发布时间】:2016-10-02 20:51:58 【问题描述】:

注意:本文中提到的所有代码都在沙盒中,而不是实时环境中。

我正在使用带有 .Net SDK 的 CreateRecurringPaymentsProfile API 来为订阅创建定期付款配置文件。虽然当买家使用信用卡时,我可以使用下面的代码 sn-p 创建一个经常性配置文件,但当买家使用 PayPal 帐户时,我不能这样做。我在 Merchant Net SDK 中找不到任何属性来指定买家拥有 PayPal 帐户。

问题:如何使用 CreateRecurringPaymentsProfile API 和 Merchant SDK 为 PayPal 买家帐户而不是信用卡创建订阅配置文件?

使用 Merchant Net SDK 使用信用卡创建RecurringPaymentsProfile API

CreateRecurringPaymentsProfileReq createRecurringPaymentsProfile = new CreateRecurringPaymentsProfileReq();
    CreateRecurringPaymentsProfileRequestType createRecurringPaymentsProfileRequest = new CreateRecurringPaymentsProfileRequestType();

RecurringPaymentsProfileDetailsType recurringPaymentsProfileDetails
 = new RecurringPaymentsProfileDetailsType("2016-06-02T16:55:19+00:00");


BasicAmountType billingAmount = new BasicAmountType(CurrencyCodeType.USD, "3.00");


BillingPeriodDetailsType paymentPeriod = new BillingPeriodDetailsType(BillingPeriodType.DAY, Convert.ToInt32("5"), billingAmount);


ScheduleDetailsType scheduleDetails = new ScheduleDetailsType("description", paymentPeriod);


CreateRecurringPaymentsProfileRequestDetailsType createRecurringPaymentsProfileRequestDetails
 = new CreateRecurringPaymentsProfileRequestDetailsType(recurringPaymentsProfileDetails, scheduleDetails);

CreditCardDetailsType creditCard = new CreditCardDetailsType();
creditCard.CreditCardType = CreditCardTypeType.VISA;
creditCard.CreditCardNumber = "5261193281604310";
creditCard.CVV2 = "235";
creditCard.ExpMonth = Convert.ToInt32("12");
creditCard.ExpYear = Convert.ToInt32("2016");
createRecurringPaymentsProfileRequestDetails.CreditCard = creditCard;

createRecurringPaymentsProfileRequest.CreateRecurringPaymentsProfileRequestDetails = createRecurringPaymentsProfileRequestDetails;
createRecurringPaymentsProfile.CreateRecurringPaymentsProfileRequest = createRecurringPaymentsProfileRequest;

PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService();

responseCreateRecurringPaymentsProfileResponseType
 = service.CreateRecurringPaymentsProfile(createRecurringPaymentsProfile);

【问题讨论】:

【参考方案1】:

答案是不能在 CreateRecurringPaymentsProfile API 中将付款人指定为 PayPal 帐户持有人。但是,该 API 仍可用于为 PayPal 帐户持有人创建订阅,如下所述。

要使用CreateRecurringPaymentsProfile API 通过现有的 PayPal 帐户而不是信用卡创建订阅,需要遵循多个步骤。 第一步是调用 SetExpressCheckout,将 BillingAgreementDetailsType 指定为 RecurringPayments,然后重定向到此 URL (https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout),传递令牌以响应 SetExpressCheckout API。

此重定向会将用户带到用户登录的 PayPal 登录页面,然后接受或取消订阅请求。 如果用户接受,则用户将被重定向到 SetExpressCheckout API 调用的ReturnURL 中指定的页面 如果用户不接受订阅,则用户将被重定向到 SetExpressChecout API 调用中指定的CancelURL

第二步是在SetExpressCheckout API调用中指定的ReturnURL页面中调用CreateRecurringPaymentsProfile,并将步骤1之后收到的令牌传递给它(从步骤1中传递给ReturnUrl的查询字符串参数中获得)。当您将令牌传递给此 API 时,您不应指定任何信用卡详细信息。此步骤将导致从在步骤 1 中接受订阅的用户的 PayPal 帐户支付订阅费用。

调用 SetExpressCheckout 并将用户重定向到 PayPal 的代码

 protected void btnSubmit_Click(object sender, EventArgs e) 
 string token = null;
 SetExpressCheckoutResponseType response = SetExpressCheckoutAPIOperation();
  if (response.Token == null) 
   txtLog.Text = "There was some issue with calling SetExpressCheckout API. Please review the failure message that should also be printed in this textbox.";
   else 
   token = response.Token;
   Response.Redirect("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" + Server.UrlEncode(token));
  


public SetExpressCheckoutResponseType SetExpressCheckoutAPIOperation() 
 // Create the SetExpressCheckoutResponseType object
 SetExpressCheckoutResponseType responseSetExpressCheckoutResponseType = new SetExpressCheckoutResponseType();

 try 
  SetExpressCheckoutRequestDetailsType setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();

  BillingAgreementDetailsType baType = new BillingAgreementDetailsType(BillingCodeType.RECURRINGPAYMENTS);
  baType.BillingAgreementDescription = "Gold Carrier";
  billingAgreementDescription = baType.BillingAgreementDescription;
  setExpressCheckoutRequestDetails.BillingAgreementDetails.Add(baType);

  // URL to which the buyer's browser is returned after choosing to pay
  // with PayPal. 
  setExpressCheckoutRequestDetails.ReturnURL = "http://www.myexample.com/completed.aspx";

  // URL to which the buyer is returned if the buyer does not approve the
  // use of PayPal to pay you.
  setExpressCheckoutRequestDetails.CancelURL = "http://www.myexample.com/canceled.aspx";

  SetExpressCheckoutReq setExpressCheckout = new SetExpressCheckoutReq();
  SetExpressCheckoutRequestType setExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails);

  setExpressCheckout.SetExpressCheckoutRequest = setExpressCheckoutRequest;

  Dictionary < string, string > configurationMap = new Dictionary < string, string > ();

  configurationMap.Add("mode", "sandbox");
  // Signature Credential
  configurationMap.Add("account1.apiUsername", "******");
  configurationMap.Add("account1.apiPassword", "*******");
  configurationMap.Add("account1.apiSignature", "********");
  configurationMap.Add("account1.applicationId", "******");
  // Create the service wrapper object to make the API call
  PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(configurationMap);

  // # API call            
  responseSetExpressCheckoutResponseType = service.SetExpressCheckout(setExpressCheckout);
 
 // # Exception log    
 catch (System.Exception ex) 
  // Log the exception message       

 
 return responseSetExpressCheckoutResponseType;

ReturnURL 中用于调用 CreateRecurringPaymentProfile API 的代码

protected void Page_Load(object sender, EventArgs e) 
 if (!string.IsNullOrWhiteSpace(Request.QueryString["token"])) 
  token = Server.UrlDecode(Request.QueryString["token"]);
  CreateRecurringPaymentProfile();
 


private void CreateRecurringPaymentProfile() 
  CreateRecurringPaymentsProfileResponseType responseCreateRecurringPaymentsProfileResponseType = new CreateRecurringPaymentsProfileResponseType();

 try 

  CreateRecurringPaymentsProfileReq createRecurringPaymentsProfile = new CreateRecurringPaymentsProfileReq();
  CreateRecurringPaymentsProfileRequestType createRecurringPaymentsProfileRequest = new CreateRecurringPaymentsProfileRequestType();


  RecurringPaymentsProfileDetailsType recurringPaymentsProfileDetails
   = new RecurringPaymentsProfileDetailsType(string.Format("01", DateTime.UtcNow.ToString("s"), "Z"));

  // Billing amount for each billing cycle during this payment period.
  BasicAmountType billingAmount = new BasicAmountType(CurrencyCodeType.USD, "3.00");
  BillingPeriodDetailsType paymentPeriod = new BillingPeriodDetailsType(BillingPeriodType.DAY, Convert.ToInt32("5"), billingAmount);
  ScheduleDetailsType scheduleDetails = new ScheduleDetailsType(this.billingAgreementDescription, paymentPeriod);
  CreateRecurringPaymentsProfileRequestDetailsType createRecurringPaymentsProfileRequestDetails
   = new CreateRecurringPaymentsProfileRequestDetailsType(recurringPaymentsProfileDetails, scheduleDetails);


  // Either EC token or a credit card number is required.If you include
  CreditCardDetailsType creditCard = new CreditCardDetailsType();
  if (string.IsNullOrEmpty(token)) 
   creditCard.CreditCardType = CreditCardTypeType.VISA;
   creditCard.CreditCardNumber = "5261193281604310";
   creditCard.CVV2 = "235";
   creditCard.ExpMonth = Convert.ToInt32("12");
   creditCard.ExpYear = Convert.ToInt32("2016");
   createRecurringPaymentsProfileRequestDetails.CreditCard = creditCard;
   else //we are using a PayPal email account i.e. not a credit card but a PayPal account to set up the subscription
  
   createRecurringPaymentsProfileRequestDetails.Token = token;
  


  createRecurringPaymentsProfileRequest.CreateRecurringPaymentsProfileRequestDetails = createRecurringPaymentsProfileRequestDetails;

  createRecurringPaymentsProfile.CreateRecurringPaymentsProfileRequest = createRecurringPaymentsProfileRequest;


  Dictionary < string, string > configurationMap = new Dictionary < string, string > ();

  configurationMap.Add("mode", "sandbox");
  // Signature Credential
  configurationMap.Add("account1.apiUsername", "*****");
  configurationMap.Add("account1.apiPassword", "*****");
  configurationMap.Add("account1.apiSignature", "*****");
  configurationMap.Add("account1.applicationId", "*****");

  // Create the PayPalAPIInterfaceServiceService service object to make the API call
  PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(configurationMap);
  responseCreateRecurringPaymentsProfileResponseType
   = service.CreateRecurringPaymentsProfile(createRecurringPaymentsProfile);
 
 // # Exception log    
 catch (System.Exception ex) 
  // Log the exception message       

 

【讨论】:

以上是关于使用 CreateRecurringPaymentsProfile API 时如何指定 PayPal 买家账户?的主要内容,如果未能解决你的问题,请参考以下文章

第一篇 用于测试使用

在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?

今目标使用教程 今目标任务使用篇

Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)

MySQL db 在按日期排序时使用“使用位置;使用临时;使用文件排序”

使用“使用严格”作为“使用强”的备份