Completed
Push — master ( d3fec9...d4f8f7 )
by Raza
02:12
created

ExpressCheckout   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 342
rs 10
wmc 24
lcom 2
cbo 2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setPreviousHttpBodyParam() 0 4 1
A setExpressCheckoutOptions() 0 20 4
A setCartItems() 0 12 1
A setExpressCheckoutRecurringPaymentConfig() 0 10 3
A setExpressCheckout() 0 19 1
A getExpressCheckoutDetails() 0 8 1
A doExpressCheckoutPayment() 0 16 2
A doCapture() 0 13 1
A doAuthorization() 0 11 1
A doVoid() 0 10 1
A createBillingAgreement() 0 8 1
A createRecurringPaymentsProfile() 0 8 1
A getRecurringPaymentsProfileDetails() 0 8 1
A updateRecurringPaymentsProfile() 0 8 1
A cancelRecurringPaymentsProfile() 0 9 1
A suspendRecurringPaymentsProfile() 0 9 1
A reactivateRecurringPaymentsProfile() 0 9 1
1
<?php
2
3
namespace Srmklive\PayPal\Services;
4
5
use Illuminate\Support\Collection;
6
use Srmklive\PayPal\Traits\PayPalRequest as PayPalAPIRequest;
7
8
class ExpressCheckout
9
{
10
    // Integrate PayPal Request trait
11
    use PayPalAPIRequest;
12
13
    /**
14
     * PayPal Processor Constructor.
15
     *
16
     * @param array $config
17
     */
18
    public function __construct(array $config = [])
19
    {
20
        // Setting PayPal API Credentials
21
        $this->setConfig($config);
22
23
        $this->httpBodyParam = 'form_params';
24
    }
25
26
    /**
27
     * Set Http Client request body param. Should only be called when Guzzle version 5 is used.
28
     */
29
    public function setPreviousHttpBodyParam()
30
    {
31
        $this->httpBodyParam = 'body';
32
    }
33
34
    /**
35
     * Set ExpressCheckout API endpoints & options.
36
     *
37
     * @param array $credentials
38
     *
39
     * @return void
40
     */
41
    public function setExpressCheckoutOptions($credentials)
42
    {
43
        // Setting API Endpoints
44
        if ($this->mode === 'sandbox') {
45
            $this->config['api_url'] = !empty($this->config['secret']) ?
46
                'https://api-3t.sandbox.paypal.com/nvp' : 'https://api.sandbox.paypal.com/nvp';
47
48
            $this->config['gateway_url'] = 'https://www.sandbox.paypal.com';
49
        } else {
50
            $this->config['api_url'] = !empty($this->config['secret']) ?
51
                'https://api-3t.paypal.com/nvp' : 'https://api.paypal.com/nvp';
52
53
            $this->config['gateway_url'] = 'https://www.paypal.com';
54
        }
55
56
        // Adding params outside sandbox / live array
57
        $this->config['payment_action'] = $credentials['payment_action'];
58
        $this->config['notify_url'] = $credentials['notify_url'];
59
        $this->config['locale'] = $credentials['locale'];
60
    }
61
62
    /**
63
     * Set cart item details for PayPal.
64
     *
65
     * @param array $items
66
     *
67
     * @return \Illuminate\Support\Collection
68
     */
69
    protected function setCartItems($items)
70
    {
71
        return (new Collection($items))->map(function ($item, $num) {
72
            return [
73
                'L_PAYMENTREQUEST_0_NAME'.$num  => $item['name'],
74
                'L_PAYMENTREQUEST_0_AMT'.$num   => $item['price'],
75
                'L_PAYMENTREQUEST_0_QTY'.$num   => $item['qty'],
76
            ];
77
        })->flatMap(function ($value) {
78
            return $value;
79
        });
80
    }
81
82
    /**
83
     * Set Recurring payments details for SetExpressCheckout API call.
84
     *
85
     * @param array $data
86
     * @param bool  $subscription
87
     */
88
    protected function setExpressCheckoutRecurringPaymentConfig($data, $subscription = false)
89
    {
90
        if ($subscription) {
91
            $this->post = $this->post->merge([
92
                'L_BILLINGTYPE0'                    => 'RecurringPayments',
93
                'L_BILLINGAGREEMENTDESCRIPTION0'    => !empty($data['subscription_desc']) ?
94
                    $data['subscription_desc'] : $data['invoice_description'],
95
            ]);
96
        }
97
    }
98
99
    /**
100
     * Function to perform SetExpressCheckout PayPal API operation.
101
     *
102
     * @param array $data
103
     * @param bool  $subscription
104
     *
105
     * @return array
106
     */
107
    public function setExpressCheckout($data, $subscription = false)
108
    {
109
        $this->post = $this->setCartItems($data['items'])->merge([
110
            'PAYMENTREQUEST_0_ITEMAMT'          => $data['total'],
111
            'PAYMENTREQUEST_0_AMT'              => $data['total'],
112
            'PAYMENTREQUEST_0_PAYMENTACTION'    => $this->paymentAction,
113
            'PAYMENTREQUEST_0_CURRENCYCODE'     => $this->currency,
114
            'PAYMENTREQUEST_0_DESC'             => $data['invoice_description'],
115
            'PAYMENTREQUEST_0_INVNUM'           => $data['invoice_id'],
116
            'NOSHIPPING'                        => 1,
117
            'RETURNURL'                         => $data['return_url'],
118
            'CANCELURL'                         => $data['cancel_url'],
119
            'LOCALE'                            => $this->locale,
120
        ]);
121
122
        $this->setExpressCheckoutRecurringPaymentConfig($data, $subscription);
123
124
        return $this->doPayPalRequest('SetExpressCheckout');
125
    }
126
127
    /**
128
     * Function to perform GetExpressCheckoutDetails PayPal API operation.
129
     *
130
     * @param string $token
131
     *
132
     * @return array
133
     */
134
    public function getExpressCheckoutDetails($token)
135
    {
136
        $this->setRequestData([
137
            'TOKEN' => $token,
138
        ]);
139
140
        return $this->doPayPalRequest('GetExpressCheckoutDetails');
141
    }
142
143
    /**
144
     * Function to perform DoExpressCheckoutPayment PayPal API operation.
145
     *
146
     * @param array  $data
147
     * @param string $token
148
     * @param string $payerid
149
     *
150
     * @return array
151
     */
152
    public function doExpressCheckoutPayment($data, $token, $payerid)
153
    {
154
        $this->post = $this->setCartItems($data['items'])->merge([
155
            'TOKEN'                             => $token,
156
            'PAYERID'                           => $payerid,
157
            'PAYMENTREQUEST_0_ITEMAMT'          => $data['total'],
158
            'PAYMENTREQUEST_0_AMT'              => $data['total'],
159
            'PAYMENTREQUEST_0_PAYMENTACTION'    => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale',
160
            'PAYMENTREQUEST_0_CURRENCYCODE'     => $this->currency,
161
            'PAYMENTREQUEST_0_DESC'             => $data['invoice_description'],
162
            'PAYMENTREQUEST_0_INVNUM'           => $data['invoice_id'],
163
            'PAYMENTREQUEST_0_NOTIFYURL'        => $this->notifyUrl,
164
        ]);
165
166
        return $this->doPayPalRequest('DoExpressCheckoutPayment');
167
    }
168
169
    /**
170
     * Function to perform DoCapture PayPal API operation.
171
     *
172
     * @param string $authorization_id Transaction ID
173
     * @param float  $amount           Amount to capture
174
     * @param string $complete         Indicates whether or not this is the last capture.
175
     * @param array  $data             Optional request fields
176
     *
177
     * @return array
178
     */
179
    public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = [])
180
    {
181
        $this->setRequestData(
182
            array_merge($data, [
183
                'AUTHORIZATIONID' => $authorization_id,
184
                'AMT'             => $amount,
185
                'COMPLETETYPE'    => $complete,
186
                'CURRENCYCODE'    => $this->currency,
187
            ])
188
        );
189
190
        return $this->doPayPalRequest('DoCapture');
191
    }
192
193
    /**
194
     * Function to perform DoAuthorization PayPal API operation.
195
     *
196
     * @param string $authorization_id Transaction ID
197
     * @param float  $amount           Amount to capture
198
     * @param array  $data             Optional request fields
199
     *
200
     * @return array
201
     */
202
    public function doAuthorization($authorization_id, $amount, $data = [])
203
    {
204
        $this->setRequestData(
205
            array_merge($data, [
206
                'AUTHORIZATIONID' => $authorization_id,
207
                'AMT'             => $amount,
208
            ])
209
        );
210
211
        return $this->doPayPalRequest('DoAuthorization');
212
    }
213
214
    /**
215
     * Function to perform DoVoid PayPal API operation.
216
     *
217
     * @param string $authorization_id Transaction ID
218
     * @param array  $data             Optional request fields
219
     *
220
     * @return array
221
     */
222
    public function doVoid($authorization_id, $data = [])
223
    {
224
        $this->setRequestData(
225
            array_merge($data, [
226
                'AUTHORIZATIONID' => $authorization_id,
227
            ])
228
        );
229
230
        return $this->doPayPalRequest('DoVoid');
231
    }
232
233
    /**
234
     * Function to perform CreateBillingAgreement PayPal API operation.
235
     *
236
     * @param string $token
237
     *
238
     * @return array
239
     */
240
    public function createBillingAgreement($token)
241
    {
242
        $this->setRequestData([
243
            'TOKEN' => $token,
244
        ]);
245
246
        return $this->doPayPalRequest('CreateBillingAgreement');
247
    }
248
249
    /**
250
     * Function to perform CreateRecurringPaymentsProfile PayPal API operation.
251
     *
252
     * @param array  $data
253
     * @param string $token
254
     *
255
     * @return array
256
     */
257
    public function createRecurringPaymentsProfile($data, $token)
258
    {
259
        $this->setRequestData(
260
            array_merge(['TOKEN' => $token], $data)
261
        );
262
263
        return $this->doPayPalRequest('CreateRecurringPaymentsProfile');
264
    }
265
266
    /**
267
     * Function to perform GetRecurringPaymentsProfileDetails PayPal API operation.
268
     *
269
     * @param string $id
270
     *
271
     * @return array
272
     */
273
    public function getRecurringPaymentsProfileDetails($id)
274
    {
275
        $this->setRequestData([
276
            'PROFILEID' => $id,
277
        ]);
278
279
        return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails');
280
    }
281
282
    /**
283
     * Function to perform UpdateRecurringPaymentsProfile PayPal API operation.
284
     *
285
     * @param array  $data
286
     * @param string $id
287
     *
288
     * @return array
289
     */
290
    public function updateRecurringPaymentsProfile($data, $id)
291
    {
292
        $this->post = (new Collection([
293
            'PROFILEID' => $id,
294
        ]))->merge($data);
295
296
        return $this->doPayPalRequest('UpdateRecurringPaymentsProfile');
297
    }
298
299
    /**
300
     * Function to cancel RecurringPaymentsProfile on PayPal.
301
     *
302
     * @param string $id
303
     *
304
     * @return array
305
     */
306
    public function cancelRecurringPaymentsProfile($id)
307
    {
308
        $this->setRequestData([
309
            'PROFILEID' => $id,
310
            'ACTION'    => 'Cancel',
311
        ]);
312
313
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
314
    }
315
316
    /**
317
     * Function to suspend an active RecurringPaymentsProfile on PayPal.
318
     *
319
     * @param string $id
320
     *
321
     * @return array
322
     */
323
    public function suspendRecurringPaymentsProfile($id)
324
    {
325
        $this->setRequestData([
326
            'PROFILEID' => $id,
327
            'ACTION'    => 'Suspend',
328
        ]);
329
330
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
331
    }
332
333
    /**
334
     * Function to reactivate a suspended RecurringPaymentsProfile on PayPal.
335
     *
336
     * @param string $id
337
     *
338
     * @return array
339
     */
340
    public function reactivateRecurringPaymentsProfile($id)
341
    {
342
        $this->setRequestData([
343
            'PROFILEID' => $id,
344
            'ACTION'    => 'Reactivate',
345
        ]);
346
347
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
348
    }
349
}
350