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.