【问题标题】:Invalid Charge Id费用 ID 无效
【发布时间】:2018-01-15 08:48:08
【问题描述】:

我总是收到这个错误:

处理您的交易时发生错误,Invalid Charge 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;
    }
}

验证支付令牌API

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,它显示了一个支付令牌。

【问题讨论】:

    标签: php checkout-payments


    【解决方案1】:

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

    支付成功后在支付灯箱中可以检索到,如下:

    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";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多