Skip to content

Commit 44d0379

Browse files
authored
Merge pull request #42 from michaelpo-rm/main
2 parents 77116a1 + 16aca29 commit 44d0379

7 files changed

+417
-1
lines changed

src/Modules/Module.php

+30
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
{
@@ -67,6 +89,9 @@ protected function callApi(string $method, $url, $payload = null)
6789
case 'delete':
6890
$request = Request::delete($url);
6991
break;
92+
case 'put':
93+
$request = Request::put($url);
94+
break;
7095
default:
7196
$request = Request::get($url);
7297
break;
@@ -99,6 +124,11 @@ protected function post(string $url, $payload = null)
99124
return $this->callApi('post', $url, $payload);
100125
}
101126

127+
protected function put(string $url)
128+
{
129+
return $this->callApi('put', $url);
130+
}
131+
102132
/**
103133
* magic function to return response
104134
*
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace RevenueMonster\SDK\Modules;
4+
5+
use RevenueMonster\SDK\Request\TokenizedCustomer;
6+
use RevenueMonster\SDK\Request\RecurringCustomer;
7+
use RevenueMonster\SDK\Request\CustomerOrderAmount;
8+
9+
class TokenizedCustomerModule extends Module
10+
{
11+
12+
/**
13+
* Create Recurring Customer
14+
* @param RecurringCustomer $args
15+
* @return stdClass
16+
* @throws ApiException
17+
*/
18+
public function createRecurringCustomer($args)
19+
{
20+
if ($args instanceof RecurringCustomer) {
21+
$args = $args->jsonSerialize();
22+
}
23+
// print_r($args);
24+
25+
$uri = $this->getOpenApiUrl('v3', '/recurring-payment');
26+
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
27+
}
28+
29+
/**
30+
* Create Tokenized Customer
31+
* @param TokenizedCustomer $args
32+
* @return stdClass
33+
* @throws ApiException
34+
*/
35+
public function createTokenizedPayment($args)
36+
{
37+
if ($args instanceof TokenizedCustomer) {
38+
$args = $args->jsonSerialize();
39+
}
40+
// print_r($args);
41+
42+
$uri = $this->getOpenApiUrl('v3', '/tokenized-payment');
43+
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
44+
}
45+
46+
47+
/**
48+
* Toggle customer status by Customer ID
49+
* @param string $customerId
50+
* @return stdClass
51+
* @throws ApiException
52+
*/
53+
public function toggleCustomerStatusById(string $customerId)
54+
{
55+
$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/status");
56+
return $this->mapResponse($this->callApi('put', $uri)->send());
57+
}
58+
59+
/**
60+
* Get Customer Orders by Customer ID
61+
* @param string $customerId
62+
* @return stdClass
63+
* @throws ApiException
64+
*/
65+
public function getCustomerOrdersById(string $customerId)
66+
{
67+
$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/orders");
68+
return $this->mapResponse($this->callApi('get', $uri)->send());
69+
}
70+
71+
72+
/**
73+
* Create Customer Order by Customer ID
74+
* @param string $customerId, obj $args
75+
* @return stdClass
76+
* @throws ApiException
77+
*/
78+
public function createCustomerOrderById(string $customerId, $args)
79+
{
80+
if ($args instanceof CustomerOrderAmount) {
81+
$args = $args->jsonSerialize();
82+
}
83+
// print_r($args);
84+
85+
$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/order");
86+
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
87+
}
88+
89+
/**
90+
* Get Customer Tokens ( Customer ID based on your side pass in to Web Payment )
91+
* @param string $customerId
92+
* @return stdClass
93+
* @throws ApiException
94+
*/
95+
public function getCustomerTokensById(string $customerId)
96+
{
97+
$uri = $this->getOpenApiUrl('v3', "/payment/tokens/$customerId");
98+
return $this->mapResponse($this->callApi('get', $uri)->send());
99+
}
100+
}

src/Request/CustomerOrderAmount.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace RevenueMonster\SDK\Request;
4+
5+
use Exception;
6+
use JsonSerializable;
7+
use Rakit\Validation\Validator;
8+
use RevenueMonster\SDK\Exceptions\ValidationException;
9+
10+
class CustomerOrderAmount implements JsonSerializable
11+
{
12+
public $currency = 'MYR';
13+
public $amount = 0;
14+
15+
public function jsonSerialize()
16+
{
17+
$data = [
18+
'currency' => $this->currency,
19+
'amount' => $this->amount,
20+
];
21+
22+
return $data;
23+
}
24+
}

src/Request/RecurringCustomer.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace RevenueMonster\SDK\Request;
4+
5+
use Exception;
6+
use JsonSerializable;
7+
use Rakit\Validation\Validator;
8+
use RevenueMonster\SDK\Exceptions\ValidationException;
9+
10+
class RecurringCustomer implements JsonSerializable
11+
{
12+
public $storeId = '';
13+
public $email = '';
14+
public $name = '';
15+
public $countryCode = '';
16+
public $phoneNumber = '';
17+
public $productName = '';
18+
public $productDescription = '';
19+
public $currency = 'MYR';
20+
public $amount = 0;
21+
public $redirectUrl = '';
22+
public $notifyUrl = '';
23+
public $recurringInterval = '';
24+
public $recurringTarget = '';
25+
public $recurringRepetition = 1;
26+
27+
public function jsonSerialize()
28+
{
29+
$data = [
30+
'storeId' => $this->storeId,
31+
'email' => $this->email,
32+
'name' => $this->name,
33+
'countryCode' => $this->countryCode,
34+
'phoneNumber' => $this->phoneNumber,
35+
'productName' => $this->productName,
36+
'productDescription' => $this->productDescription,
37+
'currency' => $this->currency,
38+
'amount' => $this->amount,
39+
'redirectUrl' => escape_url($this->redirectUrl),
40+
'notifyUrl' => escape_url($this->notifyUrl),
41+
'recurringInterval' => $this->recurringInterval,
42+
'recurringTarget' => $this->recurringTarget,
43+
'recurringRepetition' => $this->recurringRepetition,
44+
];
45+
46+
$validator = new Validator;
47+
$validation = $validator->make($data, [
48+
'storeId' => 'required|max:255',
49+
'email' => 'required',
50+
'name' => 'required',
51+
'countryCode' => 'required',
52+
'phoneNumber' => 'required',
53+
'productName' => 'required',
54+
'productDescription' => 'required',
55+
'currency' => 'required',
56+
'amount' => 'required',
57+
'redirectUrl' => 'required|url',
58+
'notifyUrl' => 'required|url',
59+
'recurringInterval' => 'required|in:WEEKLY,MONTHLY',
60+
]);
61+
62+
$validation->validate();
63+
64+
if ($validation->fails()) {
65+
throw new ValidationException($validation->errors());
66+
}
67+
68+
return $data;
69+
}
70+
}

src/Request/TokenizedCustomer.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace RevenueMonster\SDK\Request;
4+
5+
use Exception;
6+
use JsonSerializable;
7+
use Rakit\Validation\Validator;
8+
use RevenueMonster\SDK\Exceptions\ValidationException;
9+
10+
class TokenizedCustomer implements JsonSerializable
11+
{
12+
public $storeId = '';
13+
public $email = '';
14+
public $name = '';
15+
public $countryCode = '';
16+
public $phoneNumber = '';
17+
public $productName = '';
18+
public $productDescription = '';
19+
// public $currency = 'MYR';
20+
// public $amount = 0;
21+
public $redirectUrl = '';
22+
public $notifyUrl = '';
23+
24+
public function jsonSerialize()
25+
{
26+
$data = [
27+
'storeId' => $this->storeId,
28+
'email' => $this->email,
29+
'name' => $this->name,
30+
'countryCode' => $this->countryCode,
31+
'phoneNumber' => $this->phoneNumber,
32+
'productName' => $this->productName,
33+
'productDescription' => $this->productDescription,
34+
'redirectUrl' => escape_url($this->redirectUrl),
35+
'notifyUrl' => escape_url($this->notifyUrl),
36+
];
37+
38+
$validator = new Validator;
39+
$validation = $validator->make($data, [
40+
'storeId' => 'required|max:255',
41+
'email' => 'required',
42+
'name' => 'required',
43+
'countryCode' => 'required',
44+
'phoneNumber' => 'required',
45+
'productName' => 'required',
46+
'productDescription' => 'required',
47+
'redirectUrl' => 'required|url',
48+
'notifyUrl' => 'required|url',
49+
]);
50+
51+
$validation->validate();
52+
53+
if ($validation->fails()) {
54+
throw new ValidationException($validation->errors());
55+
}
56+
57+
return $data;
58+
}
59+
}

src/RevenueMonster.php

+7
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ 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,
4546
'payment' => Modules\PaymentModule::class,
4647
'ekyc' => Modules\EkycModule::class,
48+
'tokenized' => Modules\TokenizedCustomerModule::class,
4749
];
4850

4951
public function __construct(array $arguments = [])
@@ -123,6 +125,11 @@ public function getPrivateKey()
123125
{
124126
return $this->privateKey;
125127
}
128+
129+
public function getPublicKey()
130+
{
131+
return $this->publicKey;
132+
}
126133

127134
public function __get($name)
128135
{

0 commit comments

Comments
 (0)