Completed
Push — master ( fb0eae...c6e71c )
by Raza
01:27
created

getRecurringPaymentsProfileDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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