Skip to content

Commit

Permalink
🥗🥔✨Marketplace: Buying Products Pre-populate shopper email
Browse files Browse the repository at this point in the history
  • Loading branch information
zspencer committed Apr 20, 2023
1 parent 43e5779 commit 0cf00ea
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app/furniture/marketplace/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions app/furniture/marketplace/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions app/furniture/marketplace/shopper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions spec/furniture/marketplace/checkouts_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -21,22 +21,38 @@

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

context "when the Cart is empty" do
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
Expand Down

0 comments on commit 0cf00ea

Please sign in to comment.