Class: Afterpay::Order
- Inherits:
-
Object
- Object
- Afterpay::Order
- Defined in:
- lib/afterpay/order.rb
Overview
The Order object for creating an order to ‘/v2/checkouts`
Instance Attribute Summary collapse
-
#billing ⇒ Object
Returns the value of attribute billing.
-
#billing_address ⇒ Object
Returns the value of attribute billing_address.
-
#cancel_url ⇒ Object
Returns the value of attribute cancel_url.
-
#consumer ⇒ Object
Returns the value of attribute consumer.
-
#discounts ⇒ Object
Returns the value of attribute discounts.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#expiry ⇒ Object
readonly
Returns the value of attribute expiry.
-
#items ⇒ Object
Returns the value of attribute items.
-
#merchant_reference ⇒ Object
Returns the value of attribute merchant_reference.
-
#payment_type ⇒ Object
Returns the value of attribute payment_type.
-
#redirect_checkout_url ⇒ Object
Returns the value of attribute redirect_checkout_url.
-
#shipping ⇒ Object
Returns the value of attribute shipping.
-
#shipping_address ⇒ Object
Returns the value of attribute shipping_address.
-
#success_url ⇒ Object
Returns the value of attribute success_url.
-
#tax ⇒ Object
Returns the value of attribute tax.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
-
.create(*args) ⇒ Order::Response
Helper function to create an Order and calls #create.
-
.find(token) ⇒ Order
Finds Order from Afterpay API.
-
.from_response(response) ⇒ Order
Builds Order from response.
Instance Method Summary collapse
-
#create ⇒ Response
Sends the create request to Afterpay server.
-
#initialize(total: , items: , consumer: , success_url: , cancel_url: , payment_type: ) ⇒ Order
constructor
Initializes an Order object.
- #success? ⇒ Boolean
-
#to_hash ⇒ Object
Builds structure to API specs.
Constructor Details
#initialize(total: , items: , consumer: , success_url: , cancel_url: , payment_type: ) ⇒ Order
Initializes an Order object
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
#billing ⇒ Object
Returns the value of attribute billing.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def billing @billing end |
#billing_address ⇒ Object
Returns the value of attribute billing_address.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def billing_address @billing_address end |
#cancel_url ⇒ Object
Returns the value of attribute cancel_url.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def cancel_url @cancel_url end |
#consumer ⇒ Object
Returns the value of attribute consumer.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def consumer @consumer end |
#discounts ⇒ Object
Returns the value of attribute discounts.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def discounts @discounts end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'lib/afterpay/order.rb', line 10 def error @error end |
#expiry ⇒ Object (readonly)
Returns the value of attribute expiry.
10 11 12 |
# File 'lib/afterpay/order.rb', line 10 def expiry @expiry end |
#items ⇒ Object
Returns the value of attribute items.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def items @items end |
#merchant_reference ⇒ Object
Returns the value of attribute merchant_reference.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def merchant_reference @merchant_reference end |
#payment_type ⇒ Object
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_url ⇒ Object
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 |
#shipping ⇒ Object
Returns the value of attribute shipping.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def shipping @shipping end |
#shipping_address ⇒ Object
Returns the value of attribute shipping_address.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def shipping_address @shipping_address end |
#success_url ⇒ Object
Returns the value of attribute success_url.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def success_url @success_url end |
#tax ⇒ Object
Returns the value of attribute tax.
6 7 8 |
# File 'lib/afterpay/order.rb', line 6 def tax @tax end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
10 11 12 |
# File 'lib/afterpay/order.rb', line 10 def token @token end |
#total ⇒ Object
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
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
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
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
#create ⇒ Response
Sends the create request to Afterpay server
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
113 114 115 |
# File 'lib/afterpay/order.rb', line 113 def success? !@token.nil? end |
#to_hash ⇒ Object
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 |