class Afterpay::Order

The Order object for creating an order to `/v1/orders`

Attributes

billing[RW]
billing_address[RW]
cancel_url[RW]
consumer[RW]
discounts[RW]
error[R]
expiry[R]
items[RW]
payment_type[RW]
reference[RW]
shipping[RW]
shipping_address[RW]
success_url[RW]
tax[RW]
token[R]
total[RW]

Public Class Methods

create(*args) click to toggle source

Helper function to create an Order and calls create

@param (see initialize) @return [Order::Response] containing token, error, valid?

# File lib/afterpay/order.rb, line 43
def self.create(*args)
  new(*args).create
end
find(token) click to toggle source

Finds Order from Afterpay API @param token [String] @return [Order]

# File lib/afterpay/order.rb, line 15
def self.find(token)
  request = Afterpay.client.get("/v1/orders/#{token}")

  Order.from_response(request.body)
end
from_response(response) click to toggle source

Builds Order from response @param response [Hash] response params from API @return [Order]

# File lib/afterpay/order.rb, line 24
def self.from_response(response)
  return nil if response.nil?

  new(
    total: Utils::Money.from_response(response[:total]),
    consumer: Consumer.from_response(response[:consumer]),
    items: response[:items].map { |item| Item.from_response(item) },
    billing_address: Address.from_response(response[:billing]),
    shipping_address: Address.from_response(response[:shipping]),
    discounts: response[:discounts].map { |discount| Discount.from_response(discount) },
    tax: Utils::Money.from_response(response[:taxAmount]),
    shipping: Utils::Money.from_response(response[:shippingAmount])
  )
end
new(attributes = {}) click to toggle source

Initializes an Order object

@overload initialize(total:, items:, consumer:, success_url:, cancel_url:, payment_type:)

@param total [Money] a Money object
@param items [Array<Afterpay::Item>] receives items for order
@param consumer [Afterpay::Consume] the consumer for the order
@param success_url [String] the path to redirect on successful payment
@param cancel_url [String] the path to redirect on failed payment
@param payment_type [String] Payment type defaults to {Config#type}
@param shipping [Money] optional the billing Address
@param discounts [Array<Afterpay::Discount>] optional discounts
@param billing_address [<Afterpay::Address>] optional the billing Address
@param shipping_address [<Afterpay::Address>] optional the shipping Address
# File lib/afterpay/order.rb, line 60
def initialize(attributes = {})
  attributes.each { |key, value| instance_variable_set(:"@#{key}", value) }
  @apayment_type ||= Afterpay.config.type
  @token ||= nil
  @expiry ||= nil
  @error = nil
end

Public Instance Methods

create() click to toggle source

Sends the create request to Afterpay server @return [Response]

# File lib/afterpay/order.rb, line 93
def create
  request = Afterpay.client.post("/v1/orders") do |req|
    req.body = to_hash
  end
  response = request.body

  if request.success?
    @expiry = Time.parse(response[:expires])
    @token = response[:token]
  else
    @error = Error.new(response)
  end
  self
end
success?() click to toggle source
# File lib/afterpay/order.rb, line 108
def success?
  !@token.nil?
end
to_hash() click to toggle source

Builds structure to API specs

# File lib/afterpay/order.rb, line 69
def to_hash
  data = {
    totalAmount: Utils::Money.api_hash(total),
    consumer: consumer.to_hash,
    items: items.map(&:to_hash),
    merchant: {
      redirectConfirmUrl: success_url,
      redirectCancelUrl: cancel_url
    },
    merchantReference: reference,
    taxAmount: tax,
    paymentType: payment_type
  }

  data[:taxAmount] = Utils::Money.api_hash(tax) if tax
  data[:shippingAmount] = Utils::Money.api_hash(shipping) if shipping
  data[:discounts] = discounts.map(&:to_hash) if discounts
  data[:billing] = billing_address.to_hash if billing_address
  data[:shipping] = shipping_address.to_hash if shipping_address
  data
end