Passed
Push — master ( 577299...39e184 )
by mahdi
02:37
created

Paystar   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 63
c 2
b 1
f 1
dl 0
loc 156
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A triggerError() 0 25 2
A verify() 0 20 2
A purchase() 0 35 2
A pay() 0 7 1
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
     * Paystar 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
     * Paystar 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
     * @throws InvalidPaymentException
52
     * @throws \GuzzleHttp\Exception\GuzzleException
53
     */
54
    public function purchase()
55
    {
56
        $details = $this->invoice->getDetails();
57
58
        $data = array(
59
            'amount' => $this->invoice->getAmount(),
60
            'email' => $details['email'] ?? null,
61
            'phone' => $details['mobile'] ?? $details['phone'] ?? null,
62
            'pin' => $this->settings->merchantId,
63
            'desc' => $details['description'] ?? $this->settings->description,
64
            'callback' => $this->settings->callbackUrl,
65
        );
66
67
        $response = $this
68
            ->client
69
            ->request(
70
                'POST',
71
                $this->settings->apiPurchaseUrl,
72
                [
73
                    "form_params" => $data,
74
                    "http_errors" => false,
75
                ]
76
            );
77
78
        $body = $response->getBody()->getContents();
79
80
        if (is_numeric($body)) {
81
            // some error has happened
82
            $this->triggerError($body);
83
        } else {
84
            $this->invoice->transactionId($body);
85
        }
86
87
        // return the transaction's id
88
        return $this->invoice->getTransactionId();
89
    }
90
91
    /**
92
     * Pay the Invoice
93
     *
94
     * @return \Illuminate\Http\RedirectResponse|mixed
95
     */
96
    public function pay()
97
    {
98
        $apiUrl =  $this->settings->apiPaymentUrl;
99
        $payUrl = $apiUrl.$this->invoice->getTransactionId();
100
101
        // redirect using laravel logic
102
        return redirect()->to($payUrl);
103
    }
104
105
    /**
106
     * Verify payment
107
     *
108
     * @return mixed|void
109
     * @throws InvalidPaymentException
110
     * @throws \GuzzleHttp\Exception\GuzzleException
111
     */
112
    public function verify()
113
    {
114
        $data = [
115
            'amount' => $this->invoice->getAmount(),
116
            'pin' => $this->settings->merchantId,
117
            'transid' => $this->invoice->getTransactionId() ?? request()->input('transid'),
118
        ];
119
120
        $response = $this->client->request(
121
            'POST',
122
            $this->settings->apiVerificationUrl,
123
            [
124
                'form_params' => $data,
125
                "http_errors" => false,
126
            ]
127
        );
128
        $body = $response->getBody()->getContents();
129
130
        if ($body != 1) {
131
            $this->triggerError($body);
132
        }
133
    }
134
135
    /**
136
     * Trigger an exception
137
     *
138
     * @param $status
139
     * @throws InvalidPaymentException
140
     */
141
    private function triggerError($status)
142
    {
143
        $status = (string) $status;
144
145
        $translations = array(
146
            "−1" => "مبلغ پرداخت نمیتواند خالی باشد.",
147
            "−2" => "کد پین درگاه(کد مرچند) نمیتواند خالی باشد.",
148
            "−3" => "لینک برگشتی (callback) نمیتواند خالی باشد.",
149
            "−4" => "مبلغ پرداخت باید عددی باشد.",
150
            "−5" => "مبلغ پرداخت باید بزرگتر از ۱۰۰ باشد.",
151
            "−6" => "کد پین درگاه (مرچند) اشتباه است.",
152
            "−7" => "آیپی سرور با آیپی درگاه مطابقت ندارد",
153
            "−8" => "کد تراکنش (transid) نمیتواند خالی باشد.",
154
            "−9" => "تراکنش مورد نظر وجود ندارد.",
155
            "−10" => "کدپین درگاه با درگاه تراکنش مطابقت ندارد.",
156
            "−11" => "مبلغ با مبلغ تراکنش مطابقت ندارد.",
157
            "-12" => "بانک انتخابی اشتباه است.",
158
            "-13" => "درگاه غیرفعال است.",
159
            "-14" => "آیپی مشتری ارسال نشده است.",
160
        );
161
162
        if (array_key_exists($status, $translations)) {
163
            throw new InvalidPaymentException($translations[$status]);
164
        } else {
165
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
166
        }
167
    }
168
}
169