Completed
Push — master ( c775ed...baf124 )
by Orkhan
02:53
created

GoldenpayService::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
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Orkhanahmadov\Goldenpay;
4
5
use GuzzleHttp\Client;
6
use Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException;
7
8
class GoldenpayService
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
     * @return PaymentKey
46
     */
47
    public function newPaymentKey(int $amount, string $cardType, string $description, string $lang = 'lv')
48
    {
49
        $result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentKey', [
50
            'merchantName' => $this->merchantName,
51
            'amount'       => $amount,
52
            'cardType'     => $cardType,
53
            'description'  => $description,
54
            'lang'         => $lang,
55
            'hashCode'     => md5($this->authKey.$this->merchantName.$cardType.$amount.$description),
56
        ]);
57
58
        if ($result['status']['code'] !== 1) {
59
            throw new GoldenpayPaymentKeyException($result['status']['message']);
60
        }
61
62
        return new PaymentKey($result['status']['code'], $result['status']['message'], $result['paymentKey']);
63
    }
64
65
    /**
66
     * Checks payment result.
67
     *
68
     * @param string $paymentKey
69
     *
70
     * @return PaymentResult
71
     */
72
    public function checkPaymentResult(string $paymentKey)
73
    {
74
        $result = $this->sendRequest('https://rest.goldenpay.az/web/service/merchant/getPaymentResult', [
75
            'payment_key' => $paymentKey,
76
            'Hash_code'   => md5($this->authKey.$paymentKey),
77
        ]);
78
79
        return new PaymentResult($result);
80
    }
81
82
    /**
83
     * Sends requests to GoldenPay endpoint.
84
     *
85
     * @param string $url
86
     * @param array $json
87
     * @return array
88
     */
89
    private function sendRequest(string $url, array $json)
90
    {
91
        $response = $this->client->post($url, [
92
            'headers' => ['Accept' => 'application/json'],
93
            'json'    => $json,
94
        ]);
95
96
        return json_decode($response->getBody()->getContents(), true);
97
    }
98
}
99