Skip to content

Commit

Permalink
Merge pull request #228 from bookmebus/227-refactor-pre-auth-for-othe…
Browse files Browse the repository at this point in the history
…r-payment-method

close #227 refactor pre auth for other payment method
  • Loading branch information
channainfo authored Dec 3, 2024
2 parents d5ceb60 + 36c3b81 commit 782bc95
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 23 deletions.
10 changes: 0 additions & 10 deletions app/helpers/vpago/admin/base_helper_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ def available_payment_icons
]
end

def available_transaction_types
%w[
purchase
pre-auth
]
end

def preference_field_for(form, field, options)
case field
when 'preferred_acleda_type'
Expand All @@ -36,10 +29,7 @@ def preference_field_for(form, field, options)
return form.select(:preferred_acleda_payment_card, acleda_payment_card_options, {}, class: 'fullwidth select2')
when 'preferred_icon_name'
return form.select(:preferred_icon_name, available_payment_icons, {}, class: 'fullwidth select2')
when 'preferred_transaction_type'
return form.select(:preferred_transaction_type, available_transaction_types, {}, class: 'fullwidth select2')
end

super
end
end
Expand Down
1 change: 0 additions & 1 deletion app/models/spree/gateway/payway_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Gateway::PaywayV2 < PaymentMethod
preference :deeplink_scheme, :string # eg. 'bookmeplus', 'vtenh'
preference :transaction_fee_fix, :string
preference :transaction_fee_percentage, :string
preference :transaction_type, :string # 'purchase', 'pre-auth'
preference :public_key, :text

# Only enable one-click payments if spree_auth_devise is installed
Expand Down
24 changes: 13 additions & 11 deletions app/models/vpago/payment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ def payment_url
"#{ENV.fetch('DEFAULT_URL_HOST', nil)}/payway_v2_card_popups?payment_number=#{number}"
end

def pre_auth?
return false unless payment_method.type_payway_v2?

payment_method.preferred_transaction_type == 'pre-auth'
end

# COMPLETED, CANCELLED
def pre_auth_status
pre_auth_response['transaction_status']
Expand All @@ -84,17 +78,25 @@ def pre_auth_cancelled?
end

def capture_pre_auth
return if !pre_auth? || pre_auth_completed?
return if !enable_pre_auth? || pre_auth_completed?

Vpago::PaywayV2::PreAuthCompleter.new(self).call
pre_auth_service.capture_pre_auth(self)
end

def cancel_pre_auth
return if !pre_auth? || pre_auth_cancelled?
return if !enable_pre_auth? || pre_auth_cancelled?

pre_auth_service.cancel_pre_auth(self)
end

def pre_auth_service
payment_method.pre_auth_service
end

Vpago::PaywayV2::PreAuthCanceler.new(self).call
def enable_pre_auth?
payment_method.enable_pre_auth?
end
end
end

Spree::Payment.prepend(Vpago::PaymentDecorator)
Spree::Payment.prepend(Vpago::PaymentDecorator)
12 changes: 12 additions & 0 deletions app/models/vpago/payment_method_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def support_payout?
true
end

def support_pre_auth?
type_payway_v2?
end

def default_payout_profile
Spree::PayoutProfiles::PaywayV2.default
end
Expand Down Expand Up @@ -81,6 +85,14 @@ def type_payway_v2?
def type_wingsdk?
type == Spree::PaymentMethod::TYPE_WINGSDK
end

def pre_auth_service
if type_payway_v2?
Vpago::PaywayV2::PreAuthHandler.new
else
raise NotImplementedError, 'Pre-auth is not supported for this gateway'
end
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- insert_after "[data-hook='active']" -->

<% if @payment_method.support_pre_auth? %>
<div class="form-group">
<strong><%= Spree.t(:enable_pre_auth) %></strong>

<div class="form-check my-2">
<%= radio_button :payment_method, :enable_pre_auth, true, id: 'payment_method_enable_pre_auth_true', class: 'form-check-input' %>
<%= label_tag 'payment_method_enable_pre_auth_true', Spree.t(:say_yes), class: 'form-check-label' %>
</div>

<div class="form-check my-2">
<%= radio_button :payment_method, :enable_pre_auth, false, id: 'payment_method_enable_pre_auth_false', class: 'form-check-input' %>
<%= label_tag 'payment_method_enable_pre_auth_false', Spree.t(:say_no), class: 'form-check-label' %>
</div>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/services/vpago/payway_v2/pre_auth_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Vpago
module PaywayV2
class PreAuthHandler
def capture_pre_auth(payment)
Vpago::PaywayV2::PreAuthCompleter.new(payment).call
end

def cancel_pre_auth(payment)
Vpago::PaywayV2::PreAuthCanceler.new(payment).call
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEnablePreAuthToSpreePaymentMethod < ActiveRecord::Migration[7.0]
def change
add_column :spree_payment_methods, :enable_pre_auth, :boolean, default: false, if_not_exists: true
end
end
2 changes: 1 addition & 1 deletion lib/vpago/payway_v2/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def req_time
end

def type
@payment.payment_method.preferences[:transaction_type].presence || 'purchase'
@payment.payment_method.enable_pre_auth ? 'pre-auth' : 'purchase'
end

def host
Expand Down

0 comments on commit 782bc95

Please sign in to comment.