sumup 0.0.1
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 +18 -0
- data/LICENSE +201 -0
- data/README.md +188 -0
- data/lib/sumup/client.rb +213 -0
- data/lib/sumup/errors.rb +45 -0
- data/lib/sumup/event_types.rb +115 -0
- data/lib/sumup/events.rb +168 -0
- data/lib/sumup/model.rb +109 -0
- data/lib/sumup/pagination.rb +92 -0
- data/lib/sumup/resource.rb +36 -0
- data/lib/sumup/resources/checkouts.rb +155 -0
- data/lib/sumup/resources/customers.rb +89 -0
- data/lib/sumup/resources/members.rb +93 -0
- data/lib/sumup/resources/memberships.rb +23 -0
- data/lib/sumup/resources/merchants.rb +51 -0
- data/lib/sumup/resources/payouts.rb +29 -0
- data/lib/sumup/resources/readers.rb +264 -0
- data/lib/sumup/resources/receipts.rb +23 -0
- data/lib/sumup/resources/roles.rb +89 -0
- data/lib/sumup/resources/sales.rb +66 -0
- data/lib/sumup/resources/tokens.rb +31 -0
- data/lib/sumup/resources/transactions.rb +61 -0
- data/lib/sumup/resources.rb +83 -0
- data/lib/sumup/transport.rb +40 -0
- data/lib/sumup/types.rb +4720 -0
- data/lib/sumup/version.rb +5 -0
- data/lib/sumup.rb +13 -0
- data/sig/sumup/events.rbs +64 -0
- data/sig/sumup/resources/checkouts.rbs +35 -0
- data/sig/sumup/resources/customers.rbs +25 -0
- data/sig/sumup/resources/members.rbs +25 -0
- data/sig/sumup/resources/memberships.rbs +13 -0
- data/sig/sumup/resources/merchants.rbs +17 -0
- data/sig/sumup/resources/payouts.rbs +13 -0
- data/sig/sumup/resources/readers.rbs +45 -0
- data/sig/sumup/resources/receipts.rbs +13 -0
- data/sig/sumup/resources/roles.rbs +25 -0
- data/sig/sumup/resources/sales.rbs +19 -0
- data/sig/sumup/resources/tokens.rbs +15 -0
- data/sig/sumup/resources/transactions.rbs +19 -0
- data/sig/sumup/types.rbs +1635 -0
- data/sig/sumup.rbs +183 -0
- metadata +149 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Generated by codegen. DO NOT EDIT.
|
|
4
|
+
|
|
5
|
+
module SumUp
|
|
6
|
+
class CheckoutCreatedEvent < EventNotification
|
|
7
|
+
TYPE = "checkouts.created"
|
|
8
|
+
OBJECT_TYPE = [:model, "Checkout"].freeze
|
|
9
|
+
RESOURCE_TYPE = "checkout"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class CheckoutExpiredEvent < EventNotification
|
|
13
|
+
TYPE = "checkouts.expired"
|
|
14
|
+
OBJECT_TYPE = [:model, "Checkout"].freeze
|
|
15
|
+
RESOURCE_TYPE = "checkout"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class CheckoutFailedEvent < EventNotification
|
|
19
|
+
TYPE = "checkouts.failed"
|
|
20
|
+
OBJECT_TYPE = [:model, "Checkout"].freeze
|
|
21
|
+
RESOURCE_TYPE = "checkout"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class CheckoutPaidEvent < EventNotification
|
|
25
|
+
TYPE = "checkouts.paid"
|
|
26
|
+
OBJECT_TYPE = [:model, "Checkout"].freeze
|
|
27
|
+
RESOURCE_TYPE = "checkout"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class CheckoutUpdatedEvent < EventNotification
|
|
31
|
+
TYPE = "checkouts.updated"
|
|
32
|
+
OBJECT_TYPE = [:model, "Checkout"].freeze
|
|
33
|
+
RESOURCE_TYPE = "checkout"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class MemberCreatedEvent < EventNotification
|
|
37
|
+
TYPE = "members.created"
|
|
38
|
+
OBJECT_TYPE = [:model, "Member"].freeze
|
|
39
|
+
RESOURCE_TYPE = "member"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class MemberDeletedEvent < EventNotification
|
|
43
|
+
TYPE = "members.deleted"
|
|
44
|
+
OBJECT_TYPE = [:model, "Member"].freeze
|
|
45
|
+
RESOURCE_TYPE = "member"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
class MemberUpdatedEvent < EventNotification
|
|
49
|
+
TYPE = "members.updated"
|
|
50
|
+
OBJECT_TYPE = [:model, "Member"].freeze
|
|
51
|
+
RESOURCE_TYPE = "member"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class ReaderCreatedEvent < EventNotification
|
|
55
|
+
TYPE = "readers.created"
|
|
56
|
+
OBJECT_TYPE = [:model, "Reader"].freeze
|
|
57
|
+
RESOURCE_TYPE = "reader"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class ReaderDeletedEvent < EventNotification
|
|
61
|
+
TYPE = "readers.deleted"
|
|
62
|
+
OBJECT_TYPE = [:model, "Reader"].freeze
|
|
63
|
+
RESOURCE_TYPE = "reader"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
class RoleCreatedEvent < EventNotification
|
|
67
|
+
TYPE = "roles.created"
|
|
68
|
+
OBJECT_TYPE = [:model, "Role"].freeze
|
|
69
|
+
RESOURCE_TYPE = "role"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
class RoleDeletedEvent < EventNotification
|
|
73
|
+
TYPE = "roles.deleted"
|
|
74
|
+
OBJECT_TYPE = [:model, "Role"].freeze
|
|
75
|
+
RESOURCE_TYPE = "role"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
class RoleUpdatedEvent < EventNotification
|
|
79
|
+
TYPE = "roles.updated"
|
|
80
|
+
OBJECT_TYPE = [:model, "Role"].freeze
|
|
81
|
+
RESOURCE_TYPE = "role"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class SaleCompletedEvent < EventNotification
|
|
85
|
+
TYPE = "sales.completed"
|
|
86
|
+
OBJECT_TYPE = [:model, "Sale"].freeze
|
|
87
|
+
RESOURCE_TYPE = "sale"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class SaleUpdatedEvent < EventNotification
|
|
91
|
+
TYPE = "sales.updated"
|
|
92
|
+
OBJECT_TYPE = [:model, "Sale"].freeze
|
|
93
|
+
RESOURCE_TYPE = "sale"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
module Events
|
|
97
|
+
TYPES = {
|
|
98
|
+
"checkouts.created" => CheckoutCreatedEvent,
|
|
99
|
+
"checkouts.expired" => CheckoutExpiredEvent,
|
|
100
|
+
"checkouts.failed" => CheckoutFailedEvent,
|
|
101
|
+
"checkouts.paid" => CheckoutPaidEvent,
|
|
102
|
+
"checkouts.updated" => CheckoutUpdatedEvent,
|
|
103
|
+
"members.created" => MemberCreatedEvent,
|
|
104
|
+
"members.deleted" => MemberDeletedEvent,
|
|
105
|
+
"members.updated" => MemberUpdatedEvent,
|
|
106
|
+
"readers.created" => ReaderCreatedEvent,
|
|
107
|
+
"readers.deleted" => ReaderDeletedEvent,
|
|
108
|
+
"roles.created" => RoleCreatedEvent,
|
|
109
|
+
"roles.deleted" => RoleDeletedEvent,
|
|
110
|
+
"roles.updated" => RoleUpdatedEvent,
|
|
111
|
+
"sales.completed" => SaleCompletedEvent,
|
|
112
|
+
"sales.updated" => SaleUpdatedEvent
|
|
113
|
+
}.freeze
|
|
114
|
+
end
|
|
115
|
+
end
|
data/lib/sumup/events.rb
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
require "json"
|
|
5
|
+
require "time"
|
|
6
|
+
|
|
7
|
+
module SumUp
|
|
8
|
+
class EventError < Error; end
|
|
9
|
+
class EventSignatureError < EventError; end
|
|
10
|
+
class EventTimestampError < EventSignatureError; end
|
|
11
|
+
class EventSignatureExpiredError < EventSignatureError; end
|
|
12
|
+
class EventPayloadError < EventError; end
|
|
13
|
+
class EventCallbackError < EventError; end
|
|
14
|
+
class EventObjectURLError < EventError; end
|
|
15
|
+
|
|
16
|
+
class EventNotification
|
|
17
|
+
attr_reader :id, :type, :created_at, :object
|
|
18
|
+
|
|
19
|
+
def initialize(payload, client:)
|
|
20
|
+
@payload = payload
|
|
21
|
+
@client = client
|
|
22
|
+
@id = payload.fetch("id").freeze
|
|
23
|
+
@type = payload.fetch("type").freeze
|
|
24
|
+
@created_at = Time.iso8601(payload.fetch("created_at"))
|
|
25
|
+
@object = Types::EventObject.new(payload.fetch("object"))
|
|
26
|
+
freeze
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Fetches current resource state, which can differ from its state at event creation.
|
|
30
|
+
def fetch_object(options: nil)
|
|
31
|
+
raise EventObjectURLError, "unknown event resource type" unless self.class.const_defined?(:OBJECT_TYPE, false)
|
|
32
|
+
@client.request(method: "GET", path: object.url, options: options, response_type: self.class::OBJECT_TYPE)
|
|
33
|
+
rescue ArgumentError => error
|
|
34
|
+
raise EventObjectURLError, "invalid event resource URL", cause: error
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_h
|
|
38
|
+
Types.encode(@payload)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def inspect
|
|
42
|
+
"#<#{self.class} type=#{type.inspect}>"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class UnknownEvent < EventNotification; end
|
|
47
|
+
|
|
48
|
+
module Events
|
|
49
|
+
SIGNATURE_HEADER = "X-SumUp-Webhook-Signature"
|
|
50
|
+
TOLERANCE_SECONDS = 300
|
|
51
|
+
|
|
52
|
+
class << self
|
|
53
|
+
# The signing protocol matches the sibling SDKs, including the fixed
|
|
54
|
+
# five-minute window in both directions and the exact v1:<timestamp>:<body> bytes.
|
|
55
|
+
def verify_signature(secret:, body:, signature:)
|
|
56
|
+
validate_secret(secret)
|
|
57
|
+
raw = raw_body(body)
|
|
58
|
+
raise EventSignatureError, "missing event signature header" unless signature.is_a?(String) && !signature.empty?
|
|
59
|
+
fields = signature.strip.split(",", -1)
|
|
60
|
+
timestamp_field = fields.first
|
|
61
|
+
unless timestamp_field&.match?(/\At=[0-9]+\z/)
|
|
62
|
+
raise EventTimestampError, "invalid signing timestamp"
|
|
63
|
+
end
|
|
64
|
+
timestamp_text = timestamp_field.delete_prefix("t=")
|
|
65
|
+
timestamp = Integer(timestamp_text, 10)
|
|
66
|
+
raise EventTimestampError, "invalid signing timestamp" if timestamp > (2**53 - 1)
|
|
67
|
+
if (Time.now.to_i - timestamp).abs > TOLERANCE_SECONDS
|
|
68
|
+
raise EventSignatureExpiredError, "event timestamp outside the five-minute window"
|
|
69
|
+
end
|
|
70
|
+
unless fields.size == 2 && fields.last.match?(/\Av1=[0-9a-fA-F]{64}\z/)
|
|
71
|
+
raise EventSignatureError, "invalid event signature header"
|
|
72
|
+
end
|
|
73
|
+
expected = OpenSSL::HMAC.digest("SHA256", secret, "v1:#{timestamp_text}:".b + raw.b)
|
|
74
|
+
supplied = [fields.last.delete_prefix("v1=")].pack("H*")
|
|
75
|
+
unless OpenSSL.fixed_length_secure_compare(expected, supplied)
|
|
76
|
+
raise EventSignatureError, "invalid event signature"
|
|
77
|
+
end
|
|
78
|
+
true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def parse(secret:, body:, signature:, client:)
|
|
82
|
+
verify_signature(secret: secret, body: body, signature: signature)
|
|
83
|
+
parse_without_verification(body, client: client)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Only for data verified before storage in a trusted queue.
|
|
87
|
+
def parse_without_verification(body, client:)
|
|
88
|
+
payload = JSON.parse(raw_body(body))
|
|
89
|
+
unless payload.is_a?(Hash) && %w[id type created_at].all? { |field| payload[field].is_a?(String) && !payload[field].empty? }
|
|
90
|
+
raise EventPayloadError, "invalid event envelope"
|
|
91
|
+
end
|
|
92
|
+
reference = payload["object"]
|
|
93
|
+
unless reference.is_a?(Hash) && %w[id type url].all? { |field| reference[field].is_a?(String) && !reference[field].empty? }
|
|
94
|
+
raise EventPayloadError, "invalid event resource reference"
|
|
95
|
+
end
|
|
96
|
+
# Event timestamps must include a timezone, independently of signature time.
|
|
97
|
+
unless payload["created_at"].match?(/(?:Z|[+-]\d{2}:\d{2})\z/)
|
|
98
|
+
raise EventPayloadError, "event created_at must include a timezone"
|
|
99
|
+
end
|
|
100
|
+
klass = TYPES.fetch(payload["type"], UnknownEvent)
|
|
101
|
+
if klass != UnknownEvent && reference["type"] != klass::RESOURCE_TYPE
|
|
102
|
+
raise EventPayloadError, "event resource type does not match event type"
|
|
103
|
+
end
|
|
104
|
+
klass.new(payload, client: client)
|
|
105
|
+
rescue JSON::ParserError, ArgumentError => error
|
|
106
|
+
raise EventPayloadError, "invalid event payload", cause: error
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def validate_secret(secret)
|
|
110
|
+
raise EventSignatureError, "event signing secret is required" unless secret.is_a?(String) && !secret.empty?
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
def raw_body(body)
|
|
116
|
+
raise EventPayloadError, "event body must be an unchanged String of request bytes" unless body.is_a?(String)
|
|
117
|
+
utf8 = body.dup.force_encoding(Encoding::UTF_8)
|
|
118
|
+
raise EventPayloadError, "event body is not valid UTF-8" unless utf8.valid_encoding?
|
|
119
|
+
utf8
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Registration is synchronized; callbacks run without the registration lock held.
|
|
125
|
+
class EventsHandler
|
|
126
|
+
def initialize(client:, secret:, fallback:)
|
|
127
|
+
Events.validate_secret(secret)
|
|
128
|
+
raise ArgumentError, "fallback must be callable" unless fallback.respond_to?(:call)
|
|
129
|
+
@client = client
|
|
130
|
+
@secret = secret.dup.freeze
|
|
131
|
+
@fallback = fallback
|
|
132
|
+
@callbacks = {}
|
|
133
|
+
@mutex = Mutex.new
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def on(type, callback = nil, &block)
|
|
137
|
+
callback ||= block
|
|
138
|
+
raise ArgumentError, "callback must be callable" unless callback.respond_to?(:call)
|
|
139
|
+
raise ArgumentError, "event type must be a nonempty String" unless type.is_a?(String) && !type.empty?
|
|
140
|
+
@mutex.synchronize do
|
|
141
|
+
raise ArgumentError, "event callback already registered: #{type}" if @callbacks.key?(type)
|
|
142
|
+
@callbacks[type] = callback
|
|
143
|
+
end
|
|
144
|
+
self
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def parse(body, signature:)
|
|
148
|
+
Events.parse(secret: @secret, body: body, signature: signature, client: @client)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Wait for this method to return before acknowledging a delivery. Make callback
|
|
152
|
+
# effects idempotent; a delivery can be repeated or arrive out of order.
|
|
153
|
+
def handle(body, signature:)
|
|
154
|
+
event = parse(body, signature: signature)
|
|
155
|
+
callback = @mutex.synchronize { @callbacks.fetch(event.type, @fallback) }
|
|
156
|
+
begin
|
|
157
|
+
callback.call(event)
|
|
158
|
+
rescue => error
|
|
159
|
+
raise EventCallbackError, "event callback failed", cause: error
|
|
160
|
+
end
|
|
161
|
+
event
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def inspect
|
|
165
|
+
"#<SumUp::EventsHandler>"
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
data/lib/sumup/model.rb
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "date"
|
|
5
|
+
require "time"
|
|
6
|
+
|
|
7
|
+
module SumUp
|
|
8
|
+
# Base for generated API objects. Unknown fields and enum values are retained.
|
|
9
|
+
# Use #key? to distinguish a missing value from an explicit JSON null.
|
|
10
|
+
class Model
|
|
11
|
+
class << self
|
|
12
|
+
def fields
|
|
13
|
+
@fields ||= {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def field(name, type, wire: name.to_s, required: false)
|
|
17
|
+
fields[name] = {wire: wire, type: type, required: required}.freeze
|
|
18
|
+
define_method(name) { @attributes[wire] }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def from_hash(value)
|
|
22
|
+
new(value)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(attributes = {}, **keywords)
|
|
27
|
+
raise ArgumentError, "expected a Hash" unless attributes.is_a?(Hash)
|
|
28
|
+
@attributes = {}
|
|
29
|
+
attributes.merge(keywords).each do |key, value|
|
|
30
|
+
field = self.class.fields[key.to_sym]
|
|
31
|
+
wire = field ? field[:wire] : key.to_s
|
|
32
|
+
field ||= self.class.fields.values.find { |candidate| candidate[:wire] == wire }
|
|
33
|
+
@attributes[wire] = field ? Types.decode(value, field[:type]) : value
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def [](key)
|
|
38
|
+
field = self.class.fields[key.to_sym]
|
|
39
|
+
@attributes[field ? field[:wire] : key.to_s]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def key?(key)
|
|
43
|
+
field = self.class.fields[key.to_sym]
|
|
44
|
+
@attributes.key?(field ? field[:wire] : key.to_s)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def to_h
|
|
48
|
+
@attributes.transform_values { |value| Types.encode(value) }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def to_json(*arguments)
|
|
52
|
+
JSON.generate(to_h, *arguments)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Models can contain payment or personal data; inspection reports field names only.
|
|
56
|
+
def inspect
|
|
57
|
+
"#<#{self.class} fields=#{@attributes.keys.inspect}>"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def ==(other)
|
|
61
|
+
other.instance_of?(self.class) && to_h == other.to_h
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
module Types
|
|
66
|
+
class << self
|
|
67
|
+
def decode(value, descriptor)
|
|
68
|
+
return nil if value.nil?
|
|
69
|
+
return value unless descriptor.is_a?(Array)
|
|
70
|
+
|
|
71
|
+
kind, argument = descriptor
|
|
72
|
+
case kind
|
|
73
|
+
when :model
|
|
74
|
+
value.is_a?(Hash) ? const_get(argument, false).from_hash(value) : value
|
|
75
|
+
when :array
|
|
76
|
+
value.is_a?(Array) ? value.map { |item| decode(item, argument) } : value
|
|
77
|
+
when :map
|
|
78
|
+
value.is_a?(Hash) ? value.transform_values { |item| decode(item, argument) } : value
|
|
79
|
+
when :union
|
|
80
|
+
candidate = argument.max_by { |type| match_score(value, type) }
|
|
81
|
+
candidate ? decode(value, candidate) : value
|
|
82
|
+
else
|
|
83
|
+
value
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def encode(value)
|
|
88
|
+
case value
|
|
89
|
+
when Model then value.to_h
|
|
90
|
+
when Hash then value.to_h { |key, item| [key.to_s, encode(item)] }
|
|
91
|
+
when Array then value.map { |item| encode(item) }
|
|
92
|
+
when Time, DateTime then value.iso8601
|
|
93
|
+
when Date then value.iso8601
|
|
94
|
+
else value
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def match_score(value, descriptor)
|
|
101
|
+
return 0 unless descriptor.is_a?(Array) && descriptor.first == :model && value.is_a?(Hash)
|
|
102
|
+
fields = const_get(descriptor.last, false).fields.values
|
|
103
|
+
required = fields.select { |field| field[:required] }
|
|
104
|
+
return -1 unless required.all? { |field| value.key?(field[:wire]) || value.key?(field[:wire].to_sym) }
|
|
105
|
+
fields.count { |field| value.key?(field[:wire]) || value.key?(field[:wire].to_sym) }
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SumUp
|
|
4
|
+
module Resources
|
|
5
|
+
class Sales
|
|
6
|
+
# Lazily enumerates full sales, preserving the API's opaque pagination token.
|
|
7
|
+
def each(**arguments, &block)
|
|
8
|
+
return enum_for(__method__, **arguments) unless block
|
|
9
|
+
each_token_page(:list, arguments) { |page| page.items.each(&block) }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def search_each(**arguments, &block)
|
|
13
|
+
return enum_for(__method__, **arguments) unless block
|
|
14
|
+
each_token_page(:search, arguments) { |page| page.items.each(&block) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def each_token_page(method, arguments)
|
|
20
|
+
arguments = arguments.dup
|
|
21
|
+
if method == :search
|
|
22
|
+
payload = request_body(arguments.delete(:body) { UNSET }, arguments.except(:merchant_code, :options))
|
|
23
|
+
payload = payload.to_h if payload.is_a?(Model)
|
|
24
|
+
payload = {} if payload.equal?(UNSET)
|
|
25
|
+
raise ArgumentError, "search body must be a Hash or Model" unless payload.is_a?(Hash)
|
|
26
|
+
arguments = arguments.slice(:merchant_code, :options)
|
|
27
|
+
arguments[:body] = payload.transform_keys(&:to_s)
|
|
28
|
+
end
|
|
29
|
+
seen = Set.new
|
|
30
|
+
loop do
|
|
31
|
+
page = public_send(method, **arguments)
|
|
32
|
+
yield page
|
|
33
|
+
token = page.pagination_token
|
|
34
|
+
break if token.nil? || token.empty?
|
|
35
|
+
raise InvalidResponseError, "API repeated a pagination token" unless seen.add?(token)
|
|
36
|
+
if method == :search
|
|
37
|
+
arguments[:body] = arguments[:body].merge("pagination_token" => token)
|
|
38
|
+
else
|
|
39
|
+
arguments[:pagination_token] = token
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class Transactions
|
|
46
|
+
# Uses the API's next-page query string on the same history endpoint.
|
|
47
|
+
def each(**arguments, &block)
|
|
48
|
+
return enum_for(__method__, **arguments) unless block
|
|
49
|
+
page = list(**arguments)
|
|
50
|
+
seen = Set.new
|
|
51
|
+
loop do
|
|
52
|
+
Array(page.items).each(&block)
|
|
53
|
+
link = Array(page.links).find { |item| item.rel == "next" }
|
|
54
|
+
break if link&.href.nil? || link.href.empty?
|
|
55
|
+
raise InvalidResponseError, "API repeated a pagination link" unless seen.add?(link.href)
|
|
56
|
+
path = "/v2.1/merchants/#{path_segment(arguments.fetch(:merchant_code))}/transactions/history"
|
|
57
|
+
# This API returns a query string rather than an absolute URL.
|
|
58
|
+
query = link.href.delete_prefix("?")
|
|
59
|
+
if query.include?("#") || query.start_with?("/", "http:", "https:")
|
|
60
|
+
raise InvalidResponseError, "invalid transaction pagination query"
|
|
61
|
+
end
|
|
62
|
+
page = @client.request(method: "GET", path: "#{path}?#{query}", options: arguments[:options],
|
|
63
|
+
response_type: [:model, "ListTransactionsV21Response"])
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
module OffsetPagination
|
|
69
|
+
def each(**arguments, &block)
|
|
70
|
+
return enum_for(__method__, **arguments) unless block
|
|
71
|
+
arguments = arguments.dup
|
|
72
|
+
arguments[:offset] ||= 0
|
|
73
|
+
loop do
|
|
74
|
+
page = list(**arguments)
|
|
75
|
+
items = Array(page.items)
|
|
76
|
+
break if items.empty?
|
|
77
|
+
items.each(&block)
|
|
78
|
+
arguments[:offset] += items.size
|
|
79
|
+
break if page.total_count && arguments[:offset] >= page.total_count
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
class Members
|
|
85
|
+
include OffsetPagination
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
class Memberships
|
|
89
|
+
include OffsetPagination
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SumUp
|
|
4
|
+
class Resource
|
|
5
|
+
def initialize(client)
|
|
6
|
+
@client = client
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def inspect
|
|
10
|
+
"#<#{self.class}>"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def path_segment(value)
|
|
16
|
+
raise ArgumentError, "path parameters cannot be nil or empty" if value.nil? || value.to_s.empty?
|
|
17
|
+
URI.encode_www_form_component(value.to_s).gsub("+", "%20")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def request_body(body, fields)
|
|
21
|
+
unless body.equal?(UNSET)
|
|
22
|
+
raise ArgumentError, "pass body or body keywords, not both" unless fields.empty?
|
|
23
|
+
return body
|
|
24
|
+
end
|
|
25
|
+
fields.empty? ? UNSET : fields
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def merge_header_options(options, parameters)
|
|
29
|
+
options = RequestOptions.new(**options) if options.is_a?(Hash)
|
|
30
|
+
options ||= RequestOptions.new
|
|
31
|
+
headers = parameters.reject { |_, value| value.equal?(UNSET) }.merge(options.headers)
|
|
32
|
+
RequestOptions.new(headers: headers, api_key: options.api_key, timeout: options.timeout,
|
|
33
|
+
open_timeout: options.open_timeout, max_retries: options.max_retries)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Generated by codegen. DO NOT EDIT.
|
|
4
|
+
|
|
5
|
+
module SumUp
|
|
6
|
+
module Resources
|
|
7
|
+
class Checkouts < Resource
|
|
8
|
+
# Lists the payment methods available to the merchant for checkout payments. Use the optional amount and currency filters to check eligibility for a particular payment before presenting payment options to the payer.
|
|
9
|
+
# @return [Types::GetPaymentMethodsResponse]
|
|
10
|
+
def list_available_payment_methods(merchant_code:, amount: UNSET, currency: UNSET, options: nil)
|
|
11
|
+
@client.request(method: "GET", path: "/v0.1/merchants/#{path_segment(merchant_code)}/payment-methods",
|
|
12
|
+
query: {"amount" => amount, "currency" => currency}, options: options,
|
|
13
|
+
response_type: {200 => [:model, "GetPaymentMethodsResponse"]}, with_response: false)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def list_available_payment_methods_with_response(merchant_code:, amount: UNSET, currency: UNSET, options: nil)
|
|
17
|
+
@client.request(method: "GET", path: "/v0.1/merchants/#{path_segment(merchant_code)}/payment-methods",
|
|
18
|
+
query: {"amount" => amount, "currency" => currency}, options: options,
|
|
19
|
+
response_type: {200 => [:model, "GetPaymentMethodsResponse"]}, with_response: true)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Creates a payment checkout for the specified merchant, amount, and currency. Supply a `checkout_reference` to identify the payment attempt in your own systems. Creating a checkout does not charge a payment instrument.
|
|
23
|
+
#
|
|
24
|
+
# Set `hosted_checkout.enabled` to `true` to receive a [Hosted Checkout](https://developer.sumup.com/online-payments/checkouts/hosted-checkout/) URL where the customer can complete the payment.
|
|
25
|
+
# Use `redirect_url` for redirect-based payment and 3DS flows. If `return_url` is provided, SumUp sends processing updates to that backend callback URL.
|
|
26
|
+
#
|
|
27
|
+
# Complete the payment through [Hosted Checkout](https://developer.sumup.com/online-payments/checkouts/hosted-checkout/) or the [Payment Widget](https://developer.sumup.com/online-payments/checkouts/card-widget).
|
|
28
|
+
# @return [Types::Checkout]
|
|
29
|
+
def create(body: UNSET, options: nil, **body_fields)
|
|
30
|
+
payload = request_body(body, body_fields)
|
|
31
|
+
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
|
|
32
|
+
@client.request(method: "POST", path: "/v0.1/checkouts",
|
|
33
|
+
query: {}, body: payload, options: options,
|
|
34
|
+
response_type: {201 => [:model, "Checkout"]}, with_response: false)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def create_with_response(body: UNSET, options: nil, **body_fields)
|
|
38
|
+
payload = request_body(body, body_fields)
|
|
39
|
+
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
|
|
40
|
+
@client.request(method: "POST", path: "/v0.1/checkouts",
|
|
41
|
+
query: {}, body: payload, options: options,
|
|
42
|
+
response_type: {201 => [:model, "Checkout"]}, with_response: true)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Lists created checkout resources according to the applied `checkout_reference`.
|
|
46
|
+
# @return [Array[Types::CheckoutSuccess]]
|
|
47
|
+
def list(checkout_reference: UNSET, options: nil)
|
|
48
|
+
@client.request(method: "GET", path: "/v0.1/checkouts",
|
|
49
|
+
query: {"checkout_reference" => checkout_reference}, options: options,
|
|
50
|
+
response_type: {200 => [:array, [:model, "CheckoutSuccess"]]}, with_response: false)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def list_with_response(checkout_reference: UNSET, options: nil)
|
|
54
|
+
@client.request(method: "GET", path: "/v0.1/checkouts",
|
|
55
|
+
query: {"checkout_reference" => checkout_reference}, options: options,
|
|
56
|
+
response_type: {200 => [:array, [:model, "CheckoutSuccess"]]}, with_response: true)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Retrieves a checkout by its SumUp `checkout_id`. After processing a payment, returning from a redirect, or receiving a checkout notification, retrieve the checkout to confirm its current `status` before updating your order or displaying the payment outcome to the payer.
|
|
60
|
+
# @return [Types::CheckoutSuccess]
|
|
61
|
+
def get(checkout_id:, options: nil)
|
|
62
|
+
@client.request(method: "GET", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
63
|
+
query: {}, options: options,
|
|
64
|
+
response_type: {200 => [:model, "CheckoutSuccess"]}, with_response: false)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def get_with_response(checkout_id:, options: nil)
|
|
68
|
+
@client.request(method: "GET", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
69
|
+
query: {}, options: options,
|
|
70
|
+
response_type: {200 => [:model, "CheckoutSuccess"]}, with_response: true)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Updates the amount, currency, description, reference, expiration, or customer associated with an existing checkout. Only the supplied fields are updated.
|
|
74
|
+
#
|
|
75
|
+
# This request changes the checkout details; it does not charge a payment instrument. Process the checkout separately to attempt a payment.
|
|
76
|
+
# @return [Types::Checkout]
|
|
77
|
+
def update(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
78
|
+
payload = request_body(body, body_fields)
|
|
79
|
+
payload = {} if payload.equal?(UNSET)
|
|
80
|
+
@client.request(method: "PATCH", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
81
|
+
query: {}, body: payload, options: options,
|
|
82
|
+
response_type: {200 => [:model, "Checkout"]}, with_response: false)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def update_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
86
|
+
payload = request_body(body, body_fields)
|
|
87
|
+
payload = {} if payload.equal?(UNSET)
|
|
88
|
+
@client.request(method: "PATCH", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
89
|
+
query: {}, body: payload, options: options,
|
|
90
|
+
response_type: {200 => [:model, "Checkout"]}, with_response: true)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# :::caution[PCI DSS compliance required]
|
|
94
|
+
# When you submit raw card details directly to the Checkout API, your systems store, process, or transmit cardholder data and are therefore subject to applicable [PCI DSS requirements](https://www.pcisecuritystandards.org/document_library/). You should only use this integration if your environment is appropriately PCI DSS compliant.
|
|
95
|
+
# :::
|
|
96
|
+
#
|
|
97
|
+
# Processing a checkout will attempt to charge the provided payment instrument for the amount of the specified checkout resource initiated in the `Create a checkout` endpoint.
|
|
98
|
+
#
|
|
99
|
+
# A processing response can require an additional payer action, such as a 3DS challenge or a payment-provider redirect. If `next_step` is returned, follow its instructions to continue the payment flow.
|
|
100
|
+
#
|
|
101
|
+
# Retrieve the checkout afterwards to confirm its payment status. Acceptance of the processing request does not by itself mean the checkout is paid.
|
|
102
|
+
# @return [(Types::CheckoutSuccess | Types::CheckoutAccepted)]
|
|
103
|
+
def process(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
104
|
+
payload = request_body(body, body_fields)
|
|
105
|
+
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
|
|
106
|
+
@client.request(method: "PUT", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
107
|
+
query: {}, body: payload, options: options,
|
|
108
|
+
response_type: {200 => [:model, "CheckoutSuccess"], 202 => [:model, "CheckoutAccepted"]}, with_response: false)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def process_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
112
|
+
payload = request_body(body, body_fields)
|
|
113
|
+
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
|
|
114
|
+
@client.request(method: "PUT", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
115
|
+
query: {}, body: payload, options: options,
|
|
116
|
+
response_type: {200 => [:model, "CheckoutSuccess"], 202 => [:model, "CheckoutAccepted"]}, with_response: true)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Deactivates an identified checkout resource. If the checkout has already been processed it can not be deactivated.
|
|
120
|
+
# @return [Types::Checkout]
|
|
121
|
+
def deactivate(checkout_id:, options: nil)
|
|
122
|
+
@client.request(method: "DELETE", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
123
|
+
query: {}, options: options,
|
|
124
|
+
response_type: {200 => [:model, "Checkout"]}, with_response: false)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def deactivate_with_response(checkout_id:, options: nil)
|
|
128
|
+
@client.request(method: "DELETE", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
|
|
129
|
+
query: {}, options: options,
|
|
130
|
+
response_type: {200 => [:model, "Checkout"]}, with_response: true)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Creates an Apple Pay merchant session for the specified checkout.
|
|
134
|
+
#
|
|
135
|
+
# Use this endpoint after the customer selects Apple Pay and before calling
|
|
136
|
+
# `ApplePaySession.completeMerchantValidation(...)` in the browser.
|
|
137
|
+
# SumUp validates the merchant session request and returns the Apple Pay
|
|
138
|
+
# session object that your frontend should pass to Apple's JavaScript API.
|
|
139
|
+
# @return [Hash[String, untyped]]
|
|
140
|
+
def create_apple_pay_session(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
141
|
+
payload = request_body(body, body_fields)
|
|
142
|
+
@client.request(method: "PUT", path: "/v0.2/checkouts/#{path_segment(checkout_id)}/apple-pay-session",
|
|
143
|
+
query: {}, body: payload, options: options,
|
|
144
|
+
response_type: {200 => [:map, :any]}, with_response: false)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def create_apple_pay_session_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
|
|
148
|
+
payload = request_body(body, body_fields)
|
|
149
|
+
@client.request(method: "PUT", path: "/v0.2/checkouts/#{path_segment(checkout_id)}/apple-pay-session",
|
|
150
|
+
query: {}, body: payload, options: options,
|
|
151
|
+
response_type: {200 => [:map, :any]}, with_response: true)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|