Sunil
Sunil

Reputation: 21406

How to specify a PayPal buyer account when using CreateRecurringPaymentsProfile API?

NOTE: All code mentioned in this post is in sandbox and not live environment.

I am using CreateRecurringPaymentsProfile API with .Net SDK to create a recurring payment profile for a subscription. While I am able to create a recurring profile when credit card is used by the buyer by using the code snippet below, but cannot do this when buyer is using a PayPal account. There are no properties that I could find in Merchant Net SDK to specify that buyer has a PayPal account.

Question : How would I create a subscription profile for a PayPal buyer account instead of a credit card using CreateRecurringPaymentsProfile API using Merchant SDK?

CreateRecurringPaymentsProfile API with credit card using Merchant Net SDK

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

Upvotes: 0

Views: 332

Answers (1)

Sunil
Sunil

Reputation: 21406

The answer is that one cannot specify the payer as a PayPal account holder in CreateRecurringPaymentsProfile API. But, still this API can be used to create a subscription for a PayPal account holder as explained below.

To use CreateRecurringPaymentsProfile API for creating a subscription with an existing PayPal account rather than a credit card, one needs to follow a multi-step process. First step is to call SetExpressCheckout specifying BillingAgreementDetailsType as RecurringPayments and then re-direct to this URL (https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout) passing the token in response of SetExpressCheckout API.

  • This redirection will take the user to PayPal login page where user logs in and either accepts or cancels the subscription request.
  • If user accepts then user is redirected to the page specified in ReturnURL of SetExpressCheckout API call
  • and if user does not accept the subscription then the user is redirected to CancelURL specified in SetExpressChecout API call.

Second step is to call CreateRecurringPaymentsProfile in the ReturnURL page specified in SetExpressCheckout API call and pass it the token received after Step 1 (obtained from query string parameter passed to ReturnUrl in Step 1). When you pass token to this API, you should not specify any credit card details. This step will result in payment being made against the subscription from PayPal account of user who accepted the subscription in Step 1.

Code for calling SetExpressCheckout and redirecting user to 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;
}

Code in ReturnURL for calling 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       

 }
}

Upvotes: 1

Related Questions