Reddeem VINE voucher when firing "capture_and_complete_order"o

'Spree::Payment#capture_and_complete!' will try to complete the order,
so we want to redeem any VINE voucher associated with the order first.
This commit is contained in:
Gaetan Craig-Riou
2024-10-28 16:07:35 +11:00
committed by Rachel Arnould
parent afb336d789
commit 9f3da1af4f
3 changed files with 82 additions and 6 deletions

View File

@@ -79,7 +79,7 @@ Lint/UselessMethodDefinition:
Exclude:
- 'app/models/spree/gateway.rb'
# Offense count: 23
# Offense count: 24
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes, Max.
Metrics/AbcSize:
Exclude:
@@ -172,11 +172,12 @@ Metrics/ClassLength:
- 'lib/reporting/reports/enterprise_fee_summary/scope.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
# Offense count: 31
# Offense count: 32
# Configuration parameters: AllowedMethods, AllowedPatterns, Max.
Metrics/CyclomaticComplexity:
Exclude:
- 'app/controllers/admin/enterprises_controller.rb'
- 'app/controllers/spree/admin/payments_controller.rb'
- 'app/controllers/spree/orders_controller.rb'
- 'app/helpers/checkout_helper.rb'
- 'app/helpers/order_cycles_helper.rb'

View File

@@ -54,6 +54,10 @@ module Spree
event = params[:e]
return unless event && @payment.payment_source
# capture_and_complete_order will complete the order, so we want to try to redeem VINE
# voucher first and exit if it fails
return if event == "capture_and_complete_order" && !redeem_vine_voucher
# Because we have a transition method also called void, we do this to avoid conflicts.
event = "void_transaction" if event == "void"
if allowed_events.include?(event) && @payment.public_send("#{event}!")

View File

@@ -79,10 +79,7 @@ RSpec.describe Spree::Admin::PaymentsController, type: :request do
let(:vine_voucher_redeemer) { instance_double(VineVoucherRedeemerService) }
before do
# Adding voucher to the order
vine_voucher.create_adjustment(vine_voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
add_voucher_to_order(vine_voucher, order)
allow(VineVoucherRedeemerService).to receive(:new).and_return(vine_voucher_redeemer)
end
@@ -264,6 +261,74 @@ RSpec.describe Spree::Admin::PaymentsController, type: :request do
expect(flash[:error]).to eq "Could not update the payment"
end
end
context "with a VINE voucher", feature: :connected_apps do
let(:vine_voucher) {
create(:voucher_flat_rate, voucher_type: "VINE", code: 'some_code',
enterprise: order.distributor, amount: 6)
}
let(:vine_voucher_redeemer) { instance_double(VineVoucherRedeemerService) }
before do
add_voucher_to_order(vine_voucher, order)
allow(VineVoucherRedeemerService).to receive(:new).and_return(vine_voucher_redeemer)
end
it "completes the order and redirects to payment page" do
expect(vine_voucher_redeemer).to receive(:call).and_return(true)
put(
"/admin/orders/#{order.number}/payments/#{order.payments.first.id}/" \
"fire?e=capture_and_complete_order",
params: {},
headers:
)
expect(response).to redirect_to(spree.admin_order_payments_url(order))
expect(flash[:success]).to eq "Payment Updated"
expect(order.reload.state).to eq "complete"
end
context "when redeeming the voucher fails" do
it "redirect to payments page" do
allow(vine_voucher_redeemer).to receive(:call).and_return(false)
allow(vine_voucher_redeemer).to receive(:errors).and_return(
{ redeeming_failed: "Redeeming the voucher failed" }
)
put(
"/admin/orders/#{order.number}/payments/#{order.payments.first.id}/" \
"fire?e=capture_and_complete_order",
params: {},
headers:
)
expect(response).to redirect_to(spree.admin_order_payments_url(order))
expect(flash[:error]).to match "Redeeming the voucher failed"
end
end
context "when an other error happens" do
it "redirect to payments page" do
allow(vine_voucher_redeemer).to receive(:call).and_return(false)
allow(vine_voucher_redeemer).to receive(:errors).and_return(
{ vine_api: "There was an error communicating with the API" }
)
put(
"/admin/orders/#{order.number}/payments/#{order.payments.first.id}/" \
"fire?e=capture_and_complete_order",
params: {},
headers:
)
expect(response).to redirect_to(spree.admin_order_payments_url(order))
expect(flash[:error]).to match "There was an error while trying to redeem your voucher"
end
end
end
end
context "when something unexpected happen" do
@@ -298,4 +363,10 @@ RSpec.describe Spree::Admin::PaymentsController, type: :request do
end
end
end
def add_voucher_to_order(voucher, order)
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
end
end