mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Use service in admin/payments#create
This separates logic for bang and non-bang versions of Spree::Order#next. The different conditions used in both methods (state == "completed" vs order.completed?) have implications in whether a transition is attempted or not.
This commit is contained in:
@@ -19,10 +19,8 @@ Spree::Admin::PaymentsController.class_eval do
|
||||
|
||||
redirect_to admin_order_payments_path(@order)
|
||||
else
|
||||
#This is the first payment (admin created order)
|
||||
until @order.completed?
|
||||
@order.next!
|
||||
end
|
||||
AdvanceOrderService.new(@order).call(true)
|
||||
|
||||
flash[:success] = Spree.t(:new_order_completed)
|
||||
redirect_to edit_admin_order_url(@order)
|
||||
end
|
||||
|
||||
@@ -5,15 +5,31 @@ class AdvanceOrderService
|
||||
@order = order
|
||||
end
|
||||
|
||||
def call
|
||||
shipping_method_id = @order.shipping_method.id if @order.shipping_method.present?
|
||||
def call(raise_on_error = false)
|
||||
shipping_method_id = order.shipping_method.id if order.shipping_method.present?
|
||||
options = { shipping_method_id: shipping_method_id }
|
||||
raise_on_error ? advance_order!(options) : advance_order(options)
|
||||
end
|
||||
|
||||
while @order.state != "complete"
|
||||
break unless @order.next
|
||||
private
|
||||
|
||||
if @order.state == "delivery"
|
||||
@order.select_shipping_method(shipping_method_id) if shipping_method_id.present?
|
||||
end
|
||||
def advance_order(options)
|
||||
until order.state == "complete"
|
||||
break unless order.next
|
||||
after_transition_hook(options)
|
||||
end
|
||||
end
|
||||
|
||||
def advance_order!(options)
|
||||
until order.completed?
|
||||
order.next!
|
||||
after_transition_hook(options)
|
||||
end
|
||||
end
|
||||
|
||||
def after_transition_hook(options)
|
||||
if order.state == "delivery"
|
||||
order.select_shipping_method(options[:shipping_method_id]) if options[:shipping_method_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,9 +6,27 @@ describe Spree::Admin::PaymentsController, type: :controller do
|
||||
let!(:order) { create(:order, distributor: shop, state: 'complete') }
|
||||
let!(:line_item) { create(:line_item, order: order, price: 5.0) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
end
|
||||
|
||||
context "#create" do
|
||||
let!(:payment_method) { create(:payment_method, distributors: [ shop ]) }
|
||||
let!(:order) do
|
||||
create(:order_with_totals_and_distribution, distributor: shop, state: "payment")
|
||||
end
|
||||
|
||||
let(:params) { { amount: order.total, payment_method_id: payment_method.id } }
|
||||
|
||||
it "advances the order state" do
|
||||
expect {
|
||||
spree_post :create, payment: params, order_id: order.number
|
||||
}.to change { order.reload.state }.from("payment").to("complete")
|
||||
end
|
||||
end
|
||||
|
||||
context "as an enterprise user" do
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { user }
|
||||
order.reload.update_totals
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user