-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.rb
More file actions
212 lines (182 loc) · 6.7 KB
/
Copy pathexample.rb
File metadata and controls
212 lines (182 loc) · 6.7 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require 'payjpv2'
require 'pp'
require 'securerandom'
# Example usage
api_host = ENV['PAYJP_API_HOST'] || 'https://api.pay.jp'
api_key = ENV['PAYJP_API_KEY'] || ''
if api_key.empty?
warn "Error: Please set the PAYJP_API_KEY environment variable"
exit 1
end
# Configure authentication method
PAYJPv2.configure do |config|
config.host = api_host
config.access_token = api_key
end
api_instance = PAYJPv2::CustomersApi.new
checkout_sessions_api = PAYJPv2::CheckoutSessionsApi.new
products_api = PAYJPv2::ProductsApi.new
prices_api = PAYJPv2::PricesApi.new
payment_methods_api = PAYJPv2::PaymentMethodsApi.new
customer_create_request = PAYJPv2::CustomerCreateRequest.new(
email: 'jennyrosen@example.com',
metadata: { key1: 'value1', key2: 123, key3: true }
)
# Test idempotency key
idempotency_key = SecureRandom.uuid
puts "Using Idempotency-Key: #{idempotency_key}"
begin
# 1. Create Customer
puts "=== 1. Create Customer ==="
result = api_instance.create_customer(
customer_create_request,
idempotency_key: idempotency_key
)
customer_id = result.id
puts "Created customer: #{customer_id}"
puts "Email: #{result.email}"
puts "Metadata: #{result.metadata}\n\n"
# 2. Get Customer
puts "=== 2. Get Customer ==="
retrieved = api_instance.get_customer(customer_id)
puts "Retrieved customer: #{retrieved.id}"
puts "Email: #{retrieved.email}"
puts "Description: #{retrieved.description || '(none)'}"
puts "Metadata: #{retrieved.metadata}\n\n"
# 3. Update Customer
puts "=== 3. Update Customer ==="
update_request = PAYJPv2::CustomerUpdateRequest.new(
email: 'updated@example.com',
description: 'Updated description from Ruby SDK',
metadata: { key1: 'updated_value', key4: 456 }
)
updated = api_instance.update_customer(customer_id, update_request)
puts "Updated customer: #{updated.id}"
puts "New email: #{updated.email}"
puts "New description: #{updated.description || '(none)'}"
puts "Metadata: #{updated.metadata}\n\n"
# 4. List Customers
puts "=== 4. List Customers ==="
customer_list = api_instance.get_all_customers(limit: 3)
puts "Total customers retrieved: #{customer_list.data.length}"
customer_list.data.each do |c|
puts " - #{c.id} (#{c.email || 'no email'})"
end
puts "\n"
# 5. PaymentMethod operations
# Note: create_payment_method is available only in test mode. In production,
# a PaymentMethod is created by tokenizing card details on the client side
# (e.g. payment.js) and then attaching the resulting token to a Customer.
puts "=== 5. PaymentMethod operations ==="
# 5a. Create PaymentMethod (test mode)
puts "\n--- 5a. Create PaymentMethod (test mode) ---"
payment_method_request = PAYJPv2::PaymentMethodCardCreateRequest.new(
type: 'card',
card: PAYJPv2::PaymentMethodCreateCardDetailsRequest.new(
number: '4242424242424242',
exp_month: 12,
exp_year: 2030,
cvc: '123'
),
billing_details: PAYJPv2::PaymentMethodCardBillingDetailsRequest.new(
email: 'billing@example.com'
)
)
payment_method = payment_methods_api.create_payment_method(payment_method_request)
payment_method_id = payment_method.id
puts "Created PaymentMethod: #{payment_method_id}"
puts "Type: #{payment_method.type}"
# 5b. Attach PaymentMethod to Customer
puts "\n--- 5b. Attach PaymentMethod ---"
attached = payment_methods_api.attach_payment_method(
payment_method_id,
PAYJPv2::PaymentMethodAttachRequest.new(customer_id: customer_id)
)
puts "Attached PaymentMethod: #{attached.id}"
puts "Customer: #{attached.customer_id}"
# 5c. Retrieve PaymentMethod (active)
puts "\n--- 5c. Retrieve PaymentMethod ---"
retrieved_pm = payment_methods_api.get_payment_method(payment_method_id)
puts "PaymentMethod ID: #{retrieved_pm.id}"
puts "Customer: #{retrieved_pm.customer_id}"
puts "Detached at: #{retrieved_pm.detached_at || '(active)'}"
# 5d. Detach PaymentMethod
# The API marks the PaymentMethod with `detached_at` while keeping
# `customer_id` for historical reference.
puts "\n--- 5d. Detach PaymentMethod ---"
detached = payment_methods_api.detach_payment_method(payment_method_id)
puts "Detached PaymentMethod: #{detached.id}"
puts "Detached at: #{detached.detached_at}\n\n"
# 6. Delete Customer
puts "=== 6. Delete Customer ==="
api_instance.delete_customer(customer_id)
puts "Deleted customer: #{customer_id}\n\n"
# 7. Create Product, Price, and Checkout Session
puts "=== 7. Create Product, Price, and Checkout Session ==="
# 7a. Create Product
puts "\n--- 7a. Create Product ---"
product_request = PAYJPv2::ProductCreateRequest.new(
name: 'Sample Product',
description: 'A sample product for checkout session demo',
active: true
)
product_idempotency_key = SecureRandom.uuid
puts "Using Idempotency-Key: #{product_idempotency_key}"
product = products_api.create_product(
product_request,
idempotency_key: product_idempotency_key
)
product_id = product.id
puts "Created product: #{product_id}"
puts "Product name: #{product.name}"
# 7b. Create Price
puts "\n--- 7b. Create Price ---"
price_request = PAYJPv2::PriceCreateRequest.new(
currency: 'jpy',
product_id: product_id,
unit_amount: 1000, # 1000 JPY
nickname: 'Sample Price - 1000 JPY',
active: true
)
price_idempotency_key = SecureRandom.uuid
puts "Using Idempotency-Key: #{price_idempotency_key}"
price = prices_api.create_price(
price_request,
idempotency_key: price_idempotency_key
)
price_id = price.id
puts "Created price: #{price_id}"
puts "Unit amount: #{price.unit_amount} JPY"
# 7c. Create Checkout Session
puts "\n--- 7c. Create Checkout Session ---"
line_items = [
PAYJPv2::LineItemRequest.new(
price_id: price_id, # Use the actual price ID we just created
quantity: 1
)
]
checkout_request = PAYJPv2::CheckoutSessionCreateRequest.new(
mode: 'payment',
line_items: line_items,
success_url: 'https://example.com/success',
cancel_url: 'https://example.com/cancel',
currency: 'jpy',
customer_email: 'test@example.com',
locale: 'ja',
metadata: { order_id: 'order_123' }
)
checkout_idempotency_key = SecureRandom.uuid
puts "Using Idempotency-Key: #{checkout_idempotency_key}"
checkout_session = checkout_sessions_api.create_checkout_session(
checkout_request,
idempotency_key: checkout_idempotency_key
)
puts "Created checkout session: #{checkout_session.id}"
puts "Session URL: #{checkout_session.url}"
puts "Status: #{checkout_session.status}"
puts "Mode: #{checkout_session.mode}"
puts "Currency: #{checkout_session.currency}\n\n"
puts "=== All tests passed! ==="
rescue PAYJPv2::ApiError => e
puts "Exception when calling PAYJPv2 API: #{e}"
end