diff --git a/app/furniture/marketplace/cart.rb b/app/furniture/marketplace/cart.rb index 6c1fa9127..95931661c 100644 --- a/app/furniture/marketplace/cart.rb +++ b/app/furniture/marketplace/cart.rb @@ -20,6 +20,9 @@ class Cart < Record attribute :delivery_window, ::Marketplace::Delivery::WindowType.new has_encrypted :contact_phone_number has_encrypted :contact_email + def contact_email + super.presence || shopper&.email + end enum status: { pre_checkout: "pre_checkout", diff --git a/app/furniture/marketplace/checkout.rb b/app/furniture/marketplace/checkout.rb index a716b2ca0..e94cb17a3 100644 --- a/app/furniture/marketplace/checkout.rb +++ b/app/furniture/marketplace/checkout.rb @@ -16,6 +16,7 @@ def create_stripe_session(success_url:, cancel_url:) mode: "payment", success_url: success_url, cancel_url: cancel_url, + customer_email: cart.contact_email, payment_intent_data: { transfer_group: cart.id } diff --git a/app/furniture/marketplace/shopper.rb b/app/furniture/marketplace/shopper.rb index 4a7bb6e90..e4fd46055 100644 --- a/app/furniture/marketplace/shopper.rb +++ b/app/furniture/marketplace/shopper.rb @@ -5,6 +5,8 @@ class Shopper < Record self.table_name = "marketplace_shoppers" belongs_to :person, optional: true + delegate :email, to: :person, allow_nil: true + has_many :carts, inverse_of: :shopper has_many :orders, inverse_of: :shopper end diff --git a/spec/factories/furniture/marketplace.rb b/spec/factories/furniture/marketplace.rb index 21599baaf..ca6cd9425 100644 --- a/spec/factories/furniture/marketplace.rb +++ b/spec/factories/furniture/marketplace.rb @@ -57,6 +57,13 @@ end cart_products { Array.new(product_quantity) { association(:marketplace_cart_product, marketplace: marketplace) } } end + + trait :with_person do + transient do + person { build(:person) } + end + shopper { association(:marketplace_shopper, person: person) } + end end factory :marketplace_cart_product, class: "Marketplace::CartProduct" do diff --git a/spec/furniture/marketplace/checkouts_controller_request_spec.rb b/spec/furniture/marketplace/checkouts_controller_request_spec.rb index ab31dde34..830da5b71 100644 --- a/spec/furniture/marketplace/checkouts_controller_request_spec.rb +++ b/spec/furniture/marketplace/checkouts_controller_request_spec.rb @@ -4,13 +4,13 @@ let(:marketplace) { create(:marketplace) } let(:space) { marketplace.space } let(:room) { marketplace.room } - let(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } + let(:checkout) { build(:marketplace_checkout, cart: cart) } before { create(:stripe_utility, space: space) } describe "#create" do - subject(:completed_request) { + subject(:perform_request) { post polymorphic_path(checkout.location) response } @@ -21,14 +21,30 @@ before do allow(Stripe::Checkout::Session).to receive(:create).and_return(stripe_checkout_session) - allow_any_instance_of(ApplicationController).to receive(:session).and_return({guest_shopper_id: cart.shopper.id}) end - context "when a Guest checks out their Cart" do + context "when a Visitor checks out their Cart" do let(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } + before do + allow_any_instance_of(ApplicationController).to receive(:session).and_return({guest_shopper_id: cart.shopper.id}) + end + it "Redirects to Stripe" do - expect(completed_request).to redirect_to(stripe_checkout_session.url) + expect(perform_request).to redirect_to(stripe_checkout_session.url) + end + end + + context "when a Neighbor checks out their Cart" do + let(:cart) { create(:marketplace_cart, :with_person, :with_products, marketplace: marketplace) } + + it "passes the email to stripe" do + sign_in(space, cart.shopper.person) + perform_request + expect(Stripe::Checkout::Session).to have_received(:create).with( + hash_including(mode: "payment", customer_email: cart.contact_email), + {api_key: "not_real"} + ) end end @@ -36,7 +52,7 @@ let(:cart) { create(:marketplace_cart, marketplace: marketplace) } it "shows an error notice" do - completed_request + perform_request expect(flash[:alert]).to include("line items can't be blank") end end