Completed
Push — master ( fa2214...b202f9 )
by Raza
02:46
created

ExpressCheckout::setExpressCheckout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 4
b 0
f 0
nc 1
nop 2
dl 0
loc 23
rs 9.0856
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
        $response = $this->doPayPalRequest('SetExpressCheckout');
125
126
        $response['paypal_link'] = $this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN'];
127
128
        return $response;
129
    }
130
131
    /**
132
     * Function to perform GetExpressCheckoutDetails PayPal API operation.
133
     *
134
     * @param string $token
135
     *
136
     * @return array
137
     */
138
    public function getExpressCheckoutDetails($token)
139
    {
140
        $this->setRequestData([
141
            'TOKEN' => $token,
142
        ]);
143
144
        return $this->doPayPalRequest('GetExpressCheckoutDetails');
145
    }
146
147
    /**
148
     * Function to perform DoExpressCheckoutPayment PayPal API operation.
149
     *
150
     * @param array  $data
151
     * @param string $token
152
     * @param string $payerid
153
     *
154
     * @return array
155
     */
156
    public function doExpressCheckoutPayment($data, $token, $payerid)
157
    {
158
        $this->post = $this->setCartItems($data['items'])->merge([
159
            'TOKEN'                             => $token,
160
            'PAYERID'                           => $payerid,
161
            'PAYMENTREQUEST_0_ITEMAMT'          => $data['total'],
162
            'PAYMENTREQUEST_0_AMT'              => $data['total'],
163
            'PAYMENTREQUEST_0_PAYMENTACTION'    => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale',
164
            'PAYMENTREQUEST_0_CURRENCYCODE'     => $this->currency,
165
            'PAYMENTREQUEST_0_DESC'             => $data['invoice_description'],
166
            'PAYMENTREQUEST_0_INVNUM'           => $data['invoice_id'],
167
            'PAYMENTREQUEST_0_NOTIFYURL'        => $this->notifyUrl,
168
        ]);
169
170
        return $this->doPayPalRequest('DoExpressCheckoutPayment');
171
    }
172
173
    /**
174
     * Function to perform DoCapture PayPal API operation.
175
     *
176
     * @param string $authorization_id Transaction ID
177
     * @param float  $amount           Amount to capture
178
     * @param string $complete         Indicates whether or not this is the last capture.
179
     * @param array  $data             Optional request fields
180
     *
181
     * @return array
182
     */
183
    public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = [])
184
    {
185
        $this->setRequestData(
186
            array_merge($data, [
187
                'AUTHORIZATIONID' => $authorization_id,
188
                'AMT'             => $amount,
189
                'COMPLETETYPE'    => $complete,
190
                'CURRENCYCODE'    => $this->currency,
191
            ])
192
        );
193
194
        return $this->doPayPalRequest('DoCapture');
195
    }
196
197
    /**
198
     * Function to perform DoAuthorization PayPal API operation.
199
     *
200
     * @param string $authorization_id Transaction ID
201
     * @param float  $amount           Amount to capture
202
     * @param array  $data             Optional request fields
203
     *
204
     * @return array
205
     */
206
    public function doAuthorization($authorization_id, $amount, $data = [])
207
    {
208
        $this->setRequestData(
209
            array_merge($data, [
210
                'AUTHORIZATIONID' => $authorization_id,
211
                'AMT'             => $amount,
212
            ])
213
        );
214
215
        return $this->doPayPalRequest('DoAuthorization');
216
    }
217
218
    /**
219
     * Function to perform DoVoid PayPal API operation.
220
     *
221
     * @param string $authorization_id Transaction ID
222
     * @param array  $data             Optional request fields
223
     *
224
     * @return array
225
     */
226
    public function doVoid($authorization_id, $data = [])
227
    {
228
        $this->setRequestData(
229
            array_merge($data, [
230
                'AUTHORIZATIONID' => $authorization_id,
231
            ])
232
        );
233
234
        return $this->doPayPalRequest('DoVoid');
235
    }
236
237
    /**
238
     * Function to perform CreateBillingAgreement PayPal API operation.
239
     *
240
     * @param string $token
241
     *
242
     * @return array
243
     */
244
    public function createBillingAgreement($token)
245
    {
246
        $this->setRequestData([
247
            'TOKEN' => $token,
248
        ]);
249
250
        return $this->doPayPalRequest('CreateBillingAgreement');
251
    }
252
253
    /**
254
     * Function to perform CreateRecurringPaymentsProfile PayPal API operation.
255
     *
256
     * @param array  $data
257
     * @param string $token
258
     *
259
     * @return array
260
     */
261
    public function createRecurringPaymentsProfile($data, $token)
262
    {
263
        $this->post = $this->setRequestData($data)->merge([
264
            'TOKEN' => $token,
265
        ]);
266
267
        return $this->doPayPalRequest('CreateRecurringPaymentsProfile');
268
    }
269
270
    /**
271
     * Function to perform GetRecurringPaymentsProfileDetails PayPal API operation.
272
     *
273
     * @param string $id
274
     *
275
     * @return array
276
     */
277
    public function getRecurringPaymentsProfileDetails($id)
278
    {
279
        $this->setRequestData([
280
            'PROFILEID' => $id,
281
        ]);
282
283
        return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails');
284
    }
285
286
    /**
287
     * Function to perform UpdateRecurringPaymentsProfile PayPal API operation.
288
     *
289
     * @param array  $data
290
     * @param string $id
291
     *
292
     * @return array
293
     */
294
    public function updateRecurringPaymentsProfile($data, $id)
295
    {
296
        $this->post = $this->setRequestData($data)->merge([
297
            'PROFILEID' => $id,
298
        ]);
299
300
        return $this->doPayPalRequest('UpdateRecurringPaymentsProfile');
301
    }
302
303
    /**
304
     * Change Recurring payment profile status on PayPal.
305
     *
306
     * @param string $id
307
     * @param string $status
308
     *
309
     * @return array|\Psr\Http\Message\StreamInterface
310
     */
311
    protected function manageRecurringPaymentsProfileStatus($id, $status)
312
    {
313
        $this->setRequestData([
314
            'PROFILEID' => $id,
315
            'ACTION'    => $status,
316
        ]);
317
318
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
319
    }
320
321
    /**
322
     * Cancel RecurringPaymentsProfile on PayPal.
323
     *
324
     * @param string $id
325
     *
326
     * @return array
327
     */
328
    public function cancelRecurringPaymentsProfile($id)
329
    {
330
        return $this->manageRecurringPaymentsProfileStatus($id, 'Cancel');
331
    }
332
333
    /**
334
     * Suspend an active RecurringPaymentsProfile on PayPal.
335
     *
336
     * @param string $id
337
     *
338
     * @return array
339
     */
340
    public function suspendRecurringPaymentsProfile($id)
341
    {
342
        return $this->manageRecurringPaymentsProfileStatus($id, 'Suspend');
343
    }
344
345
    /**
346
     * Reactivate a suspended RecurringPaymentsProfile on PayPal.
347
     *
348
     * @param string $id
349
     *
350
     * @return array
351
     */
352
    public function reactivateRecurringPaymentsProfile($id)
353
    {
354
        return $this->manageRecurringPaymentsProfileStatus($id, 'Reactivate');
355
    }
356
}
357