Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 676 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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