module Zinc class Order < Resource attr_accessor :request_id, :_type, :code, :message, :status_updates, :error, :price_components, # A Zinc::PriceComponent object. :merchant_order_ids, # Array of Zinc::MerchantOrderId objects. :tracking, # Array of Zinc::Tracking objects. :request, # ? :delivery_dates, # ? :account_status # A Zinc::AccountStatus object. def initialize(hash) @request_id = hash['request_id'] if hash['request_id'] @_type = hash['_type'] if hash['_type'] @code = hash['code'] if hash['code'] @message = hash['message'] if hash['message'] @status_updates = hash['status_updates'] if hash['status_updates'] @error = hash['error'] if hash['error'] @price_components = PriceComponents.new(hash['price_components']) if hash['price_components'] @merchant_order_ids = hash['merchant_order_ids'].map { |hash| MerchantOrderId.new(hash) } if hash['merchant_order_ids'] @tracking = hash['tracking'].map { |hash| Tracking.new(hash) } if hash['tracking'] @request = hash['request'] if hash['request'] @delivery_dates = hash['delivery_dates'] if hash['delivery_dates'] @account_status = AccountStatus.new(hash['account_status']) if hash['account_status'] end class << self def all(offset: 0, limit: 20) response = get(path: 'orders', params: { offset: offset, limit: limit }) response['orders'].map { |hash| Order.new(hash) } end def retrieve(request_id) response = get(path: "orders/#{request_id}") Order.new(response) end def create(retailer:, retailer_credentials:, products:, payment_method:, billing_address:, shipping_address:, shipping:, options: {}) params = { retailer: retailer, retailer_credentials: retailer_credentials.to_hash, products: products.map(&:to_hash), payment_method: payment_method.to_hash, billing_address: billing_address.to_hash, shipping_address: shipping_address.to_hash, shipping: shipping.to_hash } # Valid options: # - is_gift # - gift_message # - max_price # - webhooks # - client_notes # - promo_codes # - po_number # - bundled params.merge!(options) unless params.empty? response = post(path: 'orders', params: params) Order.new(response) end def abort(request_id) response = post(path: "orders/#{request_id}/abort") Order.new(response) end def cancel(request_id) response = post(path: "orders/#{request_id}/cancel") Order.new(response) end end end end