diff --git a/README.md b/README.md index a66814d..4b0b923 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,15 @@ return [ */ 'show_logs' => false, + /* + |-------------------------------------------------------------------------- + | Enable Cancel Event On Success + |-------------------------------------------------------------------------- + | + | If the enable_cancel_event_on_success is set to "true", the cancel event will be triggered even if the payment is successful + | + */ + 'enable_cancel_event_on_success' => true, /* |-------------------------------------------------------------------------- diff --git a/src/config/moamalat-pay.php b/src/config/moamalat-pay.php index a6b21cd..4a19d18 100644 --- a/src/config/moamalat-pay.php +++ b/src/config/moamalat-pay.php @@ -41,6 +41,17 @@ */ 'show_logs' => false, + /* + |-------------------------------------------------------------------------- + | Enable Cancel Event On Success + |-------------------------------------------------------------------------- + | + | If the enable_cancel_event_on_success is set to "true", the cancel event will be triggered even if the payment is successful + | + */ + 'enable_cancel_event_on_success' => true, + + /* |-------------------------------------------------------------------------- | Generate Secure Hash api diff --git a/src/views/pay.blade.php b/src/views/pay.blade.php index 90105ac..0263d81 100644 --- a/src/views/pay.blade.php +++ b/src/views/pay.blade.php @@ -13,11 +13,12 @@ if (window.dispatchEvent) { class MoamalataPay { - constructor(MID, TID, amount, merchantReference = "", debug = false) { + constructor(MID, TID, amount, merchantReference = "", enableCancelEventOnSuccess = true, debug = false) { this.MID = MID; this.TID = TID; this.amount = amount; this.merchantReference = merchantReference; + this.enableCancelEventOnSuccess = enableCancelEventOnSuccess; this.debug = debug; this.isCompleteCalled = false } @@ -58,6 +59,7 @@ class MoamalataPay { this.log("Starting pay , mode => {{ config('moamalat-pay.production') ? 'produciton' : 'test' }}"); let parent_ = this; + this.isCompleteCalled = false; const secureHashResponse = await this.fetchSecureHash(); @@ -93,11 +95,14 @@ class MoamalataPay { }); }, cancelCallback: function() { - if (!this.isCompleteCalled) { - window.dispatchEvent( - new CustomEvent('moamalatCancel') - ) - } + if (!parent_.enableCancelEventOnSuccess && this.isCompleteCalled) { + return; + } + + window.dispatchEvent( + new CustomEvent('moamalatCancel') + ) + parent_.log({ "status": "canceled" }); @@ -114,6 +119,7 @@ class MoamalataPay { "{{ config('moamalat-pay.terminal_id') }}", 0, "", + "{{ config('moamalat-pay.enable_cancel_event_on_success') }}", "{{ config('moamalat-pay.show_logs') }}", )