Class: Spree::PaymentMethod::BogusCreditCard

Inherits:
CreditCard show all
Defined in:
app/models/spree/payment_method/bogus_credit_card.rb

Direct Known Subclasses

SimpleBogusCreditCard

Constant Summary collapse

TEST_VISA =
['4111111111111111', '4012888888881881', '4222222222222']
TEST_MC =
['5500000000000004', '5555555555554444', '5105105105105100']
TEST_AMEX =
['378282246310005', '371449635398431', '378734493671000', '340000000000009']
TEST_DISC =
['6011000000000004', '6011111111111117', '6011000990139424']
VALID_CCS =
['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten
AUTHORIZATION_CODE =
'12345'
FAILURE_MESSAGE =
'Bogus Gateway: Forced failure'
SUCCESS_MESSAGE =
'Bogus Gateway: Forced success'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from CreditCard

#partial_name, #payment_source_class, #reusable_sources, #reusable_sources_by_order, #supports?

Methods inherited from Spree::PaymentMethod

#auto_capture?, find_sti_class, #gateway, model_name, #options, #partial_name, #payment_source_class, #reusable_sources, #source_required?, #store_credit?, #supports?

Methods included from Spree::Preferences::StaticallyConfigurable

#preference_source=, #preferences, #preferences=

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Attribute Details

#testObject

Returns the value of attribute test.


16
17
18
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 16

def test
  @test
end

Instance Method Details

#actionsObject


78
79
80
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 78

def actions
  %w(capture void credit)
end

#authorize(_money, credit_card, _options = {}) ⇒ Object


30
31
32
33
34
35
36
37
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 30

def authorize(_money, credit_card, _options = {})
  profile_id = credit_card.gateway_customer_profile_id
  if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-'))
    ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'D' })
  else
    ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, { message: FAILURE_MESSAGE }, test: true)
  end
end

#capture(_money, authorization, _gateway_options) ⇒ Object


52
53
54
55
56
57
58
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 52

def capture(_money, authorization, _gateway_options)
  if authorization == '12345'
    ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true)
  else
    ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, error: FAILURE_MESSAGE, test: true)
  end
end

#create_profile(payment) ⇒ Object


22
23
24
25
26
27
28
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 22

def create_profile(payment)
  return if payment.source.has_payment_profile?
  # simulate the storage of credit card profile using remote service
  if success = VALID_CCS.include?(payment.source.number)
    payment.source.update(gateway_customer_profile_id: generate_profile_id(success))
  end
end

#credit(_money, _credit_card, _response_code, _options = {}) ⇒ Object


48
49
50
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 48

def credit(_money, _credit_card, _response_code, _options = {})
  ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE)
end

#gateway_classObject


18
19
20
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 18

def gateway_class
  self.class
end

#payment_profiles_supported?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 74

def payment_profiles_supported?
  true
end

#purchase(_money, credit_card, _options = {}) ⇒ Object


39
40
41
42
43
44
45
46
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 39

def purchase(_money, credit_card, _options = {})
  profile_id = credit_card.gateway_customer_profile_id
  if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-'))
    ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'M' })
  else
    ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, message: FAILURE_MESSAGE, test: true)
  end
end

#test?Boolean

Returns:

  • (Boolean)

69
70
71
72
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 69

def test?
  # Test mode is not really relevant with bogus gateway (no such thing as live server)
  true
end

#try_void(_payment) ⇒ Object


65
66
67
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 65

def try_void(_payment)
  ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE)
end

#void(_response_code, _credit_card, _options = {}) ⇒ Object


60
61
62
# File 'app/models/spree/payment_method/bogus_credit_card.rb', line 60

def void(_response_code, _credit_card, _options = {})
  ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE)
end