@@ -45,28 +45,23 @@ def log_info(message)
45
45
payload = Sinatra ::IndifferentHash [ JSON . parse ( request . body . read ) ]
46
46
end
47
47
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
51
49
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 ] ,
62
57
)
63
58
rescue Stripe ::StripeError => e
64
59
status 402
65
- return log_info ( "Error creating charge : #{ e . message } " )
60
+ return log_info ( "Error: #{ e . message } " )
66
61
end
67
62
68
63
status 200
69
- return log_info ( "Charge successfully created" )
64
+ return payment_intent . to_json
70
65
end
71
66
72
67
def authenticate!
@@ -97,24 +92,23 @@ def authenticate!
97
92
98
93
# This endpoint is used by the Obj-C and Android example apps to create a charge.
99
94
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
101
96
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
110
104
)
111
105
rescue Stripe ::StripeError => e
112
106
status 402
113
- return log_info ( "Error creating charge : #{ e . message } " )
107
+ return log_info ( "Error: #{ e . message } " )
114
108
end
115
109
116
110
status 200
117
- return log_info ( "Charge successfully created" )
111
+ return payment_intent . to_json
118
112
end
119
113
120
114
# This endpoint is used by the mobile example apps to create a PaymentIntent.
@@ -123,23 +117,22 @@ def authenticate!
123
117
# to prevent misuse
124
118
post '/create_intent' do
125
119
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
134
127
)
135
128
rescue Stripe ::StripeError => e
136
129
status 402
137
- return log_info ( "Error creating payment intent : #{ e . message } " )
130
+ return log_info ( "Error creating PaymentIntent : #{ e . message } " )
138
131
end
139
132
140
- log_info ( "Payment Intent successfully created" )
133
+ log_info ( "PaymentIntent successfully created: #{ payment_intent . id } " )
141
134
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
143
136
end
144
137
145
138
# This endpoint responds to webhooks sent by Stripe. To use it, you'll need
@@ -155,29 +148,49 @@ def authenticate!
155
148
156
149
# For sources that require additional user action from your customer
157
150
# (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.
159
152
# For more information, see https://stripe.com/docs/sources#best-practices
160
153
WEBHOOK_CHARGE_CREATION_TYPES = [ 'bancontact' , 'giropay' , 'ideal' , 'sofort' , 'three_d_secure' ]
161
154
if event . type == 'source.chargeable' && WEBHOOK_CHARGE_CREATION_TYPES . include? ( source . type )
162
155
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
172
163
)
173
164
rescue Stripe ::StripeError => e
174
- return log_info ( "Error creating charge : #{ e . message } " )
165
+ return log_info ( "Error creating PaymentIntent : #{ e . message } " )
175
166
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
177
168
# order and notify them that their order has been fulfilled (e.g. by sending
178
169
# an email). When creating the source in your app, consider storing any order
179
170
# information (e.g. order number) as metadata so that you can retrieve it
180
171
# here and use it to complete your customer's purchase.
181
172
end
182
173
status 200
183
174
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