Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close #202 recheck processings payment #203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ def self.prepended(base)
base.skip_before_action :ensure_order, only: [:request_update_payment]
end

# :order_token, :payment_number
# :order_token, :payment_number, :ignore_on_failed
def request_update_payment
# this action is mostly called after order is completed.
# spree_current_order will be nil in this case, so we need to manual find.
order = find_order_by_token(params[:order_token])
ignore_on_failed = params[:ignore_on_failed] == true

if order.paid?
render_serialized_payload { serialize_resource(order) }
else
payment = find_payment(order, params[:payment_number])
context = payment.request_update
context = payment.request_update(ignore_on_failed: ignore_on_failed)

if context.success? && context.error_message.blank?
render_serialized_payload { serialize_resource(order) }
Expand All @@ -27,6 +28,22 @@ def request_update_payment
end
end

# :order_token, :ignore_on_failed
def request_update_all_payments
order = find_order_by_token(params[:order_token])
ignore_on_failed = params[:ignore_on_failed] == true

return render_serialized_payload { serialize_resource(order) } if order.paid?

# check if there is a completed payment
order.payments.processing.each do |payment|
context = payment.request_update(ignore_on_failed: ignore_on_failed)
return render_serialized_payload { serialize_resource(order) } if context.success? && payment.completed?
end

head :unprocessable_entity
end

def payment_redirect
payments = spree_current_order.payments
raise ActiveRecord::RecordNotFound if payments.blank?
Expand Down Expand Up @@ -62,7 +79,6 @@ def find_payment(order, payment_number)

def find_order_by_token(token)
order = Spree::Order.find_by(token: token)

raise ActiveRecord::RecordNotFound if order.nil?
raise ActiveRecord::RecordNotFound if order.payments.blank?

Expand Down
5 changes: 3 additions & 2 deletions app/models/vpago/payment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def self.prepended(base)
base.after_create -> { Vpago::PayoutsGenerator.new(self).call }, if: :should_generate_payouts?
base.after_update :capture_pre_auth, if: :state_changed_to_complete?
base.after_update :cancel_pre_auth, if: :state_changed_to_failed?
base.scope :processing, -> { where(state: 'processing') }
end

def state_changed_to_complete?
Expand Down Expand Up @@ -44,8 +45,8 @@ def build_source
process! if order.completed?
end

def request_update
updater = payment_method.payment_request_updater.new(self, { ignore_on_failed: true })
def request_update(ignore_on_failed: true)
updater = payment_method.payment_request_updater.new(self, { ignore_on_failed: ignore_on_failed })
updater.call
updater
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
namespace :storefront do
resource :checkout, controller: :checkout do
get :payment_redirect
patch :request_update_all_payments
patch :request_update_payment
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/vpago/payway_v2/transaction_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TransactionStatus < Base
# 3 – Declined
# 4 – Refunded
# 5 – Wrong Hash
# 6 - tran_id not found
# 7 - Cancelled
# 11 – Other Server-side Error
def call
Expand Down
Loading