Last active
March 6, 2023 08:50
-
-
Save cjmamo/8918100 to your computer and use it in GitHub Desktop.
Dynamically Create BitCoin Wallets & Payment Pages on Coinbase in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FundraiserController < ApplicationController | |
... | |
JSON_CREATE_PAYMENT_PAGE = '{ "button": { | |
"name": "Donation", | |
"price_string": "1.00", | |
"price_currency_iso": "EUR", | |
"type": "donation", | |
"style": "donation_small", | |
"variable_price": "true", | |
"choose_price": "true" | |
} | |
}' | |
COINBASE_API_KEY = '...' | |
COINBASE_CLIENT_SECRET = '...' | |
# called when the user creates a fundraiser | |
def create | |
#create Coinbase wallet for fundraiser | |
coinbase = Coinbase::Client.new(COINBASE_API_KEY) | |
coinbase_user = coinbase.create_user(params[:email_address], params[:password], COINBASE_CLIENT_SECRET) | |
#create payment page for created wallet | |
oauth_access_token = coinbase_user.oauth.access_token | |
oauth_client = OAuth2::Client.new(COINBASE_API_KEY, COINBASE_CLIENT_SECRET) | |
oauth_token = OAuth2::AccessToken.new(oauth_client, oauth_access_token) | |
coinbase_page = oauth_token.post('https://coinbase.com/api/v1/buttons', {:body => JSON_CREATE_PAYMENT_PAGE, | |
:headers => {'Content-Type' => 'application/json'}}) | |
#get payment page code and persist it | |
coinbase_page_code = JSON.parse(coinbase_page.body)['button']['code'] | |
fundraiser = ActionController::Parameters.new(title: params[:title], | |
description: params[:description], | |
coinbase_page_code: coinbase_page_code) | |
Fundraiser.create!(fundraiser.permit!) | |
flash[:success] = 'Created fundraiser!' | |
rescue Coinbase::Client::Error => e | |
flash[:error] = e.message | |
ensure | |
redirect_to root_path | |
end | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FundraiserController < ApplicationController | |
... | |
def show | |
@fundraiser = Fundraiser.find_by_id(params[:id]) | |
end | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
# remember to use the forked version of coinbase-ruby: https://github.com/claudemamo/coinbase-ruby | |
gem "coinbase", "~> 1.3.0" | |
gem "oauth2", "~> 0.9.3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
... | |
<a class="coinbase-button" data-code="<%= @fundraiser.coinbase_page_code %>" data-button-style="donation_large" href="https://coinbase.com/checkouts/<%= @fundraiser.coinbase_page_code %>">Donate Bitcoins</a> | |
<script src="https://coinbase.com/assets/button.js" type="text/javascript"></script> | |
... | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment