0

I always receive this error:

An error has occurred while processing your transaction Invalid Charge id

whenever I am using a created payment token from a sandbox secret key. The documentation shows that verifyCharge() accepts a payment token as parameter, but when I pass it, I received the said error.

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

Usage

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

What else did I miss? I omitted other details that are only optional when creating the payment token, for simplicity.

Just to be sure, I echoed $token, and it is showing a payment token.

Carl Binalla
  • 5,393
  • 5
  • 27
  • 46

1 Answers1

0

The problem is that the proper payment token to be passed when verifying would be the variable named cko-payment-token, and not the newly created payment token.

This can be retrieved after the successful payment in the Payment Lightbox, like so:

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";
    }
}
Carl Binalla
  • 5,393
  • 5
  • 27
  • 46