From 2f68f514ed27a988e16b9fd9ed5095c0dcce8b96 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Sun, 19 Nov 2017 17:25:20 +0200 Subject: [PATCH] Pass-through all properties when creating invoices. To support the new `currency` and `amount` fields added in https://github.com/ElementsProject/lightning-strike/commit/4f4b1d5b9b353decf010a599ba923a1a2aa53aad --- README.md | 7 +++++-- client.php | 6 ++---- test.php | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 118b9f6..a1bb8a1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lightning-strike-client-js +# lightning-strike-client-php PHP client for the Lightning Strike REST API. @@ -15,12 +15,15 @@ $ composer require elementsproject/lightning-strike-client-php $strike = new LightingStrikeClient('http://localhost:8009'); // Create invoice -$invoice = $strike->invoice(/*msatoshi*/ 50, /*metadata*/ [ 'customer' => 'Satoshi', 'products' => [ 'potato', 'chips' ]]); +$invoice = $strike->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"); // Fetch invoice by id $invoice = $strike->fetch('m51vlVWuIKGumTLbJ1RPb'); + +// Create invoice denominated in USD +$invoice = $strike->invoice([ 'currency' => 'USD', 'amount' => 0.15 ]); ``` TODO: document missing methods diff --git a/client.php b/client.php index 7b73e2b..8ac2adf 100644 --- a/client.php +++ b/client.php @@ -17,10 +17,8 @@ public function __construct($url) { * @param object $metadata * @return object the invoice */ - public function invoice($msatoshi, $metadata=null) { - $res = $this->api->post('/invoice', - json_encode([ 'msatoshi' => $msatoshi, 'metadata' => $metadata ]), - [ 'Content-Type' => 'application/json' ]); + 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'); diff --git a/test.php b/test.php index 8d120a0..75ce1b2 100644 --- a/test.php +++ b/test.php @@ -5,7 +5,7 @@ class LightningStrikeClientTest extends \PHPUnit\Framework\TestCase { public function test_create_invoice(){ $strike = new LightningStrikeClient(getenv('STRIKE_URL')); - $invoice = $strike->invoice(50, [ 'customer' => 'Satoshi', 'products' => [ 'potato', 'chips' ]]); + $invoice = $strike->invoice([ 'msatoshi' => 50, 'metadata' => [ 'customer' => 'Satoshi', 'products' => [ 'potato', 'chips' ] ] ]); $this->assertObjectHasAttribute('id', $invoice); $this->assertObjectHasAttribute('rhash', $invoice); @@ -17,18 +17,18 @@ public function test_create_invoice(){ public function test_fetch_invoice(){ $strike = new LightningStrikeClient(getenv('STRIKE_URL')); - $saved = $strike->invoice(50, 9999); + $saved = $strike->invoice( [ 'msatoshi' => 50, 'metadata' => 'test_fetch_invoice' ]); $loaded = $strike->fetch($saved->id); $this->assertEquals($saved->id, $loaded->id); $this->assertEquals($saved->rhash, $loaded->rhash); - $this->assertEquals($loaded->metadata, 9999); + $this->assertEquals($loaded->metadata, 'test_fetch_invoice'); $this->assertEquals($loaded->msatoshi, '50'); } public function test_register_webhook(){ $strike = new LightningStrikeClient(getenv('STRIKE_URL')); - $invoice = $strike->invoice(50); + $invoice = $strike->invoice([ 'msatoshi' => 50 ]); $this->assertTrue($strike->registerHook($invoice->id, 'http://example.com/')); } }