|
| 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\Middlewares; |
| 10 | + |
| 11 | +use Closure; |
| 12 | +use Omnipay\Common\AbstractGateway; |
| 13 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Vuong Minh <vuongxuongminh@gmail.com> |
| 17 | + * @since 1.1.0 |
| 18 | + */ |
| 19 | +class CompletePurchaseMiddleware |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @param \Illuminate\Http\Request $request |
| 23 | + * @param \Closure $next |
| 24 | + * @return mixed |
| 25 | + */ |
| 26 | + public function handle($request, Closure $next, string $gateway, $successHandle, $failureHandle = null) |
| 27 | + { |
| 28 | + /** @var AbstractGateway $gateway */ |
| 29 | + $gateway = app('omnipay')->gateway($gateway); |
| 30 | + |
| 31 | + if (!$gateway->supportsCompletePurchase()) { |
| 32 | + throw new \InvalidArgumentException('Gateway configured not support complete purchase method!'); |
| 33 | + } |
| 34 | + |
| 35 | + $response = $gateway->completePurchase()->send(); |
| 36 | + |
| 37 | + if ($response->isSuccessful()) { |
| 38 | + return app()->call($successHandle, [$response]); |
| 39 | + } |
| 40 | + |
| 41 | + if ($failureHandle) { |
| 42 | + return app()->call($failureHandle, [$response]); |
| 43 | + } |
| 44 | + |
| 45 | + throw new BadRequestHttpException('Bad request'); |
| 46 | + } |
| 47 | +} |
0 commit comments