Module: FakeBraintree

Defined in:
lib/fake_braintree.rb,
lib/fake_braintree/address.rb,
lib/fake_braintree/helpers.rb,
lib/fake_braintree/version.rb,
lib/fake_braintree/customer.rb,
lib/fake_braintree/redirect.rb,
lib/fake_braintree/credit_card.rb,
lib/fake_braintree/sinatra_app.rb,
lib/fake_braintree/transaction.rb,
lib/fake_braintree/client_token.rb,
lib/fake_braintree/subscription.rb,
lib/fake_braintree/payment_method.rb,
lib/fake_braintree/merchant_account.rb,
lib/fake_braintree/valid_credit_cards.rb,
lib/fake_braintree/credit_card_serializer.rb

Defined Under Namespace

Modules: Helpers Classes: Address, ClientToken, CreditCard, CreditCardSerializer, Customer, MerchantAccount, PaymentMethod, Redirect, Registry, Server, SinatraApp, Subscription, Transaction

Constant Summary collapse

VERSION =
'0.8.0'
VALID_CREDIT_CARDS =
%w(4111111111111111 4005519200000004
4009348888881881 4012000033330026
4012000077777777 4012888888881881
4217651111111119 4500600000000061
5555555555554444 378282246310005
371449635398431  6011111111111117
3530111333300000)

Class Method Summary collapse

Class Method Details

.activate!(options = {}) ⇒ Object

Public: Prepare FakeBraintree for use and start the API server.

options: Hash options to configure (default: {}):

:gateway_port - The port to start the API server on (optional).
                If not given, an ephemeral port will be used.


19
20
21
22
23
24
25
# File 'lib/fake_braintree.rb', line 19

def self.activate!(options = {})
  initialize_registry
  self.verify_all_cards = false
  clear!
  set_configuration
  boot_server(port: options.fetch(:gateway_port, nil))
end

.clear!Object



31
32
33
34
35
# File 'lib/fake_braintree.rb', line 31

def self.clear!
  self.registry.clear!
  self.decline_all_cards = false
  clear_log!
end

.clear_log!Object



37
38
39
40
# File 'lib/fake_braintree.rb', line 37

def self.clear_log!
  FileUtils.mkdir_p(File.dirname(log_file_path))
  File.new(log_file_path, 'w').close
end

.create_failureObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fake_braintree.rb', line 64

def self.create_failure
  {
    'message' => 'Do Not Honor',
    'verification' => {
      'status' => 'processor_declined',
      'processor_response_text' => 'Do Not Honor',
      'processor_response_code' => '2000'
    },
    'errors' => { 'errors' => [] },
    'params' => {}
  }
end

.decline_all_cards!Object



77
78
79
# File 'lib/fake_braintree.rb', line 77

def self.decline_all_cards!
  self.decline_all_cards = true
end

.decline_all_cards?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/fake_braintree.rb', line 81

def self.decline_all_cards?
  decline_all_cards
end

.failure?(card_number) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/fake_braintree.rb', line 42

def self.failure?(card_number)
  registry.failure?(card_number)
end

.failure_response(card_number = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fake_braintree.rb', line 46

def self.failure_response(card_number = nil)
  failure = registry.failures[card_number] || {}
  failure['errors'] ||= { 'errors' => [] }

  {
    'message' => failure['message'],
    'verification' => {
      'status' => failure['status'],
      'processor_response_text' => failure['message'],
      'processor_response_code' => failure['code'],
      'gateway_rejection_reason' => 'cvv',
      'cvv_response_code' => failure['code']
    },
    'errors' => failure['errors'],
    'params' => {}
  }
end

.generate_transaction(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fake_braintree.rb', line 89

def self.generate_transaction(options = {})
  history_item = {
   'timestamp' => Time.now,
   'amount' => options[:amount],
   'status' => options[:status]
  }
  created_at = options[:created_at] || Time.now
  {
    'status_history' => [history_item],
    'subscription_id' => options[:subscription_id],
    'created_at' => created_at,
    'amount' => options[:amount]
  }
end

.log_file_pathObject



27
28
29
# File 'lib/fake_braintree.rb', line 27

def self.log_file_path
  'tmp/log'
end

.verify_all_cards!Object



85
86
87
# File 'lib/fake_braintree.rb', line 85

def self.verify_all_cards!
  self.verify_all_cards = true
end