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

Commit

Permalink
Add support for creating a PaymentIntent via the sample backend (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
danj-stripe authored Jul 6, 2018
1 parent d522cc4 commit 0a58d62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ gem 'dotenv', '2.2.1'
gem 'encrypted_cookie', '0.0.5'
gem 'json', '2.1.0'
gem 'sinatra', '2.0.2'
gem 'stripe', '3.11.0'
gem 'stripe', '3.17.0'
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GEM
rack (~> 2.0)
rack-protection (= 2.0.2)
tilt (~> 2.0)
stripe (3.11.0)
stripe (3.17.0)
faraday (~> 0.10)
tilt (2.0.8)

Expand All @@ -29,7 +29,7 @@ DEPENDENCIES
encrypted_cookie (= 0.0.5)
json (= 2.1.0)
sinatra (= 2.0.2)
stripe (= 3.11.0)
stripe (= 3.17.0)

RUBY VERSION
ruby 2.5.0p0
Expand Down
23 changes: 23 additions & 0 deletions web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ def authenticate!
return log_info("Charge successfully created")
end

# This endpoint is used by the mobile example apps to create a PaymentIntent.
# https://stripe.com/docs/api#create_payment_intent
# Just like the `/create_charge` endpoint, a real implementation would include controls
# to prevent misuse
post '/create_intent' do
begin
intent = Stripe::PaymentIntent.create(
:allowed_source_types => ['card'],
:amount => params[:amount],
:currency => params[:currency] || 'usd',
:description => params[:description] || 'Example PaymentIntent charge',
:return_url => params[:return_url],
)
rescue Stripe::StripeError => e
status 402
return log_info("Error creating payment intent: #{e.message}")
end

log_info("Payment Intent successfully created")
status 200
return {:intent => intent.id, :secret => intent.client_secret}.to_json
end

# This endpoint responds to webhooks sent by Stripe. To use it, you'll need
# to add its URL (https://{your-app-name}.herokuapp.com/stripe-webhook)
# in the webhook settings section of the Dashboard.
Expand Down

0 comments on commit 0a58d62

Please sign in to comment.