Class: Afterpay::Payment

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

Overview

They Payment object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Payment

Initialize Payment from response



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/afterpay/payment.rb', line 15

def initialize(attributes = {})
  @id = attributes[:id] || ""
  @token = attributes[:token] || ""
  @status = attributes[:status] || ""
  @created = attributes[:created] || ""
  @original_amount = Utils::Money.from_response(attributes[:originalAmount]) || Money.from_amount(0)
  @open_to_capture_amount = Utils::Money.from_response(attributes[:openToCaptureAmount]) || Money.from_amount(0)
  @payment_state = attributes[:paymentState] || ""
  @merchant_reference = attributes[:merchantReference] || ""
  @refunds = attributes[:refunds].map { |refund| Refund.from_response(refund) } || [] unless attributes[:refunds].nil?
  @order = Order.from_response(attributes[:orderDetails]) || Afterpay::Order.new
  @events = attributes[:events].map { |event| PaymentEvent.from_response(event) } || [] unless attributes[:events].nil?
  @error = Error.new(attributes) if attributes[:errorId]
end

Instance Attribute Details

#createdObject

Returns the value of attribute created.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def created
  @created
end

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def error
  @error
end

#eventsObject

Returns the value of attribute events.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def events
  @events
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def id
  @id
end

#merchant_referenceObject

Returns the value of attribute merchant_reference.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def merchant_reference
  @merchant_reference
end

#open_to_capture_amountObject

Returns the value of attribute open_to_capture_amount.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def open_to_capture_amount
  @open_to_capture_amount
end

#orderObject

Returns the value of attribute order.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def order
  @order
end

#original_amountObject

Returns the value of attribute original_amount.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def original_amount
  @original_amount
end

#payment_stateObject

Returns the value of attribute payment_state.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def payment_state
  @payment_state
end

#refundsObject

Returns the value of attribute refunds.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def refunds
  @refunds
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def status
  @status
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/afterpay/payment.rb', line 8

def token
  @token
end

Class Method Details

.create_url(url, type, values) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/afterpay/payment.rb', line 141

def self.create_url(url, type, values)
  if values.size.positive?
    values.each do |value|
      url += "&#{type}=#{value}"
    end
  end
  url
end

.execute(token:, merchant_reference: nil) ⇒ Payment

Executes the Payment

Parameters:

  • token (String)

    the Order token

  • merchant_reference (String) (defaults to: nil)

    the merchant_reference for payment

Returns:



46
47
48
49
50
51
52
53
54
# File 'lib/afterpay/payment.rb', line 46

def self.execute(token:, merchant_reference: nil)
  request = Afterpay.client.post("/v2/payments/capture") do |req|
    req.body = {
      token: token,
      merchantReference: merchant_reference
    }
  end
  new(request.body)
end

.execute_auth(token:, request_id: nil, merchant_reference: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/afterpay/payment.rb', line 56

def self.execute_auth(token:, request_id: nil, merchant_reference: nil)
  request = Afterpay.client.post("/v2/payments/auth") do |req|
    req.body = {
      requestId: request_id,
      token: token,
      merchantReference: merchant_reference
    }
  end
  new(request.body)
end

.execute_deferred_payment(amount:, order_id:, request_id: nil, merchant_reference: nil, payment_event_merchant_reference: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/afterpay/payment.rb', line 67

def self.execute_deferred_payment(amount:, order_id:, request_id: nil, merchant_reference: nil,
                                  payment_event_merchant_reference: nil)
  request = Afterpay.client.post("/v2/payments/#{order_id}/capture") do |req|
    req.body = {
      requestId: request_id,
      merchantReference: merchant_reference,
      amount: Utils::Money.api_hash(amount),
      paymentEventMerchantReference: payment_event_merchant_reference
    }
  end
  new(request.body)
end

.execute_void(order_id:, amount:, request_id: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/afterpay/payment.rb', line 80

def self.execute_void(order_id:, amount:, request_id: nil)
  request = Afterpay.client.post("/v2/payments/#{order_id}/void") do |req|
    req.body = {
      requestId: request_id,
      amount: Utils::Money.api_hash(amount)
    }
  end
  new(request.body)
end

.get_payment_by_order_id(order_id:) ⇒ Object

This endpoint retrieves an individual payment along with its order details.



103
104
105
106
# File 'lib/afterpay/payment.rb', line 103

def self.get_payment_by_order_id(order_id:)
  request = Afterpay.client.get("/v2/payments/#{order_id}")
  new(request.body)
end

.get_payment_by_token(token:) ⇒ Object

This endpoint retrieves an individual payment along with its order details.



109
110
111
112
# File 'lib/afterpay/payment.rb', line 109

def self.get_payment_by_token(token:)
  request = Afterpay.client.get("/v2/payments/token:#{token}")
  new(request.body)
end

.list_payments(tokens:, ids:, merchant_ref:, statuses:, to_created_date: nil, from_created_date: nil, limit: nil, offset: nil, order_by: nil, asc: nil) ⇒ Object

This endpoint retrieves a collection of payments along with their order details.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/afterpay/payment.rb', line 153

def self.list_payments(tokens:, ids:, merchant_ref:, statuses:, to_created_date: nil, from_created_date: nil, limit: nil, offset: nil, order_by: nil, asc: nil)
  url = "/v2/payments?"
  url += "toCreatedDate=#{to_created_date.gsub('+', '%2b')}" if to_created_date
  url += "&fromCreatedDate=#{from_created_date.gsub('+', '%2b')}" if from_created_date
  url += "&limit=#{limit}" if limit
  url += "&offset=#{offset}" if offset
  url += "&orderBy=#{order_by}" if order_by
  url += "&ascending=#{asc}" if asc
  url += create_url(url, "ids", ids)
  url += create_url(url, "tokens", tokens)
  url += create_url(url, "merchantReferences", merchant_ref)
  url += create_url(url, "statuses", statuses)
  request = Afterpay.client.get(url)
  request.body
end

.reverse_payment_by_token(token:) ⇒ Object

This endpoint performs a reversal of the checkout that is used to initiate the Afterpay payment process. This will cancel the order asynchronously as soon as it is created without the need of an additional call to the void endpoint. In order for a payment to be eligible, the order must be in an Auth-Approved or Captured state and must be issued within 10 minutes of the order being created. token paramater is the token of the checkout to be reversed (voided).



134
135
136
137
138
139
# File 'lib/afterpay/payment.rb', line 134

def self.reverse_payment_by_token(token:)
  request = Afterpay.client.post("/v2/payments/token:#{token}/reversal") do |req|
    req.body = {}
  end
  request.status
end

.update_payment_by_order_id(order_id:, merchant_reference:) ⇒ Object

This end point is for merchants that creates merchant side’s order id after AfterPay order id creation. The merchants should call immediately after the AfterPay order is created in order to properly update with their order id that can be tracked.



118
119
120
121
122
123
124
125
126
# File 'lib/afterpay/payment.rb', line 118

def self.update_payment_by_order_id(order_id:, merchant_reference:)
  request = Afterpay.client.put("/v2/payments/#{order_id}") do |req|
    req.body = {
      # The merchant's new order id to replace with
      merchantReference: merchant_reference
    }
  end
  request.body
end

.update_shipping_courier(order_id:, shipped_at: nil, name: nil, tracking: nil, priority: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/afterpay/payment.rb', line 90

def self.update_shipping_courier(order_id:, shipped_at: nil, name: nil, tracking: nil, priority: nil)
  request = Afterpay.client.put("/v2/payments/#{order_id}/courier") do |req|
    req.body = {
      shippedAt: shipped_at,
      name: name,
      tracking: tracking,
      priority: priority
    }
  end
  new(request.body)
end

Instance Method Details

#declined?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/afterpay/payment.rb', line 37

def declined?
  @status == "DECLINED"
end

#success?Boolean

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity

Returns:

  • (Boolean)


33
34
35
# File 'lib/afterpay/payment.rb', line 33

def success?
  @status == "APPROVED"
end