Class: SolidusAfterpay::Gateway

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

Constant Summary collapse

VOIDABLE_STATUSES =
['AUTH_APPROVED', 'PARTIALLY_CAPTURED'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gateway

Returns a new instance of Gateway.



9
10
11
12
13
14
15
16
# File 'app/models/solidus_afterpay/gateway.rb', line 9

def initialize(options)
  ::Afterpay.configure do |config|
    config.merchant_id = options[:merchant_id]
    config.secret_key = options[:secret_key]
    config.server = 'https://global-api-sandbox.afterpay.com/' if options[:test_mode]
    config.user_agent = SolidusAfterpay::UserAgentGenerator.new(merchant_id: options[:merchant_id]).generate
  end
end

Instance Method Details

#authorize(amount, payment_source, gateway_options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/solidus_afterpay/gateway.rb', line 18

def authorize(amount, payment_source, gateway_options)
  result = {}

  unless payment_source.payment_method.auto_capture
    response = ::Afterpay::API::Payment::Auth.call(
      payment: ::Afterpay::Components::Payment.new(
        token: payment_source.token,
        amount: ::Afterpay::Components::Money.new(
          amount: Money.from_cents(amount).amount.to_s,
          currency: gateway_options[:currency]
        )
      )
    )
    result = response.body
  end

  ActiveMerchant::Billing::Response.new(true, 'Transaction approved', result, authorization: result[:id])
rescue ::Afterpay::BaseError => e
  ::Afterpay::API::Payment::Reversal.call(token: payment_source.token) if e.is_a?(::Afterpay::RequestTimeoutError)

  message = e.message
  error_code = e.error_code
  if message == 'Afterpay::PaymentRequiredError'
    message = I18n.t('solidus_afterpay.payment_declined')
    error_code = 'payment_declined'
  end
  ActiveMerchant::Billing::Response.new(false, message, {}, error_code: error_code)
end

#capture(amount, response_code, gateway_options) ⇒ Object



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

def capture(amount, response_code, gateway_options)
  payment_method = gateway_options[:originator].payment_method

  response = if payment_method.auto_capture
               immediate_capture(amount, response_code, gateway_options)
             else
               deferred_capture(amount, response_code, gateway_options)
             end
  result = response.body

  if result.status != 'APPROVED'
    raise ::Afterpay::BaseError.new('payment_declined'), I18n.t('solidus_afterpay.payment_declined')
  end

  ActiveMerchant::Billing::Response.new(true, 'Transaction captured', result, authorization: result.id)
rescue ::Afterpay::BaseError => e
  if e.is_a?(::Afterpay::RequestTimeoutError)
    payment_source = gateway_options[:originator].payment_source
    ::Afterpay::API::Payment::Reversal.call(token: payment_source.token)
  end

  ActiveMerchant::Billing::Response.new(false, e.message, {}, error_code: e.error_code)
end

#create_checkout(order, gateway_options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/solidus_afterpay/gateway.rb', line 121

def create_checkout(order, gateway_options)
  response = ::Afterpay::API::Order::Create.call(
    order: SolidusAfterpay::OrderComponentBuilder.new(
      order: order,
      mode: gateway_options[:mode],
      redirect_confirm_url: gateway_options[:redirect_confirm_url],
      redirect_cancel_url: gateway_options[:redirect_cancel_url],
      popup_origin_url: gateway_options[:popup_origin_url]
    ).call
  )
  result = response.body

  ActiveMerchant::Billing::Response.new(true, 'Checkout created', result)
rescue ::Afterpay::BaseError => e
  ActiveMerchant::Billing::Response.new(false, e.message, {}, error_code: e.error_code)
end

#credit(amount, response_code, gateway_options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/solidus_afterpay/gateway.rb', line 78

def credit(amount, response_code, gateway_options)
  response = ::Afterpay::API::Payment::Refund.call(
    order_id: response_code,
    refund: ::Afterpay::Components::Refund.new(
      amount: ::Afterpay::Components::Money.new(
        amount: Money.from_cents(amount).amount.to_s,
        currency: gateway_options[:originator].payment.currency
      ),
      merchant_reference: gateway_options[:originator].payment.id
    )
  )
  result = response.body

  ActiveMerchant::Billing::Response.new(true, "Transaction Credited with #{amount}", result,
    authorization: result.refundId)
rescue ::Afterpay::BaseError => e
  ActiveMerchant::Billing::Response.new(false, e.message, {}, error_code: e.error_code)
end

#find_order(token:) ⇒ Object



144
145
146
147
148
# File 'app/models/solidus_afterpay/gateway.rb', line 144

def find_order(token:)
  ::Afterpay::API::Order::Find.call(token: token).body
rescue ::Afterpay::BaseError
  nil
end

#find_payment(order_id:) ⇒ Object



138
139
140
141
142
# File 'app/models/solidus_afterpay/gateway.rb', line 138

def find_payment(order_id:)
  ::Afterpay::API::Payment::Find.call(order_id: order_id).body
rescue ::Afterpay::BaseError
  nil
end

#purchase(amount, payment_source, gateway_options) ⇒ Object



71
72
73
74
75
76
# File 'app/models/solidus_afterpay/gateway.rb', line 71

def purchase(amount, payment_source, gateway_options)
  result = authorize(amount, payment_source, gateway_options)
  return result unless result.success?

  capture(amount, result.authorization, gateway_options)
end

#retrieve_configurationObject



150
151
152
153
154
# File 'app/models/solidus_afterpay/gateway.rb', line 150

def retrieve_configuration
  ::Afterpay::API::Configuration::Retrieve.call.body
rescue ::Afterpay::BaseError
  nil
end

#void(response_code, gateway_options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/solidus_afterpay/gateway.rb', line 97

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

  if payment_method.auto_capture
    return ActiveMerchant::Billing::Response.new(false, "Transaction can't be voided", {},
      error_code: 'void_not_allowed')
  end

  response = ::Afterpay::API::Payment::Void.call(
    order_id: response_code,
    payment: ::Afterpay::Components::Payment.new(
      amount: ::Afterpay::Components::Money.new(
        amount: gateway_options[:originator].amount.to_s,
        currency: gateway_options[:currency]
      )
    )
  )
  result = response.body

  ActiveMerchant::Billing::Response.new(true, 'Transaction voided', result, authorization: result.id)
rescue ::Afterpay::BaseError => e
  ActiveMerchant::Billing::Response.new(false, e.message, {}, error_code: e.error_code)
end