Skip to content

Commit

Permalink
Updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rosschapman committed Oct 31, 2023
1 parent 37ac3e2 commit 178ef2d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
46 changes: 27 additions & 19 deletions app/furniture/marketplace/square_order.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# SquareOrder is a service class for coordinating and executing interactions
# between Convene and a seller's Square account.
#
# The current behavior is limited to registering an order in a seller's account
# to syncronize record keeping between Convene and Square.
#
# NOTE: the simple `Markeplace > SquareOrder` namespacing was chosen
# to avoid collision with the `Square` namespace already included by Square's gem.
class Marketplace
class SquareOrder
def initialize(order)
Expand All @@ -7,19 +15,23 @@ def initialize(order)
@space = order.marketplace.space
@ordered_products = @order.ordered_products
@square_location_id = @marketplace.settings["square_location_id"]
@square_order_id = nil
@square_payment_id = nil
end

def send_to_square_seller_dashboard
def send_to_seller_dashboard
square_create_order_response = create_square_order

if square_create_order_response
square_create_payment_response = create_square_order_payment(square_create_order_response.body.order[:id])
@square_order_id = square_create_order_response.body.order[:id]
square_create_payment_response = create_square_order_payment
@square_payment_id = square_create_payment_response.body.payment[:id]

# This data is intended for use in debugging, etc... until we further
# the Square integration productize
{
order_id: square_create_order_response.body.order[:id],
payment_id: square_create_payment_response.body.payment[:id]
order_id: @square_order_id,
payment_id: @square_payment_id
}
else
# TODO: Noop for now
Expand All @@ -32,26 +44,22 @@ def send_to_square_seller_dashboard
end

private def create_square_order
square_create_order_body = build_square_create_order_body(@arketplace)
square_create_order_body = prepare_square_create_order_body(@marketplace)
@marketplace.square_client.orders.create_order(body: square_create_order_body)
end

# NOTE: Square requires that orders are paid in order to show up in the Seller
# Dashboard
private def create_square_order_payment(square_order_id)
square_create_payment_body = build_square_create_order_payment_body(
square_order_id,
@square_location_id,
@space.id
)
# NOTE: Square requires that orders must have be in a state of complete
# payment to show up in the Seller Dashboard
private def create_square_order_payment
square_create_payment_body = prepare_square_create_order_payment_body

marketplace.square_client.payments.create_payment(body: square_create_payment_body)
end

# NOTE: Square requires that orders include fulfillments in order to show up
# NOTE: Square requires that orders must include fulfillments to show up
# in the Seller Dashboard
# See: https://developer.squareup.com/docs/orders-api/create-orders
private def build_square_create_order_body(marketplace)
private def prepare_square_create_order_body
location_id = @marketplace.settings["square_location_id"]
customer_id = @shopper.id

Expand Down Expand Up @@ -115,19 +123,19 @@ def send_to_square_seller_dashboard
# square order (`line_items.sum(&base_price_money[:amount])`) to be valid
#
# TODO: Consider adding a price check?
private def build_square_create_order_payment_body(square_order_id, square_location_id, space_id)
private def prepare_square_create_order_payment_body
{
source_id: "EXTERNAL",
idempotency_key: square_idemp_key,
amount_money: {
amount: @order.product_total.cents,
currency: "USD"
},
order_id: square_order_id,
location_id: square_location_id,
order_id: @square_order_id,
location_id: @square_location_id,
external_details: {
type: "OTHER",
source: "CONVENE_SYSTEM_PAYMENT for Space #{space_id}"
source: "CONVENE_SYSTEM_PAYMENT for Space #{@space.id}"
# TODO: Need this later?
# source_fee_money: {
# amount: "test",
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 @@ -25,7 +25,7 @@ def create

if marketplace.square_order_notifications_enabled?
square_order = SquareOrder.new(order)
square_order.send_to_square_seller_dashboard
square_order.send_to_seller_dashboard
end

Order::ReceivedMailer.notification(order).deliver_later
Expand Down
36 changes: 35 additions & 1 deletion spec/furniture/marketplace/square_order_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
require "rails_helper"

RSpec.describe Marketplace::SquareOrder do
## TODO
describe "send_to_seller_dashboard" do
it "will return the correct success data" do
# TODO
end

it "will noop when an error occurs" do
# TODO
end
end

describe "#prepare_square_create_order_payment_body" do
it "returns the correct object shape and data" do
# TODO
end

it "returns the correct object shape and data" do
# TODO
end
end

describe "#prepare_square_create_order_body" do
it "returns the correct object shape and data for a logged-in shopper" do
# TODO
end

it "returns the correct object shape and data for a guest shopper" do
# TODO
end
end

describe "#square_idemp_key" do
it "returns a unique string made up of order id and 8 random numbers between 1-10" do
# TODO
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
double(Stripe::Charge, balance_transaction: "btx_2234")
}

let(:square_order) { instance_double(Marketplace::SquareOrder, send_to_square_seller_dashboard: nil) }
let(:square_order) { instance_double(Marketplace::SquareOrder, send_to_seller_dashboard: nil) }

before do
allow(Stripe::Webhook).to receive(:construct_event).with(anything, "sig_1234", marketplace.stripe_webhook_endpoint_secret).and_return(stripe_event)
Expand Down Expand Up @@ -88,7 +88,7 @@
context "when Square notifications are not enabled" do
it "does not attempt to transfer the order to seller's Square dashboard" do
call
expect(square_order).not_to(have_received(:send_to_square_seller_dashboard))
expect(square_order).not_to(have_received(:send_to_seller_dashboard))
end
end

Expand All @@ -101,7 +101,7 @@
it "attempts to transfer the order to seller's Square dashboard" do
call
expect(Marketplace::SquareOrder).to have_received(:new).with(order)
expect(square_order).to have_received(:send_to_square_seller_dashboard)
expect(square_order).to have_received(:send_to_seller_dashboard)
end
end
end
Expand Down

0 comments on commit 178ef2d

Please sign in to comment.