Created
April 16, 2015 00:36
-
-
Save jordanluyke/013a31ef4df6b13b9b01 to your computer and use it in GitHub Desktop.
SNAPCARD auth example
This file contains 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
require 'uri' | |
require 'net/http' | |
require 'digest/hmac' | |
require 'json' | |
class SnapcardApi | |
MERCHANT_ID = 'ue5hsnusf8gene87asdf4es23bt9d7ak' | |
API_KEY = '9idihi38o18mrm1234ipa1k9ooiifgts' | |
SEC_KEY = '5e3kcoogptge6s5fh2qwertyb6g368dm' | |
API_URL = 'https://api.snapcard.io' | |
def create_invoice | |
api_post('/invoices', { | |
'currency' => 'USD', | |
'lineItems' => [ | |
'itemName' => 'total', | |
'quantity' => 1, | |
'unitPrice' => 1.5 | |
], | |
'merchantId' => MERCHANT_ID | |
}) | |
end | |
private | |
def api_post(path, post_data = {}) | |
params = { | |
'timestamp' => (Time.now.to_i * 1000).to_s | |
} | |
url = API_URL + path + '?' + URI.encode_www_form(params) | |
headers = { | |
'X-Api-Key' => API_KEY, | |
'X-Api-Signature' => calc_auth_sig_hash(url, post_data.to_json.to_s), | |
'X-Api-Version' => '2' | |
} | |
uri = URI(API_URL) | |
Net::HTTP.start(uri.host, uri.port, {:use_ssl => true}) do |http| | |
http.request_post(url, post_data.to_json.to_s, headers) do |res| | |
response = JSON.parse(res.body) | |
raise response['message'] if res.code != '200' | |
return response | |
end | |
end | |
end | |
def calc_auth_sig_hash(url, post_data_s = '') | |
return Digest::HMAC.hexdigest(url + post_data_s, SEC_KEY, Digest::SHA256) | |
end | |
end | |
api = SnapcardApi.new | |
puts api.create_invoice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment