· 4 years ago · Jun 03, 2021, 11:28 AM
1<?php
2
3namespace Tests\Feature;
4use GuzzleHttp\Client;
5
6class DemoOTPPHP
7{
8/**
9 * Test OTP Kokatto API
10 * 1. install GuzzleHttp: https://docs.guzzlephp.org/en/stable/overview.html#installation
11 * 2. create request body
12 * 3. generate HMAC signature using the request body
13 * 4. insert signature in request body
14 * 5. call OTP API
15 *
16 * @return void
17 */
18 public function testExample()
19 {
20 date_default_timezone_set("Asia/Jakarta");
21 $parameters = array(
22 'clientId' => '8002',
23 'campaignName' => 'OPM',
24 'destination' => '6285695744459',
25 'codeValidity' => 120,
26 'codeLength' => 6,
27 'dlrCallbackUrl' => 'https://otp.kokatto.com/callback/kokatto',
28 'timestamp' => date('Y-m-d') . 'T' . date('H:i:s') . '+0700'
29 );
30 $signature = self::generateKokattoAPISignedRequest($parameters, "72115e7b0ff10f0d712e00c743f7c535");
31 $parameters['signature'] = $signature;
32
33 $client = new Client();
34 // dev URL
35 $url = 'https://xa7wqrhvic.execute-api.ap-southeast-1.amazonaws.com/dev/otp/request';
36 $response = $client->post($url, [
37 'json' => $parameters
38 ]);
39
40 }
41
42 private static function generateKokattoAPISignedRequest($parameters, $secretKey)
43 {
44 // construct $map like below (sorted):
45 // print
46 // [campaignName] => OPM
47 // [clientId] => 8002
48 // [codeLength] => 6
49 // [destination] => 6285695744459
50 // [timestamp] => 2021-04-22T14:37:49+07:00
51 //sort map by key
52 ksort($parameters);
53
54 //construct map into query string
55 $query = http_build_query($parameters);
56 // print $query = campaignName=OPM&clientId=1111&codeLength=6&destination=082148893982×tamp=2021-04-22T14%3A37%3A49+07%3A00
57
58 //md5 hash query string
59 $queryMd5 = md5($query);
60 // print generated md5 query
61
62 //generate hmac hash with sha256 method
63 $queryHmacSha256 = hash_hmac('sha256', $queryMd5, $secretKey);
64 // print generate hmac
65
66 //url encode hmac hash
67 return strtoupper(urlencode($queryHmacSha256));
68 // return encode url hmac
69 // insert the signature into request body
70
71 }
72}
73