Skip to content

Commit b110464

Browse files
author
George Robinson
committed
Support V2 of Receive API
1 parent 3d15ad1 commit b110464

File tree

7 files changed

+322
-9
lines changed

7 files changed

+322
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Documentation
9292

9393
[Receive](docs/receive.md) - The easiest way to accept Bitcoin payments
9494

95+
[Receive v2](docs/v2/receive.md) - The easiest way to accept Bitcoin payments with the v2 Receive API
96+
9597
[Statistics](docs/stats.md) - Bitcoin network statistics
9698

9799
[Wallet](docs/wallet.md) - Send and receive Bitcoin programmatically

docs/v2/receive.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Receive Documentation
2+
=====================
3+
4+
The simplest way to receive Bitcoin payments. Blockchain forwards all incoming Bitcoin to the address you specify.
5+
6+
Be sure to check out the [official documentation](https://blockchain.info/api/api_receive) for information on callback URLs.
7+
8+
Usage
9+
-----
10+
11+
Call `ReceiveV2->generate` on a `Blockchain` object. Pass a v2 API key, xpub and callback URL. Returns a `ReceiveResponse` object.
12+
13+
```php
14+
$blockchain = new \Blockchain\Blockchain($apiKey);
15+
16+
$v2ApiKey = 'myApiKey';
17+
$xpub = 'xpubYourXPub';
18+
$callbackUrl = 'http://example.com/transaction?secret=mySecret';
19+
20+
$response = $blockchain->ReceiveV2->generate($v2ApiKey, $xPub, $callbackUrl);
21+
22+
// Show receive address to user:
23+
echo "Send coins to " . $response->getReceiveAddress();
24+
```
25+
26+
To view the callback logs call `ReceiveV2->callbackLogs` on a `Blockchain` object. Pass an API key and callback URL. Returns an array of `CallbackLogEntry` objects.
27+
28+
```php
29+
$blockchain = new \Blockchain\Blockchain($apiKey);
30+
31+
$v2ApiKey = 'myApiKey';
32+
$callbackUrl = 'http://example.com/transaction?secret=mySecret';
33+
34+
$logs = $blockchain->ReceiveV2->callback($apiKey, $callbackUrl);
35+
36+
foreach ($logs as $log) {
37+
$log->getCallback();
38+
$log->getCalledAt(); // DateTime instance
39+
$log->getResponseCode();
40+
$log->getResponse();
41+
}
42+
```

src/Blockchain.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
use \Blockchain\Stats\Stats;
3030
use \Blockchain\Wallet\Wallet;
3131

32+
use \Blockchain\V2\Receive\Receive as ReceiveV2;
33+
3234
// Check if BCMath module installed
3335
if(!function_exists('bcscale')) {
3436
throw new Error("BC Math module not installed.");
@@ -62,13 +64,14 @@ public function __construct($api_code=null) {
6264
curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
6365
curl_setopt($this->ch, CURLOPT_CAINFO, dirname(__FILE__).'/Blockchain/ca-bundle.crt');
6466

65-
$this->Create = new Create($this);
66-
$this->Explorer = new Explorer($this);
67-
$this->Push = new Push($this);
68-
$this->Rates = new Rates($this);
69-
$this->Receive = new Receive($this);
70-
$this->Stats = new Stats($this);
71-
$this->Wallet = new Wallet($this);
67+
$this->Create = new Create($this);
68+
$this->Explorer = new Explorer($this);
69+
$this->Push = new Push($this);
70+
$this->Rates = new Rates($this);
71+
$this->Receive = new Receive($this);
72+
$this->ReceiveV2 = new ReceiveV2($this->ch);
73+
$this->Stats = new Stats($this);
74+
$this->Wallet = new Wallet($this);
7275
}
7376

7477
public function __destruct() {
@@ -140,4 +143,4 @@ private function _call() {
140143

141144
return $json;
142145
}
143-
}
146+
}

src/Receive/ReceiveResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ public function __construct($json) {
5252
if(array_key_exists('callback_url', $json))
5353
$this->callback_url = $json['callback_url'];
5454
}
55-
}
55+
}

src/V2/Receive/CallbackLogEntry.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Blockchain\V2\Receive;
4+
5+
use DateTime;
6+
7+
/**
8+
* The callback log from the receive API.
9+
*
10+
* @author George Robinson <george.robinson@blockchain.com>
11+
*/
12+
class CallbackLogEntry
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $callback;
18+
19+
/**
20+
* @var \DateTime
21+
*/
22+
private $calledAt;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $rawResponse;
28+
29+
/**
30+
* @var int
31+
*/
32+
private $responseCode;
33+
34+
public function __construct($callback, DateTime $calledAt, $rawResponse, $responseCode)
35+
{
36+
$this->callback = $callback;
37+
$this->calledAt = $calledAt;
38+
$this->rawResponse = $rawResponse;
39+
$this->responseCode = $responseCode;
40+
}
41+
42+
/**
43+
* Gets the callback URL.
44+
*
45+
* @return string
46+
*/
47+
public function getCallback()
48+
{
49+
return $this->callback;
50+
}
51+
52+
/**
53+
* Gets the called at timestamp.
54+
*
55+
* @return \DateTime
56+
*/
57+
public function getCalledAt()
58+
{
59+
return $this->calledAt;
60+
}
61+
62+
/**
63+
* Gets the raw HTTP response.
64+
*
65+
* @return string
66+
*/
67+
public function getResponse()
68+
{
69+
return $this->rawResponse;
70+
}
71+
72+
/**
73+
* Gets the response code.
74+
*
75+
* @return int
76+
*/
77+
public function getResponseCode()
78+
{
79+
return $this->responseCode;
80+
}
81+
}

src/V2/Receive/Receive.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Blockchain\V2\Receive;
4+
5+
use Blockchain\Exception\Error;
6+
use Blockchain\Exception\HttpError;
7+
8+
use DateTime;
9+
use DateTimeZone;
10+
11+
/**
12+
* The V2 Receive API client.
13+
*
14+
* @author George Robinson <george.robinson@blockchain.com>
15+
*/
16+
class Receive
17+
{
18+
/**
19+
* @var string
20+
*/
21+
const URL = 'https://api.blockchain.info/v2/receive';
22+
23+
/**
24+
* @var resource
25+
*/
26+
private $ch;
27+
28+
/**
29+
* Instantiates a receive API client.
30+
*
31+
* @param resource $ch The cURL resource.
32+
*/
33+
public function __construct($ch)
34+
{
35+
$this->ch = $ch;
36+
}
37+
38+
/**
39+
* Generates a receive adddress.
40+
*
41+
* @param string $key The API key.
42+
* @param string $xpub The public key.
43+
* @param string $callback The callback URL.
44+
* @return \Blockchain\V2\Receive\ReceiveResponse
45+
* @throws \Blockchain\Exception\Error
46+
* @throws \Blockchain\Exception\HttpError
47+
*/
48+
public function generate($key, $xpub, $callback)
49+
{
50+
$p = compact('key', 'xpub', 'callback');
51+
$q = http_build_query($p);
52+
53+
curl_setopt($this->ch, CURLOPT_POST, false);
54+
curl_setopt($this->ch, CURLOPT_URL, static::URL.'?'.$q);
55+
56+
if (($resp = curl_exec($this->ch)) === false) {
57+
throw new HttpError(curl_error($this->ch));
58+
}
59+
60+
if (($data = json_decode($resp, true)) === NULL) {
61+
throw new Error("Unable to decode JSON response from Blockchain: $resp");
62+
}
63+
64+
$info = curl_getinfo($this->ch);
65+
66+
if ($info['http_code'] == 200) {
67+
return new ReceiveResponse($data['address'], $data['index'], $data['callback']);
68+
}
69+
70+
throw new Error(implode(', ', $data));
71+
}
72+
73+
/**
74+
* Gets the callback logs.
75+
*
76+
* @param string $key The API key.
77+
* @param string $callback The callback URL.
78+
* @return \Blockchain\V2\Receive\CallbackLogEntry[]
79+
* @throws \Blochchain\Exception\Error
80+
* @throws \Blockchain\Exception\HttpError
81+
*/
82+
public function callbackLogs($key, $callback)
83+
{
84+
$p = compact('key', 'callback');
85+
$q = http_build_query($p);
86+
87+
curl_setopt($this->ch, CURLOPT_POST, false);
88+
curl_setopt($this->ch, CURLOPT_URL, static::URL.'/callback_log?'.$q);
89+
90+
if (($resp = curl_exec($this->ch)) === false) {
91+
throw new HttpError(curl_error($this->ch));
92+
}
93+
94+
if (($data = json_decode($resp, true)) === NULL) {
95+
throw new Error("Unable to decode JSON response from Blockchain: $resp");
96+
}
97+
98+
$info = curl_getinfo($this->ch);
99+
100+
if ($info['http_code'] == 200) {
101+
return array_map([$this, 'createCallbackLogEntry'], (array) $data);
102+
}
103+
104+
throw new Error(implode(', ', $data));
105+
}
106+
107+
/**
108+
* Creates a callback log entry.
109+
*
110+
* @param string[mixed] $data
111+
* @return \Blockchain\V2\Receive\CallbackLogEntry
112+
*/
113+
private function createCallbackLogEntry($data)
114+
{
115+
return new CallbackLogEntry($data['callback'],
116+
new DateTime($data['called_at']),
117+
$data['raw_response'],
118+
$data['response_code']);
119+
}
120+
}

src/V2/Receive/ReceiveResponse.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Blockchain\V2\Receive;
4+
5+
/**
6+
* The ReceiveResponse from the Receive API
7+
*
8+
* @author George Robinson <george.robinson@blockchain.com>
9+
*/
10+
class ReceiveResponse {
11+
12+
/**
13+
* @var string
14+
*/
15+
private $address;
16+
17+
/**
18+
* @var int
19+
*/
20+
private $index;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $callback;
26+
27+
/**
28+
*
29+
* @param string $address The receive address.
30+
* @param int $index The index of the receive address.
31+
* @param string $callback The callback URL.
32+
*/
33+
public function __construct($address, $index, $callback) {
34+
$this->address = $address;
35+
$this->index = $index;
36+
$this->callback = $callback;
37+
}
38+
39+
/**
40+
* Gets the receive address.
41+
*
42+
* @return string
43+
*/
44+
public function getReceiveAddress() {
45+
return $this->address;
46+
}
47+
48+
/**
49+
* Gets the index of the receive address.
50+
*
51+
* @return int
52+
*/
53+
public function getIndex() {
54+
return $this->index;
55+
}
56+
57+
/**
58+
* Gets the callback URL.
59+
*
60+
* @return string
61+
*/
62+
public function getCallback() {
63+
return $this->callback;
64+
}
65+
}

0 commit comments

Comments
 (0)