Created
September 24, 2020 14:23
-
-
Save bearded-avenger/6d277d887275e7cf621206a4bfc7f35b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module TenantServices | |
class CheckoutService | |
def initialize(params) | |
@site = params[:site] | |
@cart = params[:cart] | |
@customer = params[:customer] | |
@coupon_code = params[:coupon_code] | |
@selected_country = params[:selected_country] | |
@stripe_token = params[:stripe_token] | |
@paypal_token = params[:paypal_token] | |
@stripe_card = params[:stripe_card] | |
@ip_address = params[:ip_address] | |
@metadata = params[:metadata] | |
@save_card = params[:save_card] && params[:save_card] == '1' ? true : false | |
@location_data ||= AppServices::GeoFinderService.new(ip:@ip_address).call | |
@totals ||= TenantServices::OrderCalculationService.new({site:@site, customer: @customer, cart: @cart, amount: @cart.sub_total_cents, coupon_code: @coupon_code, location_data: @location_data, ip_address: @ip_address}).call | |
end | |
def call | |
if @stripe_token.present? | |
TenantServices::CheckoutServiceStripe.new(stripe_params).call | |
elsif @paypal_token.present? | |
TenantServices::CheckoutServicePaypal.new(paypal_params).call | |
else | |
OpenStruct.new({success?:false, error:'No token detected for Stripe or PayPal.'}) | |
end | |
end | |
private | |
attr_reader :site, :cart, :customer, :coupon_code, :selected_country, :stripe_token, :stripe_account, :paypal_token, :ip_address, :metadata, :save_card, :totals, :location_data | |
def stripe_params | |
base_params.merge(stripe_token: stripe_token, save_card: save_card) | |
end | |
def paypal_params | |
base_params.merge(paypal_token: paypal_token) | |
end | |
def base_params | |
{ | |
site: site, | |
cart: cart, | |
totals: totals, | |
customer: customer, | |
coupon_code: coupon_code, | |
selected_country: selected_country, | |
ip_address: ip_address, | |
metadata: metadata, | |
location_data: location_data | |
} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment