Skip to content

Commit

Permalink
Add fetchAll(), update README
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Jan 14, 2018
1 parent 3ec2a0d commit 145258d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ $ composer require elementsproject/lightning-charge-client-php

```php
<?php
require_once 'vendor/autoload.php';

// Initialize client
$charge = new LightingChargeClient('http://localhost:8009', '[TOKEN]');
// alternatively, the token can be provided as part of the URL:
$charge = new LightingChargeClient('http://api-token:[TOKEN]@localhost:8009');

// Create invoice
$invoice = $charge->invoice([ 'msatoshi' => 50, 'metadata' => [ 'customer' => 'Satoshi', 'products' => [ 'potato', 'chips' ] ] ]);

tell_user("to pay, send $invoice->msatoshi milli-satoshis with rhash $invoice->rhash, or copy the BOLT11 payment request: $invoice->payreq");

// Create invoice denominated in USD
$invoice = $charge->invoice([ 'currency' => 'USD', 'amount' => 0.15 ]);

// Fetch invoice by id
$invoice = $charge->fetch('m51vlVWuIKGumTLbJ1RPb');

// Create invoice denominated in USD
$invoice = $charge->invoice([ 'currency' => 'USD', 'amount' => 0.15 ]);
// Fetch all invoices
$invoice = $charge->fetchAll();

// Register web hook
$charge->registerHook('m51vlVWuIKGumTLbJ1RPb', 'http://my-server.com/my-callback-url');
```

TODO: document missing methods
*TODO*: document `wait`

## Test

Expand Down
16 changes: 14 additions & 2 deletions client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct($url, $api_token=null) {
public function invoice($props) {
$res = $this->api->post('/invoice', json_encode($props), [ 'Content-Type' => 'application/json' ]);

if ($res->info->http_code !== 201) throw new Exception('saving invoice failed');
if ($res->info->http_code !== 201) throw new Exception('failed saving invoice');

return $res->decode_response();
}
Expand All @@ -34,10 +34,22 @@ public function invoice($props) {
*/
public function fetch($invoice_id) {
$res = $this->api->get('/invoice/' . urlencode($invoice_id));
if ($res->info->http_code !== 200) throw new Exception('fetching invoice failed');
if ($res->info->http_code !== 200) throw new Exception('failed fetching invoice');
return $res->decode_response();
}

/**
* Fetch all invoices
*
* @return array
*/
public function fetchAll() {
$res = $this->api->get('/invoices');
if ($res->info->http_code !== 200) throw new Exception('failed fetching invoices');
return $res->decode_response();
}


/**
* Wait for an invoice to be paid.
*
Expand Down

0 comments on commit 145258d

Please sign in to comment.