Parcourir la source

some fixes and key map

tags/2.0.3
0nepeop1e il y a 4 ans
Parent
révision
266875e3e9
2 fichiers modifiés avec 31 ajouts et 11 suppressions
  1. 26
    11
      src/SenangPayDriver.php
  2. 5
    0
      src/SenangPayServiceProvider.php

+ 26
- 11
src/SenangPayDriver.php Voir le fichier

@@ -31,10 +31,17 @@ class SenangPayDriver implements PaymentGatewayDriver
31 31
      */
32 32
     protected $secret_key;
33 33
 
34
-    public function __construct(string $endpoint, string $merchant_id, string $secret_key){
34
+    /**
35
+     * @var array
36
+     */
37
+    protected $keyMap;
38
+
39
+    public function __construct(string $endpoint, string $merchant_id, string $secret_key, array $keyMap)
40
+    {
35 41
         $this->endpoint = rtrim($endpoint, '/');
36 42
         $this->merchant_id = $merchant_id;
37 43
         $this->secret_key = $secret_key;
44
+        $this->keyMap = $keyMap;
38 45
     }
39 46
 
40 47
     public function createPaymentURL(PayableEntity $payable, array $options = []): string
@@ -45,36 +52,44 @@ class SenangPayDriver implements PaymentGatewayDriver
45 52
 
46 53
         $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail);
47 54
         $amount = sprintf('%0.2f', $amount);
48
-        $hash = md5($this->secret_key.$detail.$amount.$order_id);
55
+        $hash = md5($this->secret_key . $detail . $amount . $order_id);
49 56
 
50
-        return "$this->endpoint/payment/$this->merchant_id?".
57
+        return "$this->endpoint/payment/$this->merchant_id?" .
51 58
             http_build_query(compact('detail', 'order_id', 'amount', 'hash'));
52 59
     }
53 60
 
54 61
     public function verifyGatewayReturn(Request $request): bool
55 62
     {
56
-        $hash = $request->query->get('hash');
63
+        $hash = $request->query->get($this->keyMap['hash']);
57 64
         return $hash == $this->computeHash($request->request->all());
58 65
     }
59 66
 
60 67
     public function verifyGatewayCallback(Request $request, Response &$response = null): bool
61 68
     {
62 69
         $response = Res::make('OK');
63
-        $hash = $request->request->get('hash');
70
+        $hash = $request->request->get($this->keyMap['hash']);
64 71
         return $hash == $this->computeHash($request->request->all());
65 72
     }
66 73
 
67
-    public function getBillIdFromRequest(Request $request): ?string{
68
-        return $request->get('order_id');
74
+    public function getBillIdFromRequest(Request $request): ?string
75
+    {
76
+        return $request->get($this->keyMap['order_id']);
69 77
     }
70 78
 
71 79
     public function getBillStatus(Request $request): ?int
72 80
     {
73
-        return $request->request->get('status') == 1 ? Payment::SUCCESS : Payment::PENDING;
81
+        $status = $request->get($this->keyMap['status_id']);
82
+        if ($status == null) {
83
+            return null;
84
+        } else {
85
+            return $status == 1 ? Payment::SUCCESS : Payment::PENDING;
86
+        }
74 87
     }
75 88
 
76
-    public function computeHash($payload){
77
-        $payload['hash'] = '[HASH]';
78
-        return md5($this->secret_key.'?'.http_build_query($payload));
89
+    public function computeHash($payload)
90
+    {
91
+        $query = http_build_query($payload);
92
+        $query = preg_replace('/(^|&)hash=[^&]*($|&)/','$1hash=[HASH]$2', $query);
93
+        return md5($this->secret_key . '?' . $query);
79 94
     }
80 95
 }

+ 5
- 0
src/SenangPayServiceProvider.php Voir le fichier

@@ -24,6 +24,11 @@ class SenangPayServiceProvider extends ServiceProvider
24 24
                 'endpoint' => env('SENANGPAY_ENDPOINT', 'https://app.senangpay.my/'),
25 25
                 'merchant_id' => env('SENANGPAY_MERCHANT_ID'),
26 26
                 'secret_key' => env('SENANGPAY_SECRET_KEY'),
27
+                'key_map' => [
28
+                    'status_id' => 'status_id',
29
+                    'order_id' => 'order_id',
30
+                    'hash' => 'hash'
31
+                ],
27 32
             ],
28 33
             $config->get('payment.gateway.senangpay', [])
29 34
         ));

Chargement…
Annuler
Enregistrer