[英]PayPal PHP SDK executes billing agreement even if setup fee fails
我的问题是即使没有支付安装费,也可以成功执行计费协议。 查看日志,通知设置费失败和协议被取消的 IPN 事件通常需要 5-10 分钟才能到达,这是一个疯狂的延迟。
我正在使用官方 PayPal PHP SDK 在Z5E056C500A1C4B6A7110B50A1C4B6A7110B50D807 . 它在一个月前被弃用,但它的替代品被标记为“尚未准备好生产”。
计费计划详细信息,旨在收取 29.99 美元/年的订阅费用。 安装费用于保证初始付款。
根据https://paypal.github.io/PayPal-PHP-SDK/sample/中记录的两步过程,为了易读性删除了包装 try/catch 块:
// Step 1: https://paypal.github.io/PayPal-PHP-SDK/sample/doc/billing/CreateBillingAgreementWithPayPal.html
use PayPal\Api\Agreement;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\Payer;
use PayPal\Api\Plan;
/**
* @var \PayPal\Rest\ApiContext $apiContext
*/
$plan = Plan::get('EXAMPLE-PLAN-ID', $apiContext);
$agreement = new Agreement();
date_default_timezone_set('America/Los_Angeles');
$agreement->setName($plan->getName())
->setDescription($plan->getDescription())
// I'm not sure why +1 hour is used here, but that's how it is in the codebase.
->setStartDate(date('c', strtotime("+1 hour", time())));
$agreement->setPlan($plan);
/**
* ------------------------------------------------------------------------------------------
* I think overriding should be optional since they currently precisely match the given
* plan's data. So for this particular plan, if I deleted everything between these comment
* blocks, nothing bad should happen.
* ------------------------------------------------------------------------------------------
*/
$preferences = new MerchantPreferences();
$preferences->setReturnUrl("https://www.example.com/actually-a-valid-site")
->setCancelUrl("https://www.example.com/actually-a-valid-site")
->setAutoBillAmount('no')
->setInitialFailAmountAction('CANCEL');
$agreement->setOverrideMerchantPreferences($preferences);
/**
* ------------------------------------------------------------------------------------------
* ------------------------------------------------------------------------------------------
*/
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
$agreement = $agreement->create($apiContext);
$approvalUrl = $agreement->getApprovalLink();
// This takes us to PayPal to login and confirm payment.
header("Location: ".$approvalUrl);
// Step 2: https://paypal.github.io/PayPal-PHP-SDK/sample/doc/billing/ExecuteAgreement.html
use PayPal\Api\Agreement;
/**
* @var \PayPal\Rest\ApiContext $apiContext
*/
try {
$agreement = new Agreement();
$agreement->execute($_GET['token'], $apiContext);
$agreement = Agreement::get($agreement->getId(), $apiContext);
/**
* I assume at this point the agreement is executed successfully. Yet, the setup fee does not
* have to be paid for us to get here. This behavior is verified on live.
*/
} catch (\Exception $e) {
// Do something.
}
我对自己做错的事情感到不知所措,即使没有支付安装费,也会导致计费协议执行。 帮助将不胜感激!
以下是创建使用的计划的方法:
use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\Patch;
use PayPal\Api\PatchRequest;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;
use PayPal\Common\PayPalModel;
$plan = new Plan();
$plan->setName('Test Name')
->setDescription('Test Description')
->setType('INFINITE');
$payment_definition = new PaymentDefinition();
$payment_definition->setName('Regular Payments')
->setType('REGULAR')
->setFrequency('YEAR')
->setFrequencyInterval(1)
->setCycles('0')
->setAmount(new Currency(['value' => '29.99', 'currency' => 'USD']));
$merchant_preferences = new MerchantPreferences();
$merchant_preferences->setReturnUrl'https://insert.actual.url.here')
->setCancelUrl('https://insert.actual.url.here')
->setAutoBillAmount('NO')
->setInitialFailAmountAction('CANCEL')
->setMaxFailAttempts('1')
->setSetupFee(new Currency(['value' => '29.99', 'currency' => 'USD']));
$plan->setPaymentDefinitions([$payment_definition]);
$plan->setMerchantPreferences($merchant_preferences);
$request = clone $plan;
try {
/**
* @var \Paypal\Rest\ApiContext $apiContext
*/
$plan->create($apiContext);
$patch = new Patch();
$value = new PayPalModel(['state' => 'ACTIVE']);
$patch->setOp('replace')
->setPath('/')
->setValue($value);
$patchRequest = new PatchRequest();
$patchRequest->addPatch($patch);
if (!$plan->update($patchRequest, $apiContext)) {
throw new \Exception("Failed to apply patch to plan.");
}
// Done.
} catch (\Exception $e) {
// Some error handling.
exit;
}
替换 SDK 是https://github.com/paypal/Checkout-PHP-SDK ,其中不包括任何计费协议或订阅用例。 对于 SDK 未涵盖的用例,您应该使用直接的 HTTPS 集成。 这记录在这里: https://developer.paypal.com/docs/api/rest-sdks/
您尝试使用的代码适用于过时的 API(旧版本的计费协议,与新订阅不兼容)的过时 SDK。
这是您应该集成的 API,没有 SDK: https://developer.ZA0A058/docs/CACEE16E86FZF6BD2
事实证明,通常的协议执行不应该引发异常。 要检查设置是否已支付,请在执行协议后检查$agreement->getState()
的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.