Skip to content

Commit

Permalink
Merge pull request #7 from phpviet/1.1
Browse files Browse the repository at this point in the history
1.1
  • Loading branch information
vuongxuongminh authored Jul 4, 2020
2 parents 09d824a + a417a49 commit 532802d
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

### 1.1.0

+ 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
complete purchase request.


### 1.0.1

+ Hổ trợ Laravel 6 & 7.
3 changes: 3 additions & 0 deletions docs/VNPay.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ if ($response->isSuccessful()) {
}
```

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ệ
của request, xem thêm tại [đây](common/CompletePurchaseMiddleware.md).

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).

### Kiểm tra thông tin `IPN` do VNPay gửi sang:
Expand Down
3 changes: 3 additions & 0 deletions docs/VTCPay.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ if ($response->isSuccessful()) {
}
```

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ệ
của request, xem thêm tại [đây](common/CompletePurchaseMiddleware.md).

Kham khảo thêm các tham trị khi VTCPay trả về tại [đây](https://vtcpay.vn/tai-lieu-tich-hop-website).


Expand Down
37 changes: 37 additions & 0 deletions docs/common/CompletePurchaseMiddleware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Complete purchase middleware
==================================================

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
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ý
nghiệp vụ khi thanh toán thành công hoặc thất bại.

Để sử dụng middleware này thì bạn cần khai báo nó vào `routeMiddleware`:

```php
// Within App\Http\Kernel Class...

protected $routeMiddleware = [
'completePurchase' => \PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware::class
];
```

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
ví dụ:

```php
Route::get('/complete-purchase', function (\Illuminate\Http\Request $request) {
/** @var \Omnipay\Common\Message\ResponseInterface $completePurchaseResponse */
$completePurchaseResponse = $request->attributes->get('completePurchaseResponse');

if ($completePurchaseResponse->isSuccessful()) {
// xử lý logic thanh toán thành công.
} elseif ($completePurchaseResponse->isCancelled()) {
// khi khách hủy giao dịch.
} else {
// các trường hợp khác
}

})->middleware('completePurchase:MoMoAIO');
```

Parameter truyền vào middleware chính là gateway name của bạn ở ví dụ trên nó là `MoMoAIO` gateway.
3 changes: 3 additions & 0 deletions docs/momo/AllInOne.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
}
```

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ệ
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).

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).

### Kiểm tra thông tin `notifyUrl` do MoMo gửi sang:
Expand Down
3 changes: 3 additions & 0 deletions docs/onepay/Domestic.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
}
```

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ệ
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).

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).

### Kiểm tra thông tin `IPN` do OnePay gửi sang:
Expand Down
3 changes: 3 additions & 0 deletions docs/onepay/International.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ if ($response->isSuccessful()) {
}
```

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ệ
của request, xem thêm tại [đây](../common/CompletePurchaseMiddleware.md).

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).

### Kiểm tra thông tin `IPN` do OnePay gửi sang:
Expand Down
46 changes: 46 additions & 0 deletions src/Middleware/CompletePurchaseMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @link https:/phpviet/laravel-omnipay
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/

namespace PHPViet\Laravel\Omnipay\Middleware;

use Closure;
use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Exception\OmnipayException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

/**
* @author Vuong Minh <[email protected]>
* @since 1.1.0
*/
class CompletePurchaseMiddleware
{
/**
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, string $gateway)
{
/** @var AbstractGateway $gateway */
$gateway = app('omnipay')->gateway($gateway);

if (! $gateway->supportsCompletePurchase()) {
throw new \InvalidArgumentException('Gateway configured not support complete purchase method!');
}

try {
$response = $gateway->completePurchase()->send();
} catch (OmnipayException $e) {
throw new BadRequestHttpException($e->getMessage());
}

$request->attributes->set('completePurchaseResponse', $response);

return $next($request);
}
}
70 changes: 70 additions & 0 deletions tests/Middleware/CompletePurchaseMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* @link https:/phpviet/laravel-omnipay
*
* @copyright (c) PHP Viet
* @license [MIT](https://opensource.org/licenses/MIT)
*/

namespace PHPViet\Laravel\Omnipay\Tests\Middleware;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Omnipay\Common\Message\ResponseInterface;
use PHPViet\Laravel\Omnipay\Middleware\CompletePurchaseMiddleware;
use PHPViet\Laravel\Omnipay\Tests\TestCase;

/**
* @author Vuong Minh <[email protected]>
* @since 1.0.0
*/
class CompletePurchaseMiddlewareTest extends TestCase
{
public function getEnvironmentSetUp($app): void
{
$_GET['partnerCode'] = 'test';
$_GET['accessKey'] = 'test';
$_GET['requestId'] = 'test';
$_GET['amount'] = 'test';
$_GET['orderId'] = 'test';
$_GET['orderInfo'] = 'test';
$_GET['orderType'] = 'test';
$_GET['transId'] = 'test';
$_GET['message'] = 'test';
$_GET['localMessage'] = 'test';
$_GET['responseTime'] = 'test';
$_GET['errorCode'] = 'test';
$_GET['extraData'] = 'test';
$_GET['signature'] = 'test';
$_GET['payType'] = 'test';

parent::getEnvironmentSetUp($app);

$router = $app->get('router');
$router->middleware(CompletePurchaseMiddleware::class);
$router->aliasMiddleware('completePurchase', CompletePurchaseMiddleware::class);
$router->get(
'/test',
function (Request $request) {
$completePurchaseResponse = $request->attributes->get('completePurchaseResponse');

$this->assertInstanceOf(ResponseInterface::class, $completePurchaseResponse);

return new Response('ok', 200);
}
)->middleware('completePurchase:MoMoAIO');
}

public function testCanGetResponse()
{
$response = $this->get('/test');
$response->assertOk();
}

public function testMissingRequestQueryParam()
{
unset($_GET['signature']);
$response = $this->get('/test');
$response->assertStatus(400);
}
}

0 comments on commit 532802d

Please sign in to comment.