paysafe-tz 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +25 -0
- data/LICENSE +21 -0
- data/README.md +162 -0
- data/lib/paysafe/api_response.rb +54 -0
- data/lib/paysafe/client.rb +210 -0
- data/lib/paysafe/errors.rb +179 -0
- data/lib/paysafe/models.rb +164 -0
- data/lib/paysafe/page.rb +60 -0
- data/lib/paysafe/request_options.rb +47 -0
- data/lib/paysafe/resources/balance.rb +19 -0
- data/lib/paysafe/resources/base.rb +76 -0
- data/lib/paysafe/resources/control_numbers.rb +70 -0
- data/lib/paysafe/resources/customers.rb +19 -0
- data/lib/paysafe/resources/events.rb +23 -0
- data/lib/paysafe/resources/payment_links.rb +66 -0
- data/lib/paysafe/resources/payments.rb +54 -0
- data/lib/paysafe/resources/refunds.rb +39 -0
- data/lib/paysafe/resources/topups.rb +35 -0
- data/lib/paysafe/resources/transfers.rb +33 -0
- data/lib/paysafe/resources/webhook_endpoints.rb +55 -0
- data/lib/paysafe/util.rb +31 -0
- data/lib/paysafe/validate.rb +141 -0
- data/lib/paysafe/version.rb +5 -0
- data/lib/paysafe/webhooks.rb +84 -0
- data/lib/paysafe.rb +37 -0
- metadata +74 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
# Read-only view of an API object. Fields are reachable as methods or with [] (string or symbol keys);
|
|
5
|
+
# nested objects stay plain Hashes with string keys.
|
|
6
|
+
class ApiObject
|
|
7
|
+
attr_accessor :last_response
|
|
8
|
+
|
|
9
|
+
def initialize(data)
|
|
10
|
+
@data = data.is_a?(Hash) ? data : {}
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.from(data, response = nil)
|
|
14
|
+
obj = new(data)
|
|
15
|
+
obj.last_response = response
|
|
16
|
+
obj
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def [](key)
|
|
20
|
+
@data[key.to_s]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def key?(key)
|
|
24
|
+
@data.key?(key.to_s)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_h
|
|
28
|
+
@data
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_json(*args)
|
|
32
|
+
@data.to_json(*args)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def ==(other)
|
|
36
|
+
other.is_a?(ApiObject) && other.to_h == @data
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def inspect
|
|
40
|
+
"#<#{self.class.name} #{@data.slice("id", "object", "status").map { |k, v| "#{k}=#{v.inspect}" }.join(" ")}>"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def method_missing(name, *args)
|
|
44
|
+
key = name.to_s
|
|
45
|
+
return @data[key] if args.empty? && @data.key?(key)
|
|
46
|
+
|
|
47
|
+
super
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def respond_to_missing?(name, include_private = false)
|
|
51
|
+
@data.key?(name.to_s) || super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class Payment < ApiObject
|
|
56
|
+
TERMINAL_STATUSES = %w[succeeded failed cancelled expired partially_refunded refunded].freeze
|
|
57
|
+
SUCCESSFUL_STATUSES = %w[succeeded partially_refunded refunded].freeze
|
|
58
|
+
|
|
59
|
+
def terminal?
|
|
60
|
+
TERMINAL_STATUSES.include?(self["status"])
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def successful?
|
|
64
|
+
SUCCESSFUL_STATUSES.include?(self["status"])
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class PaymentLink < ApiObject; end
|
|
69
|
+
|
|
70
|
+
class ControlNumber < ApiObject
|
|
71
|
+
def terminal?
|
|
72
|
+
%w[paid expired cancelled].include?(self["status"])
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def entries
|
|
76
|
+
Array(self["entries"])
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class Refund < ApiObject
|
|
81
|
+
def terminal?
|
|
82
|
+
%w[succeeded failed rejected].include?(self["status"])
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def successful?
|
|
86
|
+
self["status"] == "succeeded"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class Topup < ApiObject
|
|
91
|
+
def terminal?
|
|
92
|
+
Payment::TERMINAL_STATUSES.include?(self["status"])
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def successful?
|
|
96
|
+
Payment::SUCCESSFUL_STATUSES.include?(self["status"])
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
class Transfer < ApiObject
|
|
101
|
+
def terminal?
|
|
102
|
+
%w[completed rejected reversed].include?(self["status"])
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
class Balance < ApiObject
|
|
107
|
+
def available_amount(currency = "TZS")
|
|
108
|
+
Array(self["available"]).find { |m| m["currency"] == currency }&.dig("amount") || 0
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def pending_amount(currency = "TZS")
|
|
112
|
+
Array(self["pending"]).find { |m| m["currency"] == currency }&.dig("amount") || 0
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
class Transaction < ApiObject; end
|
|
117
|
+
class Customer < ApiObject; end
|
|
118
|
+
|
|
119
|
+
class WebhookEndpoint < ApiObject
|
|
120
|
+
def enabled?
|
|
121
|
+
self["status"] == "ENABLED"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
class Event < ApiObject
|
|
126
|
+
TYPES = %w[
|
|
127
|
+
payment.pending payment.processing payment.requires_action payment.succeeded payment.failed payment.expired payment.cancelled
|
|
128
|
+
refund.pending refund.succeeded refund.failed
|
|
129
|
+
payout.processing payout.paid payout.failed
|
|
130
|
+
transfer.sent transfer.received transfer.in_review transfer.rejected transfer.reversed
|
|
131
|
+
control_number.partially_paid control_number.paid control_number.expired control_number.cancelled
|
|
132
|
+
].freeze
|
|
133
|
+
|
|
134
|
+
def data_object
|
|
135
|
+
self["data"].is_a?(Hash) && self["data"]["object"].is_a?(Hash) ? self["data"]["object"] : {}
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def payment
|
|
139
|
+
decode_if("payment.", Payment)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def refund
|
|
143
|
+
decode_if("refund.", Refund)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def transfer
|
|
147
|
+
decode_if("transfer.", Transfer)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def control_number
|
|
151
|
+
decode_if("control_number.", ControlNumber)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def deliveries
|
|
155
|
+
Array(self["deliveries"])
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
private
|
|
159
|
+
|
|
160
|
+
def decode_if(prefix, klass)
|
|
161
|
+
self["type"].to_s.start_with?(prefix) ? klass.from(data_object, last_response) : nil
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
data/lib/paysafe/page.rb
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
# One page of a cursor-paginated list. Enumerate it for its items; auto_paging_each walks every page.
|
|
5
|
+
class Page
|
|
6
|
+
include Enumerable
|
|
7
|
+
|
|
8
|
+
attr_reader :data, :next_cursor, :last_response
|
|
9
|
+
|
|
10
|
+
def initialize(data, has_more, next_cursor, last_response, fetch_next)
|
|
11
|
+
@data = data
|
|
12
|
+
@has_more = has_more
|
|
13
|
+
@next_cursor = next_cursor
|
|
14
|
+
@last_response = last_response
|
|
15
|
+
@fetch_next = fetch_next
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.from_response(response, klass, &fetch_next)
|
|
19
|
+
root = response.json
|
|
20
|
+
items = Array(root&.dig("data")).grep(Hash).map { |e| klass.from(e, response) }
|
|
21
|
+
cursor = root&.dig("next_cursor")
|
|
22
|
+
new(items, root&.dig("has_more") == true, cursor.is_a?(String) ? cursor : nil, response, fetch_next)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def has_more?
|
|
26
|
+
@has_more
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def each(&)
|
|
30
|
+
@data.each(&)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def size
|
|
34
|
+
@data.size
|
|
35
|
+
end
|
|
36
|
+
alias length size
|
|
37
|
+
|
|
38
|
+
def [](index)
|
|
39
|
+
@data[index]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The following page, or nil when this was the last one.
|
|
43
|
+
def next_page
|
|
44
|
+
return nil if !@has_more || @next_cursor.nil? || @fetch_next.nil?
|
|
45
|
+
|
|
46
|
+
@fetch_next.call(@next_cursor)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Every item on this page and all following pages, fetched lazily.
|
|
50
|
+
def auto_paging_each(&)
|
|
51
|
+
return enum_for(:auto_paging_each) unless block_given?
|
|
52
|
+
|
|
53
|
+
page = self
|
|
54
|
+
while page
|
|
55
|
+
page.each(&)
|
|
56
|
+
page = page.next_page
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
# Per-call options, given as keyword arguments or a Hash:
|
|
5
|
+
# idempotency_key, request_id, timeout (seconds), max_retries, headers.
|
|
6
|
+
class RequestOptions
|
|
7
|
+
KEYS = %i[idempotency_key request_id timeout max_retries headers].freeze
|
|
8
|
+
|
|
9
|
+
attr_reader :idempotency_key, :request_id, :timeout, :max_retries, :headers
|
|
10
|
+
|
|
11
|
+
def initialize(idempotency_key: nil, request_id: nil, timeout: nil, max_retries: nil, headers: {})
|
|
12
|
+
Validate.idempotency_key(idempotency_key) unless idempotency_key.nil?
|
|
13
|
+
Validate.request_id(request_id) unless request_id.nil?
|
|
14
|
+
raise InvalidRequestError, "max_retries must be zero or more" if !max_retries.nil? && (!max_retries.is_a?(Integer) || max_retries.negative?)
|
|
15
|
+
raise InvalidRequestError, "timeout must be positive" if !timeout.nil? && (!timeout.is_a?(Numeric) || timeout <= 0)
|
|
16
|
+
|
|
17
|
+
Validate.headers(headers)
|
|
18
|
+
@idempotency_key = idempotency_key
|
|
19
|
+
@request_id = request_id
|
|
20
|
+
@timeout = timeout
|
|
21
|
+
@max_retries = max_retries
|
|
22
|
+
@headers = headers.to_h { |k, v| [k.to_s, v] }.freeze
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.from(opts)
|
|
26
|
+
return opts if opts.is_a?(RequestOptions)
|
|
27
|
+
raise InvalidRequestError, "options must be a Hash" unless opts.is_a?(Hash)
|
|
28
|
+
|
|
29
|
+
unknown = opts.keys.map(&:to_sym) - KEYS
|
|
30
|
+
raise InvalidRequestError, "Unknown request option #{unknown.first}" unless unknown.empty?
|
|
31
|
+
|
|
32
|
+
new(**opts.transform_keys(&:to_sym))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.with_key(opts)
|
|
36
|
+
options = from(opts)
|
|
37
|
+
options.idempotency_key ? options : options.with_generated_key
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def with_generated_key
|
|
41
|
+
RequestOptions.new(idempotency_key: SecureRandom.uuid, request_id: request_id, timeout: timeout, max_retries: max_retries, headers: headers)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# One HTTP attempt. Never contains the key or any body. status is nil when no response arrived.
|
|
46
|
+
LogRecord = Struct.new(:http_method, :path, :status, :attempt, :duration, :request_id, :error, keyword_init: true)
|
|
47
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Balance < Base
|
|
6
|
+
def get(opts = {})
|
|
7
|
+
decode(send("GET", path("balance"), opts: opts), PaySafe::Balance)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Ledger entries, newest first.
|
|
11
|
+
def transactions(params = {}, opts = {})
|
|
12
|
+
p = Util.stringify(params)
|
|
13
|
+
Page.from_response(send("GET", path("transactions"), query: query(p), opts: opts), Transaction) do |cursor|
|
|
14
|
+
transactions(p.merge("cursor" => cursor), opts)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Base
|
|
6
|
+
MIN_INTERVAL = 0.5
|
|
7
|
+
SLOW_AFTER = 30.0
|
|
8
|
+
SLOW_INTERVAL = 5.0
|
|
9
|
+
|
|
10
|
+
def initialize(client)
|
|
11
|
+
@client = client
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def path(*segments)
|
|
17
|
+
"/v1#{segments.map { |s| "/#{Util.encode(s)}" }.join}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def send(method, path, query: {}, body: nil, opts: {})
|
|
21
|
+
@client.send_request(method, path, query, body, opts)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def decode(response, klass)
|
|
25
|
+
data = response.json
|
|
26
|
+
unless data
|
|
27
|
+
raise ApiError.new(status: response.status, code: "invalid_json", message: "The API returned a body this SDK could not parse",
|
|
28
|
+
request_id: response.request_id, headers: response.headers)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
klass.from(data, response)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def encode(params)
|
|
35
|
+
JSON.generate(params)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def query(params, *extra)
|
|
39
|
+
Validate.limit(params["limit"])
|
|
40
|
+
Validate.cursor(params["cursor"])
|
|
41
|
+
q = {}
|
|
42
|
+
q["limit"] = params["limit"].to_s if params["limit"]
|
|
43
|
+
q["cursor"] = params["cursor"] if params["cursor"]
|
|
44
|
+
extra.each do |key|
|
|
45
|
+
value = params[key]
|
|
46
|
+
q[key] = value.to_s if value && !value.to_s.empty?
|
|
47
|
+
end
|
|
48
|
+
q
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def poll(fetch:, terminal:, default_interval:, default_timeout:, what:, interval: nil, timeout: nil, on_update: nil, should_stop: nil)
|
|
52
|
+
interval = [interval || default_interval, MIN_INTERVAL].max
|
|
53
|
+
timeout ||= default_timeout
|
|
54
|
+
waited = 0.0
|
|
55
|
+
last = nil
|
|
56
|
+
first = true
|
|
57
|
+
loop do
|
|
58
|
+
obj = fetch.call
|
|
59
|
+
status = obj["status"]
|
|
60
|
+
on_update&.call(status) if first || status != last
|
|
61
|
+
first = false
|
|
62
|
+
last = status
|
|
63
|
+
return obj if terminal.call(obj)
|
|
64
|
+
raise PollingTimeoutError.new("Polling for #{what} was cancelled", obj) if should_stop&.call
|
|
65
|
+
raise PollingTimeoutError.new("#{what} did not reach a terminal state within #{timeout} s", obj) if waited >= timeout
|
|
66
|
+
|
|
67
|
+
delay = interval
|
|
68
|
+
delay = SLOW_INTERVAL if waited >= SLOW_AFTER && delay < SLOW_INTERVAL
|
|
69
|
+
delay = [delay, timeout - waited].min
|
|
70
|
+
@client.pause(delay)
|
|
71
|
+
waited += delay
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class ControlNumbers < Base
|
|
6
|
+
MAX_AMOUNT = 1_000_000_000_000
|
|
7
|
+
STATUSES = %w[active paid expired cancelled].freeze
|
|
8
|
+
STATES = %w[unpaid partial paid overdue].freeze
|
|
9
|
+
MODES = %w[full instalment].freeze
|
|
10
|
+
|
|
11
|
+
# Issues a 12-digit pay-by-reference number (201). The number itself is assigned by the server.
|
|
12
|
+
def create(params, opts = {})
|
|
13
|
+
p = Util.stringify(params)
|
|
14
|
+
Validate.required(p["payer_name"], "payer_name")
|
|
15
|
+
Validate.required(p["title"], "title")
|
|
16
|
+
Validate.string(p["payer_name"], "payer_name", 1, 120)
|
|
17
|
+
Validate.string(p["payer_phone"], "payer_phone", 0, 20)
|
|
18
|
+
Validate.string(p["payer_email"], "payer_email", 0, 255)
|
|
19
|
+
Validate.string(p["payer_reference"], "payer_reference", 0, 120)
|
|
20
|
+
Validate.string(p["title"], "title", 1, 150)
|
|
21
|
+
Validate.string(p["description"], "description", 0, 500)
|
|
22
|
+
Validate.string(p["reference"], "reference", 0, 120)
|
|
23
|
+
Validate.string(p["payment_type"], "payment_type", 0, 60)
|
|
24
|
+
Validate.string(p["notes"], "notes", 0, 1000)
|
|
25
|
+
Validate.string(p["image"], "image", 0, 240)
|
|
26
|
+
Validate.enum(p["mode"], "mode", MODES)
|
|
27
|
+
Validate.enum(p["currency"], "currency", %w[TZS])
|
|
28
|
+
Validate.amount(p["amount"], 1, MAX_AMOUNT, "amount")
|
|
29
|
+
Validate.metadata(p["metadata"])
|
|
30
|
+
Validate.date_string(p["expires_at"], "expires_at")
|
|
31
|
+
unless p["instalments"].nil?
|
|
32
|
+
list = p["instalments"]
|
|
33
|
+
raise InvalidRequestError.new("instalments must be a non-empty list", "instalments") unless list.is_a?(Array) && !list.empty?
|
|
34
|
+
|
|
35
|
+
list.each_with_index do |item, i|
|
|
36
|
+
raise InvalidRequestError.new("instalments[#{i}] must be a Hash", "instalments") unless item.is_a?(Hash)
|
|
37
|
+
|
|
38
|
+
Validate.amount(item["amount"], 1, MAX_AMOUNT, "instalments[#{i}].amount")
|
|
39
|
+
Validate.required(item["due_at"], "instalments[#{i}].due_at")
|
|
40
|
+
Validate.date_string(item["due_at"], "instalments[#{i}].due_at")
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
decode(send("POST", path("control-numbers"), body: encode(p), opts: opts), ControlNumber)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def list(params = {}, opts = {})
|
|
47
|
+
p = Util.stringify(params)
|
|
48
|
+
Validate.enum(p["status"], "status", STATUSES)
|
|
49
|
+
Validate.enum(p["state"], "state", STATES)
|
|
50
|
+
Validate.string(p["q"], "q", 0, 80)
|
|
51
|
+
q = query(p, "status", "state", "batch", "customer", "q")
|
|
52
|
+
Page.from_response(send("GET", path("control-numbers"), query: q, opts: opts), ControlNumber) do |cursor|
|
|
53
|
+
list(p.merge("cursor" => cursor), opts)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The number with every payment entry recorded against it.
|
|
58
|
+
def get(id, opts = {})
|
|
59
|
+
decode(send("GET", path("control-numbers", Validate.id(id)), opts: opts), ControlNumber)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Only active numbers can be cancelled; the reason (3 to 500 characters) is kept on the number.
|
|
63
|
+
def cancel(id, reason, opts = {})
|
|
64
|
+
Validate.required(reason, "reason")
|
|
65
|
+
Validate.string(reason, "reason", 3, 500)
|
|
66
|
+
decode(send("POST", path("control-numbers", Validate.id(id), "cancel"), body: encode({ "reason" => reason }), opts: opts), ControlNumber)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Customers < Base
|
|
6
|
+
def list(params = {}, opts = {})
|
|
7
|
+
p = Util.stringify(params)
|
|
8
|
+
Validate.string(p["search"], "search", 0, 120)
|
|
9
|
+
Page.from_response(send("GET", path("customers"), query: query(p, "search"), opts: opts), Customer) do |cursor|
|
|
10
|
+
list(p.merge("cursor" => cursor), opts)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get(id, opts = {})
|
|
15
|
+
decode(send("GET", path("customers", Validate.id(id)), opts: opts), Customer)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Events < Base
|
|
6
|
+
def list(params = {}, opts = {})
|
|
7
|
+
p = Util.stringify(params)
|
|
8
|
+
Validate.enum(p["type"], "type", Event::TYPES)
|
|
9
|
+
Validate.date_string(p["from"], "from")
|
|
10
|
+
Validate.date_string(p["to"], "to")
|
|
11
|
+
Validate.id(p["resource_id"], "resource_id") if p["resource_id"]
|
|
12
|
+
q = query(p, "type", "from", "to")
|
|
13
|
+
q["resourceId"] = p["resource_id"] if p["resource_id"]
|
|
14
|
+
Page.from_response(send("GET", path("events"), query: q, opts: opts), Event) { |cursor| list(p.merge("cursor" => cursor), opts) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# The event plus every delivery attempt to your endpoints.
|
|
18
|
+
def get(id, opts = {})
|
|
19
|
+
decode(send("GET", path("events", Validate.id(id)), opts: opts), Event)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class PaymentLinks < Base
|
|
6
|
+
STATUSES = %w[active paused archived].freeze
|
|
7
|
+
|
|
8
|
+
def create(params, opts = {})
|
|
9
|
+
p = Util.stringify(params)
|
|
10
|
+
Validate.required(p["title"], "title")
|
|
11
|
+
Validate.string(p["title"], "title", 2, 120)
|
|
12
|
+
Validate.string(p["description"], "description", 1, 1000)
|
|
13
|
+
Validate.optional_amount(p["amount"], Payments::MIN_AMOUNT, Payments::MAX_AMOUNT, "amount")
|
|
14
|
+
Validate.optional_amount(p["min_amount"], Payments::MIN_AMOUNT, Payments::MAX_AMOUNT, "min_amount")
|
|
15
|
+
Validate.optional_amount(p["max_amount"], Payments::MIN_AMOUNT, Payments::MAX_AMOUNT, "max_amount")
|
|
16
|
+
Validate.enum(p["currency"], "currency", %w[TZS])
|
|
17
|
+
Validate.optional_amount(p["max_payments"], 1, 1_000_000, "max_payments")
|
|
18
|
+
Validate.date_string(p["expires_at"], "expires_at")
|
|
19
|
+
Validate.string(p["success_message"], "success_message", 1, 255)
|
|
20
|
+
Validate.url(p["redirect_url"], "redirect_url", 500)
|
|
21
|
+
Validate.string(p["logo"], "logo", 0, 240)
|
|
22
|
+
Validate.string(p["accent_color"], "accent_color", 0, 32)
|
|
23
|
+
decode(send("POST", path("payment-links"), body: encode(p), opts: opts), PaymentLink)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def list(params = {}, opts = {})
|
|
27
|
+
p = Util.stringify(params)
|
|
28
|
+
Validate.enum(p["status"], "status", STATUSES)
|
|
29
|
+
Page.from_response(send("GET", path("payment-links"), query: query(p, "status"), opts: opts), PaymentLink) do |cursor|
|
|
30
|
+
list(p.merge("cursor" => cursor), opts)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def get(id, opts = {})
|
|
35
|
+
decode(send("GET", path("payment-links", Validate.id(id)), opts: opts), PaymentLink)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Pause, resume, archive or edit a link. The amount cannot be changed; archiving is permanent.
|
|
39
|
+
def update(id, params, opts = {})
|
|
40
|
+
p = Util.stringify(params)
|
|
41
|
+
Validate.string(p["title"], "title", 2, 120)
|
|
42
|
+
Validate.enum(p["status"], "status", STATUSES)
|
|
43
|
+
Validate.string(p["description"], "description", 1, 1000)
|
|
44
|
+
Validate.optional_amount(p["max_payments"], 1, 1_000_000, "max_payments")
|
|
45
|
+
Validate.date_string(p["expires_at"], "expires_at")
|
|
46
|
+
Validate.string(p["success_message"], "success_message", 1, 255)
|
|
47
|
+
Validate.url(p["redirect_url"], "redirect_url", 500)
|
|
48
|
+
Validate.string(p["logo"], "logo", 0, 240)
|
|
49
|
+
Validate.string(p["accent_color"], "accent_color", 0, 32)
|
|
50
|
+
decode(send("PATCH", path("payment-links", Validate.id(id)), body: encode(p), opts: opts), PaymentLink)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def pause(id, opts = {})
|
|
54
|
+
update(id, { status: "paused" }, opts)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def resume(id, opts = {})
|
|
58
|
+
update(id, { status: "active" }, opts)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def archive(id, opts = {})
|
|
62
|
+
update(id, { status: "archived" }, opts)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Payments < Base
|
|
6
|
+
MIN_AMOUNT = 1000
|
|
7
|
+
MAX_AMOUNT = 10_000_000
|
|
8
|
+
CHANNELS = %w[mpesa tigopesa airtel halopesa].freeze
|
|
9
|
+
STATUSES = %w[created pending processing requires_action succeeded failed cancelled expired partially_refunded refunded].freeze
|
|
10
|
+
|
|
11
|
+
# Starts a mobile-money charge (202 Accepted). An Idempotency-Key is generated when opts has none;
|
|
12
|
+
# read it from payment.last_response.idempotency_key and persist it before retrying yourself.
|
|
13
|
+
def create(params, opts = {})
|
|
14
|
+
p = Util.stringify(params)
|
|
15
|
+
Validate.amount(p["amount"], MIN_AMOUNT, MAX_AMOUNT, "amount")
|
|
16
|
+
Validate.phone(p.dig("customer", "phone"), "customer.phone")
|
|
17
|
+
Validate.string(p.dig("customer", "name"), "customer.name", 1, 100)
|
|
18
|
+
Validate.string(p.dig("customer", "email"), "customer.email", 3, 255)
|
|
19
|
+
Validate.enum(p["currency"], "currency", %w[TZS])
|
|
20
|
+
Validate.enum(p["channel"], "channel", CHANNELS)
|
|
21
|
+
Validate.string(p["reference"], "reference", 1, 120)
|
|
22
|
+
Validate.string(p["description"], "description", 1, 255)
|
|
23
|
+
Validate.metadata(p["metadata"])
|
|
24
|
+
Validate.optional_amount(p["expires_in"], 60, 900, "expires_in")
|
|
25
|
+
decode(send("POST", path("payments"), body: encode(p), opts: RequestOptions.with_key(opts)), Payment)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def list(params = {}, opts = {})
|
|
29
|
+
p = Util.stringify(params)
|
|
30
|
+
Validate.enum(p["status"], "status", STATUSES)
|
|
31
|
+
q = query(p, "status", "reference", "customer_phone", "created_gte", "created_lte")
|
|
32
|
+
Page.from_response(send("GET", path("payments"), query: q, opts: opts), Payment) { |cursor| list(p.merge("cursor" => cursor), opts) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def get(id, opts = {})
|
|
36
|
+
decode(send("GET", path("payments", Validate.id(id)), opts: opts), Payment)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Only payments in created or pending can be cancelled. The API requires an Idempotency-Key here.
|
|
40
|
+
def cancel(id, reason = nil, opts = {})
|
|
41
|
+
Validate.string(reason, "reason", 0, 255)
|
|
42
|
+
body = reason.nil? ? {} : { "reason" => reason }
|
|
43
|
+
decode(send("POST", path("payments", Validate.id(id), "cancel"), body: encode(body), opts: RequestOptions.with_key(opts)), Payment)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Polls get until the payment is terminal (default every 2 s, up to 2 min).
|
|
47
|
+
def wait_for_terminal(id, interval: nil, timeout: nil, on_update: nil, should_stop: nil)
|
|
48
|
+
Validate.id(id)
|
|
49
|
+
poll(fetch: -> { get(id) }, terminal: lambda(&:terminal?), default_interval: 2.0, default_timeout: 120.0, what: "payment #{id}",
|
|
50
|
+
interval: interval, timeout: timeout, on_update: on_update, should_stop: should_stop)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Refunds < Base
|
|
6
|
+
REASONS = %w[requested_by_customer duplicate fraudulent other].freeze
|
|
7
|
+
STATUSES = %w[pending processing succeeded failed rejected].freeze
|
|
8
|
+
|
|
9
|
+
# Refunds a succeeded payment (202 Accepted). Omit amount to refund the remaining balance.
|
|
10
|
+
def create(params, opts = {})
|
|
11
|
+
p = Util.stringify(params)
|
|
12
|
+
Validate.id(p["payment"], "payment")
|
|
13
|
+
Validate.optional_amount(p["amount"], 1, 2**62, "amount")
|
|
14
|
+
Validate.enum(p["reason"], "reason", REASONS)
|
|
15
|
+
Validate.metadata(p["metadata"])
|
|
16
|
+
decode(send("POST", path("refunds"), body: encode(p), opts: RequestOptions.with_key(opts)), Refund)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def list(params = {}, opts = {})
|
|
20
|
+
p = Util.stringify(params)
|
|
21
|
+
Validate.enum(p["status"], "status", STATUSES)
|
|
22
|
+
Validate.id(p["payment"], "payment") if p["payment"]
|
|
23
|
+
Page.from_response(send("GET", path("refunds"), query: query(p, "payment", "status"), opts: opts), Refund) do |cursor|
|
|
24
|
+
list(p.merge("cursor" => cursor), opts)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def get(id, opts = {})
|
|
29
|
+
decode(send("GET", path("refunds", Validate.id(id)), opts: opts), Refund)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def wait_for_terminal(id, interval: nil, timeout: nil, on_update: nil, should_stop: nil)
|
|
33
|
+
Validate.id(id)
|
|
34
|
+
poll(fetch: -> { get(id) }, terminal: lambda(&:terminal?), default_interval: 1.0, default_timeout: 60.0, what: "refund #{id}",
|
|
35
|
+
interval: interval, timeout: timeout, on_update: on_update, should_stop: should_stop)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaySafe
|
|
4
|
+
module Resources
|
|
5
|
+
class Topups < Base
|
|
6
|
+
MIN_AMOUNT = 1000
|
|
7
|
+
MAX_AMOUNT = 10_000_000
|
|
8
|
+
|
|
9
|
+
# Opens a top-up into your own balance (202 Accepted). An Idempotency-Key is generated when opts has none.
|
|
10
|
+
# checkout is nil in the first response; call get a moment later to read it.
|
|
11
|
+
def create(params, opts = {})
|
|
12
|
+
p = Util.stringify(params)
|
|
13
|
+
Validate.amount(p["amount"], MIN_AMOUNT, MAX_AMOUNT, "amount")
|
|
14
|
+
Validate.phone(p["phone"], "phone")
|
|
15
|
+
raise InvalidRequestError.new("prompt must be a boolean", "prompt") unless p["prompt"].nil? || [true, false].include?(p["prompt"])
|
|
16
|
+
|
|
17
|
+
Validate.enum(p["currency"], "currency", %w[TZS])
|
|
18
|
+
Validate.string(p["reference"], "reference", 1, 120)
|
|
19
|
+
Validate.string(p["description"], "description", 1, 255)
|
|
20
|
+
Validate.optional_amount(p["expires_in"], 60, 86_400, "expires_in")
|
|
21
|
+
decode(send("POST", path("topups"), body: encode(p), opts: RequestOptions.with_key(opts)), Topup)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def list(params = {}, opts = {})
|
|
25
|
+
p = Util.stringify(params)
|
|
26
|
+
Page.from_response(send("GET", path("topups"), query: query(p), opts: opts), Topup) { |cursor| list(p.merge("cursor" => cursor), opts) }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The id is the id of the underlying payment (pay_...).
|
|
30
|
+
def get(id, opts = {})
|
|
31
|
+
decode(send("GET", path("topups", Validate.id(id)), opts: opts), Topup)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|