跳到主要内容

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

Content-Type: application/json

ParamTypeRequiredDescription
user_idStringMerchant identification
function getRandomCode() {

$url = '/merchant/random_code';
$params = json_encode(['user_id' => 1]);
$header = ['Content-Type: application/json'];
$response = sendRequest($url, $params, $header);
$response = json_decode($response, true);

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

Response

ParamTypeRequiredDescription
codeStringThe random code to fetch access token. Please note it only alive for 30 seconds
// Example
{
"code": "1000",
"message": "Accepted",
"data": {
"code": "VL4hrMLmM3THd2iQDNOoa45cGZhx2zH7" // here is the random code
}
}

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

Content-Type: application/json

ParamTypeRequiredDescription
user_idStringMerchant identification
hashStringPlease follow the tip below to generate.
提示

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 getToken()
{
global $user_id, $safecode;

$url = '/merchant/token';
$params = json_encode([
'user_id' => $user_id,
'hash' => hash('SHA256', getRandomCode().$user_id.$safecode)
]);
$header = ['Content-Type: application/json'];
$response = sendRequest($url, $params, $header);
$response = json_decode($response, true);

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

Response

ParamTypeRequiredDescription
access_tokenStringrthe token to put into your request header
token_typeStringthe type of token
expires_inIntergetoken expire time in second
// Example
{
"code": "1000",
"message": "Accepted",
"data": {
"access_token": "cyJacasxzxc...",
"token_type": "bearer",
"expires_in": 300
}
}