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

Commit f8ad686

Browse files
Merge branch 'master' of github.com:stripe/example-ios-backend
2 parents 73206f4 + 955235e commit f8ad686

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

web.rb

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def log_info(message)
3737
key.to_json
3838
end
3939

40-
post '/charge' do
40+
post '/capture_payment' do
4141
authenticate!
4242
# Get the credit card details submitted
4343
payload = params
@@ -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.
@@ -91,31 +109,9 @@ def authenticate!
91109
@customer
92110
end
93111

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-
116112
# This endpoint is used by the mobile example apps to create a PaymentIntent.
117113
# 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
119115
# to prevent misuse
120116
post '/create_intent' do
121117
begin
@@ -126,6 +122,7 @@ def authenticate!
126122
nil,
127123
params[:metadata],
128124
params[:currency],
125+
nil,
129126
nil
130127
)
131128
rescue Stripe::StripeError => e
@@ -163,6 +160,7 @@ def authenticate!
163160
source.metadata["customer"],
164161
source.metadata,
165162
source.currency,
163+
nil,
166164
nil
167165
)
168166
rescue Stripe::StripeError => e
@@ -178,7 +176,7 @@ def authenticate!
178176
end
179177

180178
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)
182180
return Stripe::PaymentIntent.create(
183181
:amount => amount,
184182
:currency => currency || 'usd',
@@ -188,15 +186,18 @@ def create_payment_intent(amount, source_id, payment_method_id, customer_id = ni
188186
:payment_method_types => ['card'],
189187
:description => "Example PaymentIntent",
190188
:shipping => shipping,
189+
:return_url => return_url,
190+
:confirm => confirm,
191+
:confirmation_method => confirm ? "manual" : "automatic",
191192
:metadata => {
192193
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
193194
}.merge(metadata || {}),
194195
)
195196
end
196197

197198
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)
199200
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

Comments
 (0)