Class: Spree::Gateway::Bogus

Inherits:
Spree::Gateway show all
Defined in:
app/models/spree/gateway/bogus.rb

Constant Summary collapse

TEST_VISA =
['4111111111111111', '4012888888881881', '4222222222222']
TEST_MC =
['5500000000000004', '5555555555554444', '5105105105105100', '2223000010309703']
TEST_AMEX =
['378282246310005', '371449635398431', '378734493671000', '340000000000009']
TEST_DISC =
['6011000000000004', '6011111111111117', '6011000990139424']
VALID_CCS =
['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten

Constants inherited from Spree::Gateway

FROM_DOLLAR_TO_CENT_RATE

Constants included from DisplayOn

DisplayOn::DISPLAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::Gateway

#disable_customer_profile, #exchange_multiplier, #gateway_dashboard_payment_url, #method_missing, #method_type, #options, #provider, #reusable_sources, #sources_by_order, #supports?

Methods inherited from PaymentMethod

#auto_capture?, #available_for_order?, #available_for_store?, #default_name, find_with_destroyed, #method_type, #parse_webhook_event, #payment_icon_name, #payment_setup_session_class, providers, #public_preferences, #reusable_sources, #source_required?, #store_credit?, #supports?, #webhook_url

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spree::Gateway

Instance Attribute Details

#testObject

Returns the value of attribute test.



10
11
12
# File 'app/models/spree/gateway/bogus.rb', line 10

def test
  @test
end

Instance Method Details

#actionsObject



147
148
149
# File 'app/models/spree/gateway/bogus.rb', line 147

def actions
  %w(capture void credit)
end

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



32
33
34
35
36
37
38
39
# File 'app/models/spree/gateway/bogus.rb', line 32

def authorize(_money, credit_card, _options = {})
  profile_id = credit_card.gateway_customer_profile_id
  if VALID_CCS.include?(credit_card.number) || (profile_id&.starts_with?('BGS-'))
    Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'D' })
  else
    Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true)
  end
end

#cancel(_response_code, _payment = nil) ⇒ Object



66
67
68
# File 'app/models/spree/gateway/bogus.rb', line 66

def cancel(_response_code, _payment = nil)
  Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

#capture(_money, authorization, _gateway_options) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/spree/gateway/bogus.rb', line 54

def capture(_money, authorization, _gateway_options)
  if authorization == '12345'
    Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true)
  else
    Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true)
  end
end

#complete_payment_session(payment_session:, params: {}) ⇒ Object



115
116
117
# File 'app/models/spree/gateway/bogus.rb', line 115

def complete_payment_session(payment_session:, params: {})
  payment_session.complete
end

#complete_payment_setup_session(setup_session:, params: {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/spree/gateway/bogus.rb', line 133

def complete_payment_setup_session(setup_session:, params: {})
  credit_card = CreditCard.create!(
    user: setup_session.customer,
    payment_method: self,
    name: 'Bogus Card',
    last_digits: '4242',
    month: '12',
    year: 1.year.from_now.year.to_s,
    gateway_customer_profile_id: "BGS-#{Array.new(6) { rand(6) }.join}"
  )
  setup_session.update!(payment_source: credit_card)
  setup_session.complete
end

#confirmation_required?Boolean

Returns:



75
76
77
# File 'app/models/spree/gateway/bogus.rb', line 75

def confirmation_required?
  true
end

#create_payment_session(order:, amount: nil, external_data: {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/spree/gateway/bogus.rb', line 95

def create_payment_session(order:, amount: nil, external_data: {})
  payment_session_class.create(
    order: order,
    payment_method: self,
    amount: amount.presence || order.total_minus_store_credits,
    currency: order.currency,
    status: 'pending',
    external_id: "bogus_#{SecureRandom.hex(12)}",
    external_data: external_data.merge('client_secret' => "bogus_secret_#{SecureRandom.hex(8)}"),
    customer: order.user
  )
end

#create_payment_setup_session(customer:, external_data: {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'app/models/spree/gateway/bogus.rb', line 123

def create_payment_setup_session(customer:, external_data: {})
  payment_setup_sessions.create(
    customer: customer,
    status: 'pending',
    external_id: "bogus_seti_#{SecureRandom.hex(12)}",
    external_client_secret: "bogus_seti_secret_#{SecureRandom.hex(8)}",
    external_data: external_data
  )
end

#create_profile(payment) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/models/spree/gateway/bogus.rb', line 23

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



50
51
52
# File 'app/models/spree/gateway/bogus.rb', line 50

def credit(_money, _credit_card, _response_code, _options = {})
  Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

#payment_profiles_supported?Boolean

Returns:



79
80
81
# File 'app/models/spree/gateway/bogus.rb', line 79

def payment_profiles_supported?
  true
end

#payment_session_classObject



91
92
93
# File 'app/models/spree/gateway/bogus.rb', line 91

def payment_session_class
  PaymentSessions::Bogus
end

#payment_source_classObject



83
84
85
# File 'app/models/spree/gateway/bogus.rb', line 83

def payment_source_class
  CreditCard
end

#provider_classObject



15
16
17
# File 'app/models/spree/gateway/bogus.rb', line 15

def provider_class
  self.class
end

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



41
42
43
44
45
46
47
48
# File 'app/models/spree/gateway/bogus.rb', line 41

def purchase(_money, credit_card, _options = {})
  profile_id = credit_card.gateway_customer_profile_id
  if VALID_CCS.include?(credit_card.number) || (profile_id&.starts_with?('BGS-'))
    Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'M' })
  else
    Spree::PaymentResponse.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)
  end
end

#session_required?Boolean

Returns:



87
88
89
# File 'app/models/spree/gateway/bogus.rb', line 87

def session_required?
  true
end

#setup_session_supported?Boolean

Returns:



119
120
121
# File 'app/models/spree/gateway/bogus.rb', line 119

def setup_session_supported?
  true
end

#show_in_admin?Boolean

Returns:



19
20
21
# File 'app/models/spree/gateway/bogus.rb', line 19

def show_in_admin?
  false
end

#test?Boolean

Returns:



70
71
72
73
# File 'app/models/spree/gateway/bogus.rb', line 70

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

#update_payment_session(payment_session:, amount: nil, external_data: {}) ⇒ Object



108
109
110
111
112
113
# File 'app/models/spree/gateway/bogus.rb', line 108

def update_payment_session(payment_session:, amount: nil, external_data: {})
  attrs = {}
  attrs[:amount] = amount if amount.present?
  attrs[:external_data] = payment_session.external_data.merge(external_data) if external_data.present?
  payment_session.update(attrs)
end

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



62
63
64
# File 'app/models/spree/gateway/bogus.rb', line 62

def void(_response_code, _credit_card, _options = {})
  Spree::PaymentResponse.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: 'void-12345')
end