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

时间:2014-05-09 18:53:52

标签: php paypal payum

我正在尝试运行示例代码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 个答案:

答案 0 :(得分:1)

令牌存储未正确配置(不是您的错,文档也是错误的)。必须将hash模型字段用作id。尝试:

<?php

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

关于您获得的例外情况。它试图通过id查找令牌并使用该令牌的哈希值。无法找到它。