Skip to content

Commit

Permalink
Refactor Invoices API implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 12, 2023
1 parent c3e2390 commit 3e15169
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 145 deletions.
250 changes: 131 additions & 119 deletions src/Traits/PayPalAPI/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@

trait Invoices
{
/**
* Generate the next invoice number.
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-next-invoice-number
*/
public function generateInvoiceNumber()
{
$this->apiEndPoint = 'v2/invoicing/generate-next-invoice-number';

$this->verb = 'post';

return $this->doPayPalRequest();
}

/**
* Create a new draft invoice.
*
Expand Down Expand Up @@ -69,117 +51,86 @@ public function listInvoices(array $fields = [])
}

/**
* Delete an invoice.
* Send an existing invoice.
*
* @param string $invoice_id
* @param string $subject
* @param string $note
* @param bool $send_recipient
* @param bool $send_merchant
* @param array $recipients
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_send
*/
public function deleteInvoice(string $invoice_id)
public function sendInvoice(string $invoice_id, string $subject = '', string $note = '', bool $send_recipient = true, bool $send_merchant = false, array $recipients = [])
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/send";

$this->verb = 'delete';
$this->options['json'] = $this->getInvoiceMessagePayload($subject, $note, $recipients, $send_recipient, $send_merchant);

$this->verb = 'post';

return $this->doPayPalRequest(false);
}

/**
* Update an existing invoice.
* Send reminder for an existing invoice.
*
* @param string $invoice_id
* @param array $data
* @param string $subject
* @param string $note
* @param bool $send_recipient
* @param bool $send_merchant
* @param array $recipients
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_update
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_remind
*/
public function updateInvoice(string $invoice_id, array $data)
public function sendInvoiceReminder(string $invoice_id, string $subject = '', string $note = '', bool $send_recipient = true, bool $send_merchant = false, array $recipients = [])
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/remind";

$this->options['json'] = $data;
$this->options['json'] = $this->getInvoiceMessagePayload($subject, $note, $recipients, $send_recipient, $send_merchant);

$this->verb = 'put';
$this->verb = 'post';

return $this->doPayPalRequest();
return $this->doPayPalRequest(false);
}

/**
* Show details for an existing invoice.
* Cancel an existing invoice which is already sent.
*
* @param string $invoice_id
*
* @throws \Throwable
* @param string $subject
* @param string $note
* @param bool $send_recipient
* @param bool $send_merchant
* @param array $recipients
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_get
*/
public function showInvoiceDetails(string $invoice_id)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";

$this->verb = 'get';

return $this->doPayPalRequest();
}

/**
* Cancel an existing invoice which is already sent.
*
* @param string $invoice_id
* @param array $notes
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_cancel
*/
public function cancelInvoice(string $invoice_id, array $notes)
public function cancelInvoice(string $invoice_id, string $subject = '', string $note = '', bool $send_recipient = true, bool $send_merchant = false, array $recipients = [])
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/cancel";

$this->options['json'] = $notes;
$this->options['json'] = $this->getInvoiceMessagePayload($subject, $note, $recipients, $send_recipient, $send_merchant);

$this->verb = 'post';

return $this->doPayPalRequest(false);
}

/**
* Generate QR code against an existing invoice.
*
* @param string $invoice_id
* @param int $width
* @param int $height
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-qr-code
*/
public function generateQRCodeInvoice(string $invoice_id, int $width = 100, int $height = 100)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/generate-qr-code";

$this->options['json'] = [
'width' => $width,
'height' => $height,
];
$this->verb = 'post';

return $this->doPayPalRequest();
}

/**
* Register payment against an existing invoice.
*
Expand Down Expand Up @@ -230,7 +181,7 @@ public function registerPaymentInvoice(string $invoice_id, string $payment_date,
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_payments-delete
*/
public function deleteExternalPaymentInvoice($invoice_id, $transaction_id)
public function deleteExternalPaymentInvoice(string $invoice_id, string $transaction_id)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/payments/{$transaction_id}";

Expand All @@ -253,7 +204,7 @@ public function deleteExternalPaymentInvoice($invoice_id, $transaction_id)
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_refunds
*/
public function refundInvoice($invoice_id, $payment_date, $payment_method, $amount)
public function refundInvoice(string $invoice_id, string $payment_date, string $payment_method, float $amount)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/refunds";

Expand Down Expand Up @@ -285,7 +236,7 @@ public function refundInvoice($invoice_id, $payment_date, $payment_method, $amou
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_refunds-delete
*/
public function deleteRefundInvoice($invoice_id, $transaction_id)
public function deleteRefundInvoice(string $invoice_id, string $transaction_id)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/refunds/{$transaction_id}";

Expand All @@ -295,72 +246,133 @@ public function deleteRefundInvoice($invoice_id, $transaction_id)
}

/**
* Send an existing invoice.
* Generate QR code against an existing invoice.
*
* @param string $invoice_id
* @param string $subject
* @param string $note
* @param bool $send_recipient
* @param bool $send_merchant
* @param array $recipients
* @param int $width
* @param int $height
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_send
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-qr-code
*/
public function sendInvoice($invoice_id, $subject = '', $note = '', $send_recipient = true, $send_merchant = false, array $recipients = [])
public function generateQRCodeInvoice(string $invoice_id, int $width = 100, int $height = 100)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/send";
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/generate-qr-code";

$data = [
'subject' => !empty($subject) ? $subject : '',
'note' => !empty($note) ? $note : '',
'additional_recipients' => (collect($recipients)->count() > 0) ? $recipients : '',
'send_to_recipient' => $send_recipient,
'send_to_invoicer' => $send_merchant,
$this->options['json'] = [
'width' => $width,
'height' => $height,
];
$this->verb = 'post';

$this->options['json'] = collect($data)->filter()->toArray();
return $this->doPayPalRequest();
}

/**
* Generate the next invoice number.
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_generate-next-invoice-number
*/
public function generateInvoiceNumber()
{
$this->apiEndPoint = 'v2/invoicing/generate-next-invoice-number';

$this->verb = 'post';

return $this->doPayPalRequest(false);
return $this->doPayPalRequest();
}

/**
* Send reminder for an existing invoice.
* Show details for an existing invoice.
*
* @param string $invoice_id
* @param string $subject
* @param string $note
* @param bool $send_recipient
* @param bool $send_merchant
* @param array $recipients
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_remind
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_get
*/
public function sendInvoiceReminder($invoice_id, $subject = '', $note = '', $send_recipient = true, $send_merchant = false, array $recipients = [])
public function showInvoiceDetails(string $invoice_id)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}/remind";
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";

$data = [
'subject' => !empty($subject) ? $subject : '',
'note' => !empty($note) ? $note : '',
'additional_recipients' => (collect($recipients)->count() > 0) ? $recipients : '',
'send_to_recipient' => $send_recipient,
'send_to_invoicer' => $send_merchant,
];
$this->verb = 'get';

$this->options['json'] = collect($data)->filter()->toArray();
return $this->doPayPalRequest();
}

$this->verb = 'post';
/**
* Update an existing invoice.
*
* @param string $invoice_id
* @param array $data
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_update
*/
public function updateInvoice(string $invoice_id, array $data)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";

$this->options['json'] = $data;

$this->verb = 'put';

return $this->doPayPalRequest();
}

/**
* Delete an invoice.
*
* @param string $invoice_id
*
* @throws \Throwable
*
* @return array|\Psr\Http\Message\StreamInterface|string
*
* @see https://developer.paypal.com/docs/api/invoicing/v2/#invoices_list
*/
public function deleteInvoice(string $invoice_id)
{
$this->apiEndPoint = "v2/invoicing/invoices/{$invoice_id}";

$this->verb = 'delete';

return $this->doPayPalRequest(false);
}

/**
* Get Invoice Message Payload.
*
* @param string $subject
* @param string $note
* @param array $recipients
* @param bool $send_recipient
* @param bool $send_merchant
*
* @return array
*/
protected function getInvoiceMessagePayload(string $subject, string $note, array $recipients, bool $send_recipient, bool $send_merchant): array
{
$data = [
'subject' => !empty($subject) ? $subject : '',
'note' => !empty($note) ? $note : '',
'additional_recipients' => (collect($recipients)->count() > 0) ? $recipients : '',
'send_to_recipient' => $send_recipient,
'send_to_invoicer' => $send_merchant,
];

return collect($data)->filter()->toArray();
}
}
Loading

0 comments on commit 3e15169

Please sign in to comment.