Zaringate::getPaymentUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Zarinpal\Strategies;
4
5
use Shetabit\Multipay\Abstracts\Driver;
6
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
7
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
8
use Shetabit\Multipay\Contracts\ReceiptInterface;
9
use Shetabit\Multipay\Invoice;
10
use Shetabit\Multipay\Receipt;
11
use Shetabit\Multipay\RedirectionForm;
12
use Shetabit\Multipay\Request;
13
use SoapClient;
14
15
class Zaringate extends Driver
16
{
17
    /**
18
     * Invoice
19
     *
20
     * @var Invoice
21
     */
22
    protected $invoice;
23
24
    /**
25
     * Driver settings
26
     *
27
     * @var object
28
     */
29
    protected $settings;
30
31
    /**
32
     * Zarinpal constructor.
33
     * Construct the class with the relevant settings.
34
     *
35
     * @param Invoice $invoice
36
     * @param $settings
37
     */
38
    public function __construct(Invoice $invoice, $settings)
39
    {
40
        $this->invoice($invoice);
41
        $this->settings = (object) $settings;
42
    }
43
44
    /**
45
     * Purchase Invoice.
46
     *
47
     * @return string
48
     *
49
     * @throws PurchaseFailedException
50
     * @throws \SoapFault
51
     */
52
    public function purchase()
53
    {
54
        if (!empty($this->invoice->getDetails()['description'])) {
55
            $description = $this->invoice->getDetails()['description'];
56
        } else {
57
            $description = $this->settings->description;
58
        }
59
60
        $mobile = !empty($this->invoice->getDetails()['mobile']) ? $this->invoice->getDetails()['mobile'] : '';
61
        $email = !empty($this->invoice->getDetails()['email']) ? $this->invoice->getDetails()['email'] : '';
62
        $amount = $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10); // convert to toman
63
64
        $data = array(
65
            'MerchantID' => $this->settings->merchantId,
66
            'Amount' => $amount,
67
            'CallbackURL' => $this->settings->callbackUrl,
68
            'Description' => $description,
69
            'Mobile' => $mobile,
70
            'Email' => $email,
71
            'AdditionalData' => $this->invoice->getDetails()
72
        );
73
74
        $client = new SoapClient($this->getPurchaseUrl(), ['encoding' => 'UTF-8']);
75
        $result = $client->PaymentRequest($data);
76
77
        $bodyResponse = $result->Status;
78
        if ($bodyResponse != 100 || empty($result->Authority)) {
79
            throw new PurchaseFailedException($this->translateStatus($bodyResponse), $bodyResponse);
80
        }
81
82
        $this->invoice->transactionId($result->Authority);
83
84
        // return the transaction's id
85
        return $this->invoice->getTransactionId();
86
    }
87
88
    /**
89
     * Pay the Invoice
90
     *
91
     * @return RedirectionForm
92
     */
93
    public function pay() : RedirectionForm
94
    {
95
        $transactionId = $this->invoice->getTransactionId();
96
        $paymentUrl = $this->getPaymentUrl();
97
98
        $payUrl = str_replace(':authority', $transactionId, $paymentUrl);
99
100
        return $this->redirectWithForm($payUrl, [], 'GET');
101
    }
102
103
    /**
104
     * Verify payment
105
     *
106
     * @return ReceiptInterface
107
     *
108
     * @throws InvalidPaymentException
109
     * @throws \SoapFault
110
     */
111
    public function verify() : ReceiptInterface
112
    {
113
        $amount = $this->invoice->getAmount() / ($this->settings->currency == 'T' ? 1 : 10); // convert to toman
114
        $authority = $this->invoice->getTransactionId() ?? Request::input('Authority');
115
116
        $data = [
117
            'MerchantID' => $this->settings->merchantId,
118
            'Authority' => $authority,
119
            'Amount' => $amount, // convert to toman
120
        ];
121
122
        $client = new SoapClient($this->getVerificationUrl(), ['encoding' => 'UTF-8']);
123
        $result = $client->PaymentVerification($data);
124
125
        $bodyResponse = $result->Status;
126
        if ($bodyResponse != 100) {
127
            throw new InvalidPaymentException($this->translateStatus($bodyResponse), $bodyResponse);
128
        }
129
130
        return $this->createReceipt($result->RefID);
131
    }
132
133
    /**
134
     * Generate the payment's receipt
135
     *
136
     * @param $referenceId
137
     *
138
     * @return Receipt
139
     */
140
    public function createReceipt($referenceId)
141
    {
142
        return new Receipt('zarinpal', $referenceId);
143
    }
144
145
    /**
146
     * Retrieve purchase url
147
     *
148
     * @return string
149
     */
150
    protected function getPurchaseUrl() : string
151
    {
152
        return $this->settings->zaringateApiPurchaseUrl;
153
    }
154
155
    /**
156
     * Retrieve Payment url
157
     *
158
     * @return string
159
     */
160
    protected function getPaymentUrl() : string
161
    {
162
        return $this->settings->zaringateApiPaymentUrl;
163
    }
164
165
    /**
166
     * Retrieve verification url
167
     *
168
     * @return string
169
     */
170
    protected function getVerificationUrl() : string
171
    {
172
        return $this->settings->zaringateApiVerificationUrl;
173
    }
174
175
    /**
176
     * Convert status to a readable message.
177
     *
178
     * @param $status
179
     *
180
     * @return mixed|string
181
     */
182
    private function translateStatus($status)
183
    {
184
        $translations = [
185
            '100' => 'تراکنش با موفقیت انجام گردید',
186
            '101' => 'عمليات پرداخت موفق بوده و قبلا عملیات وریفای تراكنش انجام شده است',
187
            '-9' => 'خطای اعتبار سنجی',
188
            '-10' => 'ای پی و يا مرچنت كد پذيرنده صحيح نمی باشد',
189
            '-11' => 'مرچنت کد فعال نیست لطفا با تیم پشتیبانی ما تماس بگیرید',
190
            '-12' => 'تلاش بیش از حد در یک بازه زمانی کوتاه',
191
            '-15' => 'ترمینال شما به حالت تعلیق در آمده با تیم پشتیبانی تماس بگیرید',
192
            '-16' => 'سطح تاييد پذيرنده پايين تر از سطح نقره ای می باشد',
193
            '-30' => 'اجازه دسترسی به تسویه اشتراکی شناور ندارید',
194
            '-31' => 'حساب بانکی تسویه را به پنل اضافه کنید مقادیر وارد شده برای تسهیم صحيح نمی باشد',
195
            '-32' => 'مقادیر وارد شده برای تسهیم صحيح نمی باشد',
196
            '-33' => 'درصد های وارد شده صحيح نمی باشد',
197
            '-34' => 'مبلغ از کل تراکنش بیشتر است',
198
            '-35' => 'تعداد افراد دریافت کننده تسهیم بیش از حد مجاز است',
199
            '-40' => 'پارامترهای اضافی نامعتبر، expire_in معتبر نیست',
200
            '-50' => 'مبلغ پرداخت شده با مقدار مبلغ در وریفای متفاوت است',
201
            '-51' => 'پرداخت ناموفق',
202
            '-52' => 'خطای غیر منتظره با پشتیبانی تماس بگیرید',
203
            '-53' => 'اتوریتی برای این مرچنت کد نیست',
204
            '-54' => 'اتوریتی نامعتبر است',
205
        ];
206
207
        $unknownError = 'خطای ناشناخته رخ داده است. در صورت کسر مبلغ از حساب حداکثر پس از 72 ساعت به حسابتان برمیگردد';
208
209
        return array_key_exists($status, $translations) ? $translations[$status] : $unknownError;
210
    }
211
}
212