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