-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefund.rb
71 lines (55 loc) · 1.77 KB
/
refund.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
require 'time'
module Tikkie
module Api
module Resources
# Resource for a Refund.
class Refund < Base
STATUS_PENDING = 'PENDING'
STATUS_PAID = 'PAID'
attr_reader :payment_request_token, :payment_token
def initialize(config, options = {})
@payment_request_token = options.delete(:payment_request_token)
@payment_token = options.delete(:payment_token)
@refund_token = options.delete(:refund_token)
super
end
def refund_token
@refund_token || body[:refundToken]
end
def amount
Tikkie::Api::Amount.from_cents(body[:amountInCents]).to_d
end
def description
body[:description]
end
def reference_id
body[:referenceId]
end
def created_at
Time.parse(body[:createdDateTime]) if body[:createdDateTime]
end
def status
body[:status]
end
def pending?
status == STATUS_PENDING
end
def paid?
status == STATUS_PAID
end
private
def load_resource
request.get("paymentrequests/#{payment_request_token}/payments/#{payment_token}/refunds/#{refund_token}", options)
end
def create_resource(attributes)
params = { description: attributes.fetch(:description) }
amount = Tikkie::Api::Amount.new(attributes.fetch(:amount))
params[:amountInCents] = amount.to_cents
params[:referenceId] = attributes[:reference_id] if attributes.key?(:reference_id)
request.post("paymentrequests/#{payment_request_token}/payments/#{payment_token}/refunds", options, params)
end
end
end
end
end