使用 Payum 进行定期付款的令牌问题

Posted

技术标签:

【中文标题】使用 Payum 进行定期付款的令牌问题【英文标题】:Token Issue with recurring payments with Payum 【发布时间】:2014-06-27 15:05:57 【问题描述】:

我正在尝试运行示例代码here

但我收到此错误:

Payum\Core\Exception\InvalidArgumentException: A token with hash `RVpxpP1m3HnTWcj2oL19SQ38NWvCDIz5qeUwfr283kY` could not be found. in /var/www/test/vendor/payum/core/Payum/Core/Security/PlainHttpRequestVerifier.php on line 47

我的代码如下所示:

namespace Paypal\Model;

use Payum\Core\Model\ArrayObject;

class AgreementDetails extends ArrayObject 



namespace Paypal\Model;

use Payum\Core\Model\Token;

class PaymentSecurityToken extends Token



namespace Paypal\Model;

use Payum\Core\Model\ArrayObject;

class RecurringPaymentDetails extends  ArrayObject


config.php

use Buzz\Client\Curl;
use Payum\Paypal\ExpressCheckout\Nvp\PaymentFactory;
use Payum\Paypal\ExpressCheckout\Nvp\Api;
use Payum\Core\Registry\SimpleRegistry;
use Payum\Core\Storage\FilesystemStorage;
use Payum\Core\Security\PlainHttpRequestVerifier;
use Payum\Core\Security\GenericTokenFactory;

$tokenStorage = new FilesystemStorage('/home/vagrant/tmp', 'Paypal\Model\PaymentSecurityToken');
$requestVerifier = new PlainHttpRequestVerifier($tokenStorage);

$agreementDetailsClass = 'Paypal\Model\AgreementDetails';
$recurringPaymentDetailsClass = 'Paypal\Model\RecurringPaymentDetails';
$storages = array(
    'paypal' => array(
        $agreementDetailsClass => new FilesystemStorage('/home/vagrant/tmp',$agreementDetailsClass),
        $recurringPaymentDetailsClass => new FilesystemStorage('/home/vagrant/tmp',$recurringPaymentDetailsClass)
    )
);

$payments = array(
    'paypal' => PaymentFactory::create(new Api(new Curl, array(
            'username' => 'REPLACE WITH YOURS',
            'password' => 'REPLACE WITH YOURS',
            'signature' => 'REPLACE WITH YOURS',
            'sandbox' => true
        )
    )));

$registry = new SimpleRegistry($payments, $storages, null, null);

$tokenFactory = new GenericTokenFactory(
    $tokenStorage,
    $registry,
    'https://'.$_SERVER['HTTP_HOST'],
    'capture.php',
    'notify.php'
);

prepare.php

use Payum\Paypal\ExpressCheckout\Nvp\Api;

include 'config.php';

$storage = $registry->getStorageForClass($agreementDetailsClass, 'paypal');

$agreementDetails = $storage->createModel();
$agreementDetails['PAYMENTREQUEST_0_AMT'] = 0;
$agreementDetails['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
$agreementDetails['L_BILLINGAGREEMENTDESCRIPTION0'] = $subscription['description'];
$agreementDetails['NOSHIPPING'] = 1;
$storage->updateModel($agreementDetails);

$captureToken = $tokenFactory->createCaptureToken('paypal', $agreementDetails, 'create_recurring_payment.php');

$agreementDetails['RETURNURL'] = $captureToken->getTargetUrl();
$agreementDetails['CANCELURL'] = $captureToken->getTargetUrl();
$storage->updateModel($agreementDetails);

header("Location: ".$captureToken->getTargetUrl());

capture.php

use Payum\Core\Request\BinaryMaskStatusRequest;
use Payum\Core\Request\SecuredCaptureRequest;
use Payum\Core\Request\RedirectUrlInteractiveRequest;

include 'config.php';

$token = $requestVerifier->verify($_REQUEST);
$payment = $registry->getPayment($token->getPaymentName());

$payment->execute($status = new BinaryMaskStatusRequest($token));
if (false == $status->isNew()) 
    header('HTTP/1.1 400 Bad Request', true, 400);
    exit;


if ($interactiveRequest = $payment->execute(new SecuredCaptureRequest($token), true)) 
    if ($interactiveRequest instanceof RedirectUrlInteractiveRequest) 
        header("Location: ".$interactiveRequest->getUrl());
        die();
    

    throw new \LogicException('Unsupported interactive request', null, $interactiveRequest);


$requestVerifier->invalidate($token);

header("Location: ".$token->getAfterUrl());

create_recurring_payment.php

same as here

我已经确认文件存储类能够将数据写入文件,但在捕获步骤中它无法验证令牌。

感谢任何帮助以使此代码运行。

【问题讨论】:

【参考方案1】:

令牌存储配置不正确(不是你的错,文档也错了)。它必须使用hash 模型字段作为id。试试:

<?php

$tokenStorage = new FilesystemStorage('/home/vagrant/tmp', 'Paypal\Model\PaymentSecurityToken', 'hash');

关于您遇到的异常。它尝试通过 id 查找令牌并用于该令牌的哈希。找不到Ofcouce。

【讨论】:

让我试试看,你有什么参考/教程等我可以遵循的建议吗? 更改后出现此错误。Catchable fatal error: Argument 1 passed to Payum\Payment::addExtension() must implement interface Payum\Extension\ExtensionInterface, instance of Payum\Core\Extension\StorageExtension given, called in /vagrant/pprp/vendor/payum/core/Payum/Core/Registry/SimpleRegistry.php on line 34 and defined in /vagrant/pprp/vendor/payum/payum/src/Payum/Payment.php on line 80 我猜你使用的是旧版本的 payum。你用的是 0.8 吗? 是的,它是最新的,例如 php composer.phar require "payum/payum-bundle:*@stable" payum.forma-dev.com/documentation/0.8/PayumBundle/…。我通过作曲家安装,我认为它应该是最新的 Payum\Payment 不是正确的命名空间,应该是 Payum\Core\Payment。 Payum\Extension\XXX 也是。这就是为什么我怀疑你安装了一个旧的,一个在 0.7 之前的。您可以尝试明确设置版本吗?有点像 0.8.*.

以上是关于使用 Payum 进行定期付款的令牌问题的主要内容,如果未能解决你的问题,请参考以下文章

我没有收到贝宝付款通知

php : 贝宝定期付款令牌无效

Paypal 定期付款 - 快速结帐 APPID 被拒绝

使用 Payflow Pro 进行定期付款 - 交易类型无效

尝试使用 ActiveMerchant 设置 Paypal 定期付款时“令牌无效”

Stripe 定期支付与 masterpass 集成