Skip to content

Instantly share code, notes, and snippets.

@alaingoldman
Created February 28, 2014 05:16
Show Gist options
  • Save alaingoldman/9265723 to your computer and use it in GitHub Desktop.
Save alaingoldman/9265723 to your computer and use it in GitHub Desktop.
require "net/http"
require "uri"
require "json"
require 'openssl'
uri = URI.parse("https://coinbase.com/api/v1/buttons")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-Type', 'application/json')
button = {
:name => 'somename',
:type => 'buy_now',
:description => "message.",
:style => 'none',
:custom => @admin_password,
:custom_secure => true,
:variable_price => true,
:price_string => "0.400",
:price_currency_iso => 'BTC',
:text => "Send to us",
:choose_price => true,
:auto_redirect => true,
:success_url => "#{root_url}",
:cancel_url => "#{root_url}",
:callback_url => "#{root_url}/hidden_url"
}
request.body = button.to_json
@API_KEY = 'aaaaaaaaaaa'
@API_SECRET = 'bbbbbbbbbbbbbbbbbbbbbbb'
@nonce = (Time.now.to_f * 1e6).to_i
@message = @nonce.to_s + "https://coinbase.com/api/v1/buttons" + request.body.to_s
@signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), @API_SECRET, @message)
request["ACCESS_KEY"] = @API_KEY
request["ACCESS_SIGNATURE"] = @signature
request["ACCESS_NONCE"] = @nonce
response = http.request(request)
data = JSON.parse(response.body)
@user.button_code = data['button']['code']
@user.save(:validate => false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment