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 diff --git a/examples/Resources/InvoiceExample.php b/examples/Resources/InvoiceExample.php new file mode 100644 index 0000000..8a37480 --- /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 invoice with id: %s \n", $invoiceObj->id); +} catch (\Exception $exception) { + echo sprintf("Enable to create invoice. Error: %s \n", $exception->getMessage()); +} + +if ($invoiceObj->id) { + // Retrieve invoice by "id" + try { + $retrievedInvoice = Invoice::retrieve($invoiceObj->id); + echo sprintf("Successfully retrieved invoice\n"); + echo $retrievedInvoice; + } catch (\Exception $exception) { + echo sprintf("Enable to retrieve invoice. Error: %s \n", $exception->getMessage()); + } +} + +try { + $list = Invoice::getList(["limit" => 5]); + echo sprintf("Successfully got list of invoices\n"); + + if (count($list)) { + echo sprintf("Invoices in list:\n"); + + foreach ($list as $invoice) { + echo $invoice; + } + } + + echo sprintf("List's pagination:\n"); + print_r($list->getPagination()); + + echo sprintf("Number of all invoices - %s \n", $list->countAll()); +} catch (\Exception $exception) { + 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 invoices: \n"); + foreach ($list as $invoice) { + echo $invoice; + } + } catch (\Exception $exception) { + echo sprintf("Enable to get new page of invoices. Error: %s \n", $exception->getMessage()); + } +} + +// Load all available invoices +try { + $allInvoice = Invoice::getAll(); + echo sprintf("Successfully got all invoices:\n"); + foreach ($allInvoice as $invoice) { + echo $invoice; + } +} catch (\Exception $exception) { + echo sprintf("Enable to get all invoices. Error: %s \n", $exception->getMessage()); +} 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); + } +}