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

Commit 16c2813

Browse files
Migrate to PaymentIntents API (#47)
**Summary** Charges have been deprecated in favor of PaymentIntents. In addition, when we migrate from Sources to PaymentMethods, we'll be required to use PaymentIntents. **Motivation** MOBILE-101 **Testing** - Manually tested endpoints via curl - Deployed backend and tested against Android apps
1 parent cc5ffaf commit 16c2813

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

web.rb

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,23 @@ def log_info(message)
4545
payload = Sinatra::IndifferentHash[JSON.parse(request.body.read)]
4646
end
4747

48-
source = payload[:source]
49-
customer = payload[:customer_id] || @customer.id
50-
# Create the charge on Stripe's servers - this will charge the user's card
48+
# Create and capture the PaymentIntent via Stripe's API - this will charge the user's card
5149
begin
52-
charge = Stripe::Charge.create(
53-
:amount => payload[:amount], # this number should be in cents
54-
:currency => "usd",
55-
:customer => customer,
56-
:source => source,
57-
:description => "Example Charge",
58-
:shipping => payload[:shipping],
59-
:metadata => {
60-
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
61-
}.merge(payload[:metadata] || {}),
50+
payment_intent = create_and_capture_payment_intent(
51+
payload[:amount],
52+
payload[:source],
53+
payload[:customer_id] || @customer.id,
54+
payload[:metadata],
55+
'usd',
56+
payload[:shipping],
6257
)
6358
rescue Stripe::StripeError => e
6459
status 402
65-
return log_info("Error creating charge: #{e.message}")
60+
return log_info("Error: #{e.message}")
6661
end
6762

6863
status 200
69-
return log_info("Charge successfully created")
64+
return payment_intent.to_json
7065
end
7166

7267
def authenticate!
@@ -97,24 +92,23 @@ def authenticate!
9792

9893
# This endpoint is used by the Obj-C and Android example apps to create a charge.
9994
post '/create_charge' do
100-
# Create the charge on Stripe's servers
95+
# Create and capture the PaymentIntent via Stripe's API - this will charge the user's card
10196
begin
102-
charge = Stripe::Charge.create(
103-
:amount => params[:amount], # this number should be in cents
104-
:currency => "usd",
105-
:source => params[:source],
106-
:description => "Example Charge",
107-
:metadata => {
108-
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
109-
}.merge(params[:metadata] || {}),
97+
payment_intent = create_and_capture_payment_intent(
98+
params[:amount],
99+
params[:source],
100+
nil,
101+
params[:metadata],
102+
'usd',
103+
nil
110104
)
111105
rescue Stripe::StripeError => e
112106
status 402
113-
return log_info("Error creating charge: #{e.message}")
107+
return log_info("Error: #{e.message}")
114108
end
115109

116110
status 200
117-
return log_info("Charge successfully created")
111+
return payment_intent.to_json
118112
end
119113

120114
# This endpoint is used by the mobile example apps to create a PaymentIntent.
@@ -123,23 +117,22 @@ def authenticate!
123117
# to prevent misuse
124118
post '/create_intent' do
125119
begin
126-
intent = Stripe::PaymentIntent.create(
127-
:payment_method_types => ['card'],
128-
:amount => params[:amount],
129-
:currency => params[:currency] || 'usd',
130-
:description => params[:description] || 'Example PaymentIntent charge',
131-
:metadata => {
132-
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
133-
}.merge(params[:metadata] || {}),
120+
payment_intent = create_payment_intent(
121+
params[:amount],
122+
nil,
123+
nil,
124+
params[:metadata],
125+
params[:currency],
126+
nil
134127
)
135128
rescue Stripe::StripeError => e
136129
status 402
137-
return log_info("Error creating payment intent: #{e.message}")
130+
return log_info("Error creating PaymentIntent: #{e.message}")
138131
end
139132

140-
log_info("Payment Intent successfully created")
133+
log_info("PaymentIntent successfully created: #{payment_intent.id}")
141134
status 200
142-
return {:intent => intent.id, :secret => intent.client_secret}.to_json
135+
return {:intent => payment_intent.id, :secret => payment_intent.client_secret}.to_json
143136
end
144137

145138
# This endpoint responds to webhooks sent by Stripe. To use it, you'll need
@@ -155,29 +148,49 @@ def authenticate!
155148

156149
# For sources that require additional user action from your customer
157150
# (e.g. authorizing the payment with their bank), you should use webhooks
158-
# to create a charge after the source becomes chargeable.
151+
# to capture a PaymentIntent after the source becomes chargeable.
159152
# For more information, see https://stripe.com/docs/sources#best-practices
160153
WEBHOOK_CHARGE_CREATION_TYPES = ['bancontact', 'giropay', 'ideal', 'sofort', 'three_d_secure']
161154
if event.type == 'source.chargeable' && WEBHOOK_CHARGE_CREATION_TYPES.include?(source.type)
162155
begin
163-
charge = Stripe::Charge.create(
164-
:amount => source.amount,
165-
:currency => source.currency,
166-
:source => source.id,
167-
:customer => source.metadata["customer"],
168-
:description => "Example Charge",
169-
:metadata => {
170-
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
171-
}.merge(source.metadata || {}),
156+
create_and_capture_payment_intent(
157+
source.amount,
158+
source.id,
159+
source.metadata["customer"],
160+
source.metadata,
161+
source.currency,
162+
nil
172163
)
173164
rescue Stripe::StripeError => e
174-
return log_info("Error creating charge: #{e.message}")
165+
return log_info("Error creating PaymentIntent: #{e.message}")
175166
end
176-
# After successfully creating a charge, you should complete your customer's
167+
# After successfully capturing a PaymentIntent, you should complete your customer's
177168
# order and notify them that their order has been fulfilled (e.g. by sending
178169
# an email). When creating the source in your app, consider storing any order
179170
# information (e.g. order number) as metadata so that you can retrieve it
180171
# here and use it to complete your customer's purchase.
181172
end
182173
status 200
183174
end
175+
176+
def create_payment_intent(amount, source_id, customer_id = nil,
177+
metadata = {}, currency = 'usd', shipping = nil)
178+
return Stripe::PaymentIntent.create(
179+
:amount => amount,
180+
:currency => currency || 'usd',
181+
:customer => customer_id,
182+
:source => source_id,
183+
:payment_method_types => ['card'],
184+
:description => "Example PaymentIntent",
185+
:shipping => shipping,
186+
:metadata => {
187+
:order_id => '5278735C-1F40-407D-933A-286E463E72D8',
188+
}.merge(metadata || {}),
189+
)
190+
end
191+
192+
def create_and_capture_payment_intent(amount, source_id, customer_id = nil,
193+
metadata = {}, currency = 'usd', shipping = nil)
194+
payment_intent = create_payment_intent(amount, source_id, customer_id, metadata, currency, shipping)
195+
return payment_intent.confirm()
196+
end

0 commit comments

Comments
 (0)