@@ -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.
@@ -104,6 +122,7 @@ def authenticate!
104
122
nil ,
105
123
params [ :metadata ] ,
106
124
params [ :currency ] ,
125
+ nil ,
107
126
nil
108
127
)
109
128
rescue Stripe ::StripeError => e
@@ -141,6 +160,7 @@ def authenticate!
141
160
source . metadata [ "customer" ] ,
142
161
source . metadata ,
143
162
source . currency ,
163
+ nil ,
144
164
nil
145
165
)
146
166
rescue Stripe ::StripeError => e
@@ -156,7 +176,7 @@ def authenticate!
156
176
end
157
177
158
178
def create_payment_intent ( amount , source_id , payment_method_id , customer_id = nil ,
159
- metadata = { } , currency = 'usd' , shipping = nil )
179
+ metadata = { } , currency = 'usd' , shipping = nil , return_url = nil , confirm = false )
160
180
return Stripe ::PaymentIntent . create (
161
181
:amount => amount ,
162
182
:currency => currency || 'usd' ,
@@ -166,15 +186,18 @@ def create_payment_intent(amount, source_id, payment_method_id, customer_id = ni
166
186
:payment_method_types => [ 'card' ] ,
167
187
:description => "Example PaymentIntent" ,
168
188
:shipping => shipping ,
189
+ :return_url => return_url ,
190
+ :confirm => confirm ,
191
+ :confirmation_method => confirm ? "manual" : "automatic" ,
169
192
:metadata => {
170
193
:order_id => '5278735C-1F40-407D-933A-286E463E72D8' ,
171
194
} . merge ( metadata || { } ) ,
172
195
)
173
196
end
174
197
175
198
def create_and_capture_payment_intent ( amount , source_id , payment_method_id , customer_id = nil ,
176
- metadata = { } , currency = 'usd' , shipping = nil )
199
+ metadata = { } , currency = 'usd' , shipping = nil , return_url = nil )
177
200
payment_intent = create_payment_intent ( amount , source_id , payment_method_id , customer_id ,
178
- metadata , currency , shipping )
179
- return payment_intent . confirm ( )
180
- end
201
+ metadata , currency , shipping , return_url , true )
202
+ return payment_intent
203
+ end
0 commit comments