forked from coinbase/coinbase-commerce-php
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ChargeExample.php
97 lines (83 loc) · 2.78 KB
/
ChargeExample.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
require_once __DIR__ . "/vendor/autoload.php";
use CoinbaseCommerce\ApiClient;
use CoinbaseCommerce\Resources\Charge;
/**
* Init ApiClient with your Api Key
* Your Api Keys are available in the Coinbase Commerce Dashboard.
* Make sure you don't store your API Key in your source code!
*/
ApiClient::init("API_KEY");
$chargeObj = new Charge(
[
"description" => "Mastering the Transition to the Information Age",
"metadata" => [
"customer_id" => "id_1005",
"customer_name" => "Satoshi Nakamoto",
],
"name" => "Test Name",
"payments" => [],
"pricing_type" => "no_price",
]
);
try {
$chargeObj->save();
echo \sprintf("Successfully created new charge with id: %s \n", $chargeObj->id);
} catch (\Exception $exception) {
echo \sprintf("Enable to create charge. Error: %s \n", $exception->getMessage());
}
if ($chargeObj->id) {
$chargeObj->description = "New description";
// Refresh attributes to previous values
try {
$chargeObj->refresh();
echo \sprintf("Successfully refreshed checkout.\n");
} catch (\Exception $exception) {
echo \sprintf("Enable to refresh checkout. Error: %s \n", $exception->getMessage());
}
// Retrieve charge by "id"
try {
$retrievedCharge = Charge::retrieve($chargeObj->id);
echo \sprintf("Successfully retrieved charge\n");
echo $retrievedCharge;
} catch (\Exception $exception) {
echo \sprintf("Enable to retrieve charge. Error: %s \n", $exception->getMessage());
}
}
try {
$list = Charge::getList(["limit" => 5]);
echo \sprintf("Successfully got list of charges\n");
if (\count($list)) {
echo \sprintf("Charges in list:\n");
foreach ($list as $charge) {
echo $charge;
}
}
echo \sprintf("List's pagination:\n");
\print_r($list->getPagination());
echo \sprintf("Number of all charges - %s \n", $list->countAll());
} catch (\Exception $exception) {
echo \sprintf("Enable to get list of charges. Error: %s \n", $exception->getMessage());
}
if (isset($list) && $list->hasNext()) {
// Load next page with previous settings (limit=5)
try {
$list->loadNext();
echo \sprintf("Next page of charges: \n");
foreach ($list as $charge) {
echo $charge;
}
} catch (\Exception $exception) {
echo \sprintf("Enable to get new page of charges. Error: %s \n", $exception->getMessage());
}
}
// Load all avaialbe charges
try {
$allCharge = Charge::getAll();
echo \sprintf("Successfully got all charges:\n");
foreach ($allCharge as $charge) {
echo $charge;
}
} catch (\Exception $exception) {
echo \sprintf("Enable to get all charges. Error: %s \n", $exception->getMessage());
}