class Afterpay::Order
The Order
object for creating an order to `/v2/checkouts`
Attributes
billing[RW]
billing_address[RW]
cancel_url[RW]
consumer[RW]
discounts[RW]
error[R]
expiry[R]
items[RW]
merchant_reference[RW]
payment_type[RW]
redirect_checkout_url[RW]
shipping[RW]
shipping_address[RW]
success_url[RW]
tax[RW]
token[R]
total[RW]
Public Class Methods
create(*args)
click to toggle source
find(token)
click to toggle source
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 98 def create request = Afterpay.client.post("/v2/checkouts") do |req| req.body = to_hash end response = request.body if request.success? @expiry = response[:expires] @token = response[:token] @redirect_checkout_url = response[:redirectCheckoutUrl] else @error = Error.new(response) end self end
success?()
click to toggle source
# File lib/afterpay/order.rb, line 113 def success? !@token.nil? end
to_hash()
click to toggle source
Builds structure to API specs
# File lib/afterpay/order.rb, line 72 def to_hash data = { amount: Utils::Money.api_hash(total), consumer: consumer.to_hash, merchant: { redirectConfirmUrl: success_url, redirectCancelUrl: cancel_url }, merchantReference: merchant_reference, taxAmount: tax, paymentType: payment_type } data[:items] = items.map(&:to_hash) if items 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