Completed
Push — master ( 7fa4ba...22bb8d )
by Rigel Kent
01:35
created

Request::request()   A

Complexity

Conditions 5
Paths 13

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.1128
c 0
b 0
f 0
cc 5
nc 13
nop 0
1
<?php
2
3
namespace Luigel\Paymongo\Traits;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use Illuminate\Database\Eloquent\Model;
8
use Luigel\Paymongo\Exceptions\BadRequestException;
9
use Luigel\Paymongo\Exceptions\NotFoundException;
10
use Luigel\Paymongo\Exceptions\PaymentErrorException;
11
use Luigel\Paymongo\Models\PaymentIntent;
12
use Luigel\Paymongo\Models\Webhook;
13
14
trait Request
15
{
16
    protected $data;
17
    protected $options;
18
19
    /**
20
     * Request a create to API
21
     *
22
     * @param array $payload
23
     * @return Model
24
     */
25
    public function create($payload)
26
    {
27
        $this->method = 'POST';
0 ignored issues
show
Bug introduced by
The property method does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->payload = $this->convertPayloadAmountsToInteger($payload);
0 ignored issues
show
Bug introduced by
The property payload does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
        $this->formRequestData();
30
31
        $this->setOptions([
32
            'headers' => [
33
                'Accept' => 'application/json',
34
                'Content-type' => 'application/json'
35
            ],
36
            'auth' => [config('paymongo.secret_key'), ''],
37
            'json' => $this->data,
38
        ]);
39
40
        return $this->request();
41
    }
42
43
    /**
44
     * Request a create to API
45
     *
46
     * @param array $payload
47
     * @return Model
48
     */
49 View Code Duplication
    public function find($payload)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $this->method = 'GET';
52
        $this->payload = $payload;
53
        $this->apiUrl = $this->apiUrl . $payload;
0 ignored issues
show
Bug introduced by
The property apiUrl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
55
        $this->setOptions([
56
            'headers' => [
57
                'Accept' => 'application/json',
58
                'Content-type' => 'application/json'
59
            ],
60
            'auth' => [config('paymongo.secret_key'), ''],
61
        ]);
62
63
        return $this->request();
64
    }
65
66
    /**
67
     * Request a get all to API
68
     *
69
     * @return Model
70
     */
71
    public function all()
72
    {
73
        $this->method = 'GET';
74
75
        $this->setOptions([
76
            'headers' => [
77
                'Accept' => 'application/json',
78
                'Content-type' => 'application/json'
79
            ],
80
            'auth' => [config('paymongo.secret_key'), ''],
81
        ]);
82
83
        return $this->request();
84
    }
85
86
    /**
87
     * Enables the webhook
88
     *
89
     * @param Webhook $webhook
90
     * @return Model
91
     */
92 View Code Duplication
    public function enable(Webhook $webhook)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $this->method = 'POST';
95
        $this->apiUrl = $this->apiUrl . "$webhook->id/enable";
96
97
        $this->setOptions([
98
            'headers' => [
99
                'Accept' => 'application/json',
100
            ],
101
            'auth' => [config('paymongo.secret_key'), ''],
102
        ]);
103
104
        return $this->request();
105
    }
106
107
    /**
108
     * Disables the webhook
109
     *
110
     * @param Webhook $webhook
111
     * @return Model
112
     */
113 View Code Duplication
    public function disable(Webhook $webhook)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $this->method = 'POST';
116
        $this->apiUrl = $this->apiUrl . "$webhook->id/disable";
117
118
        $this->setOptions([
119
            'headers' => [
120
                'Accept' => 'application/json',
121
            ],
122
            'auth' => [config('paymongo.secret_key'), ''],
123
        ]);
124
125
        return $this->request();
126
    }
127
128
    /**
129
     * Updates the webhook
130
     *
131
     * @param Webhook $webhook
132
     * @param array $payload
133
     * @return Model
134
     */
135 View Code Duplication
    public function update(Webhook $webhook, array $payload)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $this->method = 'PUT';
138
        $this->payload = $this->convertPayloadAmountsToInteger($payload);
139
        $this->apiUrl = $this->apiUrl . $webhook->id;
140
141
        $this->formRequestData();
142
        $this->setOptions([
143
            'headers' => [
144
                'Accept' => 'application/json',
145
            ],
146
            'auth' => [config('paymongo.secret_key'), ''],
147
            'json' => $this->data,
148
        ]);
149
150
        return $this->request();
151
    }
152
153
    /**
154
     * Cancels the payment intent
155
     *
156
     * @param PaymentIntent $intent
157
     * @return Model
158
     */
159
    public function cancel(PaymentIntent $intent)
160
    {
161
        $this->method = 'POST';
162
        $this->apiUrl = $this->apiUrl . $intent->getId() . '/cancel';
163
164
        $this->setOptions([
165
            'headers' => [
166
                'Accept' => 'application/json',
167
            ],
168
            'auth' => [config('paymongo.secret_key'), ''],
169
        ]);
170
171
        return $this->request();
172
    }
173
174
    /**
175
     * Attach the payment method in the payment intent
176
     *
177
     * @param PaymentIntent $intent
178
     * @param string $paymentMethodId
179
     * @return Model
180
     */
181 View Code Duplication
    public function attach(PaymentIntent $intent, $paymentMethodId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $this->method = 'POST';
184
        $this->apiUrl = $this->apiUrl . $intent->getId() . '/attach';
185
        $this->payload = ['payment_method' => $paymentMethodId];
186
187
        $this->formRequestData();
188
        $this->setOptions([
189
            'headers' => [
190
                'Accept' => 'application/json',
191
            ],
192
            'json' => $this->data,
193
            'auth' => [config('paymongo.secret_key'), ''],
194
        ]);
195
196
        return $this->request();
197
    }
198
199
    /**
200
     * Send request to API
201
     *
202
     * @return mixed|Throwable
203
     */
204
    protected function request()
205
    {
206
        $client = new Client();
207
208
        try
209
        {
210
            $response = $client->request($this->method, $this->apiUrl, $this->options);
211
212
            $array = $this->parseToArray((string) $response->getBody());
213
            return $this->setReturnModel($array);
214
        }
215
        catch (ClientException $e)
216
        {
217
            $response = json_decode($e->getResponse()->getBody()->getContents(), true);
218
            if ($e->getCode() === 400)
219
            {
220
                throw new BadRequestException($response['errors'][0]['detail'], $e->getCode());
221
            }
222
            else if ($e->getCode() === 402)
223
            {
224
                throw new PaymentErrorException($response['errors'][0]['detail'], $e->getCode());
225
            }
226
            else if ($e->getCode() === 404)
227
            {
228
                throw new NotFoundException($response['errors'][0]['detail'], $e->getCode());
229
            }
230
        }
231
232
233
234
    }
235
236
    /**
237
     * Sets the data to add data wrapper of the payload
238
     *
239
     * @return void
240
     */
241
    protected function formRequestData()
242
    {
243
        $this->data = [
244
            'data' => [
245
                'attributes' => $this->payload
246
            ]
247
        ];
248
    }
249
250
    protected function parseToArray($jsonString)
251
    {
252
        return json_decode($jsonString, true);
253
    }
254
255
    protected function setReturnModel($array)
256
    {
257
        return (new $this->returnModel)->setData($array['data']);
0 ignored issues
show
Bug introduced by
The property returnModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
258
    }
259
260
    protected function setOptions($options)
261
    {
262
        $this->options = $options;
263
    }
264
265
    protected function convertPayloadAmountsToInteger($payload)
266
    {
267
        if (isset($payload['amount']))
268
        {
269
            $payload['amount'] *= 100;
270
        }
271
        return $payload;
272
273
    }
274
}
275