Completed
Push — master ( 129e30...79af42 )
by mahdi
03:24
created

Idpay::pay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 6
rs 10
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 Idpay extends Driver
11
{
12
    /**
13
     * Idpay 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 string
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
        $data = array(
61
            'MerchantID' => $this->settings->merchantId,
62
            'Amount' => $this->invoice->getAmount(),
63
            'CallbackURL' => $this->settings->callbackUrl,
64
            'Description' => $description,
65
            'AdditionalData' => $this->invoice->getDetails()
66
        );
67
68
        $response = $this->client->request(
69
            'POST',
70
            $this->settings->apiPurchaseUrl,
71
            ["json" => $data]
72
        );
73
        $body = json_decode($response->getBody()->getContents(), true);
74
75
        if (empty($body['Authority'])) {
76
            $body['Authority'] = null;
77
        } else {
78
            $this->invoice->transactionId($body['Authority']);
79
        }
80
81
        // return the transaction's id
82
        return $this->invoice->getTransactionId();
83
    }
84
85
    /**
86
     * Pay the Invoice
87
     *
88
     * @return \Illuminate\Http\RedirectResponse|mixed
89
     */
90
    public function pay()
91
    {
92
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
93
94
        // redirect using laravel logic
95
        return redirect()->to($payUrl);
96
    }
97
98
    /**
99
     * Verify payment
100
     *
101
     * @return mixed|void
102
     * @throws InvalidPaymentException
103
     * @throws \GuzzleHttp\Exception\GuzzleException
104
     */
105
    public function verify()
106
    {
107
        $data = [
108
            'MerchantID' => $this->settings->merchantId,
109
            'Authority'  => $this->invoice->getTransactionId(),
110
            'Amount' => $this->invoice->getAmount(),
111
        ];
112
113
        $response = $this->client->request(
114
            'POST',
115
            $this->settings->apiVerificationUrl,
116
            ['json' => $data]
117
        );
118
        $body = json_decode($response->getBody()->getContents(), true);
119
120
        if (!isset($body['Status']) || $body['Status'] != 100) {
121
            $this->notVerified($body['Status']);
122
        }
123
    }
124
125
    /**
126
     * Trigger an exception
127
     *
128
     * @param $status
129
     * @throws InvalidPaymentException
130
     */
131
    private function notVerified($status)
132
    {
133
        $translations = array(
134
            "-1" => "اطلاعات ارسال شده ناقص است.",
135
            "-2" => "IP و يا مرچنت كد پذيرنده صحيح نيست",
136
            "-3" => "با توجه به محدوديت هاي شاپرك امكان پرداخت با رقم درخواست شده ميسر نمي باشد",
137
            "-4" => "سطح تاييد پذيرنده پايين تر از سطح نقره اي است.",
138
            "-11" => "درخواست مورد نظر يافت نشد.",
139
            "-12" => "امكان ويرايش درخواست ميسر نمي باشد.",
140
            "-21" => "هيچ نوع عمليات مالي براي اين تراكنش يافت نشد",
141
            "-22" => "تراكنش نا موفق ميباشد",
142
            "-33" => "رقم تراكنش با رقم پرداخت شده مطابقت ندارد",
143
            "-34" => "سقف تقسيم تراكنش از لحاظ تعداد يا رقم عبور نموده است",
144
            "-40" => "اجازه دسترسي به متد مربوطه وجود ندارد.",
145
            "-41" => "اطلاعات ارسال شده مربوط به AdditionalData غيرمعتبر ميباشد.",
146
            "-42" => "مدت زمان معتبر طول عمر شناسه پرداخت بايد بين 30 دقيه تا 45 روز مي باشد.",
147
            "-54" => "درخواست مورد نظر آرشيو شده است",
148
            "101" => "عمليات پرداخت موفق بوده و قبلا PaymentVerification تراكنش انجام شده است.",
149
        );
150
        if (array_key_exists($status, $translations)) {
151
            throw new InvalidPaymentException($translations[$status]);
152
        } else {
153
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
154
        }
155
    }
156
}
157