-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor order creation logic by introducing OrderCreator service and…
… moving order item building to the Order model
- Loading branch information
Showing
5 changed files
with
63 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationService | ||
def self.call(*args) | ||
new(*args).call | ||
end | ||
|
||
def self.call!(*args) | ||
new(*args).call! | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
class OrderCreator < ApplicationService | ||
def initialize(session) | ||
@session = session | ||
end | ||
|
||
def call! | ||
shipping_details = @session["shipping_details"] | ||
address = | ||
"#{shipping_details["address"]["line1"]} #{shipping_details["address"]["city"]}, #{shipping_details["address"]["state"]} #{shipping_details["address"]["postal_code"]}" # rubocop:disable Layout/LineLength | ||
full_session = Stripe::Checkout::Session.retrieve(id: @session["id"], expand: ["line_items"]) | ||
line_items = full_session.line_items | ||
|
||
Order.transaction do | ||
order = | ||
Order.new( | ||
user_id: @session["metadata"]["user_id"], | ||
customer_full_name: @session["metadata"]["user_full_name"], | ||
customer_email: @session["customer_details"]["email"], | ||
customer_address: address, | ||
total: @session["amount_total"].to_f / 100 | ||
) | ||
order.build_order_items(line_items) | ||
order.save! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters