Passed
Push — master ( f00701...07a655 )
by mahdi
09:03
created

Etebarino::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Shetabit\Multipay\Drivers\Etebarino;
4
5
use GuzzleHttp\Client;
6
use Shetabit\Multipay\Abstracts\Driver;
7
use Shetabit\Multipay\Exceptions\PurchaseFailedException;
8
use Shetabit\Multipay\Contracts\ReceiptInterface;
9
use Shetabit\Multipay\Invoice;
10
use Shetabit\Multipay\Receipt;
11
use Shetabit\Multipay\RedirectionForm;
12
13
class Etebarino extends Driver
14
{
15
    /**
16
     * Invoice
17
     *
18
     * @var Invoice
19
     */
20
    protected $invoice;
21
22
    /**
23
     * Driver settings
24
     *
25
     * @var object
26
     */
27
    protected $settings;
28
29
    /**
30
     * Etebarino constructor.
31
     * Construct the class with the relevant settings.
32
     *
33
     * @param Invoice $invoice
34
     * @param $settings
35
     */
36
    public function __construct(Invoice $invoice, $settings)
37
    {
38
        $this->invoice($invoice);
39
        $this->settings = (object)$settings;
40
    }
41
42
    /**
43
     * Purchase Invoice
44
     *
45
     * @return string
46
     *
47
     * @throws PurchaseFailedException
48
     */
49
    public function purchase()
50
    {
51
        $this->invoice->uuid(crc32($this->invoice->getUuid()));
52
53
        $result = $this->token();
54
55
        if (!isset($result['status_code']) or $result['status_code'] != 200) {
56
            $this->purchaseFailed($result['content']);
57
        }
58
59
        $this->invoice->transactionId($result['content']);
60
61
        // return the transaction's id
62
        return $this->invoice->getTransactionId();
63
    }
64
65
    /**
66
     * Pay the Invoice
67
     *
68
     * @return RedirectionForm
69
     */
70
    public function pay(): RedirectionForm
71
    {
72
        return $this->redirectWithForm($this->settings->apiPaymentUrl, [
73
            'token' => $this->invoice->getTransactionId()
74
        ], 'GET');
75
    }
76
77
    /**
78
     * Verify payment
79
     *
80
     * @return mixed|Receipta
0 ignored issues
show
Bug introduced by
The type Shetabit\Multipay\Drivers\Etebarino\Receipta was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
     *
82
     * @throws PurchaseFailedException
83
     */
84
    public function verify(): ReceiptInterface
85
    {
86
        $result = $this->verifyTransaction();
87
88
        if (!isset($result['status_code']) or $result['status_code'] != 200) {
89
            $this->purchaseFailed($result['content']);
90
        }
91
92
        $receipt = $this->createReceipt($this->invoice->getDetail('referenceCode'));
93
        $receipt->detail([
94
            'referenceNo' => $this->invoice->getDetail('referenceCode'),
95
        ]);
96
97
        return $receipt;
98
    }
99
100
    /**
101
     * send request to Etebarino
102
     *
103
     * @param $method
104
     * @param $url
105
     * @param array $data
106
     * @return array
107
     */
108
    protected function callApi($method, $url, $data = []): array
109
    {
110
        $client = new Client();
111
112
        $response = $client->request($method, $url, [
113
            "json" => $data,
114
            "headers" => [
115
                'Content-Type' => 'application/json',
116
            ],
117
            "http_errors" => false,
118
        ]);
119
120
        return [
121
            'status_code' => $response->getStatusCode(),
122
            'content' => $response->getBody()->getContents()
123
        ];
124
    }
125
126
    /**
127
     * Generate the payment's receipt
128
     *
129
     * @param $referenceId
130
     *
131
     * @return Receipt
132
     */
133
    protected function createReceipt($referenceId): Receipt
134
    {
135
        $receipt = new Receipt('etebarino', $referenceId);
136
137
        return $receipt;
138
    }
139
140
    /**
141
     * call create token request
142
     *
143
     * @return array
144
     */
145
    public function token(): array
146
    {
147
        return $this->callApi('POST', $this->settings->apiPurchaseUrl, [
148
            'terminalCode' => $this->settings->terminalId,
149
            'terminalUser' => $this->settings->username,
150
            'merchantCode' => $this->settings->merchantId,
151
            'terminalPass' => $this->settings->password,
152
            'merchantRefCode' => $this->invoice->getUuid(),
153
            "description" => $this->invoice->getDetail('description'),
154
            "returnUrl" => $this->settings->callbackUrl,
155
            'paymentItems' => $this->getItems(),
156
        ]);
157
    }
158
159
    /**
160
     * call verift transaction request
161
     *
162
     * @return array
163
     */
164
    public function verifyTransaction(): array
165
    {
166
        return $this->callApi('POST', $this->settings->apiVerificationUrl, [
167
            'terminalCode' => $this->settings->terminalId,
168
            'terminalUser' => $this->settings->username,
169
            'merchantCode' => $this->settings->merchantId,
170
            'terminalPass' => $this->settings->password,
171
            'merchantRefCode' => $this->invoice->getDetail('uuid'),
172
            'referenceCode' => $this->invoice->getDetail('referenceCode')
173
        ]);
174
    }
175
176
    /**
177
     * get Items for
178
     *
179
     *
180
     */
181
    private function getItems()
182
    {
183
        /**
184
         * example data
185
         *
186
         *   $items = [
187
         *       [
188
         *           "productGroup" => 1000,
189
         *           "amount" => 1000, //Rial
190
         *           "description" => "desc"
191
         *       ]
192
         *   ];
193
         */
194
        return $this->invoice->getDetails()['items'];
195
    }
196
197
198
    /**
199
     * Trigger an exception
200
     *
201
     * @param $message
202
     *
203
     * @throws PurchaseFailedException
204
     */
205
    protected function purchaseFailed($message)
206
    {
207
        throw new PurchaseFailedException($message);
208
    }
209
}
210