forked from blinkboxbooks/blinkbox-user.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from DanielL/user-add-credit
User add credit
- Loading branch information
Showing
5 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "base64" # gem: rubysl-base64 | ||
require "openssl" | ||
require "securerandom" | ||
|
||
module BraintreeEncryption | ||
def self.encrypt(value, public_key) | ||
# reverse engineered from https://github.com/braintree/braintree.js/blob/master/lib/braintree.js | ||
# this may need to be updated if braintree change their client-side encryption scripts | ||
|
||
return nil if value.nil? | ||
return "" if value.respond_to?(:empty?) && value.empty? | ||
|
||
raise "The Braintree client key is not configured" if public_key.nil? | ||
raw_key = Base64.strict_decode64(public_key) | ||
rsa = OpenSSL::PKey::RSA.new(raw_key) | ||
|
||
aes = OpenSSL::Cipher::AES256.new(:CBC).encrypt | ||
aes_key, aes_iv = aes.random_key, aes.random_iv | ||
encrypted_value = aes.update(value.to_s) + aes.final | ||
ciphertext = aes_iv + encrypted_value | ||
encoded_ciphertext = Base64.strict_encode64(ciphertext) | ||
|
||
hmac_key = SecureRandom.random_bytes(32) | ||
hmac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, hmac_key, ciphertext) | ||
signature = Base64.strict_encode64(hmac) | ||
|
||
combined_key = aes_key + hmac_key | ||
encoded_key = Base64.strict_encode64(combined_key) | ||
encrypted_key = Base64.strict_encode64(rsa.public_encrypt(encoded_key)) | ||
|
||
"$bt4|javascript_1_3_9$#{encrypted_key}$#{encoded_ciphertext}$#{signature}" | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'httparty' | ||
require 'multi_json' | ||
require 'net/http/capture' | ||
|
||
module Blinkbox | ||
class CreditCardServiceClient | ||
include HTTParty | ||
attr_accessor :headers | ||
|
||
def initialize(server_uri, proxy_uri = nil) | ||
self.class.base_uri(server_uri.to_s) | ||
self.class.http_proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) if proxy_uri | ||
self.class.debug_output($stderr) if ENV['DEBUG'] | ||
@headers = {} | ||
end | ||
|
||
def use_proxy(proxy_uri) | ||
proxy_uri = URI.parse(proxy_uri) if proxy_uri.is_a?(String) | ||
self.class.http_proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) | ||
end | ||
|
||
def add_credit_card(access_token, card_details = {}) | ||
body = { | ||
default: card_details[:default], | ||
number: card_details[:cc_number], | ||
cvv: card_details[:cvv], | ||
expirationMonth: card_details[:expiration_month], | ||
expirationYear: card_details[:expiration_year], | ||
cardholderName: card_details[:card_holder_name], | ||
billingAddress: card_details[:billing_address] | ||
} | ||
|
||
retries_left = 10 | ||
card_id = nil | ||
while card_id == nil and retries_left > 0 | ||
response = http_post "/service/my/creditcards", body, access_token | ||
card_id = MultiJson.load(response.body)['id'] | ||
retries_left -= 1 | ||
end | ||
|
||
fail 'Adding credit card failed' unless card_id | ||
|
||
card_id | ||
end | ||
|
||
private | ||
|
||
def http_post(uri, body, access_token = nil) | ||
http_send(:post, uri, body, access_token) | ||
end | ||
|
||
def http_send(verb, uri, post_body, access_token = nil) | ||
headers = {"Content-Type" => "application/vnd.blinkboxbooks.data.v1+json" }.merge(@headers) | ||
headers["Authorization"] = "Bearer #{access_token}" if access_token | ||
self.class.send(verb, uri.to_s, headers: headers, body: post_body.to_json) | ||
HttpCapture::RESPONSES.last | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require_relative '../spec_helper' | ||
|
||
describe Blinkbox::CreditCardServiceClient.new(SERVER_URI, nil) do | ||
it { is_expected.to respond_to(:use_proxy).with(1).argument } | ||
it { is_expected.to respond_to(:add_credit_card).with(1).argument } | ||
it { is_expected.to respond_to(:add_credit_card).with(2).arguments } | ||
end |
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