Skip to main content
Version: 1.0.x

Access Token

For system security purposes, there are two steps you need to fulfillment before sending transaction requests.

Step 1. Fetch random code

At the beginning of the request process, you should fetch a random code from our service.

Request

API URL: /merchant/random_code

Note

✅ Required ⭕ Optional ❌ Not used

HeaderContent
Content-Typeapplication/json
ParamTypeRequiredDescription
user_idStringMerchant identification
function get_random_code()
{
global $user_id, $base_url;

$url = "{$base_url}/merchant/random_code";
$params = json_encode(['user_id' => $user_id]);
$header = ['Content-Type: application/json'];
$response = send_curl_request($url, $params, $header);
$response = json_decode($response, true);

return $response['data']['code'];
}

function send_curl_request($url, $body, $header): string
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $header,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
]);
$response = curl_exec($curl);
curl_close($curl);

return $response;
}

Response

ParamTypeRequiredDescription
codeStringThe random code to fetch access token. Please note it only alive for 5 minutes
expires_inIntergerrandom code expire time in second
expires_atIntergerrandom code expire time at timestamp
{
"code": "1000",
"message": "Accepted",
"data": {
"code": "VL4hrMLmM3THd2iQDNOoa45cGZhx2zH7", // here is the random code
"expires_in": 300,
"expires_at": 1704699776
}
}

Step 2. Get access token

After getting random code, then you need to send second request to get access token which should be put in each transaction request you send to PTS. The access token will keep living until expire time provided in response reached. That means you should cache the access token in your service and do the two steps request again when access token expired.

Note

You should implement some mechanism to keep the access token and detect if it is expired or not before sending your transaction request.

Request

API URL: /merchant/token

HeaderContent
Content-Typeapplication/json
ParamTypeRequiredDescription
user_idStringMerchant identification
hashStringPlease follow the tip below to generate.
tip

Please concat random_code + user_id + safecode in given order without any separator or whitespace. Then put the concatenated string into SHA256 hash function to get the hash code.

function get_token(): string
{
global $user_id, $safecode, $base_url;

$url = "{$base_url}/merchant/token";
$params = json_encode([
'user_id' => $user_id,
'hash' => hash('SHA256', get_random_code().$user_id.$safecode)
]);
$header = ['Content-Type: application/json'];
$response = send_curl_request($url, $params, $header);
$response = json_decode($response, true);
$data = $response['data'];

return $data['token_type'].' '.$data['access_token'];
}

function send_curl_request($url, $body, $header): string
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $header,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
]);
$response = curl_exec($curl);
curl_close($curl);

return $response;
}

Response

ParamTypeRequiredDescription
access_tokenStringthe token to put into your request header. Please note it only alive for 60 minutes
token_typeStringthe type of token
expires_inIntergertoken expire time in second
expires_atIntergertoken expire time at timestamp
// Example
{
"code": "1000",
"message": "Accepted",
"data": {
"access_token": "cyJacasxzxc...",
"token_type": "bearer",
"expires_in": 300,
"expires_at": 1704699776
}
}