Completed
Push — master ( 9fda72...c51e97 )
by Raza
03:32
created

ExpressCheckout::setCartItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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