Skip to content
This repository was archived by the owner on Feb 27, 2023. It is now read-only.

Commit 955235e

Browse files
Adds confirm_payment endpoint for manual confirmation (#50)
* Adds confirm payment endpoint. * Adds return_url param * Adds confirm params. * Don't double confirm. * Explicitly pass whether to confirm instead of inferring from return_url
1 parent c465ad2 commit 955235e

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

web.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def log_info(message)
5555
payload[:metadata],
5656
'usd',
5757
payload[:shipping],
58+
payload[:return_url],
5859
)
5960
rescue Stripe::StripeError => e
6061
status 402
@@ -65,6 +66,23 @@ def log_info(message)
6566
return payment_intent.to_json
6667
end
6768

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+
6886
def authenticate!
6987
# This code simulates "loading the Stripe customer for your current session".
7088
# Your own logic will likely look very different.
@@ -104,6 +122,7 @@ def authenticate!
104122
nil,
105123
params[:metadata],
106124
params[:currency],
125+
nil,
107126
nil
108127
)
109128
rescue Stripe::StripeError => e
@@ -141,6 +160,7 @@ def authenticate!
141160
source.metadata["customer"],
142161
source.metadata,
143162
source.currency,
163+
nil,
144164
nil
145165
)
146166
rescue Stripe::StripeError => e
@@ -156,7 +176,7 @@ def authenticate!
156176
end
157177

158178
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)
160180
return Stripe::PaymentIntent.create(
161181
:amount => amount,
162182
:currency => currency || 'usd',
@@ -166,15 +186,18 @@ def create_payment_intent(amount, source_id, payment_method_id, customer_id = ni
166186
:payment_method_types => ['card'],
167187
:description => "Example PaymentIntent",
168188
:shipping => shipping,
189+
:return_url => return_url,
190+
:confirm => confirm,
191+
:confirmation_method => confirm ? "manual" : "automatic",
169192
:metadata => {
170193
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
171194
}.merge(metadata || {}),
172195
)
173196
end
174197

175198
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)
177200
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

Comments
 (0)