Class: Afterpay::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/afterpay/order.rb

Overview

The Order object for creating an order to ‘/v2/checkouts`

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total: , items: , consumer: , success_url: , cancel_url: , payment_type: ) ⇒ Order

Initializes an Order object

Parameters:

  • total (Money) (defaults to: )

    a Money object

  • items (Array<Afterpay::Item>) (defaults to: )

    receives items for order

  • consumer (Afterpay::Consume) (defaults to: )

    the consumer for the order

  • success_url (String) (defaults to: )

    the path to redirect on successful payment

  • cancel_url (String) (defaults to: )

    the path to redirect on failed payment

  • payment_type (String) (defaults to: )

    Payment type defaults to Config#type

  • shipping (Money)

    optional the billing Address

  • discounts (Array<Afterpay::Discount>)

    optional discounts

  • billing_address (<Afterpay::Address>)

    optional the billing Address

  • shipping_address (<Afterpay::Address>)

    optional the shipping Address



60
61
62
63
64
65
66
# 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

Instance Attribute Details

#billingObject

Returns the value of attribute billing.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def billing
  @billing
end

#billing_addressObject

Returns the value of attribute billing_address.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def billing_address
  @billing_address
end

#cancel_urlObject

Returns the value of attribute cancel_url.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def cancel_url
  @cancel_url
end

#consumerObject

Returns the value of attribute consumer.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def consumer
  @consumer
end

#discountsObject

Returns the value of attribute discounts.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def discounts
  @discounts
end

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'lib/afterpay/order.rb', line 10

def error
  @error
end

#expiryObject (readonly)

Returns the value of attribute expiry.



10
11
12
# File 'lib/afterpay/order.rb', line 10

def expiry
  @expiry
end

#itemsObject

Returns the value of attribute items.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def items
  @items
end

#merchant_referenceObject

Returns the value of attribute merchant_reference.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def merchant_reference
  @merchant_reference
end

#payment_typeObject

Returns the value of attribute payment_type.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def payment_type
  @payment_type
end

#redirect_checkout_urlObject

Returns the value of attribute redirect_checkout_url.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def redirect_checkout_url
  @redirect_checkout_url
end

#shippingObject

Returns the value of attribute shipping.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def shipping
  @shipping
end

#shipping_addressObject

Returns the value of attribute shipping_address.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def shipping_address
  @shipping_address
end

#success_urlObject

Returns the value of attribute success_url.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def success_url
  @success_url
end

#taxObject

Returns the value of attribute tax.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def tax
  @tax
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/afterpay/order.rb', line 10

def token
  @token
end

#totalObject

Returns the value of attribute total.



6
7
8
# File 'lib/afterpay/order.rb', line 6

def total
  @total
end

Class Method Details

.create(*args) ⇒ Order::Response

Helper function to create an Order and calls #create

Returns:

  • (Order::Response)

    containing token, error, valid?



43
44
45
# File 'lib/afterpay/order.rb', line 43

def self.create(*args)
  new(*args).create
end

.find(token) ⇒ Order

Finds Order from Afterpay API

Parameters:

  • token (String)

Returns:



15
16
17
18
19
# File 'lib/afterpay/order.rb', line 15

def self.find(token)
  request = Afterpay.client.get("/v2/checkouts/#{token}")

  Order.from_response(request.body)
end

.from_response(response) ⇒ Order

Builds Order from response

Parameters:

  • response (Hash)

    response params from API

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#createResponse

Sends the create request to Afterpay server

Returns:

  • (Response)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/afterpay/order.rb', line 113

def success?
  !@token.nil?
end

#to_hashObject

Builds structure to API specs



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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