Passed
Push — master ( c3951c...931336 )
by mahdi
02:51 queued 10s
created

Paystar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 5
rs 10
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 Paystar 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
     * Idpay 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
        $details = $this->invoice->getDetails();
55
56
        $data = array(
57
            'order_id' => $this->invoice->getUuid(),
58
            'amount' => $this->invoice->getAmount(),
59
            'name' => $details['name'] ?? null,
60
            'phone' => $details['mobile'] ?? $details['phone'] ?? null,
61
            'mail' => $details['email'] ?? null,
62
            'desc' => $details['description'] ?? $this->settings->description,
63
            'callback' => $this->settings->callbackUrl,
64
            'reseller' => $details['reseller'] ?? null,
65
        );
66
67
        $response = $this
68
            ->client
69
            ->request(
70
                'POST',
71
                $this->settings->apiPurchaseUrl,
72
                [
73
                    "json" => $data,
74
                    "headers" => [
75
                        'X-API-KEY' => $this->settings->merchantId,
76
                        'Content-Type' => 'application/json',
77
                        'X-SANDBOX' => (int) $this->settings->sandbox,
78
                    ],
79
                    "http_errors" => false,
80
                ]
81
            );
82
83
        $body = json_decode($response->getBody()->getContents(), true);
84
85
        if (empty($body['id'])) {
86
            // some error has happened
87
        } else {
88
            $this->invoice->transactionId($body['id']);
89
        }
90
91
        // return the transaction's id
92
        return $this->invoice->getTransactionId();
93
    }
94
95
    /**
96
     * Pay the Invoice
97
     *
98
     * @return \Illuminate\Http\RedirectResponse|mixed
99
     */
100
    public function pay()
101
    {
102
        $apiUrl =  $this->settings->apiPaymentUrl;
103
104
        // use sandbox url if we are in sandbox mode
105
        if (!empty($this->settings->sandbox)) {
106
            $apiUrl = $this->settings->apiSandboxPaymentUrl;
107
        }
108
109
        $payUrl = $apiUrl.$this->invoice->getTransactionId();
110
111
        // redirect using laravel logic
112
        return redirect()->to($payUrl);
113
    }
114
115
    /**
116
     * Verify payment
117
     *
118
     * @return mixed|void
119
     * @throws InvalidPaymentException
120
     * @throws \GuzzleHttp\Exception\GuzzleException
121
     */
122
    public function verify()
123
    {
124
        $data = [
125
            'id' => $this->invoice->getTransactionId() ?? request()->input('id'),
126
            'order_id' => request()->input('order_id'),
127
        ];
128
129
        $response = $this->client->request(
130
            'POST',
131
            $this->settings->apiVerificationUrl,
132
            [
133
                'json' => $data,
134
                "headers" => [
135
                    'X-API-KEY' => $this->settings->merchantId,
136
                    'Content-Type' => 'application/json',
137
                    'X-SANDBOX' => (int) $this->settings->sandbox,
138
                ],
139
                "http_errors" => false,
140
            ]
141
        );
142
        $body = json_decode($response->getBody()->getContents(), true);
143
144
        if (isset($body['error_code']) || $body['status'] != 100) {
145
            $errorCode = $body['status'] ?? $body['error_code'];
146
147
            $this->notVerified($errorCode);
148
        }
149
    }
150
151
    /**
152
     * Trigger an exception
153
     *
154
     * @param $status
155
     * @throws InvalidPaymentException
156
     */
157
    private function notVerified($status)
158
    {
159
        $translations = array(
160
            "1" => "پرداخت انجام نشده است.",
161
            "2" => "پرداخت ناموفق بوده است.",
162
            "3" => "خطا رخ داده است.",
163
            "4" => "بلوکه شده.",
164
            "5" => "برگشت به پرداخت کننده.",
165
            "6" => "برگشت خورده سیستمی.",
166
            "10" => "در انتظار تایید پرداخت.",
167
            "100" => "پرداخت تایید شده است.",
168
            "101" => "پرداخت قبلا تایید شده است.",
169
            "200" => "به دریافت کننده واریز شد.",
170
            "11" => "کاربر مسدود شده است.",
171
            "12" => "API Key یافت نشد.",
172
            "13" => "درخواست شما از {ip} ارسال شده است. این IP با IP های ثبت شده در وب سرویس همخوانی ندارد.",
173
            "14" => "وب سرویس تایید نشده است.",
174
            "21" => "حساب بانکی متصل به وب سرویس تایید نشده است.",
175
            "31" => "کد تراکنش id نباید خالی باشد.",
176
            "32" => "شماره سفارش order_id نباید خالی باشد.",
177
            "33" => "مبلغ amount نباید خالی باشد.",
178
            "34" => "مبلغ amount باید بیشتر از {min-amount} ریال باشد.",
179
            "35" => "مبلغ amount باید کمتر از {max-amount} ریال باشد.",
180
            "36" => "مبلغ amount بیشتر از حد مجاز است.",
181
            "37" => "آدرس بازگشت callback نباید خالی باشد.",
182
            "38" => "درخواست شما از آدرس {domain} ارسال شده است. دامنه آدرس بازگشت callback با آدرس ثبت شده در وب سرویس همخوانی ندارد.",
183
            "51" => "تراکنش ایجاد نشد.",
184
            "52" => "استعلام نتیجه ای نداشت.",
185
            "53" => "تایید پرداخت امکان پذیر نیست.",
186
            "54" => "مدت زمان تایید پرداخت سپری شده است.",
187
        );
188
        if (array_key_exists($status, $translations)) {
189
            throw new InvalidPaymentException($translations[$status]);
190
        } else {
191
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
192
        }
193
    }
194
}
195