Completed
Push — master ( 6526c3...a1d2b8 )
by Orkhan
02:50 queued 13s
created

Goldenpay::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\Goldenpay;
4
5
use GuzzleHttp\Client;
6
use Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException;
7
8
class Goldenpay
9
{
10
    /**
11
     * @var string
12
     */
13
    public $authKey;
14
    /**
15
     * @var string
16
     */
17
    public $merchantName;
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    /**
24
     * Goldenpay constructor.
25
     *
26
     * @param string      $authKey
27
     * @param string      $merchantName
28
     * @param Client|null $client
29
     */
30
    public function __construct(string $authKey, string $merchantName, Client $client = null)
31
    {
32
        $this->authKey = $authKey;
33
        $this->merchantName = $merchantName;
34
        $this->client = $client ?: new Client();
35
    }
36
37
    /**
38
     * Gets new payment key.
39
     *
40
     * @param int    $amount
41
     * @param string $cardType
42
     * @param string $description
43
     * @param string $lang
44
     *
45
     * @throws GoldenpayPaymentKeyException
46
     *
47
     * @return PaymentKey
48
     */
49
    public function newPaymentKey(int $amount, string $cardType, string $description, string $lang = 'lv')
50
    {
51
        $result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentKey', [
52
            'merchantName' => $this->merchantName,
53
            'amount'       => $amount,
54
            'cardType'     => $cardType,
55
            'description'  => $description,
56
            'lang'         => $lang,
57
            'hashCode'     => md5($this->authKey.$this->merchantName.$cardType.$amount.$description),
58
        ]);
59
60
        if ($result['status']['code'] !== 1) {
61
            throw new GoldenpayPaymentKeyException($result['status']['message']);
62
        }
63
64
        return new PaymentKey($result['status']['code'], $result['status']['message'], $result['paymentKey']);
65
    }
66
67
    /**
68
     * Checks payment result.
69
     *
70
     * @param string $paymentKey
71
     *
72
     * @return PaymentResult
73
     */
74
    public function checkPaymentResult(string $paymentKey)
75
    {
76
        $result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentResult', [
77
            'payment_key' => $paymentKey,
78
            'Hash_code'   => md5($this->authKey.$paymentKey),
79
        ]);
80
81
        return new PaymentResult($result);
82
    }
83
84
    /**
85
     * Sends requests to GoldenPay endpoint.
86
     *
87
     * @param string $url
88
     * @param array  $json
89
     *
90
     * @return array
91
     */
92
    private function sendRequest(string $url, array $json)
93
    {
94
        $response = $this->client->post($url, [
95
            'headers' => ['Accept' => 'application/json'],
96
            'json'    => $json,
97
        ]);
98
99
        return json_decode($response->getBody()->getContents(), true);
100
    }
101
}
102