GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GetPaymentData::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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