Issues (52)

src/Drivers/Minipay/Minipay.php (1 issue)

Severity
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Minipay;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Invoice;
7
use Shetabit\Multipay\Receipt;
8
use Shetabit\Multipay\Request;
9
use Shetabit\Multipay\RedirectionForm;
10
use Shetabit\Multipay\Abstracts\Driver;
11
use Shetabit\Multipay\Contracts\ReceiptInterface;
12
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
13
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
14
15
class Minipay extends Driver
16
{
17
    /**
18
     * HTTP 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
     * Minipay 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->client = new Client();
49
        $this->settings = (object)$settings;
50
    }
51
52
    /**
53
     * Retrieve data from details using its name.
54
     *
55
     * @return string
56
     */
57
    private function extractDetails($name)
0 ignored issues
show
The method extractDetails() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
58
    {
59
        return empty($this->invoice->getDetails()[$name]) ? null : $this->invoice->getDetails()[$name];
60
    }
61
62
    /**
63
     * Purchase Invoice.
64
     *
65
     * @return string
66
     *
67
     * @throws PurchaseFailedException
68
     * @throws \SoapFault
69
     */
70
    public function purchase()
71
    {
72
        $metadata = [];
73
74
        if (!empty($this->invoice->getDetails()['description'])) {
75
            $description = $this->invoice->getDetails()['description'];
76
        } else {
77
            $description = $this->settings->description;
78
        }
79
80
        $data = [
81
            "merchant_id" => $this->settings->merchantId,
82
            "amount" => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
83
            "callback_url" => $this->settings->callbackUrl,
84
            "description" => $description,
85
            "metadata" => array_merge($this->invoice->getDetails() ?? [], $metadata),
86
        ];
87
88
        $response = $this
89
            ->client
90
            ->request(
91
                'POST',
92
                $this->settings->apiPurchaseUrl,
93
                [
94
                    "json" => $data,
95
                    "headers" => [
96
                        'Content-Type' => 'application/json',
97
                    ],
98
                    "http_errors" => false,
99
                ]
100
            );
101
102
        $result = json_decode($response->getBody()->getContents(), true);
103
        if ($response->getStatusCode() != 200) {
104
            $message = $result['messages'][0]['text'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.';
105
            throw new PurchaseFailedException($message, $response->getStatusCode());
106
        }
107
108
        $this->invoice->transactionId($result['data']["authority"]);
109
110
        // return the transaction's id
111
        return $this->invoice->getTransactionId();
112
    }
113
114
    /**
115
     * Pay the Invoice
116
     *
117
     * @return RedirectionForm
118
     */
119
    public function pay(): RedirectionForm
120
    {
121
        $payUrl = $this->settings->apiPaymentUrl . '?authority=' . $this->invoice->getTransactionId();
122
123
        return $this->redirectWithForm($payUrl, [], 'GET');
124
    }
125
126
    /**
127
     * Verify payment
128
     *
129
     * @return mixed|void
130
     *
131
     * @throws InvalidPaymentException
132
     * @throws \GuzzleHttp\Exception\GuzzleException
133
     */
134
    public function verify(): ReceiptInterface
135
    {
136
        $authority = $this->invoice->getTransactionId() ?? Request::input('Authority');
137
        $data = [
138
            "merchant_id" => $this->settings->merchantId,
139
            "authority" => $authority,
140
            "amount" => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), // convert to rial
141
        ];
142
143
        $response = $this->client->request(
144
            'POST',
145
            $this->settings->apiVerificationUrl,
146
            [
147
                'json' => $data,
148
                "headers" => [
149
                    'Content-Type' => 'application/json',
150
                ],
151
                "http_errors" => false,
152
            ]
153
        );
154
155
        $result = json_decode($response->getBody()->getContents(), true);
156
157
        if ($response->getStatusCode() != 200) {
158
            $message = $result['messages'][0]['text'] ?? 'تراکنش تایید نشد.';
159
            throw new InvalidPaymentException($message, $response->getStatusCode());
160
        }
161
162
        $refId = $result['data']['id'];
163
164
        $receipt = $this->createReceipt($refId);
165
        $receipt->detail([
166
            'ref_id' => $refId,
167
            'installments_count' => $result['data']['installments_count'] ?? null,
168
            'price' => $result['data']['price'] ?? null,
169
            'credit_price' => $result['data']['credit_price'] ?? null,
170
        ]);
171
172
        return $receipt;
173
    }
174
175
    /**
176
     * Generate the payment's receipt
177
     *
178
     * @param $referenceId
179
     *
180
     * @return ReceiptInterface
181
     */
182
    protected function createReceipt($referenceId): ReceiptInterface
183
    {
184
        $receipt = new Receipt('minipay', $referenceId);
185
186
        return $receipt;
187
    }
188
}
189