Passed
Pull Request — master (#144)
by
unknown
04:15
created

Vandar::purchase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 33
rs 9.6333
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Vandar;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Contracts\ReceiptInterface;
8
use Shetabit\Multipay\Exceptions\InvalidPaymentException;
9
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
10
use Shetabit\Multipay\Invoice;
11
use Shetabit\Multipay\Receipt;
12
use Shetabit\Multipay\RedirectionForm;
13
use Shetabit\Multipay\Request;
14
15
class Vandar extends Driver
16
{
17
    /**
18
     * Vandar Client.
19
     *
20
     * @var object
21
     */
22
    protected $client;
23
24
    /**
25
     * Invoice
26
     *
27
     * @var Invoice
28
     */
29
    protected $invoice;
30
31
    /**
32
     * Driver settings
33
     *
34
     * @var object
35
     */
36
    protected $settings;
37
38
    const PAYMENT_STATUS_FAILED = 'FAILED';
39
    const PAYMENT_STATUS_OK = 'OK';
40
41
    public function __construct(Invoice $invoice, $settings)
42
    {
43
        $this->invoice($invoice);
44
        $this->settings = (object) $settings;
45
        $this->client = new Client();
46
    }
47
48
    /**
49
     * @return string
50
     * @throws \GuzzleHttp\Exception\GuzzleException
51
     * @throws \Shetabit\Multipay\Exceptions\PurchaseFailedException
52
     */
53
    public function purchase()
54
    {
55
        $data = [
56
            'api_key' => $this->settings->merchantId,
57
            'amount' => $this->invoice->getAmount(),
58
            'callback_url' => $this->settings->callbackUrl
59
        ];
60
61
        $response = $this->client
62
            ->request(
63
                'POST',
64
                $this->settings->apiPurchaseUrl,
65
                [
66
                    'json' => $data,
67
                    'headers' => [
68
                        "Accept" => "application/json",
69
                    ],
70
                    'http_errors' => false,
71
                ]
72
            );
73
74
        $responseBody = json_decode($response->getBody()->getContents(), true);
75
        $statusCode = (int) $responseBody['status'];
76
77
        if ($statusCode !== 1) {
78
            $errors = array_pop($responseBody['errors']);
79
80
            throw new PurchaseFailedException($errors);
81
        }
82
83
        $this->invoice->transactionId($responseBody['token']);
84
85
        return $this->invoice->getTransactionId();
86
    }
87
88
    /**
89
     * @return \Shetabit\Multipay\RedirectionForm
90
     */
91
    public function pay(): RedirectionForm
92
    {
93
        $url = $this->settings->apiPaymentUrl . $this->invoice->getTransactionId();
94
95
        return $this->redirectWithForm($url, [], 'GET');
96
    }
97
98
    /**
99
     * @return ReceiptInterface
100
     *
101
     * @throws \GuzzleHttp\Exception\GuzzleException
102
     * @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException
103
     */
104
    public function verify(): ReceiptInterface
105
    {
106
        $token = Request::get('token');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $token is correct as Shetabit\Multipay\Request::get('token') targeting Shetabit\Multipay\Request::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
        $paymentStatus = Request::get('payment_status');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $paymentStatus is correct as Shetabit\Multipay\Request::get('payment_status') targeting Shetabit\Multipay\Request::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
108
        $data = [
109
            'api_key' => $this->settings->merchantId,
110
            'token' => $token
111
        ];
112
113
        if ($paymentStatus == self::PAYMENT_STATUS_FAILED ) {
0 ignored issues
show
introduced by
The condition $paymentStatus == self::PAYMENT_STATUS_FAILED is always false.
Loading history...
114
            $this->notVerified('پرداخت با شکست مواجه شد.');
115
        }
116
117
        $response = $this->client
118
            ->request(
119
                'POST',
120
                $this->settings->apiVerificationUrl,
121
                [
122
                    'json' => $data,
123
                    'headers' => [
124
                        "Accept" => "application/json",
125
                    ],
126
                    'http_errors' => false,
127
                ]
128
            );
129
130
        $responseBody = json_decode($response->getBody()->getContents(), true);
131
        $statusCode = (int) $responseBody['status'];
132
133
        if ($statusCode !== 1) {
134
            if (isset($responseBody['error'])) {
135
                $message = is_array($responseBody['error']) ? array_pop($responseBody['error']) : $responseBody['error'];
136
            }
137
138
            if (isset($responseBody['errors']) and is_array($responseBody['errors'])) {
139
                $message = array_pop($responseBody['errors']);
140
            }
141
142
            $this->notVerified($message ?? '');
143
        }
144
145
        return $this->createReceipt($token);
146
    }
147
148
    /**
149
     * Generate the payment's receipt
150
     *
151
     * @param $referenceId
152
     *
153
     * @return Receipt
154
     */
155
    protected function createReceipt($referenceId)
156
    {
157
        $receipt = new Receipt('vandar', $referenceId);
158
159
        return $receipt;
160
    }
161
162
    /**
163
     * @param $message
164
     * @throws \Shetabit\Multipay\Exceptions\InvalidPaymentException
165
     */
166
    protected function notVerified($message)
167
    {
168
        if (empty($message)) {
169
            throw new InvalidPaymentException('خطای ناشناخته ای رخ داده است.');
170
        } else {
171
            throw new InvalidPaymentException($message);
172
        }
173
    }
174
}
175