Skip to content

Commit e0f3a14

Browse files
Add complete purchase middleware.
1 parent 09d824a commit e0f3a14

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 <[email protected]>
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

Comments
 (0)