-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
79 lines (65 loc) · 2.96 KB
/
example.php
File metadata and controls
79 lines (65 loc) · 2.96 KB
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
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Get settings from environment variables
$api_host = getenv('PAYJP_API_HOST') ?: 'https://api.pay.jp';
$api_key = getenv('PAYJP_API_KEY') ?: '';
if (empty($api_key)) {
fwrite(STDERR, "Error: Please set the PAYJP_API_KEY environment variable\n");
exit(1);
}
$config = PAYJPV2\Configuration::getDefaultConfiguration()
->setAccessToken($api_key)
->setHost($api_host);
$customersApi = new PAYJPV2\Api\CustomersApi(
new GuzzleHttp\Client(),
$config
);
try {
// 1. Create Customer
echo "=== 1. Create Customer ===\n";
$createRequest = new \PAYJPV2\Model\CustomerCreateRequest();
$createRequest->setEmail('test@example.com');
$createRequest->setDescription('Test customer from PHP SDK');
$createRequest->setMetadata(['key1' => 'value1', 'key2' => 123, 'key3' => true]);
$idempotencyKey = str_replace('.', '_', uniqid('', true));
echo "Using Idempotency-Key: " . $idempotencyKey . "\n";
$customer = $customersApi->createCustomer($createRequest, idempotencyKey: $idempotencyKey);
$customerId = $customer->getId();
echo "Created customer: " . $customerId . "\n";
echo "Email: " . $customer->getEmail() . "\n";
echo "Metadata: " . json_encode($customer->getMetadata()) . "\n\n";
// 2. Get Customer
echo "=== 2. Get Customer ===\n";
$retrieved = $customersApi->getCustomer($customerId);
echo "Retrieved customer: " . $retrieved->getId() . "\n";
echo "Email: " . $retrieved->getEmail() . "\n";
echo "Description: " . ($retrieved->getDescription() ?? '(none)') . "\n";
echo "Metadata: " . json_encode($retrieved->getMetadata()) . "\n\n";
// 3. Update Customer
echo "=== 3. Update Customer ===\n";
$updateRequest = new \PAYJPV2\Model\CustomerUpdateRequest();
$updateRequest->setDescription('Updated description from PHP SDK');
$updateRequest->setEmail('updated@example.com');
$updateRequest->setMetadata(['key1' => 'updated_value', 'key4' => 456]);
$updated = $customersApi->updateCustomer($customerId, $updateRequest);
echo "Updated customer: " . $updated->getId() . "\n";
echo "New email: " . $updated->getEmail() . "\n";
echo "New description: " . ($updated->getDescription() ?? '(none)') . "\n";
echo "Metadata: " . json_encode($updated->getMetadata()) . "\n\n";
// 4. List Customers
echo "=== 4. List Customers ===\n";
$customerList = $customersApi->getAllCustomers(limit: 3);
echo "Total customers retrieved: " . count($customerList->getData()) . "\n";
foreach ($customerList->getData() as $c) {
echo " - " . $c->getId() . " (" . ($c->getEmail() ?? 'no email') . ")\n";
}
echo "\n";
// 5. Delete Customer
echo "=== 5. Delete Customer ===\n";
$customersApi->deleteCustomer($customerId);
echo "Deleted customer: " . $customerId . "\n\n";
echo "=== All tests passed! ===\n";
} catch (Exception $e) {
echo 'Exception: ' . $e->getMessage() . "\n";
exit(1);
}