|
2 | 2 |
|
3 | 3 | namespace Cone\SimplePay; |
4 | 4 |
|
| 5 | +use Closure; |
5 | 6 | use Cone\SimplePay\Api\TransactionApi; |
6 | 7 | use GuzzleHttp\Client as Http; |
7 | 8 | use GuzzleHttp\ClientInterface; |
8 | 9 | use GuzzleHttp\HandlerStack; |
| 10 | +use GuzzleHttp\Promise\PromiseInterface; |
| 11 | +use GuzzleHttp\Psr7\Utils; |
| 12 | +use Psr\Http\Message\RequestInterface; |
9 | 13 |
|
10 | 14 | class Client |
11 | 15 | { |
@@ -67,14 +71,35 @@ public function client(): ClientInterface |
67 | 71 | { |
68 | 72 | $stack = HandlerStack::create(); |
69 | 73 |
|
70 | | - // Signature Header middleware |
71 | | - // Form data middleware |
| 74 | + $stack->push(function (callable $next): Closure { |
| 75 | + return function (RequestInterface $request, array $options) use ($next): PromiseInterface { |
| 76 | + $modify = [ |
| 77 | + 'body' => [ |
| 78 | + 'merchant' => $this->merchant, |
| 79 | + 'salt' => substr(str_shuffle(md5(microtime())), 0, 32), |
| 80 | + 'sdkVersion' => 'Cone OTP SimplePay PHP Client:' . static::VERSION, |
| 81 | + ], |
| 82 | + ]; |
72 | 83 |
|
73 | | - $client = new Http([ |
| 84 | + return $next(Utils::modifyRequest($request, $modify), $options); |
| 85 | + }; |
| 86 | + }); |
| 87 | + |
| 88 | + $stack->push(function (callable $next): Closure { |
| 89 | + return function (RequestInterface $request, array $options) use ($next): PromiseInterface { |
| 90 | + $modify = [ |
| 91 | + 'set_headers' => [ |
| 92 | + 'Signature' => $this->sign($request->getBody()->getContents()), |
| 93 | + ], |
| 94 | + ]; |
| 95 | + |
| 96 | + return $next(Utils::modifyRequest($request, $modify), $options); |
| 97 | + }; |
| 98 | + }); |
| 99 | + |
| 100 | + return new Http([ |
74 | 101 | 'handler' => $stack, |
75 | 102 | ]); |
76 | | - |
77 | | - return $client; |
78 | 103 | } |
79 | 104 |
|
80 | 105 | /** |
|
0 commit comments