Passed
Push — master ( 8fb633...075a6a )
by mahdi
06:46
created

Sandbox::getVerificationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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