1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gbowo\Adapter\Amplifypay\Plugin; |
4
|
|
|
|
5
|
|
|
use Gbowo\Plugin\AbstractGetPaymentData; |
6
|
|
|
use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier; |
7
|
|
|
use Gbowo\Exception\InvalidHttpResponseException; |
8
|
|
|
use Gbowo\Exception\TransactionVerficationFailedException; |
9
|
|
|
|
10
|
|
|
class GetPaymentData extends AbstractGetPaymentData |
11
|
|
|
{ |
12
|
|
|
use KeyVerifier; |
13
|
|
|
|
14
|
|
|
const APPROVED_TRANSACTION_STATUS = "APPROVED"; |
15
|
|
|
|
16
|
|
|
const UNAPPROVED_TRANSACTION_STATUS = "UNAPPROVED"; |
17
|
|
|
|
18
|
|
|
const TRANSACTION_VERIFICATION = '/verify'; |
19
|
|
|
|
20
|
|
|
protected $baseUrl; |
21
|
|
|
|
22
|
|
|
protected $apiKeys; |
23
|
|
|
|
24
|
25 |
|
public function __construct(string $baseUrl, array $apiKeys) |
25
|
|
|
{ |
26
|
25 |
|
$this->baseUrl = $baseUrl; |
27
|
25 |
|
$this->apiKeys = $apiKeys; |
28
|
25 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $transactionRef |
32
|
|
|
* @return mixed |
33
|
|
|
* @throws \Gbowo\Adapter\AmplifyPay\Exception\TransactionVerficationFailedException if the transaction failed |
34
|
|
|
* @throws \Gbowo\Exception\InvalidHttpResponseException if the status code isn't 200 |
35
|
|
|
*/ |
36
|
3 |
|
public function handle(string $transactionRef) |
37
|
|
|
{ |
38
|
3 |
|
$link = $this->baseUrl . |
39
|
3 |
|
self::TRANSACTION_VERIFICATION . |
40
|
3 |
|
"?transactionRef={$transactionRef}&merchantId={$this->apiKeys['merchantId']}"; |
41
|
|
|
|
42
|
3 |
|
$response = $this->verifyTransaction($link); |
43
|
|
|
|
44
|
3 |
|
$verificationResponse = json_decode($response->getBody(), true); |
45
|
|
|
|
46
|
3 |
|
if ($response->getStatusCode() !== 200) { |
47
|
1 |
|
throw InvalidHttpResponseException::createFromResponse($response); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$validated = false; |
51
|
|
|
|
52
|
2 |
|
if (strcmp($verificationResponse['OrderStatus'], self::APPROVED_TRANSACTION_STATUS) === 0) { |
53
|
1 |
|
$validated = true; |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
if (false === $validated) { |
57
|
1 |
|
throw TransactionVerficationFailedException::createFromResponse($response); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->verifyKeys($verificationResponse['ApiKey'], $this->apiKeys['apiKey']); |
61
|
|
|
|
62
|
|
|
//A `KeyMismatchException` would be thrown if they don't match. |
63
|
|
|
// Returning the response here signifies all went well. |
64
|
|
|
|
65
|
1 |
|
return $verificationResponse; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $link |
70
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
71
|
|
|
*/ |
72
|
3 |
|
protected function verifyTransaction(string $link) |
73
|
|
|
{ |
74
|
3 |
|
return $this->adapter->getHttpClient() |
75
|
3 |
|
->get($link); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|