Passed
Push — master ( 437f23...0ad4c0 )
by mahdi
03:18
created

Paystar::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Shetabit\Payment\Drivers\Paystar;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Payment\Abstracts\Driver;
7
use Shetabit\Payment\Exceptions\{InvalidPaymentException, PurchaseFailedException};
8
use Shetabit\Payment\{Contracts\ReceiptInterface, Invoice, Receipt};
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
     *
52
     * @throws PurchaseFailedException
53
     * @throws \GuzzleHttp\Exception\GuzzleException
54
     */
55
    public function purchase()
56
    {
57
        $details = $this->invoice->getDetails();
58
59
        $data = array(
60
            'amount' => $this->invoice->getAmount(),
61
            'email' => $details['email'] ?? null,
62
            'phone' => $details['mobile'] ?? $details['phone'] ?? null,
63
            'pin' => $this->settings->merchantId,
64
            'desc' => $details['description'] ?? $this->settings->description,
65
            'callback' => $this->settings->callbackUrl,
66
        );
67
68
        $response = $this
69
            ->client
70
            ->request(
71
                'POST',
72
                $this->settings->apiPurchaseUrl,
73
                [
74
                    "form_params" => $data,
75
                    "http_errors" => false,
76
                ]
77
            );
78
79
        $body = $response->getBody()->getContents();
80
81
        if (is_numeric($body)) {
82
            // some error has happened
83
            $message = 'خطا در هنگام درخواست برای پرداخت با کد '.$body.' رخ داده است.';
84
            throw new PurchaseFailedException($message);
85
        } else {
86
            $this->invoice->transactionId($body);
87
        }
88
89
        // return the transaction's id
90
        return $this->invoice->getTransactionId();
91
    }
92
93
    /**
94
     * Pay the Invoice
95
     *
96
     * @return \Illuminate\Http\RedirectResponse|mixed
97
     */
98
    public function pay()
99
    {
100
        $apiUrl =  $this->settings->apiPaymentUrl;
101
        $payUrl = $apiUrl . $this->invoice->getTransactionId();
102
103
        // redirect using laravel logic
104
        return redirect()->to($payUrl);
105
    }
106
107
    /**
108
     * Verify payment
109
     *
110
     * @return mixed|void
111
     *
112
     * @throws InvalidPaymentException
113
     * @throws \GuzzleHttp\Exception\GuzzleException
114
     */
115
    public function verify() : ReceiptInterface
116
    {
117
        $transId = $this->invoice->getTransactionId() ?? request()->input('transid');
118
119
        $data = [
120
            'amount' => $this->invoice->getAmount(),
121
            'pin' => $this->settings->merchantId,
122
            'transid' => $transId,
123
        ];
124
125
        $response = $this->client->request(
126
            'POST',
127
            $this->settings->apiVerificationUrl,
128
            [
129
                'form_params' => $data,
130
                "http_errors" => false,
131
            ]
132
        );
133
        $body = $response->getBody()->getContents();
134
135
        if ($body != 1) {
136
            $this->triggerError($body);
137
        }
138
139
        $this->createReceipt($transId);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Shetabit\Payment\Contracts\ReceiptInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
140
    }
141
142
    /**
143
     * Generate the payment's receipt
144
     *
145
     * @param $referenceId
146
     *
147
     * @return Receipt
148
     */
149
    public function createReceipt($referenceId)
150
    {
151
        $receipt = new Receipt('paystar', $referenceId);
152
153
        return $receipt;
154
    }
155
156
    /**
157
     * Trigger an exception
158
     *
159
     * @param $status
160
     *
161
     * @throws InvalidPaymentException
162
     */
163
    private function triggerError($status)
164
    {
165
        $status = (string) $status;
166
167
        $translations = array(
168
            "−1" => "مبلغ پرداخت نمیتواند خالی باشد.",
169
            "−2" => "کد پین درگاه(کد مرچند) نمیتواند خالی باشد.",
170
            "−3" => "لینک برگشتی (callback) نمیتواند خالی باشد.",
171
            "−4" => "مبلغ پرداخت باید عددی باشد.",
172
            "−5" => "مبلغ پرداخت باید بزرگتر از ۱۰۰ باشد.",
173
            "−6" => "کد پین درگاه (مرچند) اشتباه است.",
174
            "−7" => "آیپی سرور با آیپی درگاه مطابقت ندارد",
175
            "−8" => "کد تراکنش (transid) نمیتواند خالی باشد.",
176
            "−9" => "تراکنش مورد نظر وجود ندارد.",
177
            "−10" => "کدپین درگاه با درگاه تراکنش مطابقت ندارد.",
178
            "−11" => "مبلغ با مبلغ تراکنش مطابقت ندارد.",
179
            "-12" => "بانک انتخابی اشتباه است.",
180
            "-13" => "درگاه غیرفعال است.",
181
            "-14" => "آیپی مشتری ارسال نشده است.",
182
        );
183
184
        if (array_key_exists($status, $translations)) {
185
            throw new InvalidPaymentException($translations[$status]);
186
        } else {
187
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
188
        }
189
    }
190
}
191