diff --git a/fake_braintree.gemspec b/fake_braintree.gemspec index ead1d48..9eb032e 100644 --- a/fake_braintree.gemspec +++ b/fake_braintree.gemspec @@ -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' diff --git a/lib/fake_braintree/credit_card.rb b/lib/fake_braintree/credit_card.rb index 2f95b9c..90dc5f0 100644 --- a/lib/fake_braintree/credit_card.rb +++ b/lib/fake_braintree/credit_card.rb @@ -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) @@ -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 @@ -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') diff --git a/lib/fake_braintree/helpers.rb b/lib/fake_braintree/helpers.rb index 8709dcc..d13eaca 100644 --- a/lib/fake_braintree/helpers.rb +++ b/lib/fake_braintree/helpers.rb @@ -1,5 +1,6 @@ require 'digest/md5' require 'active_support/gzip' +require 'securerandom' module FakeBraintree module Helpers @@ -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 diff --git a/lib/fake_braintree/sinatra_app.rb b/lib/fake_braintree/sinatra_app.rb index 5c54982..b449c09 100644 --- a/lib/fake_braintree/sinatra_app.rb +++ b/lib/fake_braintree/sinatra_app.rb @@ -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 diff --git a/spec/fake_braintree/credit_card_spec.rb b/spec/fake_braintree/credit_card_spec.rb index 1843050..c688b8c 100644 --- a/spec/fake_braintree/credit_card_spec.rb +++ b/spec/fake_braintree/credit_card_spec.rb @@ -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