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

时间:2016-06-02 22:48:50

标签: c# webforms paypal-sandbox paypal

注意:这篇文章中提到的所有代码都在沙箱而不是实时环境中。

我正在使用带有.Net SDK的CreateRecurringPaymentsProfile API为订阅创建定期付款配置文件。虽然我可以在买家使用下面的代码段使用信用卡时创建定期个人资料,但是当买家使用PayPal帐户时却无法执行此操作。我在Merchant Net SDK中找不到任何可以指定买方拥有PayPal帐户的属性。

问题:如何使用Merchant SDK使用CreateRecurringPaymentsProfile API为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 个答案:

答案 0 :(得分: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("{0}{1}", 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       

 }
}