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

Nextpay   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 48
dl 0
loc 156
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A purchase() 0 31 3
A createReceipt() 0 5 1
A verify() 0 31 3
A notVerified() 0 3 1
A pay() 0 6 1
A __construct() 0 5 1
1
<?php
2
3
namespace Shetabit\Payment\Drivers\Nextpay;
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 Nextpay extends Driver
11
{
12
    /**
13
     * Nextpay Client.
14
     *
15
     * @var Client
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
     * Nextpay 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
        $data = array(
58
            'api_key' => $this->settings->merchantId,
59
            'order_id' => intval(1, time()).crc32($this->invoice->getUuid()),
60
            'amount' => $this->invoice->getAmount(),
61
            'callback_uri' => $this->settings->callbackUrl,
62
        );
63
64
        $response = $this
65
            ->client
66
            ->request(
67
                'POST',
68
                $this->settings->apiPurchaseUrl,
69
                [
70
                    "form_params" => $data,
71
                    "http_errors" => false,
72
                ]
73
            );
74
75
        $body = json_decode($response->getBody()->getContents(), true);
76
77
        if (empty($body['code']) || $body['code'] != -1) {
78
            // error has happened
79
            throw new PurchaseFailedException($body['id']);
80
        } else {
81
            $this->invoice->transactionId($body['trans_id']);
82
        }
83
84
        // return the transaction's id
85
        return $this->invoice->getTransactionId();
86
    }
87
88
    /**
89
     * Pay the Invoice
90
     *
91
     * @return \Illuminate\Http\RedirectResponse|mixed
92
     */
93
    public function pay()
94
    {
95
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
96
97
        // redirect using laravel logic
98
        return redirect()->to($payUrl);
99
    }
100
101
    /**
102
     * Verify payment
103
     *
104
     * @return ReceiptInterface
105
     *
106
     * @throws InvalidPaymentException
107
     * @throws \GuzzleHttp\Exception\GuzzleException
108
     */
109
    public function verify() : ReceiptInterface
110
    {
111
        $transactionId = $this->invoice->getTransactionId() ?? request()->input('trans_id');
112
113
        $data = [
114
            'api_key' => $this->settings->merchantId,
115
            'order_id' => request()->input('order_id'),
116
            'amount' => $this->invoice->getAmount(),
117
            'trans_id' => $transactionId,
118
        ];
119
120
        $response = $this
121
            ->client
122
            ->request(
123
                'POST',
124
                $this->settings->apiVerificationUrl,
125
                [
126
                    "form_params" => $data,
127
                    "http_errors" => false,
128
                ]
129
            );
130
131
        $body = json_decode($response->getBody()->getContents(), true);
132
133
        if (!isset($body['code']) || $body['code'] != 0) {
134
            $message = $body['message'] ?? 'خطای ناشناخته ای رخت داده است';
135
136
            $this->notVerified($message);
137
        }
138
139
        return $this->createReceipt($transactionId);
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('nextpay', $referenceId);
152
153
        return $receipt;
154
    }
155
156
    /**
157
     * Trigger an exception
158
     *
159
     * @param $message
160
     *
161
     * @throws InvalidPaymentException
162
     */
163
    private function notVerified($message)
164
    {
165
        throw new InvalidPaymentException($message);
166
    }
167
}
168