From 983b5c4f2b4d2a4c448470840c20e8901f20ba2b Mon Sep 17 00:00:00 2001 From: root Date: Thu, 4 Nov 2021 10:41:55 -0400 Subject: [PATCH 1/4] added Invoice creation, void, list, resolve functions --- src/Resources/Invoice.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/Resources/Invoice.php diff --git a/src/Resources/Invoice.php b/src/Resources/Invoice.php new file mode 100644 index 0000000..f145e90 --- /dev/null +++ b/src/Resources/Invoice.php @@ -0,0 +1,38 @@ +id; + $path = Util::joinPath(static::getResourcePath(), $id, 'void'); + $client = static::getClient(); + $response = $client->put($path, [], $headers); + $this->refreshFrom($response); + } + + public function resolve($headers = []) + { + $id = $this->id; + $path = Util::joinPath(static::getResourcePath(), $id, 'resolve'); + $client = static::getClient(); + $response = $client->post($path, [], $headers); + $this->refreshFrom($response); + } +} From 32154d8768ee542fb11b8534053949e0db028174 Mon Sep 17 00:00:00 2001 From: HawtDogFlvrWtr Date: Thu, 4 Nov 2021 10:56:03 -0400 Subject: [PATCH 2/4] updated README.md to reflect new invoice additions --- README.md | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/README.md b/README.md index eca90aa..db4bcac 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ The official PHP library for the [Coinbase Commerce API](https://commerce.coinba * [Usage](#usage) * [Checkouts](#checkouts) * [Charges](#charges) + * [Invoices](#invoices) * [Events](#events) * [Webhooks](#webhooks) * [Warnings](#warnings) @@ -264,6 +265,82 @@ if ($chargeObj) { } ``` +## Invoices +[Invoices API docs](https://commerce.coinbase.com/docs/api/#invoices) +More examples on how to use charges can be found in the [`examples/Resources/InvoiceExample.php`](examples/Resources/InvoiceExample.php) file + +### Load invoice resource class +``` php +use CoinbaseCommerce\Resources\Invoice; +``` +### Retrieve +``` php +$invoiceObj = Invoice::retrieve(); +``` +### Create +``` php +$invoiceData = [ + 'business_name' => 'Crypto Account LLC', + 'customer_email' => 'customer@test.com', + 'customer_name' => 'Test Customer', + 'local_price' => [ + 'amount' => '100.00', + 'currency' => 'USD' + ], + 'memo' => 'Taxes and Accounting Services' +]; +Invoice::create($invoiceData); + +// or +$invoiceObj = new Invoice(); + +$invoiceObj->business_name = 'Crypto Account LLC'; +$invoiceObj->customer_email = 'customer@test.com'; +$invoiceObj->customer_name = 'Test Customer'; +$invoiceObj->local_price = [ + 'amount' => '100.00', + 'currency' => 'USD' +]; +$invoiceObj->memo = 'Taxes and Accounting Services'; +$invoiceObj->save(); +``` +### List +``` php +$list = Invoice::getList(); + +foreach($list as $invoice) { + var_dump($list); +} + +$pagination = $list->getPagination(); +``` +### Get all invoices +``` php +$allInvoices = Invoice::getAll(); +``` + +### Resolve an invoice +Resolve an invoice that has been previously marked as unresolved. +``` +$invoiceObj = Invoice::retrieve(); + +if ($invoiceObj) { + $invoiceObj->resolve(); +} +``` + +### Void an invoice +Voids an invoice that has been previously created. +Note: Only new or viewed invoices can be successfully voided. Once payment is detected, invoice can no longer be canceled. + +``` +$invoiceObj = Invoice::retrieve(); + +if ($invoiceObj) { + $invoiceObj->void(); +} +``` + ## Events [Events API Docs](https://commerce.coinbase.com/docs/api/#events) More examples on how to use events can be found in the [`examples/Resources/EventExample.php`](examples/Resources/EventExample.php) file From a66d32c81a2ecf9fd70b6c45ae731c357fba044b Mon Sep 17 00:00:00 2001 From: HawtDogFlvrWtr Date: Thu, 4 Nov 2021 11:01:33 -0400 Subject: [PATCH 3/4] adding InvoiceExample.php as written in the README.md --- examples/Resources/InvoiceExample.php | 87 +++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 examples/Resources/InvoiceExample.php diff --git a/examples/Resources/InvoiceExample.php b/examples/Resources/InvoiceExample.php new file mode 100644 index 0000000..8bc9a82 --- /dev/null +++ b/examples/Resources/InvoiceExample.php @@ -0,0 +1,87 @@ + 'Crypto Account LLC', + 'customer_email' => 'customer@test.com', + 'customer_name' => 'Test Customer', + 'local_price' => [ + 'amount' => '100.00', + 'currency' => 'USD' + ], + 'memo' => 'Taxes and Accounting Services' + ]; +); + +try { + $invoiceObj->save(); + echo sprintf("Successfully created new charge with id: %s \n", $invoiceObj->id); +} catch (\Exception $exception) { + echo sprintf("Enable to create charge. Error: %s \n", $exception->getMessage()); +} + +if ($invoiceObj->id) { + // Retrieve charge by "id" + try { + $retrievedInvoice = Invoice::retrieve($invoiceObj->id); + echo sprintf("Successfully retrieved charge\n"); + echo $retrievedInvoice; + } catch (\Exception $exception) { + echo sprintf("Enable to retrieve charge. Error: %s \n", $exception->getMessage()); + } +} + +try { + $list = Invoice::getList(["limit" => 5]); + echo sprintf("Successfully got list of charges\n"); + + if (count($list)) { + echo sprintf("Invoices in list:\n"); + + foreach ($list as $charge) { + echo $charge; + } + } + + echo sprintf("List's pagination:\n"); + print_r($list->getPagination()); + + echo sprintf("Number of all charges - %s \n", $list->countAll()); +} catch (\Exception $exception) { + echo sprintf("Enable to get list of charges. Error: %s \n", $exception->getMessage()); +} + +if (isset($list) && $list->hasNext()) { + // Load next page with previous settings (limit=5) + try { + $list->loadNext(); + echo sprintf("Next page of charges: \n"); + foreach ($list as $charge) { + echo $charge; + } + } catch (\Exception $exception) { + echo sprintf("Enable to get new page of charges. Error: %s \n", $exception->getMessage()); + } +} + +// Load all avaialbe charges +try { + $allInvoice = Invoice::getAll(); + echo sprintf("Successfully got all charges:\n"); + foreach ($allInvoice as $charge) { + echo $charge; + } +} catch (\Exception $exception) { + echo sprintf("Enable to get all charges. Error: %s \n", $exception->getMessage()); +} From 6b7e2754e72e67e3c8d231de88d2013697c167cc Mon Sep 17 00:00:00 2001 From: HawtDogFlvrWtr Date: Thu, 4 Nov 2021 11:05:33 -0400 Subject: [PATCH 4/4] fixed 'available' typo, and changed a few from charge to invoice, missed when ripping the charge example --- examples/Resources/InvoiceExample.php | 38 +++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/Resources/InvoiceExample.php b/examples/Resources/InvoiceExample.php index 8bc9a82..8a37480 100644 --- a/examples/Resources/InvoiceExample.php +++ b/examples/Resources/InvoiceExample.php @@ -26,62 +26,62 @@ try { $invoiceObj->save(); - echo sprintf("Successfully created new charge with id: %s \n", $invoiceObj->id); + echo sprintf("Successfully created new invoice with id: %s \n", $invoiceObj->id); } catch (\Exception $exception) { - echo sprintf("Enable to create charge. Error: %s \n", $exception->getMessage()); + echo sprintf("Enable to create invoice. Error: %s \n", $exception->getMessage()); } if ($invoiceObj->id) { - // Retrieve charge by "id" + // Retrieve invoice by "id" try { $retrievedInvoice = Invoice::retrieve($invoiceObj->id); - echo sprintf("Successfully retrieved charge\n"); + echo sprintf("Successfully retrieved invoice\n"); echo $retrievedInvoice; } catch (\Exception $exception) { - echo sprintf("Enable to retrieve charge. Error: %s \n", $exception->getMessage()); + echo sprintf("Enable to retrieve invoice. Error: %s \n", $exception->getMessage()); } } try { $list = Invoice::getList(["limit" => 5]); - echo sprintf("Successfully got list of charges\n"); + echo sprintf("Successfully got list of invoices\n"); if (count($list)) { echo sprintf("Invoices in list:\n"); - foreach ($list as $charge) { - echo $charge; + foreach ($list as $invoice) { + echo $invoice; } } echo sprintf("List's pagination:\n"); print_r($list->getPagination()); - echo sprintf("Number of all charges - %s \n", $list->countAll()); + echo sprintf("Number of all invoices - %s \n", $list->countAll()); } catch (\Exception $exception) { - echo sprintf("Enable to get list of charges. Error: %s \n", $exception->getMessage()); + echo sprintf("Enable to get list of invoices. Error: %s \n", $exception->getMessage()); } if (isset($list) && $list->hasNext()) { // Load next page with previous settings (limit=5) try { $list->loadNext(); - echo sprintf("Next page of charges: \n"); - foreach ($list as $charge) { - echo $charge; + echo sprintf("Next page of invoices: \n"); + foreach ($list as $invoice) { + echo $invoice; } } catch (\Exception $exception) { - echo sprintf("Enable to get new page of charges. Error: %s \n", $exception->getMessage()); + echo sprintf("Enable to get new page of invoices. Error: %s \n", $exception->getMessage()); } } -// Load all avaialbe charges +// Load all available invoices try { $allInvoice = Invoice::getAll(); - echo sprintf("Successfully got all charges:\n"); - foreach ($allInvoice as $charge) { - echo $charge; + echo sprintf("Successfully got all invoices:\n"); + foreach ($allInvoice as $invoice) { + echo $invoice; } } catch (\Exception $exception) { - echo sprintf("Enable to get all charges. Error: %s \n", $exception->getMessage()); + echo sprintf("Enable to get all invoices. Error: %s \n", $exception->getMessage()); }