Skip to content

Commit 532802d

Browse files
Merge pull request #7 from phpviet/1.1
1.1
2 parents 09d824a + a417a49 commit 532802d

File tree

9 files changed

+174
-0
lines changed

9 files changed

+174
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Tất cả lịch sử tiến trình phát triển thư viện
44

5+
### 1.1.0
6+
7+
+ Thêm lớp đối tượng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` giảm nghiệp vụ xác minh tính hợp lệ của
8+
complete purchase request.
9+
10+
511
### 1.0.1
612

713
+ Hổ trợ Laravel 6 & 7.

docs/VNPay.md

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ if ($response->isSuccessful()) {
4747
}
4848
```
4949

50+
Hoặc bạn có thể sử dụng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` để giảm bớt nghiệp vụ xử lý kiểm tra tính hợp lệ
51+
của request, xem thêm tại [đây](common/CompletePurchaseMiddleware.md).
52+
5053
Kham khảo thêm các tham trị khi VNPay trả về tại [đây](https://sandbox.vnpayment.vn/apis/docs/huong-dan-tich-hop/#code-returnurl).
5154

5255
### Kiểm tra thông tin `IPN` do VNPay gửi sang:

docs/VTCPay.md

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ if ($response->isSuccessful()) {
4545
}
4646
```
4747

48+
Hoặc bạn có thể sử dụng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` để giảm bớt nghiệp vụ xử lý kiểm tra tính hợp lệ
49+
của request, xem thêm tại [đây](common/CompletePurchaseMiddleware.md).
50+
4851
Kham khảo thêm các tham trị khi VTCPay trả về tại [đây](https://vtcpay.vn/tai-lieu-tich-hop-website).
4952

5053

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Complete purchase middleware
2+
==================================================
3+
4+
Lớp `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` là một [route middleware](https://laravel.com/docs/7.x/middleware#assigning-middleware-to-routes) giúp bạn
5+
giảm bớt nghiệp vụ xác thực phiên giao dịch của khách có thành công hay không hay có phải là do nhà cung cấp dịch vụ gửi về hay không, thay vào đó bạn chỉ cần tập trung vào xử lý
6+
nghiệp vụ khi thanh toán thành công hoặc thất bại.
7+
8+
Để sử dụng middleware này thì bạn cần khai báo nó vào `routeMiddleware`:
9+
10+
```php
11+
// Within App\Http\Kernel Class...
12+
13+
protected $routeMiddleware = [
14+
'completePurchase' => \PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware::class
15+
];
16+
```
17+
18+
Và gắn nó vào route đảm nhiệm nghiệp vụ khi nhà cung cấp dịch vụ redirect khách về site của bạn
19+
ví dụ:
20+
21+
```php
22+
Route::get('/complete-purchase', function (\Illuminate\Http\Request $request) {
23+
/** @var \Omnipay\Common\Message\ResponseInterface $completePurchaseResponse */
24+
$completePurchaseResponse = $request->attributes->get('completePurchaseResponse');
25+
26+
if ($completePurchaseResponse->isSuccessful()) {
27+
// xử lý logic thanh toán thành công.
28+
} elseif ($completePurchaseResponse->isCancelled()) {
29+
// khi khách hủy giao dịch.
30+
} else {
31+
// các trường hợp khác
32+
}
33+
34+
})->middleware('completePurchase:MoMoAIO');
35+
```
36+
37+
Parameter truyền vào middleware chính là gateway name của bạn ở ví dụ trên nó là `MoMoAIO` gateway.

docs/momo/AllInOne.md

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
4444
}
4545
```
4646

47+
Hoặc bạn có thể sử dụng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` để giảm bớt nghiệp vụ xử lý kiểm tra tính hợp lệ
48+
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).
49+
4750
Kham khảo thêm các tham trị khi MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=th%c3%b4ng-tin-tham-s%e1%bb%91).
4851

4952
### Kiểm tra thông tin `notifyUrl` do MoMo gửi sang:

docs/onepay/Domestic.md

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
4444
}
4545
```
4646

47+
Hoặc bạn có thể sử dụng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` để giảm bớt nghiệp vụ xử lý kiểm tra tính hợp lệ
48+
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).
49+
4750
Kham khảo thêm các tham trị khi OnePay trả về tại [đây](https://mtf.onepay.vn/developer/resource/documents/docx/quy_trinh_tich_hop-noidia.pdf).
4851

4952
### Kiểm tra thông tin `IPN` do OnePay gửi sang:

docs/onepay/International.md

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
4444
}
4545
```
4646

47+
Hoặc bạn có thể sử dụng `PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware` để giảm bớt nghiệp vụ xử lý kiểm tra tính hợp lệ
48+
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).
49+
4750
Kham khảo thêm các tham trị khi OnePay trả về tại [đây](https://mtf.onepay.vn/developer/resource/documents/docx/quy_trinh_tich_hop-noidia.pdf).
4851

4952
### Kiểm tra thông tin `IPN` do OnePay gửi sang:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-omnipay
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Omnipay\Middleware;
10+
11+
use Closure;
12+
use Omnipay\Common\AbstractGateway;
13+
use Omnipay\Common\Exception\OmnipayException;
14+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15+
16+
/**
17+
* @author Vuong Minh <[email protected]>
18+
* @since 1.1.0
19+
*/
20+
class CompletePurchaseMiddleware
21+
{
22+
/**
23+
* @param \Illuminate\Http\Request $request
24+
* @param \Closure $next
25+
* @return mixed
26+
*/
27+
public function handle($request, Closure $next, string $gateway)
28+
{
29+
/** @var AbstractGateway $gateway */
30+
$gateway = app('omnipay')->gateway($gateway);
31+
32+
if (! $gateway->supportsCompletePurchase()) {
33+
throw new \InvalidArgumentException('Gateway configured not support complete purchase method!');
34+
}
35+
36+
try {
37+
$response = $gateway->completePurchase()->send();
38+
} catch (OmnipayException $e) {
39+
throw new BadRequestHttpException($e->getMessage());
40+
}
41+
42+
$request->attributes->set('completePurchaseResponse', $response);
43+
44+
return $next($request);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/laravel-omnipay
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace PHPViet\Laravel\Omnipay\Tests\Middleware;
10+
11+
use Illuminate\Http\Request;
12+
use Illuminate\Http\Response;
13+
use Omnipay\Common\Message\ResponseInterface;
14+
use PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware;
15+
use PHPViet\Laravel\Omnipay\Tests\TestCase;
16+
17+
/**
18+
* @author Vuong Minh <[email protected]>
19+
* @since 1.0.0
20+
*/
21+
class CompletePurchaseMiddlewareTest extends TestCase
22+
{
23+
public function getEnvironmentSetUp($app): void
24+
{
25+
$_GET['partnerCode'] = 'test';
26+
$_GET['accessKey'] = 'test';
27+
$_GET['requestId'] = 'test';
28+
$_GET['amount'] = 'test';
29+
$_GET['orderId'] = 'test';
30+
$_GET['orderInfo'] = 'test';
31+
$_GET['orderType'] = 'test';
32+
$_GET['transId'] = 'test';
33+
$_GET['message'] = 'test';
34+
$_GET['localMessage'] = 'test';
35+
$_GET['responseTime'] = 'test';
36+
$_GET['errorCode'] = 'test';
37+
$_GET['extraData'] = 'test';
38+
$_GET['signature'] = 'test';
39+
$_GET['payType'] = 'test';
40+
41+
parent::getEnvironmentSetUp($app);
42+
43+
$router = $app->get('router');
44+
$router->middleware(CompletePurchaseMiddleware::class);
45+
$router->aliasMiddleware('completePurchase', CompletePurchaseMiddleware::class);
46+
$router->get(
47+
'/test',
48+
function (Request $request) {
49+
$completePurchaseResponse = $request->attributes->get('completePurchaseResponse');
50+
51+
$this->assertInstanceOf(ResponseInterface::class, $completePurchaseResponse);
52+
53+
return new Response('ok', 200);
54+
}
55+
)->middleware('completePurchase:MoMoAIO');
56+
}
57+
58+
public function testCanGetResponse()
59+
{
60+
$response = $this->get('/test');
61+
$response->assertOk();
62+
}
63+
64+
public function testMissingRequestQueryParam()
65+
{
66+
unset($_GET['signature']);
67+
$response = $this->get('/test');
68+
$response->assertStatus(400);
69+
}
70+
}

0 commit comments

Comments
 (0)