费用标识无效

时间:2018-01-15 08:48:09

标签: php checkout-payments

我总是收到这个错误:

  

处理您的交易无效费用ID

时发生错误

每当我使用沙箱密钥中创建的付款令牌时。 documentation显示verifyCharge()接受付款令牌作为参数,但是当我通过它时,我收到了上述错误。

PaymentTokenAPI

use com\checkout;
class PaymentTokenAPI {
    private $apiClient;
    private $tokenService;
    private $tokenPayload;

    function __construct($secretKey) {
        $this->apiClient = new checkout\ApiClient($secretKey);
        $this->tokenService = $this->apiClient->tokenService();
        $this->tokenPayload = new checkout\ApiServices\Tokens\RequestModels\PaymentTokenCreate();
    }
    public function setInvestmentValue($value) {
        $this->tokenPayload->setCurrency('PHP');
        $this->tokenPayload->setValue("$value");
    }
    public function createPaymentToken() {
        $paymentToken = $this->tokenService->createPaymentToken($this->tokenPayload);
        return $paymentToken;
    }
}

VerifyPaymentTokenAPI

use com\checkout;
class VerifyPaymentTokenAPI {
    private $apiClient;
    private $charge;

    function __construct($secretKey) {
        $this->apiClient = new checkout\ApiClient($secretKey);
        $this->charge = $this->apiClient->chargeService();
    }

    public function getCharge($paymentToken) {
        $chargeResponse = $this->charge->verifyCharge($paymentToken);
        return $chargeResponse;
    }
}

用法

$paymentToken = new PaymentTokenAPI('sk_test_aaaaaaaaaaaaaaaaa');
$paymentToken->setInvestmentValue('1000');
try {   
    $token = ($paymentToken->createPaymentToken())->getId();
}catch (checkout\helpers\ApiHttpClientCustomException $e) {
    echo 'Caught exception Message: ',  $e->getErrorMessage(), "\n";
    echo 'Caught exception Error Code: ',  $e->getErrorCode(), "\n";
    echo 'Caught exception Event id: ',  $e->getEventId(), "\n";
}

$getCharge = new VerifyPaymentTokenAPI('sk_test_aaaaaaaaaaaaaaaaa');
try {   
    $charge = $getCharge->getCharge($token);
}catch (checkout\helpers\ApiHttpClientCustomException $e) {
    echo 'Caught exception Message: ',  $e->getErrorMessage(), "\n";
    echo 'Caught exception Error Code: ',  $e->getErrorCode(), "\n";
    echo 'Caught exception Event id: ',  $e->getEventId(), "\n";
}

我还想念什么?为简单起见,我省略了仅在创建付款令牌时可选的其他详细信息。

为了确定,我回复了$token,并显示了付款代币。

1 个答案:

答案 0 :(得分:0)

问题是验证时要传递的正确付款令牌是名为 cko-payment-token 的变量,而不是新创建的付款令牌

付款灯箱中成功付款后,可以检索此信息,如下所示:

if (isset($_POST['cko-payment-token'])) {
    $ckoPaymentToken = $_POST['cko-payment-token'];
    //then put the verifying code here, passing $ckoPaymentToken
    $verifyPaymentToken = new VerifyPaymentTokenAPI('sk_test_aaaaaaaaaaaaaaaaa');
    try {   
        $verifyPaymentTokenObj = $verifyPaymentToken->verifyPaymentToken($ckoPaymentToken);
    }catch (checkout\helpers\ApiHttpClientCustomException $e) {
        echo 'Caught exception Message: ',  $e->getErrorMessage(), "\n";
        echo 'Caught exception Error Code: ',  $e->getErrorCode(), "\n";
        echo 'Caught exception Event id: ',  $e->getEventId(), "\n";
    }
}