|
@@ -4,15 +4,26 @@
|
4
|
4
|
namespace MirfalahTech\Laravel\Payment\Gateway\RazerPay;
|
5
|
5
|
|
6
|
6
|
|
|
7
|
+use Illuminate\Contracts\Config\Repository;
|
|
8
|
+use Illuminate\Contracts\Events\Dispatcher;
|
7
|
9
|
use Illuminate\Http\Request;
|
8
|
10
|
use Illuminate\Support\Arr;
|
9
|
11
|
use Illuminate\Support\Facades\Response as Res;
|
10
|
12
|
use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
|
11
|
13
|
use MirfalahTech\Laravel\Payment\Contracts\PaymentGatewayDriver;
|
|
14
|
+use MirfalahTech\Laravel\Payment\Events\CallbackVerifiedEvent;
|
|
15
|
+use MirfalahTech\Laravel\Payment\Events\PaymentFailedEvent;
|
|
16
|
+use MirfalahTech\Laravel\Payment\Events\PaymentPendingEvent;
|
|
17
|
+use MirfalahTech\Laravel\Payment\Events\PaymentSuccessEvent;
|
|
18
|
+use MirfalahTech\Laravel\Payment\Events\ReturnVerifiedEvent;
|
|
19
|
+use MirfalahTech\Laravel\Payment\Facade\Payment;
|
|
20
|
+use MirfalahTech\Laravel\Payment\Traits\BillStatusBoolean;
|
12
|
21
|
use Symfony\Component\HttpFoundation\Response;
|
13
|
22
|
|
14
|
23
|
class RazerPayDriver implements PaymentGatewayDriver
|
15
|
24
|
{
|
|
25
|
+ use BillStatusBoolean;
|
|
26
|
+
|
16
|
27
|
/**
|
17
|
28
|
* @var string
|
18
|
29
|
*/
|
|
@@ -32,12 +43,25 @@ class RazerPayDriver implements PaymentGatewayDriver
|
32
|
43
|
*/
|
33
|
44
|
protected $verify_key;
|
34
|
45
|
|
35
|
|
- public function __construct(string $endpoint, string $merchant_id, string $verify_key, string $secret_key)
|
|
46
|
+ /**
|
|
47
|
+ * @var Repository
|
|
48
|
+ */
|
|
49
|
+ protected $config;
|
|
50
|
+
|
|
51
|
+ /**
|
|
52
|
+ * @var Dispatcher
|
|
53
|
+ */
|
|
54
|
+ protected $events;
|
|
55
|
+
|
|
56
|
+ public function __construct(Repository $config, Dispatcher $events)
|
36
|
57
|
{
|
37
|
|
- $this->endpoint = rtrim($endpoint, '/');
|
38
|
|
- $this->merchant_id = $merchant_id;
|
39
|
|
- $this->secret_key = $secret_key;
|
40
|
|
- $this->verify_key = $verify_key;
|
|
58
|
+ $this->config = $config;
|
|
59
|
+ $this->events = $events;
|
|
60
|
+
|
|
61
|
+ $this->endpoint = rtrim($config->get('payment.gateway.razerpay.endpoint'), '/');
|
|
62
|
+ $this->merchant_id = $config->get('payment.gateway.razerpay.merchant_id');
|
|
63
|
+ $this->secret_key = $config->get('payment.gateway.razerpay.secret_key');
|
|
64
|
+ $this->verify_key = $config->get('payment.gateway.razerpay.verify_key');
|
41
|
65
|
}
|
42
|
66
|
|
43
|
67
|
public function createPaymentURL(PayableEntity $payable, array $options = []): string
|
|
@@ -58,14 +82,38 @@ class RazerPayDriver implements PaymentGatewayDriver
|
58
|
82
|
'amount', 'orderid', 'bill_name', 'bill_email', 'bill_mobile',
|
59
|
83
|
'bill_desc', 'country', 'verify_code', 'currency'
|
60
|
84
|
),
|
|
85
|
+ Arr::only($this->config->get('payment.gateway.razerpay.options', []), ['channel']),
|
61
|
86
|
Arr::only($options, ['channel'])
|
62
|
87
|
);
|
63
|
|
-
|
64
|
88
|
return "$this->endpoint/MOLPay/pay/$this->merchant_id/?" .
|
65
|
89
|
http_build_query($query);
|
66
|
90
|
}
|
67
|
91
|
|
68
|
92
|
public function verifyGatewayReturn(Request $request): bool
|
|
93
|
+ {
|
|
94
|
+ if ($this->verifyRequest($request)) {
|
|
95
|
+ $this->events->dispatch(new ReturnVerifiedEvent('razerpay', $this, $request));
|
|
96
|
+ $this->dispatchStatusEvent($request);
|
|
97
|
+ return true;
|
|
98
|
+ }
|
|
99
|
+ return false;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ public function verifyGatewayCallback(Request $request, Response &$response = null): bool
|
|
103
|
+ {
|
|
104
|
+ $response = $request->request->has('nbcb') ?
|
|
105
|
+ Res::make('CBTOKEN:MPSTATOK') :
|
|
106
|
+ Res::make('OK');
|
|
107
|
+
|
|
108
|
+ if ($this->verifyRequest($request)) {
|
|
109
|
+ $this->events->dispatch(new CallbackVerifiedEvent('razerpay', $this, $request));
|
|
110
|
+ $this->dispatchStatusEvent($request);
|
|
111
|
+ return true;
|
|
112
|
+ }
|
|
113
|
+ return false;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ protected function verifyRequest(Request $request): bool
|
69
|
117
|
{
|
70
|
118
|
$tranID = $request->request->get('tranID');
|
71
|
119
|
$order_id = $request->request->get('orderid');
|
|
@@ -83,16 +131,35 @@ class RazerPayDriver implements PaymentGatewayDriver
|
83
|
131
|
return $key1 == $request->request->get('skey');
|
84
|
132
|
}
|
85
|
133
|
|
86
|
|
- public function verifyGatewayCallback(Request $request, Response &$response = null): bool
|
|
134
|
+ protected function dispatchStatusEvent(Request $request)
|
87
|
135
|
{
|
88
|
|
- $response = $request->request->has('nbcb') ?
|
89
|
|
- Res::make('CBTOKEN:MPSTATOK') :
|
90
|
|
- Res::make('OK');
|
91
|
|
-
|
92
|
|
- return $this->verifyGatewayReturn($request);
|
|
136
|
+ switch ($this->getBillStatus($request)) {
|
|
137
|
+ case Payment::SUCCESS:
|
|
138
|
+ $this->events->dispatch(new PaymentSuccessEvent('razerpay', $this, $request));
|
|
139
|
+ break;
|
|
140
|
+ case Payment::PENDING:
|
|
141
|
+ $this->events->dispatch(new PaymentPendingEvent('razerpay', $this, $request));
|
|
142
|
+ break;
|
|
143
|
+ case Payment::FAILED:
|
|
144
|
+ $this->events->dispatch(new PaymentFailedEvent('razerpay', $this, $request));
|
|
145
|
+ break;
|
|
146
|
+ }
|
93
|
147
|
}
|
94
|
148
|
|
95
|
|
- public function getBillIdFromRequest(Request $request){
|
|
149
|
+ public function getBillIdFromRequest(Request $request): ?string
|
|
150
|
+ {
|
96
|
151
|
return $request->request->get('orderid');
|
97
|
152
|
}
|
|
153
|
+
|
|
154
|
+ public function getBillStatus(Request $request): ?int
|
|
155
|
+ {
|
|
156
|
+ if ($request->request->has('status')) {
|
|
157
|
+ $status = $request->request->get('status');
|
|
158
|
+ return $status == '00' ? Payment::SUCCESS :
|
|
159
|
+ $status == '11' ? Payment::FAILED :
|
|
160
|
+ Payment::PENDING;
|
|
161
|
+ } else {
|
|
162
|
+ return null;
|
|
163
|
+ }
|
|
164
|
+ }
|
98
|
165
|
}
|