Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(<invoice_id>);
```
### 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(<charge_id>);

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(<invoice_id>);

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
Expand Down
87 changes: 87 additions & 0 deletions examples/Resources/InvoiceExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
require_once __DIR__ . "/vendor/autoload.php";

use CoinbaseCommerce\ApiClient;
use CoinbaseCommerce\Resources\Invoice;

/**
* Init ApiClient with your Api Key
* Your Api Keys are available in the Coinbase Commerce Dashboard.
* Make sure you don't store your API Key in your source code!
*/
ApiClient::init("API_KEY");

$invoiceObj = new Invoice(
[
'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'
];
);

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());
}
38 changes: 38 additions & 0 deletions src/Resources/Invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace CoinbaseCommerce\Resources;

use CoinbaseCommerce\Resources\Operations\CreateMethodTrait;
use CoinbaseCommerce\Resources\Operations\ReadMethodTrait;
use CoinbaseCommerce\Resources\Operations\SaveMethodTrait;
use CoinbaseCommerce\Util;

class Invoice extends ApiResource implements ResourcePathInterface
{
use ReadMethodTrait, CreateMethodTrait, SaveMethodTrait;

/**
* @return string
*/
public static function getResourcePath()
{
return 'invoices';
}

public function void($headers = [])
{
$id = $this->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);
}
}