Skip to content

Commit

Permalink
Remove call to authorize payments in confirm
Browse files Browse the repository at this point in the history
Payments are authorized when an order is completed as part of the
process! method in Spree::Payment::Processing. We want to wait until
then to authorize the payments to prevent any issues where an
amount is authorized and then the cart is abandoned or possible extra
authorizations.

Instead of authorizing we want to move the order to the confirm state
unless it is already in the confirm state. Previously this was setting
the payment amount to be the authorized amount but we are no longer
pre-authorizing. The payment amount will now be the full order amount.
  • Loading branch information
AlistairNorman committed Jun 23, 2020
1 parent e02828f commit 0f59584
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 208 deletions.
9 changes: 5 additions & 4 deletions app/controllers/spree/affirm_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ def confirm
if affirm_checkout.save!
payment = order.payments.create!({
payment_method_id: affirm_params[:payment_method_id],
source: affirm_checkout
source: affirm_checkout,
amount: order.total
})
hook = SolidusAffirm::Config.callback_hook.new
hook.authorize!(payment)
redirect_to hook.after_authorize_url(order)

order.next! unless order.state == "confirm"
redirect_to checkout_state_path(order.state)
end
end
end
Expand Down
8 changes: 0 additions & 8 deletions lib/solidus_affirm/callback_hook/base.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
module SolidusAffirm
module CallbackHook
class Base
def authorize!(payment)
payment.process!
authorized_affirm = Affirm::Charge.find(payment.response_code)
payment.amount = authorized_affirm.amount / 100.0
payment.save!
payment.order.next! if payment.order.payment?
end

def after_authorize_url(order)
order_state_checkout_path(order)
end
Expand Down
133 changes: 0 additions & 133 deletions spec/fixtures/vcr_casettes/callback_hook_authorize_success.yml

This file was deleted.

61 changes: 0 additions & 61 deletions spec/lib/solidus_affirm/callback_hook/base_spec.rb

This file was deleted.

38 changes: 36 additions & 2 deletions spec/requests/affirm_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,48 @@
end

it "redirect to the confirm page" do
VCR.use_cassette 'callback_hook_authorize_success' do
post '/affirm/confirm', params: {
checkout_token: checkout_token,
payment_method_id: payment_method.id,
order_id: order.id,
use_route: :spree
}
expect(response).to redirect_to('/checkout/confirm')
end

it "sets the payment total to the order total" do
post '/affirm/confirm', params: {
checkout_token: checkout_token,
payment_method_id: payment_method.id,
order_id: order.id,
use_route: :spree
}
expect(order.payments.last.amount).to eq(order.total)
end

it "moves the order to its next state" do
expect {
post '/affirm/confirm', params: {
checkout_token: checkout_token,
payment_method_id: payment_method.id,
order_id: order.id,
use_route: :spree
}
expect(response).to redirect_to('/checkout/confirm')
}.to change { order.reload.state }.from("payment").to("confirm")
end

context "the order is already in confirm state" do
let(:order) { create(:order_with_totals, state: "confirm") }

it "moves the order to its next state" do
expect {
post '/affirm/confirm', params: {
checkout_token: checkout_token,
payment_method_id: payment_method.id,
order_id: order.id,
use_route: :spree
}
}.not_to change { order.reload.state }
end
end
end
Expand Down

0 comments on commit 0f59584

Please sign in to comment.