Class: Gateway::PaywayV2

Inherits:
PaymentMethod
  • Object
show all
Defined in:
app/models/spree/gateway/payway_v2.rb

Constant Summary collapse

%w[open_both open_deeplink_only open_checkout_url_only].freeze
PAYMENT_OPTIONS =
%w[abapay_khqr_deeplink abapay_khqr cards wechat alipay].freeze

Instance Method Summary collapse

Instance Method Details

#abapay_khqr_deeplink?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/spree/gateway/payway_v2.rb', line 31

def abapay_khqr_deeplink?
  preferred_payment_option == 'abapay_khqr_deeplink'
end

#authorize(_amount, _source, gateway_options = {}) ⇒ Object

override authorize is used when pre auth enabled



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

def authorize(_amount, _source, gateway_options = {})
  _, payment_number = gateway_options[:order_id].split('-')
  payment = Spree::Payment.find_by(number: payment_number)

  checker = check_transaction(payment)
  payment.update(transaction_response: checker.json_response)

  params = { check: { response: checker.json_response } }

  if checker.success?
    ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Authorized', params)
  else
    ActiveMerchant::Billing::Response.new(false, 'Payway Gateway: Authorization Failed', params)
  end
end

#auto_capture?Boolean

override authorize payment if pre-auth is enabled, otherwise purchase / complete immediately.

Returns:

  • (Boolean)


73
74
75
# File 'app/models/spree/gateway/payway_v2.rb', line 73

def auto_capture?
  !enable_pre_auth?
end

#cancel(_response_code, _payment) ⇒ Object

override



154
155
156
# File 'app/models/spree/gateway/payway_v2.rb', line 154

def cancel(_response_code, _payment)
  ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Payment has been canceled.')
end

#capture(_amount, _response_code, gateway_options) ⇒ Object

override



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/spree/gateway/payway_v2.rb', line 117

def capture(_amount, _response_code, gateway_options)
  _, payment_number = gateway_options[:order_id].split('-')
  payment = Spree::Payment.find_by(number: payment_number)

  success = true
  params = {}

  success, params[:pre_auth] = complete_pre_auth(payment) if enable_pre_auth?
  success, params[:payout] = confirm_payouts(payment) if success && payout_total_from_response(payment).present?

  if success
    ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Captured', params)
  else
    ActiveMerchant::Billing::Response.new(false, 'Payway Gateway: Capture Failed', params)
  end
end

#check_transaction(payment) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/spree/gateway/payway_v2.rb', line 158

def check_transaction(payment)
  # ABA requires the use of the v2 API, but it does not yet support payout responses.
  # Until v2 fully supports payouts, we fall back to v1 when payouts are enabled.
  # Otherwise, we use v2 by default.
  checker = if payment.preload_payout_ids.any?
              Vpago::PaywayV2::TransactionStatus.new(payment)
            else
              Vpago::PaywayV2::TransactionStatusV2.new(payment)
            end
  checker.call
  checker
end

#default_payout_profileObject

override



44
45
46
# File 'app/models/spree/gateway/payway_v2.rb', line 44

def default_payout_profile
  Spree::PayoutProfiles::PaywayV2.default
end

#enable_payout?Boolean

override

Returns:

  • (Boolean)


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

def enable_payout?
  default_payout_profile.present? && default_payout_profile.receivable?
end

#enable_pre_auth?Boolean

override

Returns:

  • (Boolean)


67
68
69
# File 'app/models/spree/gateway/payway_v2.rb', line 67

def enable_pre_auth?
  self[:enable_pre_auth]
end

#method_typeObject

override: partial to render in admin



27
28
29
# File 'app/models/spree/gateway/payway_v2.rb', line 27

def method_type
  'payway_v2'
end

#open_abapay_deeplink?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/spree/gateway/payway_v2.rb', line 35

def open_abapay_deeplink?
  preferred_abapay_khqr_deeplink_option.in?(%w[open_both open_deeplink_only])
end

#open_checkout_url?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/spree/gateway/payway_v2.rb', line 39

def open_checkout_url?
  preferred_abapay_khqr_deeplink_option.in?(%w[open_both open_checkout_url_only])
end

#payment_source_classObject

override



49
50
51
# File 'app/models/spree/gateway/payway_v2.rb', line 49

def payment_source_class
  Spree::VpagoPaymentSource
end

#purchase(_amount, _source, gateway_options = {}) ⇒ Object

override purchase is used when pre auth disabled



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/spree/gateway/payway_v2.rb', line 97

def purchase(_amount, _source, gateway_options = {})
  _, payment_number = gateway_options[:order_id].split('-')
  payment = Spree::Payment.find_by(number: payment_number)

  checker = check_transaction(payment)
  payment.update(transaction_response: checker.json_response)

  success = checker.success?
  params = {}

  success, params[:payout] = confirm_payouts(payment) if success && payout_total_from_response(payment).present?

  if success
    ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Purchased', params)
  else
    ActiveMerchant::Billing::Response.new(false, 'Payway Gateway: Purchasing Failed', params)
  end
end

#reviewing_mode?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/spree/gateway/payway_v2.rb', line 22

def reviewing_mode?
  preferred_reviewing_mode
end

#support_payout?Boolean

override

Returns:

  • (Boolean)


54
55
56
# File 'app/models/spree/gateway/payway_v2.rb', line 54

def support_payout?
  preferred_payment_option.in?(%w[abapay_khqr abapay_khqr_deeplink])
end

#support_pre_auth?Boolean

override

Returns:

  • (Boolean)


59
# File 'app/models/spree/gateway/payway_v2.rb', line 59

def support_pre_auth? = true

#void(_response_code, gateway_options) ⇒ Object

override



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/spree/gateway/payway_v2.rb', line 135

def void(_response_code, gateway_options)
  _, payment_number = gateway_options[:order_id].split('-')
  payment = Spree::Payment.find_by(number: payment_number)

  if enable_pre_auth?
    params = {}
    success, params[:pre_auth] = cancel_pre_auth(payment)

    if success
      ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Pre-authorization successfully canceled.', params)
    else
      ActiveMerchant::Billing::Response.new(false, 'Payway Gateway: Failed to cancel pre-authorization.', params)
    end
  else
    ActiveMerchant::Billing::Response.new(true, 'Payway Gateway: Payment has been voided.')
  end
end