Skip to content

Commit 2309676

Browse files
committed
Initial commit
1 parent 5945622 commit 2309676

File tree

6 files changed

+319
-1
lines changed

6 files changed

+319
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# laravel-coinbase
1+
# Laravel API wrapper for the Coinbase Commerce
2+
3+
## Backers
4+
5+
- [@antimech](https://github.com/antimech)

composer.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "shakurov/coinbase",
3+
"description": "Laravel API wrapper for the Coinbase Commerce API",
4+
"keywords": [
5+
"laravel",
6+
"laravel 5",
7+
"coinbase",
8+
"coinbase commerce"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Vladimir Shakurov",
14+
"email": "vladimir@shakurov.com"
15+
}
16+
],
17+
"require": {
18+
"guzzlehttp/guzzle": "~6.0"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~6.0"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Shakurov\\Coinbase\\": "src/"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Shakurov\\Coinbase\\Tests\\": "tests/"
31+
}
32+
},
33+
"extra": {
34+
"laravel": {
35+
"providers": [
36+
"Shakurov\\Coinbase\\CoinbaseServiceProvider"
37+
],
38+
"aliases": {
39+
"Coinbase": "Shakurov\\Coinbase\\Facades\\Coinbase"
40+
}
41+
}
42+
}
43+
}

config/coinbase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
return [
4+
'apiKey' => env('COINBASE_API_KEY'),
5+
'apiVersion' => env('COINBASE_API_VERSION'),
6+
];

src/Coinbase.php

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
<?php
2+
3+
namespace Shakurov\Coinbase;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
8+
class Coinbase
9+
{
10+
/**
11+
* @const string
12+
*/
13+
const BASE_URI = 'https://api.commerce.coinbase.com';
14+
15+
/**
16+
* @var Client
17+
*/
18+
private $client;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $apiKey;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $apiVersion;
29+
30+
public function __construct()
31+
{
32+
$this->apiKey = config('coinbase.apiKey');
33+
$this->apiVersion = config('coinbase.apiVersion');
34+
35+
$this->client = new Client([
36+
'base_uri' => self::BASE_URI,
37+
'headers' => [
38+
'Content-Type' => 'application/json',
39+
'X-CC-Api-Key' => $this->apiKey,
40+
'X-CC-Version' => $this->apiVersion,
41+
],
42+
]);
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getApiKey()
49+
{
50+
return $this->apiKey;
51+
}
52+
53+
/**
54+
* @param string $apiKey
55+
* @return Coinbase
56+
*/
57+
public function setApiKey($apiKey)
58+
{
59+
$this->apiKey = $apiKey;
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getApiVersion()
68+
{
69+
return $this->apiVersion;
70+
}
71+
72+
/**
73+
* @param string $apiVersion
74+
* @return Coinbase
75+
*/
76+
public function setApiVersion($apiVersion)
77+
{
78+
$this->apiVersion = $apiVersion;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* Make request.
85+
*
86+
* @param string $method
87+
* @param string $uri
88+
* @param null|array $params
89+
* @return array
90+
*/
91+
public function makeRequest(string $method, string $uri, array $params = [])
92+
{
93+
try {
94+
$response = $this->client->request($method, $uri, ['body' => json_encode($params)]);
95+
96+
return json_decode((string) $response->getBody(), true);
97+
} catch(GuzzleException $e) {
98+
dd($e->getMessage());
99+
}
100+
}
101+
102+
/**
103+
* Lists all charges.
104+
*
105+
* @return array
106+
*/
107+
public function getCharges()
108+
{
109+
return $this->makeRequest('get', 'charges');
110+
}
111+
112+
/**
113+
* Creates a new charge.
114+
*
115+
* @param array $params
116+
* @return array
117+
*/
118+
public function createCharge(array $params = [])
119+
{
120+
return $this->makeRequest('post', 'charges', $params);
121+
}
122+
123+
/**
124+
* Retrieves an existing charge.
125+
*
126+
* @param string $chargeId
127+
* @return array
128+
*/
129+
public function getCharge($chargeId)
130+
{
131+
return $this->makeRequest('get', "charges/{$chargeId}");
132+
}
133+
134+
/**
135+
* Lists all checkouts.
136+
*
137+
* @return array
138+
*/
139+
public function getCheckouts()
140+
{
141+
return $this->makeRequest('get', 'checkouts');
142+
}
143+
144+
/**
145+
* Creates a new checkout.
146+
*
147+
* @param array $params
148+
* @return array
149+
*/
150+
public function createCheckout(array $params = [])
151+
{
152+
return $this->makeRequest('post', 'checkouts', $params);
153+
}
154+
155+
/**
156+
* Retrieves an existing checkout.
157+
*
158+
* @param string $checkoutId
159+
* @return array
160+
*/
161+
public function getCheckout($checkoutId)
162+
{
163+
return $this->makeRequest('get', "checkouts/{$checkoutId}");
164+
}
165+
166+
/**
167+
* Updates an existing checkout.
168+
*
169+
* @param string $checkoutId
170+
* @param array $params
171+
* @return array
172+
*/
173+
public function updateCheckout($checkoutId, array $params = [])
174+
{
175+
return $this->makeRequest('put', "checkouts/{$checkoutId}", $params);
176+
}
177+
178+
/**
179+
* Deletes an existing checkout.
180+
*
181+
* @param string $checkoutId
182+
* @return array
183+
*/
184+
public function deleteCheckout($checkoutId)
185+
{
186+
return $this->makeRequest('delete', "checkouts/{$checkoutId}");
187+
}
188+
189+
/**
190+
* Lists all events.
191+
*
192+
* @return array
193+
*/
194+
public function getEvents()
195+
{
196+
return $this->makeRequest('get', 'events');
197+
}
198+
199+
/**
200+
* Retrieves an existing event.
201+
*
202+
* @param string $eventId
203+
* @return array
204+
*/
205+
public function getEvent($eventId)
206+
{
207+
return $this->makeRequest('get', "events/{$eventId}");
208+
}
209+
}

src/CoinbaseServiceProvider.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Shakurov\Coinbase;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class CoinbaseServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->publishes([
17+
__DIR__.'/../config/coinbase.php' => config_path('coinbase.php'),
18+
]);
19+
}
20+
21+
/**
22+
* Register the application services.
23+
*
24+
* @return void
25+
*/
26+
public function register()
27+
{
28+
$this->mergeConfigFrom(
29+
__DIR__.'/../config/coinbase.php', 'coinbase'
30+
);
31+
32+
$this->app->bind('coinbase', function ($app) {
33+
return new Coinbase($app);
34+
});
35+
36+
$this->app->alias('coinbase', Coinbase::class);
37+
}
38+
}

src/Facades/Coinbase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Shakurov\Coinbase\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Coinbase extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'coinbase';
17+
}
18+
}

0 commit comments

Comments
 (0)