Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fake_braintree.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = Gem::Requirement.new(">= 1.9.3")

s.add_dependency 'activesupport'
s.add_dependency 'braintree', '~> 2.32'
s.add_dependency 'braintree', '~> 4.26'
s.add_dependency 'capybara', '>= 2.2.0'
s.add_dependency 'sinatra'

Expand Down
9 changes: 7 additions & 2 deletions lib/fake_braintree/credit_card.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'fake_braintree/helpers'
require 'fake_braintree/valid_credit_cards'
require 'fake_braintree/payment_method'

module FakeBraintree
class CreditCard
class CreditCard < PaymentMethod
include Helpers

def initialize(credit_card_hash_from_params, options)
Expand Down Expand Up @@ -43,6 +44,10 @@ def update

def delete
if credit_card_exists_in_registry?
if customer = FakeBraintree.registry.customers[credit_card_from_registry["customer_id"]]
customer["credit_cards"].reject! { |card| card["token"] === token }
end

delete_credit_card
deletion_response
else
Expand Down Expand Up @@ -103,7 +108,7 @@ def response_for_card_not_found
end

def response_for_invalid_card
body = FakeBraintree.failure_response.merge(
body = FakeBraintree.failure_response(number).merge(
'params' => {credit_card: @credit_card}
).to_xml(root: 'api_error_response')

Expand Down
3 changes: 2 additions & 1 deletion lib/fake_braintree/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'digest/md5'
require 'active_support/gzip'
require 'securerandom'

module FakeBraintree
module Helpers
Expand All @@ -16,7 +17,7 @@ def md5(content)
end

def create_id(merchant_id)
md5("#{merchant_id}#{Time.now.to_f}")
SecureRandom.hex
end
end
end
21 changes: 9 additions & 12 deletions lib/fake_braintree/sinatra_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,16 @@ def hash_from_request_body_with_key(key)
request_hash = Hash.from_xml(request.body)
request.body.rewind

credit_card_hash =
if request_hash.key?('credit_card')
hash_from_request_body_with_key('credit_card')
else
payment_method_hash = hash_from_request_body_with_key('payment_method')
nonce = payment_method_hash.delete('payment_method_nonce')
h = FakeBraintree.registry.payment_methods
h[nonce] = (h[nonce] || {}).merge(payment_method_hash)
end
options = {merchant_id: params[:merchant_id]}
credit_card_hash = request_hash.key?("credit_card") ? hash_from_request_body_with_key("credit_card") : hash_from_request_body_with_key("payment_method")

if nonce = credit_card_hash.delete("payment_method_nonce")
credit_card_hash.merge!(FakeBraintree.registry.payment_methods[nonce] || {})
end

options = { merchant_id: params[:merchant_id] }

if credit_card_hash['options']
options.merge!(credit_card_hash.delete('options')).symbolize_keys!
if credit_card_hash["options"]
options.merge!(credit_card_hash.delete("options")).symbolize_keys!
end

CreditCard.new(credit_card_hash, options).create
Expand Down
10 changes: 10 additions & 0 deletions spec/fake_braintree/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def token_for(month, year)
expect(Braintree::Customer.find(@customer.id).credit_cards.last.billing_address.postal_code).to eq "94110"
end

it 'successfully creates a credit card from a payment_method_nonce' do
nonce = FakeBraintree::CreditCard.tokenize_card(build_credit_card_hash)
result = Braintree::CreditCard.create(payment_method_nonce: nonce, customer_id: @customer.id)

expect(result).to be_success
expect(Braintree::Customer.find(@customer.id).credit_cards.last.token).to eq 'token'
expect(Braintree::Customer.find(@customer.id).credit_cards.last).to be_default
expect(Braintree::Customer.find(@customer.id).credit_cards.last.billing_address.postal_code).to eq "94110"
end

it 'only allows one credit card to be default' do
result = Braintree::CreditCard.create(build_credit_card_hash)
expect(result).to be_success
Expand Down