Passed
Push — master ( bdffe9...9cc199 )
by mahdi
02:48
created

Nextpay::notVerified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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;
8
use Shetabit\Payment\Exceptions\PurchaseFailedException;
9
use Shetabit\Payment\Contracts\ReceiptInterface;
10
use Shetabit\Payment\Invoice;
11
use Shetabit\Payment\Receipt;
12
13
class Nextpay extends Driver
14
{
15
    /**
16
     * Nextpay Client.
17
     *
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * Invoice
24
     *
25
     * @var Invoice
26
     */
27
    protected $invoice;
28
29
    /**
30
     * Driver settings
31
     *
32
     * @var object
33
     */
34
    protected $settings;
35
36
    /**
37
     * Nextpay constructor.
38
     * Construct the class with the relevant settings.
39
     *
40
     * @param Invoice $invoice
41
     * @param $settings
42
     */
43
    public function __construct(Invoice $invoice, $settings)
44
    {
45
        $this->invoice($invoice);
46
        $this->settings = (object) $settings;
47
        $this->client = new Client();
48
    }
49
50
    /**
51
     * Purchase Invoice.
52
     *
53
     * @return string
54
     *
55
     * @throws PurchaseFailedException
56
     * @throws \GuzzleHttp\Exception\GuzzleException
57
     */
58
    public function purchase()
59
    {
60
        $data = array(
61
            'api_key' => $this->settings->merchantId,
62
            'order_id' => intval(1, time()).crc32($this->invoice->getUuid()),
63
            'amount' => $this->invoice->getAmount(),
64
            'callback_uri' => $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 = json_decode($response->getBody()->getContents(), true);
79
80
        if (empty($body['code']) || $body['code'] != -1) {
81
            // error has happened
82
            throw new PurchaseFailedException($body['message']);
83
        } else {
84
            $this->invoice->transactionId($body['trans_id']);
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
        $payUrl = $this->settings->apiPaymentUrl.$this->invoice->getTransactionId();
99
100
        // redirect using laravel logic
101
        return redirect()->to($payUrl);
102
    }
103
104
    /**
105
     * Verify payment
106
     *
107
     * @return ReceiptInterface
108
     *
109
     * @throws InvalidPaymentException
110
     * @throws \GuzzleHttp\Exception\GuzzleException
111
     */
112
    public function verify() : ReceiptInterface
113
    {
114
        $transactionId = $this->invoice->getTransactionId() ?? request()->input('trans_id');
115
116
        $data = [
117
            'api_key' => $this->settings->merchantId,
118
            'order_id' => request()->input('order_id'),
119
            'amount' => $this->invoice->getAmount(),
120
            'trans_id' => $transactionId,
121
        ];
122
123
        $response = $this
124
            ->client
125
            ->request(
126
                'POST',
127
                $this->settings->apiVerificationUrl,
128
                [
129
                    "form_params" => $data,
130
                    "http_errors" => false,
131
                ]
132
            );
133
134
        $body = json_decode($response->getBody()->getContents(), true);
135
136
        if (!isset($body['code']) || $body['code'] != 0) {
137
            $message = $body['message'] ?? 'خطای ناشناخته ای رخت داده است';
138
139
            throw new InvalidPaymentException($message);
140
        }
141
142
        return $this->createReceipt($transactionId);
143
    }
144
145
    /**
146
     * Generate the payment's receipt
147
     *
148
     * @param $referenceId
149
     *
150
     * @return Receipt
151
     */
152
    protected function createReceipt($referenceId)
153
    {
154
        $receipt = new Receipt('nextpay', $referenceId);
155
156
        return $receipt;
157
    }
158
}
159