-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from phpviet/1.1
1.1
- Loading branch information
Showing
9 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |