|
1
|
|
|
<?php namespace Yani\Coinbase; |
|
2
|
|
|
|
|
3
|
|
|
use Yani\Coinbase\Exceptions\CoinbaseCheckoutException; |
|
4
|
|
|
use GuzzleHttp\Client as Guzzle; |
|
5
|
|
|
|
|
6
|
|
|
class CoinbaseClient { |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The coinbase API KEY |
|
10
|
|
|
* |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $apiKey; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The coinbase API SECRET |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $apiSecret; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The Guzzle HTTP client |
|
24
|
|
|
* |
|
25
|
|
|
* @var \GuzzleHttp\Client |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $client = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The Coinbase endpoint |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $endpoint = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Instantiate a new client |
|
38
|
|
|
* |
|
39
|
|
|
* @param \GuzzleHttp\Client $client |
|
40
|
|
|
* @param string $apiKey |
|
41
|
|
|
* @param string $apiSecret |
|
42
|
|
|
* @param string $endpoint |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Guzzle $client, $apiKey, $apiSecret, $endpoint) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->apiKey = $apiKey; |
|
47
|
|
|
$this->apiSecret = $apiSecret; |
|
48
|
|
|
$this->client = $client; |
|
49
|
|
|
$this->endpoint = $endpoint; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create order with Coinbase API |
|
54
|
|
|
* |
|
55
|
|
|
* @param float $amount |
|
56
|
|
|
* @param string $currency |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* @param string $description |
|
|
|
|
|
|
59
|
|
|
* @param array $metadata |
|
|
|
|
|
|
60
|
|
|
* |
|
61
|
|
|
* @return stdClass |
|
62
|
|
|
*/ |
|
63
|
|
|
public function createCheckout($amount, $currency, $name, $data = []) |
|
64
|
|
|
{ |
|
65
|
|
|
$payload = [ |
|
66
|
|
|
'amount' => $amount, |
|
67
|
|
|
'currency' => $currency, |
|
68
|
|
|
'name' => $name, |
|
69
|
|
|
]; |
|
70
|
|
|
foreach ($data as $key => $value) |
|
71
|
|
|
{ |
|
72
|
|
|
$payload[$key] = $value; |
|
73
|
|
|
} |
|
74
|
|
|
$payload = json_encode($payload); |
|
75
|
|
|
$path = '/v2/checkouts'; |
|
76
|
|
|
$headers = $this->getHeaders(time(), 'POST', $path, $payload); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
try |
|
79
|
|
|
{ |
|
80
|
|
|
$response = $this->client->post($this->endpoint . $path, [ |
|
81
|
|
|
'body' => $payload, |
|
82
|
|
|
'headers' => $headers |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
catch (\Exception $e) |
|
86
|
|
|
{ |
|
87
|
|
|
echo $e->getResponse(); |
|
|
|
|
|
|
88
|
|
|
exit(0); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
if ((int) $response->getStatusCode() === 201) |
|
91
|
|
|
{ |
|
92
|
|
|
return json_decode($response->getBody())->data; |
|
93
|
|
|
} |
|
94
|
|
|
else |
|
95
|
|
|
{ |
|
96
|
|
|
throw new CoinbaseCheckoutException($response->getBody()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return headers with coinbase signature |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $timestamp |
|
105
|
|
|
* @param string $method |
|
106
|
|
|
* @param string $requestPath |
|
107
|
|
|
* @param array $body |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getHeaders($timestamp, $method, $requestPath, $body) |
|
112
|
|
|
{ |
|
113
|
|
|
$accessSign = hash_hmac('sha256', ($timestamp . $method . $requestPath . $body), $this->apiSecret); |
|
114
|
|
|
|
|
115
|
|
|
return [ |
|
116
|
|
|
'CB-ACCESS-KEY' => $this->apiKey, |
|
117
|
|
|
'CB-ACCESS-SIGN' => $accessSign, |
|
118
|
|
|
'CB-ACCESS-TIMESTAMP' => $timestamp, |
|
119
|
|
|
'CB-VERSION' => '2015-04-08', |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.