Skip to content

Commit

Permalink
Merge pull request #42 from michaelpo-rm/main
Browse files Browse the repository at this point in the history
  • Loading branch information
rexlow authored May 19, 2023
2 parents 77116a1 + 16aca29 commit 44d0379
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/Modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ public function generateSignature($method, $url, $nonceStr, $timestamp, $payload
$signature = base64_encode($signature);
return $signature;
}

public function verifySignature($signature, $method, $url, $nonceStr, $timestamp, $base64Payload = null)
{
$res = openssl_pkey_get_public($this->rm->getPublicKey());
$signType = 'sha256';

$arr = array();
if ($base64Payload) {
array_push($arr, "data=$base64Payload");
}
array_push($arr, "method=$method");
array_push($arr, "nonceStr=$nonceStr");
array_push($arr, "requestUrl=$url");
array_push($arr, "signType=$signType");
array_push($arr, "timestamp=$timestamp");


$result = openssl_verify(join("&", $arr), base64_decode($signature), $res, OPENSSL_ALGO_SHA256);
openssl_free_key($res);

return $result;
}

protected function callApi(string $method, $url, $payload = null)
{
Expand All @@ -67,6 +89,9 @@ protected function callApi(string $method, $url, $payload = null)
case 'delete':
$request = Request::delete($url);
break;
case 'put':
$request = Request::put($url);
break;
default:
$request = Request::get($url);
break;
Expand Down Expand Up @@ -99,6 +124,11 @@ protected function post(string $url, $payload = null)
return $this->callApi('post', $url, $payload);
}

protected function put(string $url)
{
return $this->callApi('put', $url);
}

/**
* magic function to return response
*
Expand Down
100 changes: 100 additions & 0 deletions src/Modules/TokenizedCustomerModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace RevenueMonster\SDK\Modules;

use RevenueMonster\SDK\Request\TokenizedCustomer;
use RevenueMonster\SDK\Request\RecurringCustomer;
use RevenueMonster\SDK\Request\CustomerOrderAmount;

class TokenizedCustomerModule extends Module
{

/**
* Create Recurring Customer
* @param RecurringCustomer $args
* @return stdClass
* @throws ApiException
*/
public function createRecurringCustomer($args)
{
if ($args instanceof RecurringCustomer) {
$args = $args->jsonSerialize();
}
// print_r($args);

$uri = $this->getOpenApiUrl('v3', '/recurring-payment');
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
}

/**
* Create Tokenized Customer
* @param TokenizedCustomer $args
* @return stdClass
* @throws ApiException
*/
public function createTokenizedPayment($args)
{
if ($args instanceof TokenizedCustomer) {
$args = $args->jsonSerialize();
}
// print_r($args);

$uri = $this->getOpenApiUrl('v3', '/tokenized-payment');
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
}


/**
* Toggle customer status by Customer ID
* @param string $customerId
* @return stdClass
* @throws ApiException
*/
public function toggleCustomerStatusById(string $customerId)
{
$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/status");
return $this->mapResponse($this->callApi('put', $uri)->send());
}

/**
* Get Customer Orders by Customer ID
* @param string $customerId
* @return stdClass
* @throws ApiException
*/
public function getCustomerOrdersById(string $customerId)
{
$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/orders");
return $this->mapResponse($this->callApi('get', $uri)->send());
}


/**
* Create Customer Order by Customer ID
* @param string $customerId, obj $args
* @return stdClass
* @throws ApiException
*/
public function createCustomerOrderById(string $customerId, $args)
{
if ($args instanceof CustomerOrderAmount) {
$args = $args->jsonSerialize();
}
// print_r($args);

$uri = $this->getOpenApiUrl('v3', "/customer/$customerId/order");
return $this->mapResponse($this->callApi('post', $uri, $args)->send());
}

/**
* Get Customer Tokens ( Customer ID based on your side pass in to Web Payment )
* @param string $customerId
* @return stdClass
* @throws ApiException
*/
public function getCustomerTokensById(string $customerId)
{
$uri = $this->getOpenApiUrl('v3', "/payment/tokens/$customerId");
return $this->mapResponse($this->callApi('get', $uri)->send());
}
}
24 changes: 24 additions & 0 deletions src/Request/CustomerOrderAmount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace RevenueMonster\SDK\Request;

use Exception;
use JsonSerializable;
use Rakit\Validation\Validator;
use RevenueMonster\SDK\Exceptions\ValidationException;

class CustomerOrderAmount implements JsonSerializable
{
public $currency = 'MYR';
public $amount = 0;

public function jsonSerialize()
{
$data = [
'currency' => $this->currency,
'amount' => $this->amount,
];

return $data;
}
}
70 changes: 70 additions & 0 deletions src/Request/RecurringCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace RevenueMonster\SDK\Request;

use Exception;
use JsonSerializable;
use Rakit\Validation\Validator;
use RevenueMonster\SDK\Exceptions\ValidationException;

class RecurringCustomer implements JsonSerializable
{
public $storeId = '';
public $email = '';
public $name = '';
public $countryCode = '';
public $phoneNumber = '';
public $productName = '';
public $productDescription = '';
public $currency = 'MYR';
public $amount = 0;
public $redirectUrl = '';
public $notifyUrl = '';
public $recurringInterval = '';
public $recurringTarget = '';
public $recurringRepetition = 1;

public function jsonSerialize()
{
$data = [
'storeId' => $this->storeId,
'email' => $this->email,
'name' => $this->name,
'countryCode' => $this->countryCode,
'phoneNumber' => $this->phoneNumber,
'productName' => $this->productName,
'productDescription' => $this->productDescription,
'currency' => $this->currency,
'amount' => $this->amount,
'redirectUrl' => escape_url($this->redirectUrl),
'notifyUrl' => escape_url($this->notifyUrl),
'recurringInterval' => $this->recurringInterval,
'recurringTarget' => $this->recurringTarget,
'recurringRepetition' => $this->recurringRepetition,
];

$validator = new Validator;
$validation = $validator->make($data, [
'storeId' => 'required|max:255',
'email' => 'required',
'name' => 'required',
'countryCode' => 'required',
'phoneNumber' => 'required',
'productName' => 'required',
'productDescription' => 'required',
'currency' => 'required',
'amount' => 'required',
'redirectUrl' => 'required|url',
'notifyUrl' => 'required|url',
'recurringInterval' => 'required|in:WEEKLY,MONTHLY',
]);

$validation->validate();

if ($validation->fails()) {
throw new ValidationException($validation->errors());
}

return $data;
}
}
59 changes: 59 additions & 0 deletions src/Request/TokenizedCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace RevenueMonster\SDK\Request;

use Exception;
use JsonSerializable;
use Rakit\Validation\Validator;
use RevenueMonster\SDK\Exceptions\ValidationException;

class TokenizedCustomer implements JsonSerializable
{
public $storeId = '';
public $email = '';
public $name = '';
public $countryCode = '';
public $phoneNumber = '';
public $productName = '';
public $productDescription = '';
// public $currency = 'MYR';
// public $amount = 0;
public $redirectUrl = '';
public $notifyUrl = '';

public function jsonSerialize()
{
$data = [
'storeId' => $this->storeId,
'email' => $this->email,
'name' => $this->name,
'countryCode' => $this->countryCode,
'phoneNumber' => $this->phoneNumber,
'productName' => $this->productName,
'productDescription' => $this->productDescription,
'redirectUrl' => escape_url($this->redirectUrl),
'notifyUrl' => escape_url($this->notifyUrl),
];

$validator = new Validator;
$validation = $validator->make($data, [
'storeId' => 'required|max:255',
'email' => 'required',
'name' => 'required',
'countryCode' => 'required',
'phoneNumber' => 'required',
'productName' => 'required',
'productDescription' => 'required',
'redirectUrl' => 'required|url',
'notifyUrl' => 'required|url',
]);

$validation->validate();

if ($validation->fails()) {
throw new ValidationException($validation->errors());
}

return $data;
}
}
7 changes: 7 additions & 0 deletions src/RevenueMonster.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class RevenueMonster
// private $tokenPath = '/storage/access_token.json';

private $modules = [
'module' => Modules\Module::class,
'merchant' => Modules\MerchantModule::class,
'store' => Modules\StoreModule::class,
'user', Modules\UserModule::class,
'payment' => Modules\PaymentModule::class,
'ekyc' => Modules\EkycModule::class,
'tokenized' => Modules\TokenizedCustomerModule::class,
];

public function __construct(array $arguments = [])
Expand Down Expand Up @@ -123,6 +125,11 @@ public function getPrivateKey()
{
return $this->privateKey;
}

public function getPublicKey()
{
return $this->publicKey;
}

public function __get($name)
{
Expand Down
Loading

0 comments on commit 44d0379

Please sign in to comment.