@@ -37,7 +37,7 @@ def log_info(message)
37
37
key . to_json
38
38
end
39
39
40
- post '/charge ' do
40
+ post '/capture_payment ' do
41
41
authenticate!
42
42
# Get the credit card details submitted
43
43
payload = params
@@ -55,6 +55,7 @@ def log_info(message)
55
55
payload [ :metadata ] ,
56
56
'usd' ,
57
57
payload [ :shipping ] ,
58
+ payload [ :return_url ] ,
58
59
)
59
60
rescue Stripe ::StripeError => e
60
61
status 402
@@ -65,6 +66,23 @@ def log_info(message)
65
66
return payment_intent . to_json
66
67
end
67
68
69
+ post '/confirm_payment' do
70
+ authenticate!
71
+ payload = params
72
+ if request . content_type . include? 'application/json' and params . empty?
73
+ payload = Sinatra ::IndifferentHash [ JSON . parse ( request . body . read ) ]
74
+ end
75
+ begin
76
+ payment_intent = Stripe ::PaymentIntent . confirm ( payload [ :payment_intent_id ] )
77
+ rescue Stripe ::StripeError => e
78
+ status 402
79
+ return log_info ( "Error: #{ e . message } " )
80
+ end
81
+
82
+ status 200
83
+ return payment_intent . to_json
84
+ end
85
+
68
86
def authenticate!
69
87
# This code simulates "loading the Stripe customer for your current session".
70
88
# Your own logic will likely look very different.
@@ -91,31 +109,9 @@ def authenticate!
91
109
@customer
92
110
end
93
111
94
- # This endpoint is used by the Obj-C and Android example apps to create a charge.
95
- post '/create_charge' do
96
- # Create and capture the PaymentIntent via Stripe's API - this will charge the user's card
97
- begin
98
- payment_intent = create_and_capture_payment_intent (
99
- params [ :amount ] ,
100
- params [ :source ] ,
101
- params [ :payment_method ] ,
102
- nil ,
103
- params [ :metadata ] ,
104
- 'usd' ,
105
- nil
106
- )
107
- rescue Stripe ::StripeError => e
108
- status 402
109
- return log_info ( "Error: #{ e . message } " )
110
- end
111
-
112
- status 200
113
- return payment_intent . to_json
114
- end
115
-
116
112
# This endpoint is used by the mobile example apps to create a PaymentIntent.
117
113
# https://stripe.com/docs/api/payment_intents/create
118
- # Just like the `/create_charge ` endpoint, a real implementation would include controls
114
+ # Just like the `/capture_payment ` endpoint, a real implementation would include controls
119
115
# to prevent misuse
120
116
post '/create_intent' do
121
117
begin
@@ -126,6 +122,7 @@ def authenticate!
126
122
nil ,
127
123
params [ :metadata ] ,
128
124
params [ :currency ] ,
125
+ nil ,
129
126
nil
130
127
)
131
128
rescue Stripe ::StripeError => e
@@ -163,6 +160,7 @@ def authenticate!
163
160
source . metadata [ "customer" ] ,
164
161
source . metadata ,
165
162
source . currency ,
163
+ nil ,
166
164
nil
167
165
)
168
166
rescue Stripe ::StripeError => e
@@ -178,7 +176,7 @@ def authenticate!
178
176
end
179
177
180
178
def create_payment_intent ( amount , source_id , payment_method_id , customer_id = nil ,
181
- metadata = { } , currency = 'usd' , shipping = nil )
179
+ metadata = { } , currency = 'usd' , shipping = nil , return_url = nil , confirm = false )
182
180
return Stripe ::PaymentIntent . create (
183
181
:amount => amount ,
184
182
:currency => currency || 'usd' ,
@@ -188,15 +186,18 @@ def create_payment_intent(amount, source_id, payment_method_id, customer_id = ni
188
186
:payment_method_types => [ 'card' ] ,
189
187
:description => "Example PaymentIntent" ,
190
188
:shipping => shipping ,
189
+ :return_url => return_url ,
190
+ :confirm => confirm ,
191
+ :confirmation_method => confirm ? "manual" : "automatic" ,
191
192
:metadata => {
192
193
:order_id => '5278735C-1F40-407D-933A-286E463E72D8' ,
193
194
} . merge ( metadata || { } ) ,
194
195
)
195
196
end
196
197
197
198
def create_and_capture_payment_intent ( amount , source_id , payment_method_id , customer_id = nil ,
198
- metadata = { } , currency = 'usd' , shipping = nil )
199
+ metadata = { } , currency = 'usd' , shipping = nil , return_url = nil )
199
200
payment_intent = create_payment_intent ( amount , source_id , payment_method_id , customer_id ,
200
- metadata , currency , shipping )
201
- return payment_intent . confirm ( )
202
- end
201
+ metadata , currency , shipping , return_url , true )
202
+ return payment_intent
203
+ end
0 commit comments