Passed
Pull Request — master (#155)
by
unknown
08:21
created

Aqayepardakht::pay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Aqayepardakht;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Contracts\ReceiptInterface;
8
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
9
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
10
use Shetabit\Multipay\Invoice;
11
use Shetabit\Multipay\Receipt;
12
use Shetabit\Multipay\RedirectionForm;
13
use Shetabit\Multipay\Request;
14
15
class Aqayepardakht extends Driver
16
{
17
    /**
18
     * Aqayepardakht 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
    const PAYMENT_STATUS_FAILED = 'error';
39
    const PAYMENT_STATUS_OK = 'success';
40
41
    public function __construct(Invoice $invoice, $settings)
42
    {
43
        $this->invoice($invoice);
44
        $this->settings = (object) $settings;
45
        $this->client = new Client();
46
    }
47
48
    /**
49
     * @return string
50
     * @throws \GuzzleHttp\Exception\GuzzleException
51
     * @throws \Shetabit\Multipay\Exceptions\PurchaseFailedException
52
     */
53
    public function purchase()
54
    {
55
        $data = [
56
            'pin' => $this->settings->pin,
57
            'amount' => $this->invoice->getAmount(),
58
            'callback' => $this->settings->callbackUrl,
59
            'invoice_id' => $this->settings->invoice_id,
60
            'mobile' => $this->settings->mobile,
61
            'email' => $this->settings->email,
62
        ];
63
64
        $response = $this->client
65
            ->request(
66
                'POST',
67
                $this->settings->apiPurchaseUrl,
68
                [
69
                    'json' => $data,
70
                    'headers' => [
71
                        "Accept" => "application/json",
72
                    ],
73
                    'http_errors' => false,
74
                ]
75
            );
76
77
        $responseBody = json_decode($response->getBody()->getContents(), true);
78
79
        if ($responseBody['status'] !== self::PAYMENT_STATUS_OK) {
80
            throw new PurchaseFailedException($this->getErrorMessage($responseBody['code']));
81
        }
82
83
        $this->invoice->transactionId($responseBody['transid']);
84
85
        return $this->invoice->getTransactionId();
86
    }
87
88
    /**
89
     * @return \Shetabit\Multipay\RedirectionForm
90
     */
91
    public function pay(): RedirectionForm
92
    {
93
        $url = $this->settings->mode === "normal" ? $this->settings->apiPaymentUrl : $this->settings->apiPaymentUrlSandbox;
94
        $url = $url . $this->invoice->getTransactionId();
95
96
        return $this->redirectWithForm($url, [], 'GET');
97
    }
98
99
    /**
100
     * @return ReceiptInterface
101
     *
102
     * @throws \GuzzleHttp\Exception\GuzzleException
103
     * @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException
104
     */
105
    public function verify(): ReceiptInterface
106
    {
107
        $tracking_number = Request::get('tracking_number');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tracking_number is correct as Shetabit\Multipay\Request::get('tracking_number') targeting Shetabit\Multipay\Request::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108
        if ($tracking_number === null || $tracking_number === ""){
0 ignored issues
show
introduced by
The condition $tracking_number === null is always true.
Loading history...
introduced by
The condition $tracking_number === '' is always false.
Loading history...
109
            $this->notVerified('پرداخت ناموفق.');
110
        }
111
        $data = [
112
            'pin' => $this->settings->pin,
113
            'amount' => $this->invoice->getAmount(),
114
            'transid' => $tracking_number
115
        ];
116
        $response = $this->client
117
            ->request(
118
                'POST',
119
                $this->settings->apiVerificationUrl,
120
                [
121
                    'json' => $data,
122
                    'headers' => [
123
                        "Accept" => "application/json",
124
                    ],
125
                    'http_errors' => false,
126
                ]
127
            );
128
129
        $responseBody = json_decode($response->getBody()->getContents(), true);
130
131
        if ($responseBody['status'] !== self::PAYMENT_STATUS_OK) {
132
            if (isset($responseBody['code'])) {
133
                $message = $this->getErrorMessage($responseBody["code"]);
134
            }
135
136
            $this->notVerified($message ?? '');
137
        }
138
        return $this->createReceipt($tracking_number);
139
    }
140
141
    /**
142
     * Generate the payment's receipt
143
     *
144
     * @param $referenceId
145
     *
146
     * @return Receipt
147
     */
148
    protected function createReceipt($referenceId)
149
    {
150
        $receipt = new Receipt('aqayepardakht', $referenceId);
151
152
        return $receipt;
153
    }
154
155
    /**
156
     * @param $message
157
     * @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException
158
     */
159
    protected function notVerified($message){
160
        if (empty($message)) {
161
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
162
        } else {
163
            throw new InvalidPaymentException($message);
164
        }
165
    }
166
167
    /**
168
     * @param $code
169
     * @return  string
170
     */
171
    protected function getErrorMessage($code){
172
        $code = (int)$code;
173
        switch ($code){
174
            case -1: return "مبلغ نباید خالی باشد.";
175
            case -2: return "کد پین درگاه نمیتواند خالی باشد.";
176
            case -3: return "آدرس بازگشت نمیتواند خالی باشد.";
177
            case -4: return "مبلغ وارد شده اشتباه است.";
178
            case -5: return "مبلع باید بین 100 تومان تا 50 میلیون تومان باشد.";
179
            case -6: return "کد پین وارد شده اشتباه است.";
180
            case -7: return "کد تراکنش نمیتواند خالی باشد.";
181
            case -8: return "تراکنش مورد نظر وجود ندارد.";
182
            case -9: return "کد پین درگاه با درگاه تراکنش مطابقت ندارد.";
183
            case -10: return "مبلغ با مبلغ تراکنش مطابقت ندارد.";
184
            case -11: return "درگاه در انتظار تایید و یا غیرفعال است.";
185
            case -12: return "امکان ارسال درخواست برای این پذیرنده وجود ندارد.";
186
            case -13: return "شماره کارت باید 16 رقم چسبیده بهم باشد.";
187
            case 0: return "پرداخت انجام نشد.";
188
            case 1: return "پرداخت با موفقیت انجام شد.";
189
            case 2: return "تراکنش قبلا وریفای شده است.";
190
            default : return "خطای نامشخص.";
191
        }
192
    }
193
}
194