Browse Source

3.0

tags/3.0
Lim Eng Shun 3 years ago
parent
commit
af60192ed7
5 changed files with 46 additions and 27 deletions
  1. 1
    1
      README.md
  2. 1
    1
      composer.json
  3. 11
    0
      res/view/response.php
  4. 33
    14
      src/SenangPayDriver.php
  5. 0
    11
      src/SenangPayServiceProvider.php

+ 1
- 1
README.md View File

13
 }
13
 }
14
 ```
14
 ```
15
 ```shell script
15
 ```shell script
16
-composer install mirfalah/laravel-payment-senangpay ^1.0
16
+composer require mirfalah/laravel-payment-senangpay ^1.0
17
 ```
17
 ```

+ 1
- 1
composer.json View File

6
     "readme": "README.md",
6
     "readme": "README.md",
7
     "require": {
7
     "require": {
8
         "php": ">=7.1",
8
         "php": ">=7.1",
9
-        "mirfalah/laravel-payment": "^2.0"
9
+        "mirfalah/laravel-payment": "^3.0"
10
     },
10
     },
11
     "license": "Mirfalah-Tech",
11
     "license": "Mirfalah-Tech",
12
     "authors": [
12
     "authors": [

+ 11
- 0
res/view/response.php View File

1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<body>
4
+<form action="<?= e($this->endpoint) ?>" method="post">
5
+<?php foreach ($params as $key => $value): ?>
6
+<input type="hidden" name="<?= e($key) ?>" value="<?= e($value) ?>">
7
+<?php endforeach; ?>
8
+</form>
9
+<script>document.getElementsByTagName('form')[0].submit();</script>
10
+</body>
11
+</html>

+ 33
- 14
src/SenangPayDriver.php View File

4
 namespace MirfalahTech\Laravel\Payment\Gateway\SenangPay;
4
 namespace MirfalahTech\Laravel\Payment\Gateway\SenangPay;
5
 
5
 
6
 
6
 
7
+use Illuminate\Contracts\Config\Repository;
7
 use Illuminate\Http\Request;
8
 use Illuminate\Http\Request;
8
 use Illuminate\Support\Facades\Response as Res;
9
 use Illuminate\Support\Facades\Response as Res;
9
 use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
10
 use MirfalahTech\Laravel\Payment\Contracts\PayableEntity;
36
      */
37
      */
37
     protected $keyMap;
38
     protected $keyMap;
38
 
39
 
39
-    public function __construct(string $endpoint, string $merchant_id, string $secret_key, array $keyMap)
40
+    public function __construct(Repository $config, array $options)
40
     {
41
     {
41
-        $this->endpoint = rtrim($endpoint, '/');
42
-        $this->merchant_id = $merchant_id;
43
-        $this->secret_key = $secret_key;
44
-        $this->keyMap = $keyMap;
42
+        $finalConfig = array_merge($config->get('payment.gateway'), $options);
43
+        $this->endpoint = rtrim($finalConfig['endpoint'], '/');
44
+        $this->merchant_id = $finalConfig['merchant_id'];
45
+        $this->secret_key = $finalConfig['secret_key'];
46
+        $this->keyMap = $finalConfig['key_map'];
45
     }
47
     }
46
 
48
 
47
     public function createPaymentURL(PayableEntity $payable, array $options = []): string
49
     public function createPaymentURL(PayableEntity $payable, array $options = []): string
48
     {
50
     {
49
-        $detail = $payable->getBillDescription();
50
-        $amount = $payable->getBillAmount();
51
-        $order_id = $payable->getBillId();
52
-
53
-        $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail);
54
-        $amount = sprintf('%0.2f', $amount);
55
-        $hash = md5($this->secret_key . $detail . $amount . $order_id);
56
-
57
         return "$this->endpoint/payment/$this->merchant_id?" .
51
         return "$this->endpoint/payment/$this->merchant_id?" .
58
-            http_build_query(compact('detail', 'order_id', 'amount', 'hash'));
52
+            http_build_query($this->preparePaymentParam($payable, $options));
59
     }
53
     }
60
 
54
 
61
     public function verifyGatewayReturn(Request $request): bool
55
     public function verifyGatewayReturn(Request $request): bool
93
         $query = preg_replace('/(^|&)'.$key.'[^&]*($|&)/','$1hash=[HASH]$2', $query);
87
         $query = preg_replace('/(^|&)'.$key.'[^&]*($|&)/','$1hash=[HASH]$2', $query);
94
         return md5($this->secret_key . '?' . $query);
88
         return md5($this->secret_key . '?' . $query);
95
     }
89
     }
90
+
91
+    public function isPostMethodSupported(): bool
92
+    {
93
+        return true;
94
+    }
95
+
96
+    public function createPostMethodPayment(PayableEntity $payable, array $options = []): Response
97
+    {
98
+        $params = $this->preparePaymentParam($payable, $options);
99
+        ob_start();
100
+        include __DIR__.'../res/view/response.php';
101
+        return new Response(ob_get_clean());
102
+    }
103
+
104
+    protected function preparePaymentParam(PayableEntity $payable, array $options = []): array{
105
+        $detail = $payable->getBillDescription();
106
+        $amount = $payable->getBillAmount();
107
+        $order_id = $payable->getBillId();
108
+
109
+        $detail = preg_replace('/[^A-Z0-9,\-_]/i', '_', $detail);
110
+        $amount = sprintf('%0.2f', $amount);
111
+        $hash = md5($this->secret_key . $detail . $amount . $order_id);
112
+
113
+        return compact('detail', 'order_id', 'amount', 'hash');
114
+    }
96
 }
115
 }

+ 0
- 11
src/SenangPayServiceProvider.php View File

32
             ],
32
             ],
33
             $config->get('payment.gateway.senangpay', [])
33
             $config->get('payment.gateway.senangpay', [])
34
         ));
34
         ));
35
-
36
-        $this->app->afterResolving('payment', function (PaymentManager $manager) use ($config) {
37
-            $manager->extend('senangpay', function () use ($config) {
38
-                return new SenangPayDriver(
39
-                    $config->get('payment.gateway.senangpay.endpoint'),
40
-                    $config->get('payment.gateway.senangpay.merchant_id'),
41
-                    $config->get('payment.gateway.senangpay.secret_key'),
42
-                    $config->get('payment.gateway.senangpay.key_map')
43
-                );
44
-            });
45
-        });
46
     }
35
     }
47
 }
36
 }

Loading…
Cancel
Save