答案是不能在 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
}
}