Skip to content

Commit

Permalink
added support to hex key
Browse files Browse the repository at this point in the history
  • Loading branch information
alifaraun committed Nov 13, 2024
1 parent 05ac177 commit 8a5aa80
Show file tree
Hide file tree
Showing 11 changed files with 821 additions and 801 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ return [
// Secure key
'key' => env('MOAMALATPAY_KEY'),

// Is key hex
'key_hex' => env('MOAMALATPAY_KEY_HEX', true),

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -184,14 +186,14 @@ MOAMALATPAY_PRODUCTION=
```bash
Merchant id : 10081014649
Terminal Id : 99179395
Secure key : 39636630633731362D663963322D346362642D386531662D633963303432353936373431
Secure key : 3a488a89b3f7993476c252f017c488bb
```

### Card

```bash
Card : 6394993077260781
EXP : 12/24
Card : 6395003016111159
EXP : 02/26
OTP : 111111
```

Expand Down
1,575 changes: 785 additions & 790 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/Http/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ public function generateSecureKey(GenerateSecureKeyRequest $request)
$MerchantId = config('moamalat-pay.merchant_id');
$amount = $request->amount;
$merchantReference = $request->merchantReference;
$key = pack('H*', config('moamalat-pay.key'));
if (config('moamalat-pay.key_hex')) {
$key = hex2bin(config('moamalat-pay.key'));
} else {
$key = pack('H*', config('moamalat-pay.key'));
}
$DateTimeLocalTrxn = time();
$encode_data = "Amount={$amount}&DateTimeLocalTrxn={$DateTimeLocalTrxn}&MerchantId={$MerchantId}&MerchantReference={$merchantReference}&TerminalId={$TerminalId}";

Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ protected function validateSecureHas($secureHash, $Amount, $Currency, $DateTimeL
try {
$encode_data = "Amount=$Amount&Currency=$Currency&DateTimeLocalTrxn=$DateTimeLocalTrxn&MerchantId=$MerchantId&TerminalId=$TerminalId";
$key = pack('H*', config('moamalat-pay.notification.key'));
if (config('moamalat-pay.key_hex')) {
$key = hex2bin(config('moamalat-pay.notification.key'));
} else {
$key = pack('H*', config('moamalat-pay.notification.key'));
}

return strtoupper(hash_hmac('sha256', $encode_data, $key)) === strtoupper($secureHash);
} catch (Exception $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/MoamalatPayProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function register()
$this->mergeConfigFrom(__DIR__.'/../config/moamalat-pay.php', 'moamalat-pay');

$this->app->singleton('moamalat-pay', function ($app) {
return new Pay();
return new Pay;
});

$this->app->singleton('moamalat-pay-refund', function ($app) {
return new Refund();
return new Refund;
});
}
}
6 changes: 5 additions & 1 deletion src/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public function __construct()
{
$this->terminal_id = config('moamalat-pay.terminal_id');
$this->merchant_id = config('moamalat-pay.merchant_id');
$this->key = pack('H*', config(('moamalat-pay.key')));
if (config('moamalat-pay.key_hex')) {
$this->key = hex2bin(config('moamalat-pay.key'));
} else {
$this->key = pack('H*', config('moamalat-pay.key'));
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public function __construct($networkReference, $merchantReference)
{
$TerminalId = config('moamalat-pay.terminal_id');
$MerchantId = config('moamalat-pay.merchant_id');
$key = pack('H*', config(('moamalat-pay.key')));

if (config('moamalat-pay.key_hex')) {
$key = hex2bin(config('moamalat-pay.key'));
} else {
$key = pack('H*', config('moamalat-pay.key'));
}

$DateTimeLocalTrxn = time();
$encode_data = "DateTimeLocalTrxn={$DateTimeLocalTrxn}&MerchantId={$MerchantId}&TerminalId={$TerminalId}";

Expand Down
4 changes: 3 additions & 1 deletion src/config/moamalat-pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// Secure key
'key' => env('MOAMALATPAY_KEY'),

// Is key hex
'key_hex' => env('MOAMALATPAY_KEY_HEX', true),

/*
|--------------------------------------------------------------------------
| Production
Expand Down Expand Up @@ -51,7 +54,6 @@
*/
'enable_cancel_event_on_success' => true,


/*
|--------------------------------------------------------------------------
| Generate Secure Hash api
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/NotificationsAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function test_config_loaded()
$this->assertNotNull(config('moamalat-pay.merchant_id'));
$this->assertNotNull(config('moamalat-pay.terminal_id'));
$this->assertNotNull(config('moamalat-pay.key'));
$this->assertNotNull(config('moamalat-pay.key_hex'));
$this->assertNotNull(config('moamalat-pay.production'));
$this->assertNotNull(config('moamalat-pay.show_logs'));
$this->assertNotNull(config('moamalat-pay.notification.key'));
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/RefundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function test_refund_by_network_reference()
public function test_already_refunded()
{
$this->expectExceptionMessage('Transaction Already Refunded');
(new Refund())->refundByNetworkReference('testing_already_refunded', '10');
(new Refund)->refundByNetworkReference('testing_already_refunded', '10');
}

/**
Expand All @@ -127,7 +127,7 @@ public function test_authentication_failed()
{
$this->expectExceptionMessage('Authentication failed.');
Config::set('moamalat-pay.merchant_id', 'testing_authentication_failed');
(new Refund())->refundByNetworkReference('226214209277', '10');
(new Refund)->refundByNetworkReference('226214209277', '10');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function getEnvironmentSetUp($app)
'merchant_id' => '10004188779',
'terminal_id' => '49077229',
'key' => '39353638663431622D303136622D343235322D623330632D383361633838383965373965',
'key_hex' => false,
'production' => false,
'show_logs' => true,
'generate-securekey' => [
Expand Down

0 comments on commit 8a5aa80

Please sign in to comment.