Completed
Push — master ( 4c7939...95448b )
by Raza
02:14
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
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
     * Function to perform SetExpressCheckout PayPal API operation.
84
     *
85
     * @param array $data
86
     * @param bool  $subscription
87
     *
88
     * @return array
89
     */
90
    public function setExpressCheckout($data, $subscription = false)
91
    {
92
        $this->post = $this->setCartItems($data['items'])->merge([
93
            'PAYMENTREQUEST_0_ITEMAMT'          => $data['total'],
94
            'PAYMENTREQUEST_0_AMT'              => $data['total'],
95
            'PAYMENTREQUEST_0_PAYMENTACTION'    => $this->paymentAction,
96
            'PAYMENTREQUEST_0_CURRENCYCODE'     => $this->currency,
97
            'PAYMENTREQUEST_0_DESC'             => $data['invoice_description'],
98
            'PAYMENTREQUEST_0_INVNUM'           => $data['invoice_id'],
99
            'NOSHIPPING'                        => 1,
100
            'RETURNURL'                         => $data['return_url'],
101
            'CANCELURL'                         => $data['cancel_url'],
102
            'LOCALE'                            => $this->locale,
103
        ]);
104
105
        if ($subscription) {
106
            $this->post->merge([
107
                'L_BILLINGTYPE0'                    => 'RecurringPayment',
108
                'L_BILLINGAGREEMENTDESCRIPTION0'    => !empty($data['subscription_desc']) ?
109
                    $data['subscription_desc'] : $data['invoice_description'],
110
            ]);
111
        }
112
113
        $response = $this->doPayPalRequest('SetExpressCheckout');
114
115
        $response['paypal_link'] = empty($response['TOKEN']) ?:
116
            $this->config['gateway_url'].'/webscr?cmd=_express-checkout&token='.$response['TOKEN'];
117
118
        return $response;
119
    }
120
121
    /**
122
     * Function to perform GetExpressCheckoutDetails PayPal API operation.
123
     *
124
     * @param string $token
125
     *
126
     * @return array
127
     */
128
    public function getExpressCheckoutDetails($token)
129
    {
130
        $this->setRequestData([
131
            'TOKEN' => $token,
132
        ]);
133
134
        return $this->doPayPalRequest('GetExpressCheckoutDetails');
135
    }
136
137
    /**
138
     * Function to perform DoExpressCheckoutPayment PayPal API operation.
139
     *
140
     * @param array  $data
141
     * @param string $token
142
     * @param string $payerid
143
     *
144
     * @return array
145
     */
146
    public function doExpressCheckoutPayment($data, $token, $payerid)
147
    {
148
        $this->post = $this->setCartItems($data['items'])->merge([
149
            'TOKEN'                             => $token,
150
            'PAYERID'                           => $payerid,
151
            'PAYMENTREQUEST_0_ITEMAMT'          => $data['total'],
152
            'PAYMENTREQUEST_0_AMT'              => $data['total'],
153
            'PAYMENTREQUEST_0_PAYMENTACTION'    => !empty($this->config['payment_action']) ? $this->config['payment_action'] : 'Sale',
154
            'PAYMENTREQUEST_0_CURRENCYCODE'     => $this->currency,
155
            'PAYMENTREQUEST_0_DESC'             => $data['invoice_description'],
156
            'PAYMENTREQUEST_0_INVNUM'           => $data['invoice_id'],
157
            'PAYMENTREQUEST_0_NOTIFYURL'        => $this->notifyUrl,
158
        ]);
159
160
        return $this->doPayPalRequest('DoExpressCheckoutPayment');
161
    }
162
163
    /**
164
     * Function to perform DoCapture PayPal API operation.
165
     *
166
     * @param string $authorization_id Transaction ID
167
     * @param float  $amount           Amount to capture
168
     * @param string $complete         Indicates whether or not this is the last capture.
169
     * @param array  $data             Optional request fields
170
     *
171
     * @return array
172
     */
173
    public function doCapture($authorization_id, $amount, $complete = 'Complete', $data = [])
174
    {
175
        $this->setRequestData(
176
            array_merge($data, [
177
                'AUTHORIZATIONID' => $authorization_id,
178
                'AMT'             => $amount,
179
                'COMPLETETYPE'    => $complete,
180
                'CURRENCYCODE'    => $this->currency,
181
            ])
182
        );
183
184
        return $this->doPayPalRequest('DoCapture');
185
    }
186
187
    /**
188
     * Function to perform DoAuthorization PayPal API operation.
189
     *
190
     * @param string $authorization_id Transaction ID
191
     * @param float  $amount           Amount to capture
192
     * @param array  $data             Optional request fields
193
     *
194
     * @return array
195
     */
196
    public function doAuthorization($authorization_id, $amount, $data = [])
197
    {
198
        $this->setRequestData(
199
            array_merge($data, [
200
                'AUTHORIZATIONID' => $authorization_id,
201
                'AMT'             => $amount,
202
            ])
203
        );
204
205
        return $this->doPayPalRequest('DoAuthorization');
206
    }
207
208
    /**
209
     * Function to perform DoVoid PayPal API operation.
210
     *
211
     * @param string $authorization_id Transaction ID
212
     * @param array  $data             Optional request fields
213
     *
214
     * @return array
215
     */
216
    public function doVoid($authorization_id, $data = [])
217
    {
218
        $this->setRequestData(
219
            array_merge($data, [
220
                'AUTHORIZATIONID' => $authorization_id,
221
            ])
222
        );
223
224
        return $this->doPayPalRequest('DoVoid');
225
    }
226
227
    /**
228
     * Function to perform CreateBillingAgreement PayPal API operation.
229
     *
230
     * @param string $token
231
     *
232
     * @return array
233
     */
234
    public function createBillingAgreement($token)
235
    {
236
        $this->setRequestData([
237
            'TOKEN' => $token,
238
        ]);
239
240
        return $this->doPayPalRequest('CreateBillingAgreement');
241
    }
242
243
    /**
244
     * Function to perform CreateRecurringPaymentsProfile PayPal API operation.
245
     *
246
     * @param array  $data
247
     * @param string $token
248
     *
249
     * @return array
250
     */
251
    public function createRecurringPaymentsProfile($data, $token)
252
    {
253
        $this->setRequestData(
254
            array_merge(['TOKEN' => $token], $data)
255
        );
256
257
        return $this->doPayPalRequest('CreateRecurringPaymentsProfile');
258
    }
259
260
    /**
261
     * Function to perform GetRecurringPaymentsProfileDetails PayPal API operation.
262
     *
263
     * @param string $id
264
     *
265
     * @return array
266
     */
267
    public function getRecurringPaymentsProfileDetails($id)
268
    {
269
        $this->setRequestData([
270
            'PROFILEID' => $id,
271
        ]);
272
273
        return $this->doPayPalRequest('GetRecurringPaymentsProfileDetails');
274
    }
275
276
    /**
277
     * Function to perform UpdateRecurringPaymentsProfile PayPal API operation.
278
     *
279
     * @param array  $data
280
     * @param string $id
281
     *
282
     * @return array
283
     */
284
    public function updateRecurringPaymentsProfile($data, $id)
285
    {
286
        $this->post = (new Collection([
287
            'PROFILEID' => $id,
288
        ]))->merge($data);
289
290
        return $this->doPayPalRequest('UpdateRecurringPaymentsProfile');
291
    }
292
293
    /**
294
     * Function to cancel RecurringPaymentsProfile on PayPal.
295
     *
296
     * @param string $id
297
     *
298
     * @return array
299
     */
300
    public function cancelRecurringPaymentsProfile($id)
301
    {
302
        $this->setRequestData([
303
            'PROFILEID' => $id,
304
            'ACTION'    => 'Cancel',
305
        ]);
306
307
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
308
    }
309
310
    /**
311
     * Function to suspend an active RecurringPaymentsProfile on PayPal.
312
     *
313
     * @param string $id
314
     *
315
     * @return array
316
     */
317
    public function suspendRecurringPaymentsProfile($id)
318
    {
319
        $this->setRequestData([
320
            'PROFILEID' => $id,
321
            'ACTION'    => 'Suspend',
322
        ]);
323
324
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
325
    }
326
327
    /**
328
     * Function to reactivate a suspended RecurringPaymentsProfile on PayPal.
329
     *
330
     * @param string $id
331
     *
332
     * @return array
333
     */
334
    public function reactivateRecurringPaymentsProfile($id)
335
    {
336
        $this->setRequestData([
337
            'PROFILEID' => $id,
338
            'ACTION'    => 'Reactivate',
339
        ]);
340
341
        return $this->doPayPalRequest('ManageRecurringPaymentsProfileStatus');
342
    }
343
}
344