即使安装费失败,PayPal PHP SDK也会执行计费协议。

问题描述 投票:0回答:1

我的问题是,即使没有支付设置费,计费协议也能成功执行。 从日志来看,IPN事件通知安装费失败和协议被取消通常需要5-10分钟才能到达,这是一个疯狂的延迟量。

我使用的是PayPal官方的PHP SDK,网址是 https:/github.compaypalPayPal-PHP-SDK。. 它在一个月前被废弃,但其替代品被标记为 "尚未准备好生产"。

计费计划详情,打算收取29.99美元的订阅费。 设置费用于保证初始付款。

Billing Plan settings

根据文件中的2个步骤流程 https:/paypal.github.ioPayPal-PHP-SDKsample。,为了便于阅读,去掉了包装的trycatch块。

// 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;
}
php paypal paypal-sandbox paypal-rest-sdk paypal-subscriptions
1个回答
1
投票

替换的SDK是 https:/github.compaypalCheckout-PHP-SDK。 该SDK不包括任何计费协议或订阅用例。对于该SDK没有涵盖的用例,你应该使用直接的HTTPS集成。这里有相关文档。https:/developer.paypal.comdocsapirest-sdks

你试图使用的代码是一个过时的SDK,用于一个过时的API(旧版本的计费协议,不兼容新的订阅)。

这里是你应该集成的API,没有SDK。https:/developer.paypal.comdocssubscriptions。

© www.soinside.com 2019 - 2024. All rights reserved.