|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Traits\PayPalAPI; |
|
4
|
|
|
|
|
5
|
|
|
trait Invoices |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Generate the next invoice number. |
|
9
|
|
|
* |
|
10
|
|
|
* @throws \Throwable |
|
11
|
|
|
* |
|
12
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
13
|
|
|
* |
|
14
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-next-invoice-number |
|
15
|
|
|
*/ |
|
16
|
|
|
public function generateInvoiceNumber() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->apiEndPoint = "v2/invoicing/generate-next-invoice-number"; |
|
|
|
|
|
|
19
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
return $this->doPayPalRequest(); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Create a new draft invoice. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $data |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \Throwable |
|
32
|
|
|
* |
|
33
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
34
|
|
|
* |
|
35
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_create |
|
36
|
|
|
*/ |
|
37
|
|
|
public function createInvoice(array $data) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices"; |
|
|
|
|
|
|
40
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
return $this->doPayPalRequest(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get list of invoices. |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $page |
|
53
|
|
|
* @param int $size |
|
54
|
|
|
* @param bool $totals |
|
55
|
|
|
* @param array $fields |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Throwable |
|
58
|
|
|
* |
|
59
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
60
|
|
|
* |
|
61
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list |
|
62
|
|
|
*/ |
|
63
|
|
|
public function listInvoices($page=1, $size=20, $totals=true, array $fields=[]) |
|
64
|
|
|
{ |
|
65
|
|
|
$fields_list = collect($fields); |
|
66
|
|
|
|
|
67
|
|
|
$fields = ""; |
|
68
|
|
|
if ($fields_list->count() > 0) |
|
69
|
|
|
$fields = "&fields={$fields_list->implode(",")}"; |
|
70
|
|
|
|
|
71
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices?page={$page}&page_size={$size}&total_required={$totals}{$fields}"; |
|
|
|
|
|
|
72
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$this->verb = 'get'; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return $this->doPayPalRequest(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Delete an invoice. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $invoice_id |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Throwable |
|
85
|
|
|
* |
|
86
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
87
|
|
|
* |
|
88
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list |
|
89
|
|
|
*/ |
|
90
|
|
|
public function deleteInvoice($invoice_id) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}"; |
|
|
|
|
|
|
93
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$this->verb = 'delete'; |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return $this->doPayPalRequest(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Update an existing invoice. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $invoice_id |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \Throwable |
|
107
|
|
|
* |
|
108
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
109
|
|
|
* |
|
110
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_update |
|
111
|
|
|
*/ |
|
112
|
|
|
public function updateInvoice($invoice_id, array $data) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}"; |
|
|
|
|
|
|
115
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$this->verb = 'put'; |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
return $this->doPayPalRequest(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Show details for an existing invoice. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $invoice_id |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \Throwable |
|
130
|
|
|
* |
|
131
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
132
|
|
|
* |
|
133
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_get |
|
134
|
|
|
*/ |
|
135
|
|
|
public function showInvoiceDetails($invoice_id) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}"; |
|
|
|
|
|
|
138
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
$this->verb = 'get'; |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
return $this->doPayPalRequest(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Cancel an existing invoice which is already sent. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $invoice_id |
|
149
|
|
|
* |
|
150
|
|
|
* @throws \Throwable |
|
151
|
|
|
* |
|
152
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
153
|
|
|
* |
|
154
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_cancel |
|
155
|
|
|
*/ |
|
156
|
|
|
public function cancelInvoice($invoice_id) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/cancel"; |
|
|
|
|
|
|
159
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
return $this->doPayPalRequest(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Generate QR code against an existing invoice. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $invoice_id |
|
170
|
|
|
* @param int $width |
|
171
|
|
|
* @param int $height |
|
172
|
|
|
* |
|
173
|
|
|
* @throws \Throwable |
|
174
|
|
|
* |
|
175
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
176
|
|
|
* |
|
177
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-qr-code |
|
178
|
|
|
*/ |
|
179
|
|
|
public function generateQRCodeInvoice($invoice_id, $width=200, $height=20) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/generate-qr-code"; |
|
|
|
|
|
|
182
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
$this->options['json'] = [ |
|
|
|
|
|
|
185
|
|
|
"width" => $width, |
|
186
|
|
|
"height" => $height, |
|
187
|
|
|
]; |
|
188
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return $this->doPayPalRequest(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Register payment against an existing invoice. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $invoice_id |
|
197
|
|
|
* @param string $payment_id |
|
198
|
|
|
* @param string $payment_date |
|
199
|
|
|
* @param string $payment_method |
|
200
|
|
|
* @param string $payment_note |
|
201
|
|
|
* @param float $amount |
|
202
|
|
|
* @param string $currency |
|
203
|
|
|
* |
|
204
|
|
|
* @throws \Throwable |
|
205
|
|
|
* |
|
206
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
207
|
|
|
* |
|
208
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_payments |
|
209
|
|
|
*/ |
|
210
|
|
|
public function registerPaymentInvoice($invoice_id, $payment_id, $payment_date, $payment_method, $payment_note, $amount, $currency='') |
|
211
|
|
|
{ |
|
212
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/payments"; |
|
|
|
|
|
|
213
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
if (isset($currency)) { |
|
216
|
|
|
$this->setCurrency($currency); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$data = [ |
|
220
|
|
|
"payment_id" => $payment_id, |
|
221
|
|
|
"payment_date" => $payment_date, |
|
222
|
|
|
"method" => $payment_method, |
|
223
|
|
|
"note" => $payment_note, |
|
224
|
|
|
"amount" => [ |
|
225
|
|
|
"currency" => $this->currency, |
|
226
|
|
|
"value" => $amount, |
|
227
|
|
|
], |
|
228
|
|
|
]; |
|
229
|
|
|
|
|
230
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
return $this->doPayPalRequest(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Delete payment against an existing invoice. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $invoice_id |
|
241
|
|
|
* @param string $transaction_id |
|
242
|
|
|
* |
|
243
|
|
|
* @throws \Throwable |
|
244
|
|
|
* |
|
245
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
246
|
|
|
* |
|
247
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_payments-delete |
|
248
|
|
|
*/ |
|
249
|
|
|
public function deleteExternalPaymentInvoice($invoice_id, $transaction_id) |
|
250
|
|
|
{ |
|
251
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/payments/{$transaction_id}"; |
|
|
|
|
|
|
252
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
$this->verb = 'delete'; |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
return $this->doPayPalRequest(); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Register payment against an existing invoice. |
|
261
|
|
|
* |
|
262
|
|
|
* @param string $invoice_id |
|
263
|
|
|
* @param string $payment_date |
|
264
|
|
|
* @param string $payment_method |
|
265
|
|
|
* @param float $amount |
|
266
|
|
|
* @param string $currency |
|
267
|
|
|
* |
|
268
|
|
|
* @throws \Throwable |
|
269
|
|
|
* |
|
270
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
271
|
|
|
* |
|
272
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_refunds |
|
273
|
|
|
*/ |
|
274
|
|
|
public function refundInvoice($invoice_id, $payment_date, $payment_method, $amount, $currency='') |
|
275
|
|
|
{ |
|
276
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/refunds"; |
|
|
|
|
|
|
277
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
if (isset($currency)) { |
|
280
|
|
|
$this->setCurrency($currency); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
$data = [ |
|
284
|
|
|
"refund_date" => $payment_date, |
|
285
|
|
|
"method" => $payment_method, |
|
286
|
|
|
"amount" => [ |
|
287
|
|
|
"currency" => $this->currency, |
|
288
|
|
|
"value" => $amount, |
|
289
|
|
|
], |
|
290
|
|
|
]; |
|
291
|
|
|
|
|
292
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
return $this->doPayPalRequest(); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Delete refund against an existing invoice. |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $invoice_id |
|
303
|
|
|
* @param string $transaction_id |
|
304
|
|
|
* |
|
305
|
|
|
* @throws \Throwable |
|
306
|
|
|
* |
|
307
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
308
|
|
|
* |
|
309
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_refunds-delete |
|
310
|
|
|
*/ |
|
311
|
|
|
public function deleteRefundInvoice($invoice_id, $transaction_id) |
|
312
|
|
|
{ |
|
313
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/refunds/{$transaction_id}"; |
|
|
|
|
|
|
314
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
$this->verb = 'delete'; |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
return $this->doPayPalRequest(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Send an existing invoice. |
|
323
|
|
|
* |
|
324
|
|
|
* @param string $invoice_id |
|
325
|
|
|
* @param string $subject |
|
326
|
|
|
* @param string $note |
|
327
|
|
|
* @param bool $send_recipient |
|
328
|
|
|
* @param bool $send_merchant |
|
329
|
|
|
* @param array $recipients |
|
330
|
|
|
* |
|
331
|
|
|
* @throws \Throwable |
|
332
|
|
|
* |
|
333
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
334
|
|
|
* |
|
335
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_send |
|
336
|
|
|
*/ |
|
337
|
|
|
public function sendInvoice($invoice_id, $subject='', $note='', $send_recipient=true, $send_merchant=false, array $recipients=[]) |
|
338
|
|
|
{ |
|
339
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/send"; |
|
|
|
|
|
|
340
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
$data = []; |
|
343
|
|
|
if (!empty($subject)) { |
|
344
|
|
|
$data["subject"] = $subject; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
if (!empty($note)) { |
|
348
|
|
|
$data["note"] = $note; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
if (collect($recipients)->count() > 0) { |
|
352
|
|
|
$data["additional_recipients"] = $recipients; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$data["send_to_recipient"] = $send_recipient; |
|
356
|
|
|
$data["send_to_invoicer"] = $send_merchant; |
|
357
|
|
|
|
|
358
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
361
|
|
|
|
|
362
|
|
|
return $this->doPayPalRequest(); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* Send reminder for an existing invoice. |
|
367
|
|
|
* |
|
368
|
|
|
* @param string $invoice_id |
|
369
|
|
|
* @param string $subject |
|
370
|
|
|
* @param string $note |
|
371
|
|
|
* @param bool $send_recipient |
|
372
|
|
|
* @param bool $send_merchant |
|
373
|
|
|
* @param array $recipients |
|
374
|
|
|
* |
|
375
|
|
|
* @throws \Throwable |
|
376
|
|
|
* |
|
377
|
|
|
* @return array|\Psr\Http\Message\StreamInterface|string |
|
378
|
|
|
* |
|
379
|
|
|
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_remind |
|
380
|
|
|
*/ |
|
381
|
|
|
public function sendInvoiceReminder($invoice_id, $subject='', $note='', $send_recipient=true, $send_merchant=false, array $recipients=[]) |
|
382
|
|
|
{ |
|
383
|
|
|
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/remind"; |
|
|
|
|
|
|
384
|
|
|
$this->apiUrl = collect([$this->apiUrl, $this->apiEndPoint])->implode('/'); |
|
|
|
|
|
|
385
|
|
|
|
|
386
|
|
|
$data = []; |
|
387
|
|
|
if (!empty($subject)) { |
|
388
|
|
|
$data["subject"] = $subject; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
if (!empty($note)) { |
|
392
|
|
|
$data["note"] = $note; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
if (collect($recipients)->count() > 0) { |
|
396
|
|
|
$data["additional_recipients"] = $recipients; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
$data["send_to_recipient"] = $send_recipient; |
|
400
|
|
|
$data["send_to_invoicer"] = $send_merchant; |
|
401
|
|
|
|
|
402
|
|
|
$this->options['json'] = $data; |
|
|
|
|
|
|
403
|
|
|
|
|
404
|
|
|
$this->verb = 'post'; |
|
|
|
|
|
|
405
|
|
|
|
|
406
|
|
|
return $this->doPayPalRequest(); |
|
407
|
|
|
} |
|
408
|
|
|
} |
|
409
|
|
|
|