Skip to content

Commit

Permalink
initial commit, PoC and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shesek committed Nov 14, 2017
0 parents commit 170a3eb
Show file tree
Hide file tree
Showing 6 changed files with 2,587 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# lightning-strike-client-js

PHP client for the Lightning Strike REST API.

## Install

```bash
$ composer install blockstream/lightning-strike-client
```

## Use

```php
<?php
// initialize client
$strike = new LightingStrikeClient('http://localhost:8009');

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

echo "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');
```

## Test

```bash
$ STRIKE_URL=http://localhost:8009 phpunit test
```

## License
MIT
35 changes: 35 additions & 0 deletions client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
require_once __DIR__.'/vendor/autoload.php';

class LightningStrikeClient {
protected $api;

public function __construct($url) {
$this->api = new RestClient([ 'base_url' => $url, 'format' => 'json' ]);
}

public function invoice($msatoshi, $metadata=null) {
$res = $this->api->post('/invoice',
json_encode([ 'msatoshi' => $msatoshi, 'metadata' => $metadata ]),
[ 'Content-Type' => 'application/json' ]);

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

return $res->decode_response();
}

public function fetch($invoice_id) {
$res = $this->api->get('/invoice/' . urlencode($invoice_id));
if ($res->info->http_code !== 200)
throw new Exception('unable to fetch invoice');
return $res->decode_response();
}

public function registerHook($invoice_id, $url) {
$res = $this->api->post('/invoice/' . urlencode($invoice_id) . '/webhook', [ 'url' => $url ]);
if ($res->info->http_code !== 201)
throw new Exception('unable to register hook');
return true;
}
}
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "elementsproject/lightning-strike-client-php",
"description": "PHP client for the Lightning Strike REST API",
"license": "MIT",
"repositories": [{
"type": "vcs",
"url": "https://github.com/shesek/php-restclient.git"
}],
"require": {
"tcdent/php-restclient": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "^6.4",
"clue/phar-composer": "^1.0"
},
"autoload": {
"files": ["client.php"]
}
}
Loading

0 comments on commit 170a3eb

Please sign in to comment.