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

Commit df99f57

Browse files
Add support for DEFAULT_CUSTOMER_ID and DEFAULT_PAYMENT_INTENT_ID env variables (#63)
This allows for re-use of a given Customer or PaymentIntent, rather than creating a new one for each request/session.
1 parent 09dbcfa commit df99f57

File tree

1 file changed

+58
-41
lines changed

1 file changed

+58
-41
lines changed

web.rb

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,21 @@ def log_info(message)
4747

4848
# Create and capture the PaymentIntent via Stripe's API - this will charge the user's card
4949
begin
50-
payment_intent = create_and_capture_payment_intent(
51-
payload[:amount],
52-
payload[:source],
53-
payload[:payment_method],
54-
payload[:customer_id] || @customer.id,
55-
payload[:metadata],
56-
'usd',
57-
payload[:shipping],
58-
payload[:return_url],
59-
)
50+
payment_intent_id = ENV['DEFAULT_PAYMENT_INTENT_ID']
51+
if payment_intent_id
52+
payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
53+
else
54+
payment_intent = create_and_capture_payment_intent(
55+
payload[:amount],
56+
payload[:source],
57+
payload[:payment_method],
58+
payload[:customer_id] || @customer.id,
59+
payload[:metadata],
60+
'usd',
61+
payload[:shipping],
62+
payload[:return_url],
63+
)
64+
end
6065
rescue Stripe::StripeError => e
6166
status 402
6267
return log_info("Error: #{e.message}")
@@ -98,32 +103,40 @@ def authenticate!
98103
rescue Stripe::InvalidRequestError
99104
end
100105
else
101-
begin
102-
@customer = Stripe::Customer.create(
103-
:description => 'mobile SDK example customer',
104-
:metadata => {
105-
# Add our application's customer id for this Customer, so it'll be easier to look up
106-
:my_customer_id => '72F8C533-FCD5-47A6-A45B-3956CA8C792D',
107-
},
108-
)
109-
# Attach some test cards to the customer for testing convenience.
110-
# See https://stripe.com/docs/testing#cards
111-
['pm_card_threeDSecure2Required', 'pm_card_visa'].each { |pm_id|
112-
Stripe::PaymentMethod.attach(
113-
pm_id,
114-
{
115-
customer: @customer.id,
116-
}
117-
)
106+
default_cusomer_id = ENV['DEFAULT_CUSTOMER_ID']
107+
if default_cusomer_id
108+
@customer = Stripe::Customer.retrieve(default_cusomer_id)
109+
else
110+
begin
111+
@customer = create_customer()
112+
# Attach some test cards to the customer for testing convenience.
113+
# See https://stripe.com/docs/testing#cards
114+
['pm_card_threeDSecure2Required', 'pm_card_visa'].each { |pm_id|
115+
Stripe::PaymentMethod.attach(
116+
pm_id,
117+
{
118+
customer: @customer.id,
119+
}
120+
)
118121
}
119-
120-
rescue Stripe::InvalidRequestError
122+
rescue Stripe::InvalidRequestError
123+
end
121124
end
122125
session[:customer_id] = @customer.id
123126
end
124127
@customer
125128
end
126129

130+
def create_customer
131+
Stripe::Customer.create(
132+
:description => 'mobile SDK example customer',
133+
:metadata => {
134+
# Add our application's customer id for this Customer, so it'll be easier to look up
135+
:my_customer_id => '72F8C533-FCD5-47A6-A45B-3956CA8C792D',
136+
},
137+
)
138+
end
139+
127140
# This endpoint is used by the mobile example apps to create a SetupIntent.
128141
# https://stripe.com/docs/api/setup_intents/create
129142
# Just like the `/capture_payment` endpoint, a real implementation would include controls
@@ -161,16 +174,21 @@ def authenticate!
161174
# to prevent misuse
162175
post '/create_intent' do
163176
begin
164-
payment_intent = create_payment_intent(
165-
params[:amount],
166-
nil,
167-
nil,
168-
nil,
169-
params[:metadata],
170-
params[:currency],
171-
nil,
172-
nil
173-
)
177+
payment_intent_id = ENV['DEFAULT_PAYMENT_INTENT_ID']
178+
if payment_intent_id
179+
payment_intent = Stripe::PaymentIntent.retrieve(payment_intent_id)
180+
else
181+
payment_intent = create_payment_intent(
182+
params[:amount],
183+
nil,
184+
nil,
185+
nil,
186+
params[:metadata],
187+
params[:currency],
188+
nil,
189+
nil
190+
)
191+
end
174192
rescue Stripe::StripeError => e
175193
status 402
176194
return log_info("Error creating PaymentIntent: #{e.message}")
@@ -249,7 +267,6 @@ def create_payment_intent(amount, source_id, payment_method_id, customer_id = ni
249267

250268
def create_and_capture_payment_intent(amount, source_id, payment_method_id, customer_id = nil,
251269
metadata = {}, currency = 'usd', shipping = nil, return_url = nil)
252-
payment_intent = create_payment_intent(amount, source_id, payment_method_id, customer_id,
270+
return create_payment_intent(amount, source_id, payment_method_id, customer_id,
253271
metadata, currency, shipping, return_url, true)
254-
return payment_intent
255272
end

0 commit comments

Comments
 (0)