1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gbowo\Adapter\Amplifypay\Plugin; |
4
|
|
|
|
5
|
|
|
use function GuzzleHttp\json_decode; |
6
|
|
|
use function GuzzleHttp\json_encode; |
7
|
|
|
use Gbowo\Plugin\AbstractChargeWithToken; |
8
|
|
|
use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier; |
9
|
|
|
use Gbowo\Exception\InvalidHttpResponseException; |
10
|
|
|
use Gbowo\Exception\TransactionVerficationFailedException; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
class ChargeWithToken extends AbstractChargeWithToken |
14
|
|
|
{ |
15
|
|
|
use KeyVerifier; |
16
|
|
|
|
17
|
|
|
const SUCCESSFUL_TRANSACTION = "Successfull Request"; |
18
|
|
|
|
19
|
|
|
const CHARGE_RETURNING_USER = "/returning/charge"; |
20
|
|
|
|
21
|
|
|
protected $baseUrl; |
22
|
|
|
|
23
|
|
|
protected $apiKeys; |
24
|
|
|
|
25
|
25 |
|
public function __construct(string $baseUrl, array $apiKeys) |
26
|
|
|
{ |
27
|
25 |
|
$this->baseUrl = $baseUrl; |
28
|
25 |
|
$this->apiKeys = $apiKeys; |
29
|
25 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $args |
33
|
|
|
* @return mixed |
34
|
|
|
* @throws \Gbowo\Adapter\AmplifyPay\Exception\TransactionVerficationFailedException |
35
|
|
|
* @throws \Gbowo\Exception\InvalidHttpResponseException |
36
|
|
|
*/ |
37
|
3 |
|
public function handle(array $args) |
38
|
|
|
{ |
39
|
3 |
|
$res = $this->chargeByToken($args); |
40
|
|
|
|
41
|
3 |
|
if ($res->getStatusCode() !== 200) { |
42
|
1 |
|
throw new InvalidHttpResponseException( |
43
|
1 |
|
"Expected 200 . Got {$res->getStatusCode()}" |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$response = json_decode($res->getBody(), true); |
48
|
|
|
|
49
|
2 |
|
$validated = false; |
50
|
|
|
|
51
|
2 |
|
if (strcmp($response['StatusDesc'], self::SUCCESSFUL_TRANSACTION) === 0) { |
52
|
1 |
|
$validated = true; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
if (false === $validated) { |
56
|
1 |
|
throw TransactionVerficationFailedException::createFromResponse($res); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$this->verifyKeys($response['apiKey'], $this->apiKeys['apiKey']); |
60
|
|
|
|
61
|
1 |
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $data |
67
|
|
|
* @return ResponseInterface |
68
|
|
|
*/ |
69
|
3 |
View Code Duplication |
protected function chargeByToken($data) |
|
|
|
|
70
|
|
|
{ |
71
|
3 |
|
$link = $this->baseUrl . self::CHARGE_RETURNING_USER; |
72
|
|
|
|
73
|
3 |
|
return $this->adapter->getHttpClient() |
74
|
3 |
|
->post( |
75
|
3 |
|
$link, [ |
76
|
3 |
|
'body' => json_encode(array_merge($this->apiKeys, $data)) |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.