Passed
Push — master ( 437f23...0ad4c0 )
by mahdi
03:18
created

Poolam::purchase()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 9.7333
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers\Poolam;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Payment\Abstracts\Driver;
7
use Shetabit\Payment\Exceptions\{InvalidPaymentException, PurchaseFailedException};
8
use Shetabit\Payment\{Contracts\ReceiptInterface, Invoice, Receipt};
9
10
class Poolam extends Driver
11
{
12
    /**
13
     * Poolam Client.
14
     *
15
     * @var object
16
     */
17
    protected $client;
18
19
    /**
20
     * Invoice
21
     *
22
     * @var Invoice
23
     */
24
    protected $invoice;
25
26
    /**
27
     * Driver settings
28
     *
29
     * @var object
30
     */
31
    protected $settings;
32
33
    /**
34
     * Poolam constructor.
35
     * Construct the class with the relevant settings.
36
     *
37
     * @param Invoice $invoice
38
     * @param $settings
39
     */
40
    public function __construct(Invoice $invoice, $settings)
41
    {
42
        $this->invoice($invoice);
43
        $this->settings = (object) $settings;
44
        $this->client = new Client();
45
    }
46
47
    /**
48
     * Purchase Invoice.
49
     *
50
     * @return string
51
     *
52
     * @throws \GuzzleHttp\Exception\GuzzleException
53
     */
54
    public function purchase()
55
    {
56
        // convert to toman
57
        $toman = $this->invoice->getAmount() * 10;
58
59
        $data = array(
60
            'api_key' => $this->settings->merchantId,
61
            'amount' => $toman,
62
            'return_url' => $this->settings->callbackUrl,
63
        );
64
65
        $response = $this->client->request(
66
            'POST',
67
            $this->settings->apiPurchaseUrl,
68
            [
69
                "form_params" => $data,
70
                "http_errors" => false,
71
            ]
72
        );
73
        $body = json_decode($response->getBody()->getContents(), true);
74
75
        if (empty($body['status']) || $body['status'] != 1) {
76
            // some error has happened
77
            throw new PurchaseFailedException($body['status']);
78
        } else {
79
            $this->invoice->transactionId($body['invoice_key']);
80
        }
81
82
        // return the transaction's id
83
        return $this->invoice->getTransactionId();
84
    }
85
86
    /**
87
     * Pay the Invoice
88
     *
89
     * @return \Illuminate\Http\RedirectResponse|mixed
90
     */
91
    public function pay()
92
    {
93
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
94
95
        // redirect using laravel logic
96
        return redirect()->to($payUrl);
97
    }
98
99
    /**
100
     * Verify payment
101
     *
102
     * @return mixed|void
103
     *
104
     * @throws InvalidPaymentException
105
     * @throws \GuzzleHttp\Exception\GuzzleException
106
     */
107
    public function verify() : ReceiptInterface
108
    {
109
        $data = [
110
            'api_key' => $this->settings->merchantId,
111
        ];
112
113
        $transactionId = $this->invoice->getTransactionId() ?? request()->input('invoice_key');
114
        $url = $this->settings->apiVerificationUrl.$transactionId;
115
116
        $response = $this->client->request(
117
            'POST',
118
            $url,
119
            ["form_params" => $data, "http_errors" => false]
120
        );
121
        $body = json_decode($response->getBody()->getContents(), true);
122
123
        if (empty($body['status']) || $body['status'] != 1) {
124
            $message = $body['errorDescription'] ?? null;
125
126
            $this->notVerified($message);
127
        }
128
129
        return $this->createReceipt($body['bank_code']);
130
    }
131
132
    /**
133
     * Generate the payment's receipt
134
     *
135
     * @param $referenceId
136
     *
137
     * @return Receipt
138
     */
139
    public function createReceipt($referenceId)
140
    {
141
        $receipt = new Receipt('poolam', $referenceId);
142
143
        return $receipt;
144
    }
145
146
    /**
147
     * Trigger an exception
148
     *
149
     * @param $message
150
     * @throws InvalidPaymentException
151
     */
152
    private function notVerified($message)
153
    {
154
        if (empty($message)) {
155
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
156
        } else {
157
            throw new InvalidPaymentException($message);
158
        }
159
    }
160
}
161