-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.rb
More file actions
96 lines (88 loc) · 2.88 KB
/
app.rb
File metadata and controls
96 lines (88 loc) · 2.88 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
require './keys/keyinfo'
require './libs/signature'
require 'sinatra'
require 'json'
require 'securerandom'
config = {
region: 'jp',
public_key_id: KeyInfo::PUBLIC_KEY_ID,
amazon_signature_algorithm: KeyInfo::AMAZON_SIGNATURE_ALGORITHM,
private_key: File.read('./keys/privateKey.pem'),
sandbox: true
}
client = AmazonPayClient.new config
get '/' do
redirect '/cart'
end
get '/cart' do
payload = JSON.generate({
webCheckoutDetails: {
checkoutReviewReturnUrl: 'http://localhost:4567/review'
},
storeId: KeyInfo::STORE_ID
})
erb :cart, locals: {
merchant_id: KeyInfo::MERCHANT_ID,
payload: payload,
signature: client.generate_button_signature(payload), # Sign the payload
amazon_signature_algorithm: KeyInfo::AMAZON_SIGNATURE_ALGORITHM,
public_key_id: KeyInfo::PUBLIC_KEY_ID
}
end
get '/review' do
# Get Checkout Session
response = client.api_call("checkoutSessions/#{params['amazonCheckoutSessionId']}", 'GET')
if response.code.to_i == 201 || response.code.to_i == 200
erb :review, locals: JSON.parse(response.body)
else
erb :error, locals: {status: response.code.to_i, body: response.body}
end
end
post '/auth' do
# Update Checkout Session
response = client.api_call("checkoutSessions/#{params['amazonCheckoutSessionId']}", 'PATCH',
payload: {
webCheckoutDetails: {
checkoutResultReturnUrl: 'http://localhost:4567/thanks'
},
paymentDetails: {
paymentIntent: 'Authorize',
canHandlePendingAuthorization: false,
chargeAmount: {
amount: '29980',
currencyCode: "JPY"
}
},
merchantMetadata: {
merchantReferenceId: "MY-ORDER-100",
merchantStoreName: "Test Store",
noteToBuyer: "Description of the order that is shared in buyer communication.",
customInformation: "Custom info for the order. This data is not shared in any buyer communication."
}
},
headers: {
'x-amz-pay-idempotency-key': SecureRandom.hex(10)
}
)
if response.code.to_i == 201 || response.code.to_i == 200
response.body
else
"{\"status\": \"#{response.code.to_i}\", \"body\": \"#{response.body}\"}"
end
end
get '/thanks' do
# Complete Checkout Session
response = client.api_call("checkoutSessions/#{params['amazonCheckoutSessionId']}/complete", 'POST',
payload: {
chargeAmount: {
amount: '29980',
currencyCode: "JPY"
}
}
)
if response.code.to_i == 201 || response.code.to_i == 200
erb :thanks
else
erb :error, locals: {status: response.code.to_i, body: response.body}
end
end