Passed
Push — master ( 00e36d...d95162 )
by mahdi
01:57
created

Zarinpal::notVerified()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 20
nc 2
nop 1
dl 0
loc 22
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Payment\Abstracts\Driver;
7
use Shetabit\Payment\Exceptions\InvalidPaymentException;
8
use Shetabit\Payment\Invoice;
9
10
class Zarinpal extends Driver
11
{
12
    /**
13
     * Zarinpal Client.
14
     *
15
     * @var object
16
     */
17
    protected $client;
18
19
    /**
20
     * Invoice
21
     *
22
     * @var Invoice
23
     */
24
    protected $invoice;
25
26
    /**
27
     * Driver settings
28
     *
29
     * @var object
30
     */
31
    protected $settings;
32
33
    /**
34
     * Zarinpal constructor.
35
     * Construct the class with the relevant settings.
36
     *
37
     * @param Invoice $invoice
38
     * @param $settings
39
     */
40
    public function __construct(Invoice $invoice, $settings)
41
    {
42
        $this->invoice($invoice);
43
        $this->settings = (object) $settings;
44
        $this->client = new Client();
45
    }
46
47
    /**
48
     * Purchase Invoice.
49
     *
50
     * @return mixed
51
     * @throws \GuzzleHttp\Exception\GuzzleException
52
     */
53
    public function purchase()
54
    {
55
        if (!empty($this->invoice->getDetails()['description'])) {
56
            $description = $this->invoice->getDetails()['description'];
57
        } else {
58
            $description = $this->settings->description;
59
        }
60
61
        $data = array(
62
            'MerchantID' => $this->settings->merchantId,
63
            'Amount' => $this->invoice->getAmount(),
64
            'CallbackURL' => $this->settings->callbackUrl,
65
            'Description' => $description,
66
            'AdditionalData' => $this->invoice->getDetails()
67
        );
68
69
        $response = $this->client->request(
70
            'POST',
71
            $this->settings->apiPurchaseUrl,
72
            [
73
                "json" => $data,
74
            ]
75
        );
76
        $body = json_decode($response->getBody()->getContents(), true);
77
78
        if (empty($body['Authority'])) {
79
            $body['Authority'] = null;
80
        } else {
81
            $this->invoice->transactionId($body['Authority']);
82
        }
83
84
        return $body;
85
    }
86
87
    /**
88
     * Pay the Invoice
89
     *
90
     * @return \Illuminate\Http\RedirectResponse|mixed
91
     */
92
    public function pay()
93
    {
94
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
95
96
        // redirect using laravel logic
97
        return redirect()->to($payUrl);
98
    }
99
100
    /**
101
     * Verify payment
102
     *
103
     * @return mixed|void
104
     * @throws InvalidPaymentException
105
     * @throws \GuzzleHttp\Exception\GuzzleException
106
     */
107
    public function verify()
108
    {
109
        $data = [
110
            'MerchantID' => $this->settings->merchantId,
111
            'Authority'  => $this->invoice->getTransactionId(),
112
            'Amount' => $this->invoice->getAmount(),
113
        ];
114
115
        $response = $this->client->request(
116
            'POST',
117
            $this->settings->apiVerificationUrl,
118
            ['json' => $data]
119
        );
120
        $body = json_decode($response->getBody()->getContents(), true);
121
122
        if (!isset($body['Status']) || $body['Status'] != 100) {
123
            $this->notVerified($body['Status']);
124
        }
125
    }
126
127
    /**
128
     * Trigger an exception
129
     *
130
     * @param $status
131
     * @throws InvalidPaymentException
132
     */
133
    private function notVerified($status) {
134
        $translations = array(
135
            "-1" => "اطلاعات ارسال شده ناقص است.",
136
            "-2" => "IP و يا مرچنت كد پذيرنده صحيح نيست",
137
            "-3" => "با توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد",
138
            "-4" => "سطح تاييد پذيرنده پايين تر از سطح نقره اي است.",
139
            "-11" => "درخواست مورد نظر يافت نشد.",
140
            "-12" => "امكان ويرايش درخواست ميسر نمي باشد.",
141
            "-21" => "هيچ نوع عمليات مالي براي اين تراكنش يافت نشد",
142
            "-22" => "تراكنش نا موفق ميباشد",
143
            "-33" => "رقم تراكنش با رقم پرداخت شده مطابقت ندارد",
144
            "-34" => "سقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است",
145
            "-40" => "اجازه دسترسي به متد مربوطه وجود ندارد.",
146
            "-41" => "اطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.",
147
            "-42" => "مدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.",
148
            "-54" => "درخواست مورد نظر آرشيو شده است",
149
            "101" => "عمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.",
150
        );
151
        if (array_key_exists($status, $translations)) {
152
            throw new InvalidPaymentException($translations[$status]);
153
        } else {
154
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
155
        }
156
    }
157
}
158