Skip to content

Commit

Permalink
🧹 Marketplace: Cache the fee amount on the Order (#1701)
Browse files Browse the repository at this point in the history
- #1327

In order to pull the splitting of the payment to a retriable background
job; we need to cache the payment processor fee information.
  • Loading branch information
zspencer authored Jul 27, 2023
1 parent 57a898b commit 3186adf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/furniture/marketplace/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Order < Record
paid: "paid"
}

monetize :payment_processor_fee_cents
def vendors_share
product_total - payment_processor_fee
end

def product_total
ordered_products.sum(0, &:price_total)
end
Expand Down
2 changes: 1 addition & 1 deletion app/furniture/marketplace/stripe_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create
latest_charge = Stripe::Charge.retrieve(payment_intent.latest_charge, api_key: marketplace.stripe_api_key)
balance_transaction = Stripe::BalanceTransaction.retrieve(latest_charge.balance_transaction, api_key: marketplace.stripe_api_key)

order.update(status: :paid, placed_at: DateTime.now)
order.update(status: :paid, placed_at: DateTime.now, payment_processor_fee_cents: balance_transaction.fee)

Order::ReceivedMailer.notification(order).deliver_later
Order::PlacedMailer.notification(order).deliver_later
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MarketplaceAddPaymentProcessorFeesToOrder < ActiveRecord::Migration[7.0]
def change
add_monetize :marketplace_orders, :payment_processor_fee, null: true, default: nil
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_07_13_230334) do
ActiveRecord::Schema[7.0].define(version: 2023_07_25_192832) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -171,6 +171,8 @@
t.string "delivery_notes"
t.string "contact_email_ciphertext"
t.uuid "delivery_area_id"
t.integer "payment_processor_fee_cents", default: 0, null: false
t.string "payment_processor_fee_currency", default: "USD", null: false
t.index ["delivery_area_id"], name: "index_marketplace_orders_on_delivery_area_id"
t.index ["marketplace_id"], name: "index_marketplace_orders_on_marketplace_id"
t.index ["shopper_id"], name: "index_marketplace_orders_on_shopper_id"
Expand Down
8 changes: 8 additions & 0 deletions spec/furniture/marketplace/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
it { is_expected.to belong_to(:shopper).inverse_of(:orders) }
it { is_expected.to belong_to(:delivery_area).inverse_of(:orders).optional }

describe "#vendors_share" do
subject(:vendors_share) { order.vendors_share }

let(:order) { build(:marketplace_order, :with_products, payment_processor_fee: 10_00) }

it { is_expected.to eq(order.product_total - order.payment_processor_fee) }
end

describe "#price_total" do
subject(:price_total) { order.price_total }

Expand Down

0 comments on commit 3186adf

Please sign in to comment.