Merge pull request #7315 from Matt-Yorkley/double-checkout

Double checkout
This commit is contained in:
Andy Brett
2021-04-20 10:44:32 -07:00
committed by GitHub
6 changed files with 62 additions and 25 deletions

View File

@@ -1,16 +0,0 @@
# frozen_string_literal: true
# This controller (and respective route in the Spree engine)
# is only needed for the spree_paypal_express gem that redirects here explicitly.
#
# According to the rails docs it would be possible to redirect
# to CheckoutController directly in the routes
# with a slash like "to: '/checkout#edit'", but it does not work in this case.
module Spree
class CheckoutController < ::BaseController
def edit
flash.keep
redirect_to main_app.checkout_path
end
end
end

View File

@@ -93,7 +93,7 @@ module Spree
format.html do
if params.key?(:checkout)
@order.next_transition.run_callbacks if @order.cart?
redirect_to checkout_state_path(@order.checkout_steps.first)
redirect_to main_app.checkout_state_path(@order.checkout_steps.first)
elsif @order.complete?
redirect_to order_path(@order)
else

View File

@@ -27,11 +27,11 @@ module Spree
redirect_to provider.express_checkout_url(pp_response, useraction: 'commit')
else
flash[:error] = Spree.t('flash.generic_error', scope: 'paypal', reasons: pp_response.errors.map(&:long_message).join(" "))
redirect_to spree.checkout_state_path(:payment)
redirect_to main_app.checkout_state_path(:payment)
end
rescue SocketError
flash[:error] = Spree.t('flash.connection_failed', scope: 'paypal')
redirect_to spree.checkout_state_path(:payment)
redirect_to main_app.checkout_state_path(:payment)
end
end
@@ -57,7 +57,7 @@ module Spree
session[:order_id] = nil
redirect_to completion_route(@order)
else
redirect_to checkout_state_path(@order.state)
redirect_to main_app.checkout_state_path(@order.state)
end
end

View File

@@ -68,8 +68,9 @@ Openfoodnetwork::Application.routes.draw do
resources :webhooks, only: [:create]
end
get '/checkout', :to => 'checkout#edit' , :as => :checkout
put '/checkout', :to => 'checkout#update' , :as => :update_checkout
get '/checkout', to: 'checkout#edit' , as: :checkout
put '/checkout', to: 'checkout#update' , as: :update_checkout
get '/checkout/:state', to: 'checkout#edit', as: :checkout_state
get '/checkout/paypal_payment/:order_id', to: 'checkout#paypal_payment', as: :paypal_payment
get 'embedded_shopfront/shopfront_session', to: 'application#shopfront_session'

View File

@@ -170,7 +170,6 @@ Spree::Core::Engine.routes.draw do
resources :products
# Used by spree_paypal_express
get '/checkout/:state', :to => 'checkout#edit', :as => :checkout_state
get '/content/cvv', :to => 'content#cvv', :as => :cvv
get '/content/*path', :to => 'content#show', :as => :content
get '/paypal', :to => "paypal#express", :as => :paypal_express

View File

@@ -4,13 +4,13 @@ require 'spec_helper'
module Spree
describe PaypalController, type: :controller do
context 'when cancelling' do
context '#cancel' do
it 'redirects back to checkout' do
expect(spree_get(:cancel)).to redirect_to checkout_path
end
end
context 'when confirming' do
context '#confirm' do
let(:previous_order) { controller.current_order(true) }
let(:payment_method) { create(:payment_method) }
@@ -44,6 +44,59 @@ module Spree
expect(order.payments.count).to eq 0
end
end
context "when order completion fails" do
before do
allow(previous_order).to receive(:complete?).and_return(false)
end
it "redirects to checkout state path" do
expect(spree_post(:confirm, payment_method_id: payment_method.id)).
to redirect_to checkout_state_path(:cart)
end
end
end
describe "#express" do
let(:order) { create(:order_with_distributor) }
let(:response) { true }
let(:provider_success_url) { "https://test.com/success" }
let(:response_mock) { double(:response, success?: response, errors: [] ) }
let(:provider_mock) { double(:provider, build_set_express_checkout: true,
set_express_checkout: response_mock,
express_checkout_url: provider_success_url) }
before do
allow(controller).to receive(:current_order) { order }
allow(controller).to receive(:provider) { provider_mock }
allow(controller).to receive(:express_checkout_request_details) { {} }
end
context "when processing is successful" do
it "redirects to a success URL generated by the payment provider" do
expect(spree_post :express).to redirect_to provider_success_url
end
end
context "when processing fails" do
let(:response) { false }
it "redirects to checkout_state_path with a flash error" do
expect(spree_post :express).to redirect_to checkout_state_path(:payment)
expect(flash[:error]).to eq "PayPal failed. "
end
end
context "when a SocketError is encountered during processing" do
before do
allow(response_mock).to receive(:success?).and_raise(SocketError)
end
it "redirects to checkout_state_path with a flash error" do
expect(spree_post :express).to redirect_to checkout_state_path(:payment)
expect(flash[:error]).to eq "Could not connect to PayPal."
end
end
end
describe '#expire_current_order' do