-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
79 lines (64 loc) · 2.53 KB
/
example.py
File metadata and controls
79 lines (64 loc) · 2.53 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
import os
import sys
import uuid
import payjpv2
# Get settings from environment variables
api_host = os.environ.get("PAYJP_API_HOST", "https://api.pay.jp")
api_key = os.environ.get("PAYJP_API_KEY", "")
if not api_key:
print("Error: Please set the PAYJP_API_KEY environment variable", file=sys.stderr)
sys.exit(1)
# Configure authentication method (Bearer token)
configuration = payjpv2.Configuration(
host=api_host,
access_token=api_key,
)
with payjpv2.ApiClient(configuration) as api_client:
customers_api = payjpv2.CustomersApi(api_client)
try:
# 1. Create Customer
print("=== 1. Create Customer ===")
create_request = payjpv2.CustomerCreateRequest(
email="test@example.com",
description="Test customer from Python SDK",
)
idempotency_key = str(uuid.uuid4())
print(f"Using Idempotency-Key: {idempotency_key}")
customer = customers_api.create_customer(
create_request,
idempotency_key=idempotency_key,
)
customer_id = customer.id
print(f"Created customer: {customer_id}")
print(f"Email: {customer.email}\n")
# 2. Get Customer
print("=== 2. Get Customer ===")
retrieved = customers_api.get_customer(customer_id)
print(f"Retrieved customer: {retrieved.id}")
print(f"Email: {retrieved.email}")
print(f"Description: {retrieved.description or '(none)'}\n")
# 3. Update Customer
print("=== 3. Update Customer ===")
update_request = payjpv2.CustomerUpdateRequest(
description="Updated description from Python SDK",
email="updated@example.com",
)
updated = customers_api.update_customer(customer_id, update_request)
print(f"Updated customer: {updated.id}")
print(f"New email: {updated.email}")
print(f"New description: {updated.description or '(none)'}\n")
# 4. List Customers
print("=== 4. List Customers ===")
customer_list = customers_api.get_all_customers(limit=3)
print(f"Total customers retrieved: {len(customer_list.data)}")
for c in customer_list.data:
print(f" - {c.id} ({c.email or 'no email'})")
print()
# 5. Delete Customer
print("=== 5. Delete Customer ===")
customers_api.delete_customer(customer_id)
print(f"Deleted customer: {customer_id}\n")
print("=== All tests passed! ===")
except payjpv2.ApiException as e:
print(f"Exception: {e}")
sys.exit(1)