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

Commit 0a58d62

Browse files
authored
Add support for creating a PaymentIntent via the sample backend (#34)
* Making all parameters controllable by client * Pass back intent ID + secret * Update Stripe ruby library to 3.17 for PaymentIntents. Support was added in 3.16: https://github.com/stripe/stripe-ruby/pull/657/files
1 parent d522cc4 commit 0a58d62

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Diff for: Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ gem 'dotenv', '2.2.1'
66
gem 'encrypted_cookie', '0.0.5'
77
gem 'json', '2.1.0'
88
gem 'sinatra', '2.0.2'
9-
gem 'stripe', '3.11.0'
9+
gem 'stripe', '3.17.0'

Diff for: Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GEM
1717
rack (~> 2.0)
1818
rack-protection (= 2.0.2)
1919
tilt (~> 2.0)
20-
stripe (3.11.0)
20+
stripe (3.17.0)
2121
faraday (~> 0.10)
2222
tilt (2.0.8)
2323

@@ -29,7 +29,7 @@ DEPENDENCIES
2929
encrypted_cookie (= 0.0.5)
3030
json (= 2.1.0)
3131
sinatra (= 2.0.2)
32-
stripe (= 3.11.0)
32+
stripe (= 3.17.0)
3333

3434
RUBY VERSION
3535
ruby 2.5.0p0

Diff for: web.rb

+23
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ def authenticate!
105105
return log_info("Charge successfully created")
106106
end
107107

108+
# This endpoint is used by the mobile example apps to create a PaymentIntent.
109+
# https://stripe.com/docs/api#create_payment_intent
110+
# Just like the `/create_charge` endpoint, a real implementation would include controls
111+
# to prevent misuse
112+
post '/create_intent' do
113+
begin
114+
intent = Stripe::PaymentIntent.create(
115+
:allowed_source_types => ['card'],
116+
:amount => params[:amount],
117+
:currency => params[:currency] || 'usd',
118+
:description => params[:description] || 'Example PaymentIntent charge',
119+
:return_url => params[:return_url],
120+
)
121+
rescue Stripe::StripeError => e
122+
status 402
123+
return log_info("Error creating payment intent: #{e.message}")
124+
end
125+
126+
log_info("Payment Intent successfully created")
127+
status 200
128+
return {:intent => intent.id, :secret => intent.client_secret}.to_json
129+
end
130+
108131
# This endpoint responds to webhooks sent by Stripe. To use it, you'll need
109132
# to add its URL (https://{your-app-name}.herokuapp.com/stripe-webhook)
110133
# in the webhook settings section of the Dashboard.

0 commit comments

Comments
 (0)