Passed
Push — master ( 049317...1e7dcb )
by Orkhan
02:16
created

Goldenpay::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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