我正在尝试通过PHP SDK集成PayPal订阅。根据目标,用户可以在12/18/24个月内或他们想要多少个月后才能在我的网站上进行订阅,并且支付的时间间隔是固定的,在我的情况下,每月一次。
但是当我运行代码时,脚本会引发以下错误:
访问https://api.sandbox.paypal.com/v1/payments/billing-agreements/时获得Http响应代码400。
我的代码是:
$startDate = gmdate("Y-m-d\TH:i:s\Z");
$monthlyAmmount = 10;
$setupFees = $monthlyAmmount;
$plan = new \PayPal\Api\Plan();
$plan->setName('Monthly membership at ipconnect')
->setDescription('You will be charged ' . $monthlyAmmount .' BRL per Month')
->setType('fixed');
$paymentDefinition = new \PayPal\Api\PaymentDefinition();
$paymentDefinition->setName('Regular Payments')
->setType('REGULAR')
->setFrequency('Month')
->setFrequencyInterval("1")
->setCycles($totalMonth)
->setAmount(new \PayPal\Api\Currency(array('value' => $monthlyAmmount, 'currency' => 'BRL')));
$merchantPreferences = new \PayPal\Api\MerchantPreferences();
$merchantPreferences->setReturnUrl($returnUrl)
->setCancelUrl($cancelUrl)
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new \PayPal\Api\Currency(array('value' => $setupFees, 'currency' => 'BRL')));
$plan->setPaymentDefinitions(array($paymentDefinition));
$plan->setMerchantPreferences($merchantPreferences);
try {
$createdPlan = $plan->create($apiContext);
} catch (Exception $ex) {
echo $ex->getMessage();
die();
}
try {
$patch = new \PayPal\Api\Patch();
$value = new \PayPal\Common\PayPalModel('{
"state":"ACTIVE"
}');
$patch->setOp('replace')
->setPath('/')
->setValue($value);
$patchRequest = new \PayPal\Api\PatchRequest();
$patchRequest->addPatch($patch);
$createdPlan->update($patchRequest, $apiContext);
$createdPlan = \PayPal\Api\Plan::get($createdPlan->getId(), $apiContext);
} catch (Exception $ex) {
debug($ex->getMessage());
die();
}
$agreement = new \PayPal\Api\Agreement();
$agreement->setName('Ipconnect subscription agrement')
->setDescription('Monthly Basic Agrement')
// set the start date to 1 month from now as we take our first payment via the setup fee
->setStartDate($startDate);
// Link the plan up with the agreement
$plan = new \PayPal\Api\Plan();
$plan->setId($createdPlan->getId());
$agreement->setPlan($plan);
// Add Payer
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
try {
// Please note that as the agreement has not yet activated, we wont be receiving the ID just yet.
$agreement = $agreement->create($apiContext);
// Get redirect url
$approvalUrl = $agreement->getApprovalLink();
} catch (Exception $ex) {
echo $ex->getMessage(); //***** The error is coming here
die();
}
header("Location: {$approvalUrl}");
我也尝试通过添加shippingAddress(是否有必要?)
我正在关注本文档-> https://paypal.github.io/PayPal-PHP-SDK/sample/#billing