Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Storefront: new checkout experience
Damian Legawiecf00b7ef11ed5
diff --git a/storefront/app/views/layouts/spree/checkout.html.erb b/storefront/app/views/layouts/spree/checkout.html.erbnew file mode 100644index 0000000000..738672a32d--- /dev/null+++ b/storefront/app/views/layouts/spree/checkout.html.erb@@ -0,0 +1,58 @@+<% content_for(:head) do %>+ <%= render 'spree/shared/google_places_api_loader' %>+ <meta name="robots" content="noindex, nofollow">+<% end %>++<!doctype html>+<html lang="<%= I18n.locale %>">+ <head>+ <%= render 'spree/shared/head' %>+ </head>+ <body id="checkout-page" class="w-full bg-background text-text">+ <%= render_storefront_partials(:body_start_partials) %>+ <%= current_store.storefront_custom_code_body_start&.html_safe %>++ <div id="overlay" class="overlay"></div>++ <div class="w-full min-h-screen text-muted bg-background" id="checkout-container">+ <header+ class="checkout-mobile-header lg:hidden relative z-[999] bg-background tailwind-container text-center py-3 mx-auto"+ id="checkout-mobile-header"+ data-turbo-permanent>+ <%= render 'spree/shared/logo', logo: current_header_logo, logo_css: 'ml-5' %>+ </header>+ <div class="grid min-h-screen checkout-container">+ <div class="flex flex-col items-end" id="checkout-main">+ <div class="checkout-main-container">+ <header+ class="mb-0 hidden lg:block"+ id="checkout-desktop-header"+ data-turbo-permanent>+ <%= render 'spree/shared/logo', logo: current_header_logo %>+ </header>+ <%= turbo_frame_tag :flash do %>+ <%= render('spree/shared/flashes') %>+ <% end %>+ <div id="checkout">+ <%= yield %>+ </div>+ </div>+ <footer+ class="mt-5"+ id="checkout-footer"+ data-turbo-permanent>+ <%= render 'spree/checkout/footer' %>+ </footer>+ </div>+ <div class="flex flex-col items-start col-span-2" id="checkout-summary">+ <div class="checkout-summary-container">+ <%= render 'spree/checkout/sidebar' %>+ </div>+ </div>+ </div>+ </div>++ <%= current_store.storefront_custom_code_body_end&.html_safe %>+ <%= render_storefront_partials(:body_end_partials) %>+ </body>+</html>diff --git a/storefront/app/views/spree/checkout/_address.html.erb b/storefront/app/views/spree/checkout/_address.html.erbnew file mode 100644index 0000000000..86e13b79d2--- /dev/null+++ b/storefront/app/views/spree/checkout/_address.html.erb@@ -0,0 +1,153 @@+<%= render 'spree/checkout/quick_checkout' %>++<% address_type = 'shipping' %>+<% address_name = "#{address_type[0...4]}_address" %>++<% if user_available_addresses.present? %>+ <div data-controller="checkout-address-book">+ <%= turbo_frame_tag :checkout_address_book do %>+ <div class="mb-4" data-checkout-address-book-target="list">+ <div id="<%= address_type %>">+ <h5 class="font-body">+ <%= Spree.t(address_type + '_address') %>+ </h5>+ <ul class="select_address list-group mb-5 border mt-3 rounded-md border-default">+ <% user_available_addresses.each_with_index do |address, idx| %>+ <li class="p-0 m-0 border-b border-default" id="<%= [address_type, dom_id(address)].join('_') %>">+ <% checked = (address.id == @order.ship_address_id || ([email protected]_address_id && idx == 0)) %>+ <div class="custom-control custom-radio px-5 py-4 flex items-center">+ <%= radio_button_tag "order_#{address_name}_id", address.id, checked, class: 'custom-control-input radio-input mt-1 mr-5', data: { action: 'checkout-address-book#select' } %>+ <label class="custom-control-label d-block select-none cursor-pointer max-w-[calc(100%-58px)]" for="<%= "order_#{address_name}_id_#{address.id}" %>">+ <%= turbo_frame_tag dom_id(address) do %>+ <%= render 'spree/shared/address', address: address %>+ <div class="address-book-actions mt-3 <% unless checked %>hidden<% end %>">+ <%= link_to Spree.t(:edit), spree.edit_address_path(address, checkout: @order.token), class: 'text-dark' %>+ </div>+ <% end %>+ </label>+ </div>+ </li>+ <% end %>+ <li class="list-group-item p-0 m-0">+ <div class="custom-control custom-radio px-5 py-4 flex items-center">+ <%= radio_button_tag "order_#{address_name}_id", 0, false, class: 'custom-control-input radio-input mr-5', data: { action: 'checkout-address-book#select' } %>+ <label class="custom-control-label d-block select-none cursor-pointer w-full max-w-[calc(100%-58px)]" for="<%= "order_#{address_name}_id_0" %>">+ <%= Spree.t(:new_address, scope: :address_book) %>+ <div class="mt-4 hidden" data-checkout-address-book-target="newAddressForm">+ <%= turbo_frame_tag :new_address do %>+ <%= form_for try_spree_current_user.addresses.build(country: current_store.default_country), html: { id: 'checkout_form_address' } do |f| %>+ <%= render 'spree/addresses/form',+ address_name: 'address',+ address_form: f,+ address_type: 'shipping',+ address: @address %>+ <%= hidden_field_tag :order_token, @order.token %>+ <%= hidden_field_tag "default_shipping", true %>+ <%= hidden_field_tag "default_billing", true %>+ <%= f.submit t(:save, scope: :address_book), class: 'btn btn-primary btn-block spree-btn mb-5' %>+ <% end %>+ <% end %>+ </div>+ </label>+ </label>+ </li>+ </ul>+ </div>+ </div>+ <% end %>++ <%= form_for @order, url: spree.update_checkout_path(@order.token, @order.state), html: { id: "checkout_form_#{@order.state}" } do |form| %>+ <div>+ <%= form.hidden_field :state_lock_version %>+ <%= form.hidden_field :ship_address_id, data: { 'checkout-address-book-target': 'addressId' } %>+ </div>+ <div>+ <%= button_tag Spree.t(:save_and_continue),+ data: {+ 'checkout-address-book-target': 'submit',+ 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false)+ },+ class: 'btn btn-primary font-weight-bold w-full lg:w-48 float-right checkout-content-save-continue-button',+ disabled: @order.ship_address_id.nil? %>+ </div>+ <% end %>+ </div>+<% else %>+ <%= form_for @order, url: spree.update_checkout_path(@order.token, @order.state), html: { id: "checkout_form_#{@order.state}" } do |form| %>+ <div class="mb-5">+ <%= form.hidden_field :state_lock_version %>++ <% if !try_spree_current_user || try_spree_current_user.email.blank? %>+ <div class="grid grid-cols-1 lg:grid-cols-2 mb-3 font-body">+ <h5 class="font-body">+ <%= Spree.t(:contact_information) %>+ </h5>+ <% unless already_have_an_account? %>+ <div class="lg:text-right">+ <span class="text-gray-600 text-sm">+ <%= Spree.t(:already_have_an_account) %> <span class="text-primary"><%= link_to Spree.t(:login), spree_login_path, data: { turbo_frame: '_top' } %></span>+ </span>+ </div>+ <% end %>+ </div>+ <div class="form-group has-float-label mb-3">+ <%= form.email_field :email, class: 'w-full text-input', required: true, placeholder: Spree.t(:email), id: 'order_ship_address_attributes_email' %>+ <label for="order_ship_address_attributes_email"><%= Spree.t(:email) %></label>+ </div>+ <% end %>+ <div class="py-2">+ <div class="flex items-center gap-2">+ <%= form.check_box :accept_marketing, class: 'checkbox-input' %>+ <%= form.label :accept_marketing, class: 'cursor-pointer text-sm select-none' do %>+ <%= Spree.t('newsletter_subscription.join') %>+ <% end %>+ </div>++ <% if !try_spree_current_user && !already_have_an_account? %>+ <div class="flex items-center gap-2 mt-2">+ <%= form.check_box :signup_for_an_account, class: 'checkbox-input' %>+ <%= form.label :signup_for_an_account, class: 'cursor-pointer text-sm select-none' do %>+ <%= Spree.t(:signup_for_an_account) %>+ <% end %>+ </div>+ <% end %>+ </div>+ <% unless try_spree_current_user %>+ <% if already_have_an_account? %>+ <div class="py-2 text-sm">+ It seems you already have an account with us. Please+ <%= link_to Spree.t(:login), spree_login_path, class: 'text-primary font-semibold' %> to continue.+ </div>+ <% end %>+ <% end %>++ <div class="mb-4" data-controller="checkout-address-book" data-checkout-address-book-target="list">+ <div id="<%= address_type %>">+ <h5 class="mt-5 mb-4 font-body">+ <%= Spree.t(address_type + '_address') %>+ </h5>+ <%= form.fields_for address_name do |address_form| %>+ <div class="inner checkout-content-inner" data-hook='<%= "#{address_type}_inner" %>'>+ <%= render partial: 'spree/addresses/form', locals: {+ address_name: address_name,+ address_form: address_form,+ address_type: address_type,+ address: Spree::Address.new(country: current_store.default_country, user: try_spree_current_user),+ form: form+ } %>+ </div>+ <% end %>+ </div>+ </div>++ <%= hidden_field_tag 'save_user_address', true, data: { hook: "save_user_address" } %>+ </div>+ <div class="flex justify-end w-full">+ <%= button_tag Spree.t(:save_and_continue),+ class: 'btn-primary w-full lg:w-2/5 font-semibold checkout-content-save-continue-button my-5 !py-4',+ data: {+ 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false)+ } %>+ </div>+ <% end %>+<% end %>diff --git a/storefront/app/views/spree/checkout/_button_processing.html.erb b/storefront/app/views/spree/checkout/_button_processing.html.erbnew file mode 100644index 0000000000..b3ccd5190f--- /dev/null+++ b/storefront/app/views/spree/checkout/_button_processing.html.erb@@ -0,0 +1,7 @@+<div class="<%= 'hidden' if is_hidden %> spinner flex justify-center">+ <svg class="animate-spin h-5 w-5 mr-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">+ <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>+ <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>+ </svg>+ <%= Spree.t(:processing) %>...+</div>diff --git a/storefront/app/views/spree/checkout/_coupon_code.html.erb b/storefront/app/views/spree/checkout/_coupon_code.html.erbnew file mode 100644index 0000000000..37d2f6a3d2--- /dev/null+++ b/storefront/app/views/spree/checkout/_coupon_code.html.erb@@ -0,0 +1,55 @@+<%= turbo_frame_tag :checkout_coupon_code, class: "shopping-cart-coupon-code #{local_assigns[:classes]} flex flex-col gap-3" do %>+ <% if promotion.nil? && [email protected]_card %>+ <%= form_tag spree.checkout_apply_coupon_code_path(@order.token), method: :patch, data: {controller: "checkout-promotions"} do %>+ <div class="flex" data-controller="enable-button">+ <%= hidden_field_tag :promotion_email, @order.email, data: {checkout_promotions_target: 'promotionEmailField'} %>+ <%= text_field_tag :coupon_code, params[:coupon_code], class: 'w-full text-input',+ placeholder: Spree.t('cart_page.add_promo_code').capitalize,+ aria: { label: Spree.t('cart_page.add_promo_code') },+ data: { 'enable-button-target': 'input' },+ required: true %>+ <%= button_tag 'Apply', class: 'min-h-full ml-3 btn-primary !px-4 !py-3', data: { 'enable-button-target': 'button' }, disabled: true %>+ </div>+ <% end %>+ <% elsif promotion.present? %>+ <%= form_tag spree.checkout_remove_coupon_code_path(@order.token), method: :delete do %>+ <%= hidden_field_tag :coupon_code, promotion.code_for_order(@order) %>+ <div class="m-0">+ <div class="flex items-center justify-between text-sm">+ <span class="max-w-[250px] truncate flex items-center p-2 rounded bg-gray-200">+ <%= heroicon('tag', options: { class: 'h-4 w-4 mr-2' }) %>+ <%= promotion.name_for_order(@order) %>+ </span>+ <%= button_tag(class: 'btn btn-sm p-1 ml-2', id: 'remove-promotion') do %>+ <%= heroicon('x-mark', options: { variant: :solid, class: 'h-5 w-5' }) %>+ <% end %>+ </div>+ </div>+ <% end %>+ <% elsif @order.gift_card %>+ <%= form_tag spree.checkout_remove_coupon_code_path(@order.token), method: :delete do %>+ <%= hidden_field_tag :gift_card, @order.gift_card.code %>+ <div class="m-0">+ <div class="flex items-center justify-between text-sm">+ <span class="max-w-[250px] truncate flex items-center p-2 rounded bg-gray-200">+ <%= heroicon('gift', options: { class: 'h-4 w-4 mr-2' }) %>+ <%= @order.gift_card.code.upcase %>+ </span>+ <%= button_tag(class: 'btn btn-sm p-1 ml-2', id: 'remove-promotion') do %>+ <%= heroicon('x-mark', options: { variant: :solid, class: 'h-5 w-5' }) %>+ <% end %>+ </div>+ </div>+ <% end %>+ <% end %>++ <% if local_assigns[:result] && action_name == 'apply_coupon_code' %>+ <% if result.error.blank? %>+ <div class="alert-success p-2 text-xs rounded"><%= result.success %></div>+ <% else %>+ <div class="text-red-500 !text-md text-xs ">+ <span><%= result.error %></span>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/storefront/app/views/spree/checkout/_credit_card.html.erb b/storefront/app/views/spree/checkout/_credit_card.html.erbnew file mode 100644index 0000000000..48d00c1646--- /dev/null+++ b/storefront/app/views/spree/checkout/_credit_card.html.erb@@ -0,0 +1,12 @@+<li class="p-0 m-0 border-b" id="<%= spree_dom_id(card) %>">+ <div class="custom-control custom-radio px-5 py-4 flex items-center">+ <label class="flex flex-row items-center cursor-pointer">+ <%= radio_button_tag "order[existing_card]", card.gateway_payment_profile_id, index == 0, { data: {action: "checkout-credit-card-book#select" }, class: "mr-5" } %>+ <div class="flex space-x-4 items-center">+ <%= payment_method_icon_tag(card.cc_type) %>+ <span>****<%= card.last_digits %></span>+ <span><%= card.month.to_s.rjust(2, '0') %>/<%= card.year %></span>+ </div>+ </label>+ </div>+</li>diff --git a/storefront/app/views/spree/checkout/_delivery.html.erb b/storefront/app/views/spree/checkout/_delivery.html.erbnew file mode 100644index 0000000000..3ee188726d--- /dev/null+++ b/storefront/app/views/spree/checkout/_delivery.html.erb@@ -0,0 +1,70 @@+<%= form_for @order, url: spree.update_checkout_path(@order.token, @order.state), data: { controller: 'checkout-delivery' } do |form| %>+ <div class="mb-5">+ <%= turbo_frame_tag :checkout_lock_version do %>+ <%= hidden_field_tag 'order[state_lock_version]', @order.state_lock_version %>+ <% end %>+ <div id="shipping-method">+ <% if @order.backordered_variants.any? %>+ <div class="alert rounded border border-default text-sm p-5 mb-6">+ Some products in your cart will be dispatched a bit later than the rest of your order:+ <ul class="list-disc list-inside flex-col flex space-y-1 mt-2">+ <% @order.backordered_variants.each do |variant| %>+ <li><%= variant.name %></li>+ <% end %>+ </ul>+ </div>+ <% end %>++ <div id="methods">+ <%= form.fields_for :shipments do |ship_form| %>+ <div class="shipment">+ <h5 class="checkout-content-header mb-3 font-body">+ <%= Spree.t('checkout_page.delivery_method').capitalize %>+ <% if ship_form.object.stock_location.present? %>+ <%= Spree.t(:from).downcase %>+ <strong><%= ship_form.object.stock_location.name %></strong>+ <% end %>+ </h5>+ <ul class="rounded-md list-group mb-4 border border-default border-b-0 text-sm border-default" data-checkout-delivery-target="shippingList">+ <% ship_form.object.shipping_rates.includes(:shipping_method, :tax_rate).each do |rate| %>+ <li class="list-group-item p-0 m-0 border-b delivery-list-item border-default" data-checkout-delivery-target="shippingRate">+ <div class="custom-control custom-radio flex items-center px-5 py-4">+ <%= ship_form.radio_button :selected_shipping_rate_id,+ rate.id,+ class: 'custom-control-input radio-input',+ id: "shipping-rate-#{rate.id}",+ data: {+ action: 'click->checkout-delivery#update',+ cost: rate.display_cost,+ tax: rate.display_tax_amount+ } %>+ <label class="select-none cursor-pointer px-2 word-break w-full" for="<%= "shipping-rate-#{rate.id}" %>">+ <%= rate.name %>+ <% if rate.delivery_range %>+ <span class="text-gray-600 text-xs ml-3"><%= rate.display_delivery_range %></span>+ <% end %>+ </label>+ <span class="text-right">+ <%= rate.cost <= 0 ? Spree.t(:free) : rate.display_cost %>+ </span>+ </div>+ </li>+ <% end %>+ </ul>+ </div>+ <% end %>+ </div>+ </div>+ </div>+ <div class="flex justify-end w-full mt-5">+ <div class="flex justify-end w-full">+ <%= button_tag Spree.t(:save_and_continue),+ class: 'btn-primary w-full lg:w-2/5 font-semibold checkout-content-save-continue-button my-5 !py-4',+ data: { action: 'click->checkout-delivery#disableShippingRates',+ 'checkout-delivery-target': 'submit',+ 'turbo-submits-with': (render 'button_processing', is_hidden: false)+ } %>+ </div>+ </div>+<% end %>+diff --git a/storefront/app/views/spree/checkout/_line_item.html.erb b/storefront/app/views/spree/checkout/_line_item.html.erbnew file mode 100644index 0000000000..78c2a7e16f--- /dev/null+++ b/storefront/app/views/spree/checkout/_line_item.html.erb@@ -0,0 +1,26 @@+<div class="flex justify-between items-center">+ <div class="flex items-center">+ <div class="mr-5 w-[64px] h-[64px] relative rounded bg-accent-100">+ <span class="rounded-full text-xs text-center font-bold pl-[0.5px] py-1 w-6 text-sidebar-text bg-background absolute -right-2 -top-3">+ <%= line_item.quantity %>+ </span>+ <% image = line_item.variant.default_image %>+ <% if image.present? && image.attached? && image.variable? %>+ <%= image_tag main_app.cdn_image_url(image.variant(spree_image_variant_options(resize_to_fill: [128, 128]))), class: 'rounded border border-default bg-transparent object-cover object-center', loading: :lazy, width: 64, height: 64 %>+ <% end %>+ </div>+ <div class="col-6 col-md-8 pr-3 text-sm">+ <p class="font-bold word-break"><%= line_item.name %></p>+ <p class="text-xs"><%= line_item.options_text %></p>+ </div>+ </div>+ <div class="font-semibold text-sm text-right">+ <% if should_display_compare_at_price?(line_item.variant) %>+ <span class="line-through text-red-500">+ <%= Spree::Money.new(line_item.variant.compare_at_amount_in(line_item.currency) * line_item.quantity, currency: line_item.currency) %>+ </span>+ <br />+ <% end %>+ <%= line_item.display_pre_tax_amount %>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/_line_items.html.erb b/storefront/app/views/spree/checkout/_line_items.html.erbnew file mode 100644index 0000000000..bd1fca361b--- /dev/null+++ b/storefront/app/views/spree/checkout/_line_items.html.erb@@ -0,0 +1,22 @@+<%= turbo_frame_tag :checkout_line_items, id: 'line_items', target: '_top' do %>+ <% cache spree_base_cache_scope.call(order.line_items.cache_key_with_version) do %>+ <div class="overflow-y-auto line-items flex flex-col gap-5 pt-5 md:pt-0" data-checkout-summary-target="line_items">+ <% if order.vendors.any? || order.splitted? %>+ <% order.all_line_items.group_by(&:vendor).each do |vendor, line_items| %>+ <div class="flex flex-col gap-4">+ <% if vendor.present? %>+ <p class="font-normal text-sm">+ <%= Spree.t(:fulfilled_by) %> <span class="font-semibold"><%= vendor.name %></span>+ </p>+ <% end %>+ <div class="flex flex-col gap-3">+ <%= render collection: line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>+ </div>+ </div>+ <% end %>+ <% else %>+ <%= render collection: order.line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>+ <% end %>+ </div>+ <% end %>+<% end %>diff --git a/storefront/app/views/spree/checkout/_missing_all_line_items.html.erb b/storefront/app/views/spree/checkout/_missing_all_line_items.html.erbnew file mode 100644index 0000000000..10ef2cfb2d--- /dev/null+++ b/storefront/app/views/spree/checkout/_missing_all_line_items.html.erb@@ -0,0 +1,17 @@+<div data-controller="modal" data-modal-opened-value="true" data-modal-allow-background-close="false">+ <div data-modal-target="container" data-action="click->modal#closeBackground keyup@window->modal#closeWithKeyboard" class="hidden animated fadeIn fixed inset-0 overflow-y-auto flex items-center justify-center" style="z-index: 9999;">+ <div class="max-h-screen w-full max-w-lg relative">+ <div class="m-1 bg-white rounded shadow">+ <div class="p-8">+ <h2 class="text-xl mb-4"><%= Spree.t(:shipping_not_available) %></h2>+ <p class="mb-4"><%= Spree.t('errors.messages.no_shipping_methods_available') %> </p>++ <div class="flex justify-end items-center flex-wrap mt-6">+ <%= link_to 'Return to cart', spree.cart_path, class: 'mx-4 py-4', target: :_top %>+ <%= link_to 'Close', checkout_state_path(@order.token, @order.state), class: 'btn-primary font-semibold checkout-content-save-continue-button my-5 ml-4 !py-4', target: :_top %>+ </div>+ </div>+ </div>+ </div>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/_missing_line_items.html.erb b/storefront/app/views/spree/checkout/_missing_line_items.html.erbnew file mode 100644index 0000000000..4da1095434--- /dev/null+++ b/storefront/app/views/spree/checkout/_missing_line_items.html.erb@@ -0,0 +1,41 @@+<div data-controller="modal" data-modal-opened-value="true" data-modal-allow-background-close="false">+ <div data-modal-target="container" data-action="click->modal#closeBackground keyup@window->modal#closeWithKeyboard" class="hidden animated fadeIn fixed inset-0 overflow-y-auto flex items-center justify-center" style="z-index: 9999;">+ <div class="max-h-screen w-full max-w-lg relative">+ <div class="m-1 bg-white rounded shadow">+ <div class="p-8">+ <h2 class="text-xl mb-4"><%= Spree.t(:shipping_not_available) %></h2>+ <p class="mb-4">+ <%= Spree.t('errors.messages.invalid_line_items', count: @order.invalid_line_items.size) %>+ </p>++ <div class="mb-5 overflow-y-auto pt-3 mx-3 lg:mx-8 lg:mt-4 lg:pt-6 line-items" data-checkout-summary-target="line_items">+ <% if @order.vendors.any? || @order.splitted? %>+ <% @order.invalid_line_items.group_by(&:vendor).each do |vendor, line_items| %>+ <div class="mb-5">+ <% if vendor.present? %>+ <h5 class="py-3 font-normal text-sm mb-2">+ <%= Spree.t(:fulfilled_by) %> <span class="font-semibold"><%= vendor.name %></span>+ </h5>+ <% end %>+ <%= render collection: line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>+ </div>+ <% end %>+ <% else %>+ <%= render collection: @order.invalid_line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>+ <% end %>+ </div>++ <div class="flex justify-end items-center flex-wrap mt-6">+ <%= link_to 'return to cart', spree.cart_path, class: 'mx-4 py-4', target: :_top %>+ <%= button_to 'Continue to checkout',+ spree.checkout_remove_missing_items_path(@order.token, @order.state, line_item_ids: @order.invalid_line_items.map(&:id)),+ class: 'btn-primary font-semibold checkout-content-save-continue-button my-5 ml-4 !py-4',+ method: :delete,+ data: { turbo: false, 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false) }+ %>+ </div>+ </div>+ </div>+ </div>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/_payment.html.erb b/storefront/app/views/spree/checkout/_payment.html.erbnew file mode 100644index 0000000000..6d58ebf76a--- /dev/null+++ b/storefront/app/views/spree/checkout/_payment.html.erb@@ -0,0 +1,33 @@+<%= form_for @order, url: spree.update_checkout_path(@order.token, @order.state), html: { id: "checkout_form_#{@order.state}" } do |form| %>+ <%= form.hidden_field :state_lock_version %>++ <%= turbo_frame_tag :gift_card_code_field do %>+ <%= hidden_field_tag 'order[gift_card_code]', @order.gift_card&.code %>+ <%= hidden_field_tag 'order[gift_card_amount]', @order.gift_card_total %>+ <% end %>++ <% use_shipping = @order.shipping_eq_billing_address? || @order.bill_address.nil? || @order.bill_address.new_record? %>++ <div id="billing-address" class="mb-8 max-w-full" data-controller="toggle">+ <h5 class="mb-3 font-body font-semibold">+ <%= Spree.t(:billing_address) %>+ </h5>+ <div class="flex items-center">+ <%= form.check_box :use_shipping, { checked: use_shipping, class: 'checkbox-input', data: { action: 'change->toggle#toggle' } } %>+ <%= form.label :use_shipping, Spree.t(:use_shipping_address), class: 'ml-1 cursor-pointer select-none' %>+ </div>+ <%= form.fields_for :bill_address do |address_form| %>+ <div class="mt-4 <%= 'hidden' if use_shipping %>" data-toggle-target='toggleable'>+ <%= render partial: 'spree/addresses/form', locals: {+ address_name: 'bill_address',+ address_form: address_form,+ address_type: 'billing',+ address: @order.bill_address,+ form: form+ } %>+ </div>+ <% end %>+ </div>++ <%= render 'spree/checkout/payment_methods', form: form %>+<% end %>diff --git a/storefront/app/views/spree/checkout/_payment_methods.html.erb b/storefront/app/views/spree/checkout/_payment_methods.html.erbnew file mode 100644index 0000000000..5db9e27bce--- /dev/null+++ b/storefront/app/views/spree/checkout/_payment_methods.html.erb@@ -0,0 +1,72 @@+<%= turbo_frame_tag :checkout_payment_methods do %>+ <% if [email protected]_required? && @order.using_store_credit? %>+ <div class="d-flex flex-column mb-5">+ <p>+ <% if @order.gift_card.present? %>+ <%= Spree.t(:gift_card_amount_applied, amount: @order.display_gift_card_total, code: @order.gift_card.code) %>+ <% else %>+ <%= Spree.t(:store_credits_amount_applied, amount: @order.display_total_applicable_store_credit.abs) %>+ <% end %>++ <%= Spree.t(:order_already_paid) %>+ </p>+ </div>+ <div class="flex justify-end w-full">+ <%= button_tag Spree.t(:place_now),+ class: 'btn-primary w-full lg:w-2/5 font-semibold checkout-content-save-continue-button my-5 !py-4',+ data: { 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false) } %>+ </div>+ <% else %>+ <h5 class="mb-3 font-body font-semibold">+ <%= Spree.t(:payment) %>+ </h5>+ <p class="text-muted mb-3">+ <%= Spree.t(:all_transactions_are_secure_and_encrypted) %>+ </p>++ <%= render partial: 'spree/checkout/payment/store_credit' %>++ <% if checkout_available_payment_methods.size > 1 %>+ <ul id="payment-method-fields" class="rounded-md list-group mb-4 border border-default border-b-0 text-sm border-default">+ <% selected_method = params[:payment_method_id].present? ? checkout_available_payment_methods.find { |method| method.id.to_s == params[:payment_method_id].to_s } : checkout_available_payment_methods.first %>++ <% checkout_available_payment_methods.each do |method| %>+ <li class="list-group-item p-0 m-0 border-b delivery-list-item border-default">+ <%= link_to spree.checkout_state_path(token: @order.token, state: 'payment', payment_method_id: method.id), class: 'custom-control custom-radio flex items-center px-5 py-4 cursor-pointer w-full' do %>+ <%= radio_button_tag "order[payments_attributes][][payment_method_id]",+ method.id,+ selected_method == method,+ class: 'mr-3' %>+ <%= Spree.t(method.name, scope: :payment_methods, default: method.name) %>+ <% end %>++ <% if selected_method == method %>+ <div class="px-5 py-4">+ <%= render partial: "spree/checkout/payment/#{method.method_type}", locals: { payment_method: method } %>+ </div>+ <% end %>+ </li>+ <% end %>+ </ul>+ <% elsif checkout_available_payment_methods.any? %>+ <%= hidden_field_tag "order[payments_attributes][][payment_method_id]", checkout_available_payment_methods.first&.id %>+ <%= render partial: "spree/checkout/payment/#{checkout_available_payment_methods.first.method_type}", locals: { payment_method: checkout_available_payment_methods.first } %>+ <% end %>+ <% end %>++ <p class="text-xs mt-2 mb-4">+ <%= Spree.t(:by_placing_this_order_you_agree_to) %>+ <%= link_to Spree.t(:terms_of_service), policy_path('terms_of_service'), target: :_blank, class: 'text-primary' %> and+ <%= link_to Spree.t(:privacy_policy), policy_path('privacy_policy'), target: :_blank, class: 'text-primary' %>+ </p>++ <div class="flex justify-end w-full">+ <button type="submit"+ id="checkout-payment-submit"+ class="btn-primary w-full lg:w-2/5 font-semibold checkout-content-save-continue-button my-5 !py-4"+ >+ <%= render 'button_processing', is_hidden: true %>+ <%= Spree.t(:pay).titleize %>+ </button>+ </div>+<% end %>diff --git a/storefront/app/views/spree/checkout/_payment_sources.html.erb b/storefront/app/views/spree/checkout/_payment_sources.html.erbnew file mode 100644index 0000000000..90acb8c395--- /dev/null+++ b/storefront/app/views/spree/checkout/_payment_sources.html.erb@@ -0,0 +1,25 @@+<div id="existing_cards" data-controller="checkout-credit-card-book">+ <div class="form-group">+ <ul class="list-group mb-5 border mt-3 rounded-md">+ <% checkout_stripe_payment_sources.each_with_index do |card, i| %>+ <%= render partial: 'credit_card', locals: { card: card, index: i } %>+ <% end %>+ <li>+ <div class="custom-control custom-radio px-5 py-4 flex items-center w-full">+ <label class="flex flex-row items-center w-full pr-8">+ <%= radio_button_tag "order[existing_card]", nil, false, { data: {action: "checkout-credit-card-book#select checkout-stripe#initializeStripe"}, class: "mr-5" } %>+ <div class="w-full flex flex-col items-start space-y-4 justify-center">+ <span class="cursor-pointer">+ <%= Spree.t(:add_new_credit_card) %>+ </span>+ <!-- Display a payment form -->+ <div data-checkout-stripe-target="paymentElement" class="w-full hidden" data-checkout-credit-card-book-target="newCreditCardForm">+ <!--Stripe.js injects the Payment Element-->+ </div>+ </div>+ </label>+ </div>+ </li>+ </ul>+ </div>+</div>\ No newline at end of filediff --git a/storefront/app/views/spree/checkout/_sidebar.html.erb b/storefront/app/views/spree/checkout/_sidebar.html.erbnew file mode 100644index 0000000000..5f7d7c45ea--- /dev/null+++ b/storefront/app/views/spree/checkout/_sidebar.html.erb@@ -0,0 +1,34 @@+<div data-controller="checkout-summary" class="w-full">+ <div data-controller='toggle' data-toggle-class="summary-folded" class="w-full summary-wrapper flex flex-col leading-none" data-toggle-target="toggleable" data-checkout-summary-target="wrapper">+ <button data-action='click->toggle#toggle touch->toggle#toggle' class="w-full lg:hidden relative z-[999] bg-accent", id="toggle-order-summary">+ <div class="h-full flex justify-between text-sm items-center">+ <p class="flex gap-1 chevron-summary items-center">+ <%= heroicon("shopping-bag", variant: :outline) %>+ <span class="show-summary hidden">+ <%= Spree.t(:show) %>+ </span>+ <span class="hide-summary">+ <%= Spree.t(:hide) %>+ </span>+ <span class="lowercase"><%= Spree.t(:order_summary) %></span>+ </p>+ <span class="font-bold" id='summary-order-total'>+ <%= @order.display_total.to_html %>+ </span>+ </div>+ </button>+ <div class="summary overflow-hidden transition-[height] h-0 p-0" data-checkout-summary-target="container">+ <div class="summary-content flex flex-col gap-5" data-checkout-summary-target="content">+ <%= render 'line_items', order: @order %>++ <div data-checkout-summary-target="coupon_area" class="flex flex-col gap-5">+ <% unless action_name == 'complete' %>+ <%= render 'coupon_code', promotion: @order.valid_coupon_promotions.first %>+ <%= render 'store_credit' %>+ <% end %>+ <%= render 'summary', order: @order %>+ </div>+ </div>+ </div>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/_store_credit.html.erb b/storefront/app/views/spree/checkout/_store_credit.html.erbnew file mode 100644index 0000000000..8990fb7fc7--- /dev/null+++ b/storefront/app/views/spree/checkout/_store_credit.html.erb@@ -0,0 +1,21 @@+<%= turbo_frame_tag :checkout_store_credit do %>+ <% if can_use_store_credit_on_checkout?(@order) %>+ <%= form_tag(+ @order.using_store_credit? ? spree.checkout_remove_store_credit_path(@order.token) : spree.checkout_apply_store_credit_path(@order.token),+ method: @order.using_store_credit? ? :delete : :patch,+ class: 'aria-busy:opacity-25',+ data: { controller: 'auto-submit' }+ ) do %>+ <div class="flex items-center justify-between border text-sm px-5 py-4 rounded-md border-neutral-200 cursor-pointer">+ <%= label_tag :store_credit, class: 'flex items-center gap-3 text-sm cursor-pointer' do %>+ <%= check_box_tag :store_credit, '1', @order.using_store_credit?,+ class: 'input-checkbox !border-neutral-200 rounded-md',+ data: { action: 'auto-submit#submit' } %>+ <%= Spree.t("store_credit.apply") %>+ <% end %>++ <div><%= @order.display_total_available_store_credit %></div>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/storefront/app/views/spree/checkout/_summary.html.erb b/storefront/app/views/spree/checkout/_summary.html.erbnew file mode 100644index 0000000000..85cb551e7e--- /dev/null+++ b/storefront/app/views/spree/checkout/_summary.html.erb@@ -0,0 +1,116 @@+<%= turbo_frame_tag :checkout_summary, id: 'summary' do %>+ <div class="checkout-content-summary text-sm">+ <% cache [*spree_base_cache_scope.call(order.cache_key_with_version), order.state] do %>+ <div data-hook="order_summary">+ <div class="flex justify-between items-center mb-2">+ <span><%= Spree.t(:subtotal) %>:</span>+ <span><%= order.display_item_total.to_html %></span>+ </div>+ <div class="d-table-cell text-right font-weight-bold"></div>++ <% cache spree_base_cache_scope.call(order.line_item_adjustments.nonzero.cache_key_with_version) do %>+ <% if order.line_item_adjustments.nonzero.exists? %>+ <% order.line_item_adjustments.nonzero.promotion.eligible.group_by(&:label).each do |label, adjustments| %>+ <div class="flex justify-between items-center mb-2">+ <span class="max-w-[250px] truncate">+ <%= label %>+ </span>+ <span>+ <%= Spree::Money.new(adjustments.sum(&:amount), currency: order.currency).to_html %>+ </span>+ </div>+ <% end %>+ <% end %>+ <% end %>++ <% if order.state != 'address' && order.shipments.any? %>+ <% if order.has_free_shipping? %>+ <div class="flex justify-between items-center mb-2">+ <span><%= Spree.t(:shipping) %>:</span>+ <span><%= Spree.t(:free) %></span>+ </div>+ <% else %>+ <div class="flex justify-between items-center mb-2">+ <span><%= Spree.t(:shipping) %>:</span>+ <% shipping_total = Spree::Money.new(order.shipments.to_a.sum(&:cost), currency: order.currency) %>+ <span>+ <%= order.ship_total <= 0 ? Spree.t(:free) : shipping_total.to_html %>+ </span>+ </div>++ <% if order.shipment_adjustments.nonzero.exists? %>+ <% order.shipment_adjustments.nonzero.promotion.eligible.group_by(&:label).each do |label, adjustments| %>+ <div class="flex justify-between items-center mb-2">+ <span>+ <%= label %>:+ </span>+ <span>+ <%= Spree::Money.new(adjustments.sum(&:amount), currency: order.currency).to_html %>+ </span>+ <% end %>+ <% end %>+ <% end %>+ <% elsif order.state == 'address' %>+ <div class="flex justify-between items-center mb-2">+ <span><%= Spree.t(:shipping) %>:</span>+ <span class="text-xs text-gray-500"><%= Spree.t(:calculated_at_next_step) %></span>+ </div>+ <% end %>++ <% if order.payment? || order.completed? %>+ <% cache spree_base_cache_scope.call(order.all_adjustments.nonzero.tax.eligible.cache_key_with_version) do %>+ <% order.all_adjustments.nonzero.tax.eligible.group_by(&:label).each do |label, adjustments| %>+ <div class="flex justify-between items-center mb-2">+ <span><%= label %></span>++ <% tax_total = Spree::Money.new(adjustments.sum(&:amount), currency: order.currency) %>+ <span><%= tax_total.to_html %></span>+ </div>+ <% end %>+ <% end %>+ <% end %>++ <% if order.adjustments.nonzero.non_tax.eligible.exists? %>+ <% order.adjustments.nonzero.non_tax.eligible.each do |adjustment| %>+ <div class="flex justify-between items-center mb-2">+ <span>+ <%= adjustment.label %>:+ </span>+ <span>+ <%= adjustment.display_amount.to_html %>+ </span>+ </div>+ <% end %>+ <% end %>++ <% if order.gift_card.present? %>+ <div class="flex justify-between items-center mb-2">+ <span><%= Spree.t(:gift_card) %>:</span>+ <span>-<%= order.display_gift_card_total %></span>+ </div>+ <% elsif order.using_store_credit? %>+ <div class="flex justify-between items-center mb-2">+ <span>Store credit:</span>+ <span><%= order.display_total_applied_store_credit %></span>+ </div>+ <% end %>++ <div>+ <div class="flex justify-between items-center">+ <span class="font-bold text-lg"><%= Spree.t(:total) %></span>+ <div class="">+ <span class="text-xs mr-1"><%= order.currency.upcase %></span>+ <span class="font-semibold text-lg inline" id='summary-order-total' data-currency="<%= Money::Currency.find(order.currency).symbol %>">+ <%= order.display_total_minus_store_credits.to_html %>+ </span>+ </div>+ </div>+ </div>+ </div>+ <% end %>++ <div id="checkout-message" class="mt-8">+ <%= current_store.checkout_message if current_store.checkout_message.present? %>+ </div>+ </div>+ <% end %>diff --git a/storefront/app/views/spree/checkout/_user_account.html.erb b/storefront/app/views/spree/checkout/_user_account.html.erbnew file mode 100644index 0000000000..3e8c34c208--- /dev/null+++ b/storefront/app/views/spree/checkout/_user_account.html.erb@@ -0,0 +1,9 @@+<div class="flex items-center">+ <div class="min-w-[60px] max-w-[60px]">+ <%= Spree.t(:account) %>+ </div>+ <div class="px-5">+ <p class="font-bold"><%= @order.user.name&.full %></p>+ <p><%= @order.user.email %></p>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/apply_coupon_code.turbo_stream.erb b/storefront/app/views/spree/checkout/apply_coupon_code.turbo_stream.erbnew file mode 100644index 0000000000..f5dbd71e90--- /dev/null+++ b/storefront/app/views/spree/checkout/apply_coupon_code.turbo_stream.erb@@ -0,0 +1,43 @@+<% if @result.error.blank? && @result.status_code != :gift_card_applied %>+ <%= turbo_stream.replace :checkout_line_items do %>+ <%= render 'line_items', order: @order %>+ <% end %>+ <%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+ <% end %>+ <%= turbo_stream.replace :checkout_payment_methods do %>+ <%= render 'payment_methods', order: @order %>+ <% end %>+ <%= turbo_stream.replace :quick_checkout do %>+ <%= render 'quick_checkout', order: @order %>+ <% end %>+ <%= turbo_stream.update 'summary-order-total' do %>+ <%= @order.display_total.to_html %>+ <% end %>+<% elsif @result.error.blank? && @result.status_code == :gift_card_applied %>+ <%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+ <% end %>+ <%= turbo_stream.replace :quick_checkout do %>+ <%= render 'quick_checkout', order: @order %>+ <% end %>+ <%= turbo_stream.replace :checkout_payment_methods do %>+ <%= render 'payment_methods', order: @order %>+ <% end %>+<% end %>++<%= turbo_stream.replace :checkout_coupon_code do %>+ <%= render 'coupon_code', promotion: @order.valid_coupon_promotions.first,+ result: @result, classes: @result.error.present? ? 'error' : '' %>+<% end %>++<%= turbo_stream.replace :gift_card_code_field do %>+ <%= turbo_frame_tag :gift_card_code_field do %>+ <%= hidden_field_tag 'order[gift_card_code]', @order.gift_card&.code %>+ <%= hidden_field_tag 'order[gift_card_amount]', @order.gift_card_total %>+ <% end %>+<% end %>++<%= turbo_stream.replace :checkout_store_credit do %>+ <%= render 'store_credit' %>+<% end %>diff --git a/storefront/app/views/spree/checkout/apply_store_credit.turbo_stream.erb b/storefront/app/views/spree/checkout/apply_store_credit.turbo_stream.erbnew file mode 100644index 0000000000..aa37d99097--- /dev/null+++ b/storefront/app/views/spree/checkout/apply_store_credit.turbo_stream.erb@@ -0,0 +1,19 @@+<%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+<% end %>++<%= turbo_stream.replace :quick_checkout do %>+ <%= render 'quick_checkout', order: @order %>+<% end %>++<%= turbo_stream.replace :checkout_payment_methods do %>+ <%= render 'payment_methods', order: @order %>+<% end %>++<%= turbo_stream.replace :checkout_store_credit do %>+ <%= render 'store_credit' %>+<% end %>++<%= turbo_stream.replace :checkout_coupon_code do %>+ <%= render 'coupon_code', promotion: @order.valid_coupon_promotions.first %>+<% end %>diff --git a/storefront/app/views/spree/checkout/edit.html.erb b/storefront/app/views/spree/checkout/edit.html.erbnew file mode 100644index 0000000000..889f977f73--- /dev/null+++ b/storefront/app/views/spree/checkout/edit.html.erb@@ -0,0 +1,96 @@+<div class="checkout-progress" data-hook="checkout_progress" role="navigation" aria-label="<%= Spree.t('checkout_page.checkout_navigation') %>">+ <nav aria-label="breadcrumb">+ <%= checkout_progress %>+ </nav>+</div>++<%= render partial: 'spree/shared/error_messages_without_base_attribute', locals: { target: @order } %>++<%= turbo_frame_tag :modal %>++<% if @order.address? && try_spree_current_user %>+ <div class="border text-sm px-5 py-4 mb-5 rounded-md border-default">+ <%= render 'user_account' %>+ </div>+<% end %>+<% if @order.checkout_steps.index(@order.state) > 0 %>+ <div class="border text-sm px-5 mb-6 py-4 rounded-md border-default">+ <% if @order.user.present? %>+ <%= render 'user_account' %>+ <% elsif @order.email.present? %>+ <div class="flex justify-between items-center">+ <div class="flex items-center">+ <div class="min-w-[60px] max-w-[60px]">+ <%= Spree.t(:contact) %>+ </div>+ <div class="px-5 word-break">+ <%= @order.email %>+ </div>+ </div>+ <div class="text-xs">+ <%= link_to Spree.t(:edit), spree.checkout_state_path(@order.token, 'address'), class: 'text-dark' %>+ </div>+ </div>+ <% end %>+ <% if @order.ship_address.present? %>+ <div class="flex justify-between mt-3 border-t pt-3 items-center border-default">+ <div class="flex items-center">+ <div class="min-w-[60px] max-w-[60px]">+ <%= Spree.t(:ship_address) %>+ </div>+ <div class="px-5 word-break">+ <%= @order.ship_address.to_s.gsub('<br/>', ', ').html_safe %>+ </div>+ </div>+ <div class="text-xs">+ <%= link_to Spree.t(:edit), spree.checkout_state_path(@order.token, 'address'), class: 'text-dark' %>+ </div>+ </div>+ <% end %>+ <% if @order.checkout_steps.index(@order.state) > 1 && @order.shipments.any? %>+ <div class="mt-3 border-t pt-3 flex justify-between items-center border-default">+ <div class="flex items-center">+ <div class="min-w-[60px] max-w-[60px]">+ <%= Spree.t(:delivery_method) %>+ </div>+ <div class="px-5 flex flex-col gap-4">+ <% @order.shipments.each do |shipment| %>+ <p class="word-break">+ <% if @order.shipments.count > 1 || shipment.shipping_method.vendor.present? %>+ <%= shipment.stock_location&.name %> -+ <% end %>+ <%= shipment.shipping_method.name %>+ · <strong><%= shipment.cost <= 0 ? Spree.t(:free) : shipment.display_final_price %></strong>+ </p>+ <% end %>+ </div>+ </div>+ <div class="text-xs">+ <%= link_to Spree.t(:edit), spree.checkout_state_path(@order.token, 'delivery'), class: 'text-dark' %>+ </div>+ </div>+ <% end %>+ </div>+<% end %>++<div class="checkout-content mt-5" id="checkout_content">+ <% if @order.address? %>+ <%= render partial: 'spree/checkout/address', locals: { order: @order } %>+ <% elsif @order.payment? || @order.confirm? %>+ <%= render partial: 'spree/checkout/payment', locals: { order: @order } %>+ <% elsif @order.delivery? %>+ <%= render partial: 'spree/checkout/delivery', locals: { order: @order } %>+ <% else %>+ <%= form_for @order, url: spree.update_checkout_path(@order.token, @order.state), html: { id: "checkout_form_#{@order.state}" } do |form| %>+ <div>+ <%= form.hidden_field :state_lock_version unless @order.errors.any? %>+ <%= render @order.state, form: form %>+ </div>+ <div>+ <%= button_tag Spree.t(:save_and_continue),+ class: 'btn btn-primary font-weight-bold w-full lg:w-48 float-right checkout-content-save-continue-button',+ data: { 'turbo-submits-with': (render 'button_processing', is_hidden: false) } %>+ </div>+ <% end %>+ <% end %>+</div>diff --git a/storefront/app/views/spree/checkout/payment/_check.html.erb b/storefront/app/views/spree/checkout/payment/_check.html.erbnew file mode 100644index 0000000000..e69de29bb2diff --git a/storefront/app/views/spree/checkout/payment/_gateway.html.erb b/storefront/app/views/spree/checkout/payment/_gateway.html.erbnew file mode 100644index 0000000000..2f048fecbc--- /dev/null+++ b/storefront/app/views/spree/checkout/payment/_gateway.html.erb@@ -0,0 +1,30 @@+<div class="payment-gateway">++ <% param_prefix = "payment_source[#{payment_method.id}]" %>++ <div class="payment-gateway-fields">+ <div class="mb-4 payment-gateway-field">+ <%= text_field_tag "#{param_prefix}[name]", "#{@order.bill_address_firstname} #{@order.bill_address_lastname}", { id: "name_on_card", placeholder: Spree.t(:name_on_card)} %>+ </div>++ <div class="mb-4 payment-gateway-field" data-hook="card_number">+ <% options_hash = Rails.env.production? ? {autocomplete: 'off'} : {} %>+ <%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(id: 'card_number', class: 'cardNumber', size: 19, maxlength: 19, autocomplete: "off", placeholder: Spree.t(:card_number)) %>+ <span id="card_type" style="display:none;">+ ( <span id="looks_like"><%= Spree.t(:card_type_is) %> <span id="type"></span></span>+ <span id="unrecognized"><%= Spree.t(:unrecognized_card_type) %></span>+ )+ </span>+ </div>+ <div class="payment-gateway-half-fields d-flex justify-content-between">+ <div class="payment-gateway-field" data-hook="card_expiration">+ <%= text_field_tag "#{param_prefix}[expiry]", '', id: 'card_expiry', class: 'cardExpiry', placeholder: "MM/YYYY" %>+ </div>+ <div class="payment-gateway-field" data-hook="card_code">+ <%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(id: 'card_code', class: 'cardCode', size: 5, placeholder: Spree.t(:cvv)) %>+ </div>+ </div>++ <%= hidden_field_tag "#{param_prefix}[cc_type]", '', id: "cc_type", class: 'ccType' %>+ </div>+</div>diff --git a/storefront/app/views/spree/checkout/payment/_store_credit.html.erb b/storefront/app/views/spree/checkout/payment/_store_credit.html.erbnew file mode 100644index 0000000000..1fc95d50e4--- /dev/null+++ b/storefront/app/views/spree/checkout/payment/_store_credit.html.erb@@ -0,0 +1,13 @@+<% if @order.using_store_credit? %>+ <div class="d-flex flex-column mb-5" data-hook="checkout_payment_store_credit_success">+ <p class="store-credit-title font-body">+ <% if @order.gift_card.present? %>+ <%= Spree.t(:gift_card_amount_applied, amount: @order.display_gift_card_total, code: @order.gift_card.code) %>+ <% else %>+ <%= Spree.t(:store_credits_amount_applied, amount: @order.display_total_applicable_store_credit.abs) %>+ <% end %>++ <%= Spree.t(:you_will_need_to_pay_only, amount: @order.display_total_minus_store_credits) %>+ </p>+ </div>+<% end %>diff --git a/storefront/app/views/spree/checkout/remove_coupon_code.turbo_stream.erb b/storefront/app/views/spree/checkout/remove_coupon_code.turbo_stream.erbnew file mode 100644index 0000000000..743db5fa1b--- /dev/null+++ b/storefront/app/views/spree/checkout/remove_coupon_code.turbo_stream.erb@@ -0,0 +1,31 @@+<% if @result.error.blank? %>+ <%= turbo_stream.replace :checkout_line_items do %>+ <%= render 'line_items', order: @order %>+ <% end %>+ <%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+ <% end %>+ <%= turbo_stream.replace :checkout_payment_methods do %>+ <%= render 'payment_methods', order: @order %>+ <% end %>+ <%= turbo_stream.replace :quick_checkout do %>+ <%= render 'quick_checkout', order: @order %>+ <% end %>+ <%= turbo_stream.update 'summary-order-total' do %>+ <%= @order.display_total.to_html %>+ <% end %>+<% end %>+<%= turbo_stream.replace :checkout_coupon_code do %>+ <%= render 'coupon_code', promotion: @order.valid_coupon_promotions.first,+ result: @result, classes: @result.error.present? ? 'error' : '' %>+<% end %>+<%= turbo_stream.replace :gift_card_code_field do %>+ <%= turbo_frame_tag :gift_card_code_field do %>+ <%= hidden_field_tag 'order[gift_card_code]', @order.gift_card&.code %>+ <%= hidden_field_tag 'order[gift_card_amount]', @order.gift_card_total %>+ <% end %>+<% end %>++<%= turbo_stream.replace :checkout_store_credit do %>+ <%= render 'store_credit' %>+<% end %>diff --git a/storefront/app/views/spree/checkout/remove_store_credit.turbo_stream.erb b/storefront/app/views/spree/checkout/remove_store_credit.turbo_stream.erbnew file mode 100644index 0000000000..aa37d99097--- /dev/null+++ b/storefront/app/views/spree/checkout/remove_store_credit.turbo_stream.erb@@ -0,0 +1,19 @@+<%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+<% end %>++<%= turbo_stream.replace :quick_checkout do %>+ <%= render 'quick_checkout', order: @order %>+<% end %>++<%= turbo_stream.replace :checkout_payment_methods do %>+ <%= render 'payment_methods', order: @order %>+<% end %>++<%= turbo_stream.replace :checkout_store_credit do %>+ <%= render 'store_credit' %>+<% end %>++<%= turbo_stream.replace :checkout_coupon_code do %>+ <%= render 'coupon_code', promotion: @order.valid_coupon_promotions.first %>+<% end %>diff --git a/storefront/app/views/spree/checkout/update.turbo_stream.erb b/storefront/app/views/spree/checkout/update.turbo_stream.erbnew file mode 100644index 0000000000..b2851bca90--- /dev/null+++ b/storefront/app/views/spree/checkout/update.turbo_stream.erb@@ -0,0 +1,25 @@+<%= turbo_stream.replace :checkout_summary do %>+ <%= render 'summary', order: @order %>+<% end %>+<%= turbo_stream.replace :checkout_lock_version do %>+ <%= turbo_frame_tag :checkout_lock_version do %>+ <%= hidden_field_tag 'order[state_lock_version]', @order.state_lock_version %>+ <% end %>+<% end %>+<%= turbo_stream.update 'summary-order-total' do %>+ <%= @order.display_total.to_html %>+<% end %>+<% if @order.invalid_line_items.any? && [email protected]_line_items_invalid? %>+ <%= turbo_stream.append :modal do %>+ <%= turbo_frame_tag :modal do %>+ <%= render partial: 'spree/checkout/missing_line_items' %>+ <% end %>+ <% end %>+<% end %>+<% if @order.invalid_line_items.any? && @order.all_line_items_invalid? %>+ <%= turbo_stream.append :modal do %>+ <%= turbo_frame_tag :modal do %>+ <%= render partial: 'spree/checkout/missing_all_line_items' %>+ <% end %>+ <% end %>+<% end %>diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rbnew file mode 100644index 0000000000..b06eacaba3--- /dev/null+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb@@ -0,0 +1,1088 @@+require 'spec_helper'++describe Spree::CheckoutController, type: :controller do+ let(:store) { Spree::Store.default }+ let(:country) { store.default_country }+ let(:state) { country.states.first }+ let(:user) { nil }+ let(:order) { create(:order_with_totals, store: store, user: user, email: '[email protected]') }++ let(:address_params) do+ address = build(:address, country: country, state: state)+ address.attributes.except('created_at', 'updated_at')+ end+ let(:accept_marketing) { true }++ before do+ allow(controller).to receive(:current_store).and_return(store)+ allow(controller).to receive_messages try_spree_current_user: user+ allow(controller).to receive_messages spree_current_user: user+ end++ describe '#edit' do+ it 'redirects to the cart path if token is invalid' do+ get :edit, params: { state: 'delivery', token: 'INVALID' }+ expect(response).to redirect_to(spree.cart_path)+ end++ it 'redirects to cart if order is completed' do+ order.update_columns(completed_at: Time.current, state: 'complete')+ get :edit, params: { state: 'address', token: order.token }+ expect(response).to redirect_to(spree.cart_path)+ end++ it 'redirects to cart if order has removed line items' do+ product = order.line_items.first.product++ order.update_column(:state, 'delivery')++ get :edit, params: { state: 'delivery', token: order.token }+ expect(response).not_to redirect_to(spree.cart_path)++ product.update!(status: 'draft')+ get :edit, params: { state: 'delivery', token: order.token }++ expect(response).to redirect_to(spree.cart_path)+ expect(flash[:error]).to eq(Spree.t('cart_line_item.discontinued', li_name: product.name))+ end++ it 'redirects to current step trying to access a future step' do+ order.update_column(:state, 'address')+ get :edit, params: { state: 'delivery', token: order.token }+ expect(response).to redirect_to spree.checkout_state_path(order.token, 'address')+ end++ it 'sets the order state to address if it is cart' do+ order.update_column(:state, 'cart')+ expect(get(:edit, params: { token: order.token })).to redirect_to(spree.checkout_state_path(order.token, 'address'))+ expect(order.reload.state).to eql('address')+ end++ it 'tracks events' do+ expect(controller).to receive(:track_event).with('checkout_started', { order: order })+ expect(controller).to receive(:track_event).with('checkout_step_viewed', { order: order, step: 'address' })+ get :edit, params: { token: order.token, state: 'address' }+ end++ context 'when entering the checkout' do+ context 'when user is signed in' do+ let(:user) { create(:user) }++ it 'associates the order with a user' do+ order.update_column :user_id, nil+ expect { get :edit, params: { token: order.token } }.to change { order.reload.user }.from(nil).to(user)+ end+ end++ context 'when order has no user assigned to it' do+ let(:user) { create(:user) }++ before do+ order.update_column :user_id, nil+ end++ it 'allows any user to access the order' do+ get :edit, params: { token: order.token }+ expect(response).to redirect_to spree.checkout_state_path(order.token, 'address')+ end+ end++ context 'when order has user assigned to it' do+ let(:order_user) { create(:user) }++ before do+ order.update_column :user_id, order_user.id+ end++ context 'when user is not logged in' do+ let(:user) { nil }++ it 'redirects to login page' do+ get :edit, params: { token: order.token }+ expect(response).to redirect_to(spree.new_user_session_path)+ end+ end++ context 'when user is logged in' do+ context 'when user is the order user' do+ let(:user) { order_user }++ it 'allows user to access the order' do+ get :edit, params: { token: order.token }+ expect(response).to redirect_to spree.checkout_state_path(order.token, 'address')+ end+ end++ context 'when user is not the order user' do+ let(:user) { create(:user) }++ it 'redirects to cart page' do+ store.update(url: "http://#{store.code}.#{Spree.root_domain}")++ get :edit, params: { token: order.token }+ expect(flash[:error]).to eq('You cannot access this checkout')+ expect(response).to redirect_to(spree.cart_url(host: store.url))+ end+ end+ end+ end+ end++ describe 'before_action :restart_checkout' do+ shared_examples_for 'restarting checkout' do+ context 'on address step' do+ before do+ order.update!(state: 'address')+ end++ it 'proceeds with the checkout' do+ get :edit, params: { token: order.token }+ expect(response).to be_ok+ end+ end++ context 'on delivery step' do+ before do+ order.update!(ship_address: address, state: 'delivery')+ end++ it 'redirects back to the address step' do+ get :edit, params: { token: order.token }++ expect(response).to redirect_to(spree.checkout_state_path(order.token, 'address'))++ expect(order.reload).to be_address+ expect(order.ship_address).to eq(nil)+ end+ end++ context 'on payment step' do+ before do+ order.update!(ship_address: address, state: 'payment')+ end++ it 'redirects back to the address step' do+ get :edit, params: { token: order.token }++ expect(response).to redirect_to(spree.checkout_state_path(order.token, 'address'))++ expect(order.reload).to be_address+ expect(order.ship_address).to eq(nil)+ end+ end+ end++ context 'on quick checkout' do+ let(:address) { create(:address, quick_checkout: true) }+ include_examples 'restarting checkout'+ end++ context 'with a missing shipping address' do+ let(:address) { nil }+ include_examples 'restarting checkout'+ end+ end++ xcontext 'removes expired gift card' do+ let!(:gift_card) { create :gift_card, code: '123', amount: 10, state: 'active' }++ before do+ order.update(state: :address)+ order.update_column(:total, 30)++ patch :apply_coupon_code, params: { token: order.token, coupon_code: gift_card.code.upcase }+ gift_card.update_columns(expires_at: 1.day.ago)+ end++ it 'removes gift card from order' do+ expect(order.reload.gift_card).to eq(gift_card)++ get :edit, params: { token: order.token }++ expect(order.reload.gift_card).to eq(nil)+ end+ end+ end++ describe '#update' do+ context 'save successful' do+ before do+ allow_any_instance_of(Spree::OrderDecorator).to receive(:ensure_available_shipping_rates).and_return(true)+ end++ def spree_post_address+ post :update, params: {+ token: order.token,+ state: 'address',+ order: {+ ship_address_attributes: address_params+ }+ }+ end++ context 'with the order in the cart state' do+ before do+ order.update_column(:state, 'cart')+ allow(order).to receive_messages user: user+ end++ it 'assigns order' do+ post :update, params: { state: 'address', token: order.token }+ expect(assigns[:order]).not_to be_nil+ end++ it 'advances the state' do+ spree_post_address+ expect(order.reload.state).to eq('delivery')+ end++ it 'tracks events' do+ expect(controller).to receive(:track_event).with('checkout_step_completed', { order: order, step: 'address' })+ spree_post_address+ end++ it 'redirects the next state' do+ spree_post_address+ expect(response).to redirect_to spree.checkout_state_path(order.token, 'delivery')+ end+ end++ context 'with the order in the address state' do+ subject :update do+ patch :update, params: update_params+ order.reload+ end++ let(:user) { create(:user) }+ let(:ship_address) { nil }++ let!(:order) do+ create(:order_with_totals,+ ship_address: ship_address,+ state: 'address',+ user: user,+ store: store)+ end+ let(:save_user_address) { true }+ let(:update_params) do+ {+ token: order.token,+ state: 'address',+ save_user_address: save_user_address,+ accept_marketing: accept_marketing,+ order: {+ ship_address_attributes: ship_address_params,+ }+ }+ end++ shared_examples 'address not created' do+ it 'does not create new address' do+ expect { update }.to change { Spree::Address.count }.by(0)+ end+ end++ shared_examples 'new address created' do+ it 'creates new address' do+ expect { update }.to change { Spree::Address.count }.by(1)+ end+ end++ shared_examples 'created address assigned to current user' do+ it 'assigns created address to current user' do+ update++ expect(Spree::Address.last.user_id).to eq user.id+ end+ end++ shared_examples 'default address not changed' do+ it 'does not change default address' do+ update++ expect(default_ship_address.reload.city).to eq 'Herndon'+ end+ end++ shared_examples 'address user not changed' do+ it 'keeps address assigned to user' do+ update++ expect(default_ship_address.reload.user).to eq user+ end+ end++ shared_examples 'same user assigned' do+ it 'keeps addresses assigned to user' do+ update++ expect(default_ship_address.reload.user).to eq user+ end+ end++ context 'with shipping address (with delivery step)' do+ let(:ship_address) { create(:address, user: user) }++ context 'when all addresses attributes are nil' do+ let!(:ship_address_params) { nil }+ let(:expected_ship_address_id) { order.ship_address_id }++ it 'takes default ship addresses' do+ update++ expect(order.ship_address_id).to eq(expected_ship_address_id)+ end++ it_behaves_like 'address not created'+ end++ context 'when some attributes are invalid' do+ let!(:default_ship_address) { order.ship_address }+ let(:ship_address_params) do+ build(:address, firstname: nil, city: 'Washington').attributes.merge(id: default_ship_address.id).except(:user_id, :created_at,+ :updated_at)+ end+ let(:ship_address_error) { { 'ship_address.firstname': ["can't be blank"] } }++ it_behaves_like 'address not created'+ end++ context 'when addresses attributes are not changed' do+ let(:ship_address_params) { order.ship_address.attributes.except(:user_id, :created_at, :updated_at) }+ let(:expected_ship_address_id) { order.ship_address_id }++ it 'takes same default addresses' do+ update++ expect(order.ship_address_id).to eq(expected_ship_address_id)+ end++ it_behaves_like 'address not created'+ end++ context 'when addresses attributes are changed' do+ let!(:default_ship_address) { order.ship_address }++ context 'when default address is editable' do+ let(:ship_address_params) do+ {+ firstname: 'John',+ lastname: 'Doe',+ address1: '123 DC',+ city: 'Washington',+ zipcode: '12345',+ phone: '123-456-7890',+ state_id: default_ship_address.state_id,+ country_id: default_ship_address.country_id,+ id: default_ship_address.id+ }+ end++ it 'updates current addresses' do+ expect(default_ship_address.city).to eq 'Herndon'++ update++ expect(default_ship_address.reload.city).to eq 'Washington'+ end++ it_behaves_like 'address not created'+ it_behaves_like 'same user assigned'+ end++ context 'when default address is not editable' do+ let!(:completed_order) { create(:completed_order_with_totals, ship_address: default_ship_address) }+ let(:ship_address_params) do+ build(:address, city: 'Washington', state: state).attributes.merge(id: default_ship_address.id).except(:user_id, :created_at,+ :updated_at)+ end+ let(:created_address_id) { Spree::Address.find_by(city: 'Washington').id }++ it_behaves_like 'default address not changed'+ it_behaves_like 'new address created'++ it 'assigns created address to both bill and ship addresses' do+ update++ expect(order.ship_address_id).to eq created_address_id+ end++ it_behaves_like 'created address assigned to current user'+ end+ end++ context 'when user is a guest' do+ let(:user) { nil }++ before do+ allow(controller).to receive_messages try_spree_current_user: nil+ allow(controller).to receive_messages spree_current_user: nil+ end++ context 'when submitted addresses already exist' do+ let!(:ship_address) { create(:address, city: 'Washington', user: nil) }+ let(:order) do+ create(:order_with_totals, store: store, bill_address: nil, ship_address: nil, state: 'address', user: nil,+ email: '[email protected]')+ end+ let(:ship_address_params) { ship_address.attributes.except(:id, :user_id, :created_at, :updated_at) }++ it 'keeps addresses unassigned' do+ update++ expect(ship_address.reload.user).to be_nil+ end++ it_behaves_like 'address not created'+ end++ context 'when submitted addresses does not exist' do+ let(:order) do+ create(:order_with_totals, store: store, bill_address: nil, ship_address: nil, state: 'address', user: nil,+ email: '[email protected]')+ end+ let(:ship_address_params) do+ build(:address, city: 'Washington', user: nil, state: state).attributes.except(:user_id, :created_at, :updated_at)+ end++ it 'keeps created addresses unassigned' do+ update++ expect(Spree::Address.find_by(city: 'Washington').user).to be_nil+ end++ it 'creates new addresses' do+ expect { update }.to change { Spree::Address.count }.by(1)+ end+ end++ context 'when attributes are invalid' do+ let(:order) do+ create(:order_with_totals, store: store, bill_address: nil, ship_address: nil, state: 'address', user: nil,+ email: '[email protected]')+ end+ let(:ship_address_params) do+ build(:address, firstname: nil, city: 'Washington', user: nil).attributes.except(:user_id, :created_at, :updated_at)+ end+ let(:ship_address_error) { { 'ship_address.firstname': ["can't be blank"] } }++ it_behaves_like 'address not created'+ end+ end+ end+ end++ context 'with the order in the delivery state' do+ let(:ship_address) { create(:address, user: user) }+ let(:order) { create(:order_with_line_items, state: 'delivery', ship_address: ship_address, user: user, store: store, email: '[email protected]') }++ before do+ order.create_proposed_shipments+ order.ensure_available_shipping_rates+ order.set_shipments_cost+ order.reload+ end++ let(:update_params) do+ {+ token: order.token,+ state: 'delivery',+ order: {+ selected_shipping_rate_id: order.shipments.first.shipping_rates.first.id+ }+ }+ end++ subject :update do+ patch :update, params: update_params+ order.reload+ end++ it 'sets shipping rate and moves to payment state' do+ expect(controller).to receive(:track_event).with('checkout_step_completed', { order: order, step: 'delivery' })+ expect { update }.to change { order.state }.from('delivery').to('payment')+ expect(order.shipments.first.selected_shipping_rate).to eq(order.shipments.first.shipping_rates.first)+ end+ end++ context 'with the order in the payment state' do+ let(:order) { create(:order_with_line_items, state: 'payment', user: user,store: store, email: '[email protected]') }+ let(:payment_method) { create(:credit_card_payment_method, stores: [store]) }++ let(:payment_source_attributes) do+ {+ gateway_payment_profile_id: 'BGS-123',+ gateway_customer_profile_id: 'BGS-123',+ month: 1.month.from_now.month,+ year: 1.month.from_now.year,+ name: 'Spree Commerce',+ last_digits: '1111'+ }+ end++ let(:update_params) do+ {+ token: order.token,+ state: 'payment',+ order: {+ bill_address_attributes: {+ firstname: 'John',+ lastname: 'Doe',+ address1: '7735 Old Georgetown Road',+ city: 'Bethesda',+ zipcode: '20814',+ phone: '3014445002',+ state_id: store.default_country.states.first.id,+ country_id: store.default_country.id,+ },+ payments_attributes: [{+ payment_method_id: payment_method.id,+ }]+ },+ payment_source: {+ payment_method.id.to_s => payment_source_attributes+ }+ }+ end++ subject :update do+ patch :update, params: update_params+ order.reload+ end++ it 'saves payment method' do+ expect { update }.to change { order.payments.count }.by(1)+ expect(order.payment_method).to eq(payment_method)+ expect(order.payment_source.gateway_payment_profile_id).to eq('BGS-123')+ end++ it 'saves bill address' do+ expect { update }.to change { order.bill_address.address1 }.to('7735 Old Georgetown Road')+ end++ it 'moves to confirm state' do+ expect { update }.to change { order.state }.from('payment').to('confirm')+ end++ it 'tracks event' do+ expect(controller).to receive(:track_event).with('payment_info_entered', { order: order })+ expect(controller).to receive(:track_event).with('checkout_step_completed', { order: order, step: 'payment' })+ update+ end++ context 'bill address same as shipping' do+ let(:update_params) do+ {+ token: order.token,+ state: 'payment',+ order: {+ use_shipping: 'true',+ payments_attributes: [{+ payment_method_id: payment_method.id,+ }]+ },+ payment_source: {+ payment_method.id.to_s => payment_source_attributes+ }+ }+ end++ it 'moves to confirm state' do+ expect { update }.to change { order.state }.from('payment').to('confirm')+ end++ it 'saves bill address' do+ expect { update }.to change { order.bill_address }.to(order.ship_address)+ end+ end++ context 'without confirmation required' do+ before do+ allow_any_instance_of(Spree::Order).to receive(:confirmation_required?).and_return(false)+ end++ it 'moves to complete state' do+ expect(controller).to receive(:track_event).with('payment_info_entered', { order: order })+ expect(controller).to receive(:track_event).with('checkout_step_completed', { order: order, step: 'payment' })+ expect(controller).to receive(:track_event).with('checkout_completed', { order: order })+ expect { update }.to change { order.state }.from('payment').to('complete')+ expect(response).to redirect_to spree.checkout_complete_path(order.token)+ end+ end+ end++ context 'state_lock_version' do+ let(:post_params) do+ {+ token: order.token,+ state: 'address',+ order: {+ ship_address_attributes: {+ firstname: 'John',+ lastname: 'Doe',+ address1: '7735 Old Georgetown Road',+ city: 'Bethesda',+ zipcode: '20814',+ phone: '3014445002',+ state_id: store.default_country.states.first.id,+ country_id: store.default_country.id,+ },+ state_lock_version: 0+ }+ }+ end++ context 'correct' do+ it 'properly updates and increment version' do+ post :update, params: post_params+ expect(order.reload.state_lock_version).to eq 1+ end++ it 'order should receive ensure_valid_order_version callback' do+ expect_any_instance_of(described_class).to receive(:ensure_valid_state_lock_version)+ post :update, params: post_params+ end++ it 'order should have assigned changed attributes properly' do+ expect(order.last_ip_address).to be nil+ post :update, params: post_params+ expect(order.reload.last_ip_address).to eq '0.0.0.0'+ end+ end++ context 'incorrect' do+ before do+ order.update_columns(state_lock_version: 1, state: 'address')+ end++ it 'order should receieve ensure_valid_order_version callback' do+ expect_any_instance_of(described_class).to receive(:ensure_valid_state_lock_version)+ post :update, params: post_params+ end++ it 'redirects back to current state' do+ post :update, params: post_params+ expect(response).to redirect_to spree.checkout_state_path(order.token, 'address')+ expect(flash[:error]).to eq 'The order has already been updated.'+ end+ end+ end+ end++ context 'save unsuccessful' do+ before do+ allow(order).to receive_messages user: user+ allow(order).to receive_messages update: false+ end++ it 'does not assign order' do+ post :update, params: { state: 'address', token: order.token }+ expect(assigns[:order]).not_to be_nil+ end++ it 'does not change the order state' do+ post :update, params: { state: 'address', token: order.token }+ end++ it 'renders the edit template' do+ post :update, params: { state: 'address', token: order.token }+ expect(response).to render_template :edit+ end+ end++ context 'Spree::Core::GatewayError' do+ before do+ allow_any_instance_of(Spree::Order).to receive(:update).and_raise(Spree::Core::GatewayError.new('Invalid something or other.'))+ post :update, params: { state: 'address', token: order.token }+ end++ it 'renders the edit template and display exception message' do+ expect(response).to render_template :edit+ expect(flash.now[:error]).to eq(Spree.t(:spree_gateway_error_flash_for_checkout))+ expect(assigns(:order).errors[:base]).to include('Invalid something or other.')+ end+ end++ context 'fails to transition from address' do+ let(:order) do+ create(:order_with_line_items).tap do |order|+ order.next!+ expect(order.state).to eq('address')+ end+ end++ context 'when the country is not a shippable country' do+ before do+ order.update_column(:user_id, nil)+ order.ship_address.tap do |address|+ # A different country which is not included in the list of shippable countries+ address.country = FactoryBot.create(:country, name: 'Australia')+ address.state_name = 'Victoria'+ address.save+ end+ end++ it 'due to no available shipping rates for any of the shipments' do+ expect(order.shipments.count).to eq(1)+ order.shipments.first.shipping_rates.delete_all++ put :update, params: { state: order.state, order: {}, token: order.token }+ expect(flash[:error]).to eq(Spree.t(:items_cannot_be_shipped, product_names: order.products.pluck(:name).to_sentence))+ expect(response).to redirect_to(spree.checkout_state_path(order.token, 'address'))+ end+ end+ end++ context 'with missing shipping rates' do+ let(:address) { create(:address) }+ let!(:order) { create(:order_with_totals, store: store, ship_address: address, user: user) }++ before do+ allow_any_instance_of(Spree::Order).to receive_messages empty_rate_products: [order.line_items.first]+ end++ it 'renders update without redirect if missing items detected' do+ put :update, params: { state: 'address', order: {}, token: order.token }, format: :turbo_stream+ expect(response).not_to have_http_status(:redirect)+ expect(response).to render_template :update+ end+ end++ context 'When last inventory item has been purchased' do+ let(:product) { create(:product, name: 'Amazing Object') }+ let(:variant) { create(:variant, product: product) }+ let(:stock_item) { variant.stock_items.first }+ let(:line_item) { create(:line_item, variant: variant, quantity: 1) }+ let(:order) { create(:order_with_line_items, store: store, state: :payment, user_id: nil) }++ before do+ order.line_items << line_item+ stock_item.update(backorderable: false)+ end++ context 'and back orders are not allowed' do+ before do+ post :update, params: { state: 'payment', token: order.token }+ end++ it 'redirects to cart' do+ expect(response).to redirect_to spree.cart_path+ end++ it 'sets flash message for no inventory' do+ expect(flash[:error]).to eq(+ Spree.t('cart_line_item.out_of_stock', li_name: line_item.name)+ )+ end+ end+ end++ it 'does remove unshippable items before payment' do+ expect { post :update, params: { state: 'payment', token: order.token } }.to change { order.reload.line_items.size }+ end++ context 'Address Book' do+ let!(:user) { create(:user) }+ let!(:variant) { create(:product, sku: 'Demo-SKU').master }+ let!(:address) { create(:address, user: user, country: country, state: state) }+ let!(:order) { create(:order, store: store, bill_address_id: nil, ship_address_id: nil, user: user, state: 'address') }+ let(:address_params) { build(:address, country: country, state: state).attributes }++ before do+ Spree::Cart::AddItem.call(order: order, variant: variant, quantity: 1)+ allow(order).to receive(:available_shipping_methods).and_return [stub_model(Spree::ShippingMethod)]+ allow(order).to receive(:available_payment_methods).and_return [stub_model(Spree::PaymentMethod)]+ allow(order).to receive(:ensure_available_shipping_rates).and_return true+ end++ describe 'on address step' do+ it 'set ship_address_id' do+ put_address_to_order(ship_address_id: address.id)+ expect(order.reload.ship_address).to eq(address)+ end++ it 'set address attributes' do+ expect { put_address_to_order(ship_address_attributes: address_params) }.to change(user.addresses, :count).by(1)+ expect(order.ship_address).not_to be_nil+ expect(order.ship_address.user).to eq(user)+ end+ end++ private++ def put_address_to_order(params)+ put :update, params: { state: 'address', order: params, token: order.token }+ order.reload+ end+ end++ context 'same as shipping' do+ let(:user) { create(:user) }+ let(:ship_address) { create(:address, user: user) }+ let(:order) { create(:order_with_line_items, state: 'payment', store: store, bill_address: nil, ship_address: ship_address, user: user) }++ context 'same as shipping' do+ subject { patch :update, params: { token: order.token, state: 'payment', order: { use_shipping: 'true' } } }++ before do+ subject+ order.reload+ user.reload+ end++ it 'uses shipping address' do+ expect(order.bill_address).to eq(ship_address)+ end++ it 'sets the address as user billing default' do+ expect(user.bill_address).to eq(ship_address)+ end+ end++ context 'different from shipping' do+ let(:bill_address_attributes) do+ {+ firstname: 'John',+ lastname: 'Doe',+ address1: '7735 Old Georgetown Road',+ city: 'Bethesda',+ country_id: store.default_country.id,+ state_id: store.default_country.states.first.id,+ zipcode: '20814',+ phone: '3014445555'+ }+ end++ it 'creates new address and assigns it' do+ order+ expect { put :update, params: { token: order.token, state: 'payment',order: { bill_address_attributes: bill_address_attributes } } }.to change {+ order.reload.bill_address+ }.from(nil).to(an_instance_of(Spree::Address)).and change {+ Spree::Address.count+ }.by(1)+ expect(order.reload.billing_address).to be_present+ expect(order.billing_address.address1).to eq bill_address_attributes[:address1]+ expect(order.billing_address.user).to eq user++ expect(user.reload.bill_address).to eq(order.billing_address)+ end+ end+ end+ end++ describe '#complete' do+ context 'when order is in payment state' do+ let(:order) { create(:order_with_line_items, state: 'payment', store: store) }++ it 'redirects back to cart' do+ get :complete, params: { token: order.token }+ expect(response).to redirect_to spree.cart_path+ end+ end++ context 'when order is in complete state' do+ let(:order) { create(:completed_order_with_totals, store: store) }++ before { get :complete, params: { token: order.token } }++ it 'renders the page' do+ expect(response).to render_template(:complete)+ end++ it 'clears out the session' do+ expect(session[:checkout_completed]).to be_nil+ end+ end+ end++ describe '#apply_coupon_code' do+ let(:user) { create(:user) }+ let!(:order) { create(:order_with_line_items, state: 'payment', store: store, bill_address: nil, user: user) }+ let!(:line_item) { create(:line_item, order: order) }+ let!(:shipment) { create(:shipment, order: order) }+ let!(:promotion) { create(:promotion, name: 'Free shipping', code: 'freeship', stores: [store]) }+ let!(:promotion_action) { Spree::PromotionAction.create(promotion_id: promotion.id, type: 'Spree::Promotion::Actions::FreeShipping') }++ subject { patch :apply_coupon_code, params: { token: order.token, coupon_code: coupon_code } }++ context 'when coupon code is valid' do+ let(:coupon_code) { 'FREESHIP' }++ it 'applies the coupon code' do+ expect(controller).to receive(:track_event).with('coupon_entered', hash_including(order: order, coupon_code: coupon_code))+ expect(controller).to receive(:track_event).with('coupon_applied', hash_including(order: order, coupon_code: coupon_code))++ subject+ expect(response).to redirect_to spree.checkout_path(order.token)+ expect(order.reload.promotions).to include(promotion)+ end+ end++ context 'when coupon code is invalid' do+ let(:coupon_code) { 'fake_coupon_code' }++ it 'does not apply the coupon code' do+ expect(controller).to receive(:track_event).with('coupon_entered', hash_including(order: order, coupon_code: coupon_code))+ expect(controller).to receive(:track_event).with('coupon_denied', hash_including(order: order, coupon_code: coupon_code))++ subject+ expect(response).to redirect_to spree.checkout_path(order.token)+ expect(order.reload.promotions).to be_empty+ end+ end++ xdescribe 'apply gift card' do+ let(:user) { create(:user) }+ let(:gift_card) { create :gift_card, store: store, user: user }+ let(:coupon_code) { gift_card.code.upcase }++ subject { patch :apply_coupon_code, params: { token: order.token, coupon_code: gift_card.code.upcase } }++ context 'when gift card is valid' do+ it 'applies the gift card' do+ expect(controller).to receive(:track_event).with('coupon_entered', hash_including(order: order, coupon_code: gift_card.code.upcase))+ expect(controller).to receive(:track_event).with('coupon_applied', hash_including(order: order, coupon_code: gift_card.code.upcase))++ subject+ expect(order.reload.gift_card).to eq(gift_card)+ expect(order.gift_card_total).to eq(gift_card.amount)+ expect(order.total_applied_store_credit).to eq(gift_card.amount)+ end+ end++ xcontext 'for a gift card with minimal order amount' do+ let(:user) { create(:user) }+ let(:gift_card) { create(:gift_card, store: store, minimum_order_amount: minimal_order_amount, user: user) }++ context 'when the condition is met' do+ let(:minimal_order_amount) { order.total }++ it 'applies the gift card' do+ expect(controller).to receive(:track_event).with('coupon_entered', hash_including(order: order, coupon_code: gift_card.code.upcase))+ expect(controller).to receive(:track_event).with('coupon_applied', hash_including(order: order, coupon_code: gift_card.code.upcase))++ subject++ expect(assigns[:result].status_code).to eq(:gift_card_applied)+ expect(assigns[:result].error).to be(nil)++ expect(order.reload.gift_card).to eq(gift_card)+ expect(order.gift_card_total).to eq(gift_card.amount)+ expect(order.total_applied_store_credit).to eq(gift_card.amount)+ end+ end++ context 'when the condition is not met' do+ let(:minimal_order_amount) { order.total + 1 }++ it 'skips applying the gift card' do+ subject++ expect(assigns[:result].status_code).to eq(:gift_card_minimum_order_value_error)+ expect(assigns[:result].error).to eq("You can't apply the gift card. The order total must be at least #{gift_card.display_minimum_order_amount}, excluding shipping cost.")++ expect(order.reload.gift_card).to be(nil)+ expect(order.gift_card_total).to eq(0)+ expect(order.total_applied_store_credit).to eq(0)+ end++ it 'tracks the denied gift card' do+ expect(controller).to receive(:track_event).with('coupon_entered', hash_including(order: order, coupon_code: gift_card.code.upcase))+ expect(controller).to receive(:track_event).with('coupon_denied', hash_including(order: order, coupon_code: gift_card.code.upcase))++ subject+ end+ end+ end++ xcontext 'applying a gift card already redeemed by order' do+ let!(:old_order) { create(:order_with_totals, store: store, user: user) }+ let(:gift_card) { create :gift_card, store: store, amount: 50 }+ let(:coupon_code) { gift_card.code.upcase }++ before do+ old_order.update_column(:total, 30)+ order.update_column(:total, 30)+ end++ it 'applies the gift card to both orders' do+ patch :apply_coupon_code, params: { token: old_order.token, coupon_code: gift_card.code.upcase }+ patch :apply_coupon_code, params: { token: order.token, coupon_code: gift_card.code.upcase }++ expect(old_order.reload.gift_card).to eq(gift_card)+ expect(old_order.gift_card_total).to eq(30)++ expect(order.reload.gift_card).to eq(gift_card)+ expect(order.gift_card_total).to eq(20)+ end+ end+ end+ end++ describe '#remove_coupon_code' do+ let(:user) { create(:user) }+ let!(:order) { create(:order_with_line_items, state: 'payment', store: store, bill_address: nil, user: user) }+ let!(:line_item) { create(:line_item, order: order) }+ let!(:shipment) { create(:shipment, order: order) }+ let(:coupon_code) { 'FREESHIP' }+ let!(:promotion) { create(:promotion, name: 'Free shipping', code: coupon_code, stores: [store]) }+ let!(:promotion_action) { Spree::PromotionAction.create(promotion_id: promotion.id, type: 'Spree::Promotion::Actions::FreeShipping') }++ subject { delete :remove_coupon_code, params: { token: order.token, coupon_code: coupon_code } }++ before do+ order.coupon_code = coupon_code+ Spree::PromotionHandler::Coupon.new(order).apply+ order.save!+ end++ it 'removes the promotion' do+ expect(order.reload.promotions).to include(promotion)+ expect(controller).to receive(:track_event).with('coupon_removed', hash_including(order: order, coupon_code: coupon_code))++ subject+ expect(response).to redirect_to spree.checkout_path(order.token)+ expect(order.promotions).not_to include(promotion)+ end++ xcontext 'for a gift card' do+ let(:gift_card) { create :gift_card, store: store }+ let(:coupon_code) { gift_card.code.upcase }++ subject { patch :remove_coupon_code, params: { token: order.token, gift_card: coupon_code } }++ before do+ Spree::GiftCards::Apply.call(order: order, gift_card: gift_card)+ end++ it 'removes the gift card' do+ subject+ expect(order.reload.gift_card).to be nil+ expect(order.gift_card_total).to eq(0)+ expect(order.total_applied_store_credit).to eq(0)+ end+ end+ end++ describe '#load_order' do+ subject do+ get :edit, params: { state: 'address', token: order.token }+ end++ context 'when order is found' do+ context 'when order is completed' do+ before { allow_any_instance_of(Spree::Order).to receive_messages(completed?: true) }++ it 'does not update order tracking attributes' do+ expect{ subject }.not_to change{ order.updated_at }+ end+ end++ context 'when order is editable and not completed' do+ it 'does not update order tracking attributes' do+ expect{ subject }.not_to change{ order.reload.updated_at }+ end+ end+ end+ end+end