【问题标题】:How to specify a PayPal buyer account when using CreateRecurringPaymentsProfile API?使用 CreateRecurringPaymentsProfile API 时如何指定 PayPal 买家账户?
【发布时间】: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);

【问题讨论】:

    标签: c# webforms paypal-sandbox paypal


    【解决方案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       
    
     }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 2023-03-05
      • 1970-01-01
      • 2011-04-20
      • 2016-06-21
      • 2019-06-07
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多