Class: SolidusBolt::Gateway

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_bolt/gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gateway

Returns a new instance of Gateway.



5
# File 'app/models/solidus_bolt/gateway.rb', line 5

def initialize(options = {}); end

Instance Method Details

#authorize(_amount, payment_source, gateway_options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/solidus_bolt/gateway.rb', line 7

def authorize(_amount, payment_source, gateway_options)
  order_number = gateway_options[:order_id].split('-').first
  order = fetch_spree_order(order_number)

  authorization_response = ::SolidusBolt::Transactions::AuthorizeService.call(
    order: order,
    create_bolt_account: payment_source.,
    credit_card: credit_card_params(payment_source),
    payment_method: payment_source.payment_method,
    repeat: payment_source.card_id.present?
  )

  unless payment_source.card_id
    payment_source.update(card_id: authorization_response['transaction']['from_credit_card']['id'])
  end

  ActiveMerchant::Billing::Response.new(true, 'Transaction approved', payment_source.attributes,
    authorization: authorization_response['transaction']['reference'])
rescue ServerError => e
  ActiveMerchant::Billing::Response.new(false, e, {})
end

#capture(float_amount, response_code, gateway_options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/solidus_bolt/gateway.rb', line 29

def capture(float_amount, response_code, gateway_options)
  payment = gateway_options[:originator]
  payment_source = payment.source
  payment_method = payment.payment_method
  currency = gateway_options[:currency]

  capture_response = SolidusBolt::Transactions::CaptureService.call(
    transaction_reference: response_code,
    amount: float_amount,
    currency: currency,
    payment_method: payment_method
  )

  ActiveMerchant::Billing::Response.new(
    true,
    'Transaction Captured', payment_source.attributes,
    authorization: capture_response['reference']
  )
rescue ServerError => e
  ActiveMerchant::Billing::Response.new(false, e, {})
end

#credit(amount, response_code, _gateway_options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/solidus_bolt/gateway.rb', line 70

def credit(amount, response_code, _gateway_options)
  payment = Spree::Payment.find_by(response_code: response_code)
  payment_source = payment.source
  payment_method = payment.payment_method
  currency = payment.currency

  credit_response = SolidusBolt::Transactions::RefundService.call(
    transaction_reference: response_code,
    amount: amount,
    currency: currency,
    payment_method: payment_method
  )

  ActiveMerchant::Billing::Response.new(
    true,
    'Transaction Refunded', payment_source.attributes,
    authorization: credit_response['reference']
  )
rescue ServerError => e
  ActiveMerchant::Billing::Response.new(false, e, {})
end

#purchase(_float_amount, payment_source, gateway_options) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/solidus_bolt/gateway.rb', line 92

def purchase(_float_amount, payment_source, gateway_options)
  order_number = gateway_options[:order_id].split('-').first
  order = fetch_spree_order(order_number)

  authorization_response = ::SolidusBolt::Transactions::AuthorizeService.call(
    order: order,
    create_bolt_account: payment_source.,
    credit_card: credit_card_params(payment_source),
    payment_method: payment_source.payment_method,
    auto_capture: true,
    repeat: payment_source.card_id.present?
  )

  unless payment_source.card_id
    payment_source.update(card_id: authorization_response['transaction']['from_credit_card']['id'])
  end

  ActiveMerchant::Billing::Response.new(true, 'Transaction approved and captured', payment_source.attributes,
    authorization: authorization_response['transaction']['reference'])
rescue ServerError => e
  ActiveMerchant::Billing::Response.new(false, e, {})
end

#void(response_code, gateway_options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/solidus_bolt/gateway.rb', line 51

def void(response_code, gateway_options)
  payment = gateway_options[:originator]
  payment_source = payment.source
  payment_method = payment.payment_method

  void_response = SolidusBolt::Transactions::VoidService.call(
    transaction_reference: response_code,
    payment_method: payment_method
  )

  ActiveMerchant::Billing::Response.new(
    true,
    'Transaction Void', payment_source.attributes,
    authorization: void_response['reference']
  )
rescue ServerError => e
  ActiveMerchant::Billing::Response.new(false, e, {})
end