@@ -47,16 +47,21 @@ def log_info(message)
47
47
48
48
# Create and capture the PaymentIntent via Stripe's API - this will charge the user's card
49
49
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
60
65
rescue Stripe ::StripeError => e
61
66
status 402
62
67
return log_info ( "Error: #{ e . message } " )
@@ -98,32 +103,40 @@ def authenticate!
98
103
rescue Stripe ::InvalidRequestError
99
104
end
100
105
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
+ )
118
121
}
119
-
120
- rescue Stripe :: InvalidRequestError
122
+ rescue Stripe :: InvalidRequestError
123
+ end
121
124
end
122
125
session [ :customer_id ] = @customer . id
123
126
end
124
127
@customer
125
128
end
126
129
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
+
127
140
# This endpoint is used by the mobile example apps to create a SetupIntent.
128
141
# https://stripe.com/docs/api/setup_intents/create
129
142
# Just like the `/capture_payment` endpoint, a real implementation would include controls
@@ -161,16 +174,21 @@ def authenticate!
161
174
# to prevent misuse
162
175
post '/create_intent' do
163
176
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
174
192
rescue Stripe ::StripeError => e
175
193
status 402
176
194
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
249
267
250
268
def create_and_capture_payment_intent ( amount , source_id , payment_method_id , customer_id = nil ,
251
269
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 ,
253
271
metadata , currency , shipping , return_url , true )
254
- return payment_intent
255
272
end
0 commit comments