Poolam   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 146
rs 10
wmc 12

6 Methods

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