Skip to content

Instantly share code, notes, and snippets.

@amfeng
Last active August 8, 2022 19:33
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save amfeng/2314782 to your computer and use it in GitHub Desktop.
Save amfeng/2314782 to your computer and use it in GitHub Desktop.
Stripe OAuth Example -- Ruby
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
<%= @access_token %>
</body>
</html>
api_key: YOUR_SECRET_API_KEY
client_id: YOUR_CLIENT_ID
<!doctype html>
<head>
<title>Stripe OAuth Example</title>
</head>
<body>
<a href="/authorize">Connect with Stripe</a>
</body>
</html>
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'yaml'
require 'json'
require 'stripe'
class Server < Sinatra::Base
configure do
config = YAML::load(File.open('config.yml'))
set :api_key, config['api_key']
set :client_id, config['client_id']
options = {
:site => 'https://connect.stripe.com',
:authorize_url => '/oauth/authorize',
:token_url => '/oauth/token'
}
set :client, OAuth2::Client.new(settings.client_id, settings.api_key, options)
end
get '/' do
erb :index
end
get '/authorize' do
params = {
:scope => 'read_write'
}
# Redirect the user to the authorize_uri endpoint
url = settings.client.auth_code.authorize_url(params)
redirect url
end
get '/oauth/callback' do
# Pull the authorization_code out of the response
code = params[:code]
# Make a request to the access_token_uri endpoint to get an access_token
@resp = settings.client.auth_code.get_token(code, :params => {:scope => 'read_write'})
@access_token = @resp.token
# Use the access_token as you would a regular live-mode API key
# TODO: Stripe logic
erb :callback
end
end
Server.run!
@tlaverdure
Copy link

hey do you have a php version of this?

@acoyfellow
Copy link

I would also love a php version

@amfeng
Copy link
Author

amfeng commented Aug 29, 2012

Sorry for the late response, but I've posted a PHP example here: https://gist.github.com/3507366. Will be putting these in the documentation shortly!

@rebelvc
Copy link

rebelvc commented Nov 7, 2012

Here is what I did to pre-fill some user data. Let me know if there is a better way.

params = {
  :scope => 'read_write',
   #optional user info pre-fill
   'stripe_user[first_name]' => 'Betty',
   'stripe_user[last_name]' => 'Crocker',
   'stripe_user[email]' => 'me@bettycrocker.com',
   'stripe_user[url]' => 'http://dessertmarket.com/betty-crocker' 
  }

@rebelvc
Copy link

rebelvc commented Nov 7, 2012

Here is how I pre-fill the user's info, let me know if there is a better way.

    params = {
      :scope => 'read_write',
      #pre-fill user's info (optional)
      'stripe_user[email]' => 'me@bettycrocker.com',
      'stripe_user[url]' => 'https://dessertmarket.com/betty-crocker',
      'stripe_user[first_name]' => 'Betty',
      'stripe_user[last_name]' => 'Crocker'
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment