-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.rb
165 lines (135 loc) · 3.93 KB
/
server.rb
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
require 'sinatra'
require 'stripe'
# This is your test secret API key.
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
set :static, true
set :port, 4242
before do
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
response.headers['Content-Security-Policy'] = "sandbox allow-same-origin"
end
options "*" do
response.headers["Allow"] = "GET, POST, OPTIONS"
200
end
# Securely calculate the order amount
def calculate_order_amount(_items)
# Calculate the order total on the server to prevent
# people from directly manipulating the amount on the client
_items.sum {|h| h['amount']}
end
# An endpoint to start the payment process
post '/create-setup-intent' do
content_type 'application/json'
data = JSON.parse(request.body.read)
setup_intent = Stripe::SetupIntent.create(
customer: data['customer_id'],
payment_method_types: ['card'],
usage: 'off_session',
payment_method_options: {
card: {
request_three_d_secure: 'any' # 義務化のシミュレーションのための設定です。義務化以降はStipeが自動的に判定されます。
}
}
)
{
clientSecret: setup_intent.client_secret,
}.to_json
end
# An endpoint to start the payment process
post '/create-payment-intent' do
content_type 'application/json'
data = JSON.parse(request.body.read)
# Create a PaymentIntent with amount and currency
payment_intent = Stripe::PaymentIntent.create(
currency: 'jpy',
)
{
clientSecret: payment_intent.client_secret,
}.to_json
end
post '/create-customer' do
content_type 'application/json'
data = JSON.parse(request.body.read)
# Create a PaymentIntent with amount and currency
customer = Stripe::Customer.create(
email: data['email'],
)
{
id: customer.id,
email: customer.email
}.to_json
end
# 保存済みのカード情報を取得するエンドポイント
get '/get-saved-cards' do
content_type 'application/json'
customer_id = params[:customer_id]
begin
payment_methods = Stripe::PaymentMethod.list({
customer: customer_id,
type: 'card'
})
cards = payment_methods.data.map do |pm|
{
id: pm.id,
brand: pm.card.brand,
last4: pm.card.last4,
exp_month: pm.card.exp_month,
exp_year: pm.card.exp_year
}
end
customer = Stripe::Customer.retrieve(customer_id)
{ cards: cards, customer: customer }.to_json
rescue Stripe::StripeError => e
status 400
{ error: e.message }.to_json
end
end
post '/create-intent-off-session' do
content_type 'application/json'
data = JSON.parse(request.body.read)
intent = Stripe::PaymentIntent.create({
amount: 1099,
currency: 'jpy',
customer: data['customer_id'],
payment_method: data['payment_method_id'],
off_session: true,
confirm: true
})
content_type :json
{
intent: intent
}.to_json
end
post '/create-intent-and-customer-session' do
content_type 'application/json'
data = JSON.parse(request.body.read)
intent = Stripe::PaymentIntent.create({
amount: 1099,
currency: 'jpy',
# In the latest version of the API, specifying the `automatic_payment_methods` parameter
# is optional because Stripe enables its functionality by default.
automatic_payment_methods: { enabled: true },
customer: data['customer_id'],
metadata: data['metadata'],
})
customer_session = Stripe::CustomerSession.create({
customer: data['customer_id'],
components: {
payment_element: {
enabled: true,
features: {
payment_method_redisplay: 'enabled',
payment_method_allow_redisplay_filters: ['always', 'limited', 'unspecified'],
},
},
},
})
content_type :json
{
clientSecret: intent.client_secret,
customerSessionClientSecret: customer_session.client_secret
}.to_json
end