class MusicTodayApiWrapper::Services::CheckoutServices

Public Class Methods

new() click to toggle source
# File lib/services/checkout_services.rb, line 11
def initialize
  @common_response = RestClients::CommonResponse.new
  @rest_client = RestClient.new
end

Public Instance Methods

checkout(address, items) click to toggle source

rubocop:disable AbcSize

# File lib/services/checkout_services.rb, line 17
def checkout(address, items)
  items_hash = items.map(&:as_hash)
  address_hash =
    address.as_hash.merge(id: 1, items: items_hash)

  @common_response.work do
    params = checkout_params(address_hash)
    response = @rest_client.checkout(params)

    session = response.data[:session]
    response_data = @common_response.data

    response_data[:session] =
      Resources::Checkout::Session.from_hash(session)
    response_data[:items] = session['items'].map do |item|
      Resources::Purchase::Item.from_hash(item)
    end
  end
end
confirm_and_purchase(order) click to toggle source
# File lib/services/checkout_services.rb, line 44
def confirm_and_purchase(order)
  purchase do
    address_collection =
      Resources::Address.from_destinations(order.destinations)
    response = checkout(address_collection.first, order.items)
    order.items = response.data[:items]
    @rest_client.purchase(orders: [order.as_hash])
  end
end
only_purchase(order) click to toggle source
# File lib/services/checkout_services.rb, line 37
def only_purchase(order)
  purchase do
    orders = [order.as_hash]
    @rest_client.purchase(orders: orders)
  end
end

Private Instance Methods

checkout_params(address) click to toggle source
# File lib/services/checkout_services.rb, line 69
def checkout_params(address)
  { storeId: @rest_client.catalog_number,
    currency: 'USD',
    priceLevel: '',
    addresses: [address],
    promotions: [] }
end
purchase() { || ... } click to toggle source
# File lib/services/checkout_services.rb, line 56
def purchase
  @common_response.work do
    response = yield
    if response.success?
      @common_response.data[:invoice] =
        Resources::Purchase::Invoice.from_hash(response.data[:order])
    else
      @common_response.errors = response.errors
      @common_response.data = {}
    end
  end
end