Skip to content

Commit 16aca29

Browse files
author
mike
committed
verify signature function
1 parent 05c4955 commit 16aca29

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Modules/Module.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ public function generateSignature($method, $url, $nonceStr, $timestamp, $payload
5151
$signature = base64_encode($signature);
5252
return $signature;
5353
}
54+
55+
public function verifySignature($signature, $method, $url, $nonceStr, $timestamp, $base64Payload = null)
56+
{
57+
$res = openssl_pkey_get_public($this->rm->getPublicKey());
58+
$signType = 'sha256';
59+
60+
$arr = array();
61+
if ($base64Payload) {
62+
array_push($arr, "data=$base64Payload");
63+
}
64+
array_push($arr, "method=$method");
65+
array_push($arr, "nonceStr=$nonceStr");
66+
array_push($arr, "requestUrl=$url");
67+
array_push($arr, "signType=$signType");
68+
array_push($arr, "timestamp=$timestamp");
69+
70+
71+
$result = openssl_verify(join("&", $arr), base64_decode($signature), $res, OPENSSL_ALGO_SHA256);
72+
openssl_free_key($res);
73+
74+
return $result;
75+
}
5476

5577
protected function callApi(string $method, $url, $payload = null)
5678
{

src/RevenueMonster.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class RevenueMonster
3939
// private $tokenPath = '/storage/access_token.json';
4040

4141
private $modules = [
42+
'module' => Modules\Module::class,
4243
'merchant' => Modules\MerchantModule::class,
4344
'store' => Modules\StoreModule::class,
4445
'user', Modules\UserModule::class,
@@ -124,6 +125,11 @@ public function getPrivateKey()
124125
{
125126
return $this->privateKey;
126127
}
128+
129+
public function getPublicKey()
130+
{
131+
return $this->publicKey;
132+
}
127133

128134
public function __get($name)
129135
{

tests/index.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,26 @@
1919
'clientId' => '1553826822294112891',
2020
'clientSecret' => 'nbPqwJtxdiZBiSQkyWLOYPQEufOABAuv',
2121
'privateKey' => file_get_contents(__DIR__.'/private_key.pem'),
22-
// 'publicKey' => file_get_contents(__DIR__.'/public_key.pem'),
22+
'publicKey' => file_get_contents(__DIR__.'/public_key.pem'),
2323
'version' => 'stable',
2424
'isSandbox' => true,
2525
]);
2626

2727

28+
// Verify Signature
29+
try {
30+
$signature = 'AMDgYwP7kZQ03OP0dZolB9aKKU/zSE3mF7pSmuGSbclGDv2+2/83zZwiRUIj0apSzdn/zv02A8BAY9ubugfzhEAf5L4cOGIF2xPr6mUniODLAdDSImh8XFP8hflBMd8oZ1vo8RhouRwZWp2bomaQzdql12GawVhT9ItADbccaJ3yNfPm1cLfWG88KFctFn09VJqmXy0q71CYmh5/FjdsP8jEdfuN9YVPJj+vhEvkkXxI/PwVdRR0DCKidwCELK+A4NTnqe+RUARg/Ez3z/ChaktW6x5clTFn9LwA/V3QRlQSi2vWcfoBoSQWrSf1fd6ee29CkVFbiHMZShoke9w5wA==';
31+
$method = 'post';
32+
$url = 'https://sb-open.revenuemonster.my/v3/customer/1684129308191958988/order';
33+
$nonceStr = 'xNNXvXgOfnLdWoPPaluemi5Y1Lz1MF2g';
34+
$timestamp = '1684387287';
35+
$base64Payload = 'eyJhbW91bnQiOjEwMCwiY3VycmVuY3kiOiJNWVIifQ==';
36+
37+
$response = $rm->module->verifySignature($signature, $method, $url, $nonceStr, $timestamp, $base64Payload);
38+
echo $response;
39+
} catch(Exception $e) {
40+
echo $e->getMessage();
41+
}
2842

2943

3044
// create Recurring Customer

0 commit comments

Comments
 (0)