paymos 1.6.0 → 1.7.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.
data/lib/paymos/client.rb CHANGED
@@ -211,7 +211,7 @@ module Paymos
211
211
 
212
212
  class Invoices < Resource
213
213
  def create(project_id:, amount:, currency:, external_order_id:, network: nil,
214
- customer_fee_percent: nil, client_id: nil)
214
+ customer_fee_percent: nil, client_id: nil, products: nil)
215
215
  required_keywords(project_id:, amount:, currency:, external_order_id:)
216
216
  if !customer_fee_percent.nil? && !(customer_fee_percent.is_a?(Integer) && customer_fee_percent.between?(0, 100))
217
217
  raise ArgumentError, 'customer_fee_percent must be between 0 and 100'
@@ -219,6 +219,7 @@ module Paymos
219
219
 
220
220
  payload = { project_id:, amount:, currency:, external_order_id:, network:,
221
221
  customer_fee_percent:, client_id: }.compact
222
+ payload[:products] = serialize_products(products) unless products.nil?
222
223
  Invoice.from(@client.request('POST', '/v1/invoices', payload:))
223
224
  end
224
225
 
@@ -261,6 +262,18 @@ module Paymos
261
262
  missing = values.select { |_name, value| !value.is_a?(String) || value.strip.empty? }.keys
262
263
  raise ArgumentError, "Required values are missing: #{missing.join(', ')}" unless missing.empty?
263
264
  end
265
+
266
+ def serialize_products(products)
267
+ raise ArgumentError, 'products must be an array' unless products.is_a?(Array)
268
+
269
+ products.map do |item|
270
+ raise ArgumentError, 'products items must respond to to_h' unless item.respond_to?(:to_h)
271
+
272
+ item.to_h.transform_keys(&:to_sym).reject do |key, value|
273
+ %i[description image_url].include?(key) && value.nil?
274
+ end
275
+ end
276
+ end
264
277
  end
265
278
 
266
279
  class Withdrawals < Resource
@@ -444,16 +457,19 @@ module Paymos
444
457
  raise ArgumentError, 'max_pages must be a positive integer' unless max_pages.is_a?(Integer) && max_pages.positive?
445
458
 
446
459
  filters = filters.dup
447
- cursor = filters[:cursor]
460
+ # Every cursor this walk has requested, not only the previous one: a cycle A -> B -> A
461
+ # repeats a position without two equal cursors in a row.
462
+ used = {}
463
+ used[filters[:cursor]] = true unless filters[:cursor].nil?
448
464
  max_pages.times do
449
465
  page = fetch.call(**filters)
450
466
  page.items.each(&block)
451
467
  next_cursor = page.next_cursor
452
468
  return if next_cursor.nil? || next_cursor.to_s.empty?
453
- raise Error, 'Paymos API returned the same pagination cursor twice' if next_cursor == cursor
469
+ raise Error, 'Paymos API returned a pagination cursor this walk already used' if used.key?(next_cursor)
454
470
 
455
- cursor = next_cursor
456
- filters[:cursor] = cursor
471
+ used[next_cursor] = true
472
+ filters[:cursor] = next_cursor
457
473
  end
458
474
  end
459
475
  end
data/lib/paymos/models.rb CHANGED
@@ -79,9 +79,28 @@ module Paymos
79
79
  end
80
80
  end
81
81
 
82
+ class InvoiceProduct < Model
83
+ fields :title, :description, :image_url, :quantity, :unit_amount,
84
+ required: %i[title quantity unit_amount]
85
+
86
+ def to_h
87
+ super.reject { |key, value| %i[description image_url].include?(key) && value.nil? }
88
+ end
89
+ end
90
+
82
91
  class Order < Model
83
- fields :external_id, :client_id, :amount, :currency, :network,
92
+ fields :external_id, :client_id, :amount, :currency, :network, :products,
84
93
  required: %i[external_id amount currency]
94
+
95
+ def self.from(value)
96
+ model = super
97
+ return model if model.products.nil?
98
+
99
+ raise Error, 'Paymos API order products must be an array' unless model.products.is_a?(Array)
100
+
101
+ products = model.products.map { |item| InvoiceProduct.from(item) }
102
+ new(**model.to_h, products: products)
103
+ end
85
104
  end
86
105
 
87
106
  class Transfer < Model
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Paymos
4
- VERSION = '1.6.0'
4
+ VERSION = '1.7.0'
5
5
  end
data/sig/paymos.rbs CHANGED
@@ -31,6 +31,8 @@ module Paymos
31
31
  end
32
32
  class AuthenticationError < ApiError
33
33
  end
34
+ class PermissionError < AuthenticationError
35
+ end
34
36
  class NotFoundError < ApiError
35
37
  end
36
38
  class ConflictError < ApiError
@@ -44,17 +46,50 @@ module Paymos
44
46
  class UnavailableError < ServerError
45
47
  end
46
48
 
49
+ module InvoiceStatus
50
+ AWAITING_CLIENT: "awaiting_client"
51
+ AWAITING_PAYMENT: "awaiting_payment"
52
+ CONFIRMING: "confirming"
53
+ UNDERPAID_WAITING: "underpaid_waiting"
54
+ PAID: "paid"
55
+ PAID_OVER: "paid_over"
56
+ UNDERPAID: "underpaid"
57
+ EXPIRED: "expired"
58
+ CANCELLED: "cancelled"
59
+ end
60
+
61
+ module WithdrawalStatus
62
+ CREATED: "created"
63
+ PENDING_REVIEW: "pending_review"
64
+ SIGNED: "signed"
65
+ CANCELLING: "cancelling"
66
+ COMPLETED: "completed"
67
+ FAILED: "failed"
68
+ CANCELLED: "cancelled"
69
+ end
70
+
47
71
  class Model
48
72
  def []: (String | Symbol name) -> untyped
49
73
  def to_h: () -> Hash[Symbol, untyped]
50
74
  end
51
75
 
76
+ class InvoiceProduct < Model
77
+ attr_reader title: String
78
+ attr_reader description: String?
79
+ attr_reader image_url: String?
80
+ attr_reader quantity: Integer
81
+ attr_reader unit_amount: String
82
+
83
+ def initialize: (title: String, quantity: Integer, unit_amount: String, ?description: String?, ?image_url: String?) -> void
84
+ end
85
+
52
86
  class Order < Model
53
87
  attr_reader external_id: String
54
88
  attr_reader client_id: String?
55
89
  attr_reader amount: String
56
90
  attr_reader currency: String
57
91
  attr_reader network: String?
92
+ attr_reader products: Array[InvoiceProduct]?
58
93
  end
59
94
 
60
95
  class Transfer < Model
@@ -96,6 +131,7 @@ module Paymos
96
131
  attr_reader updated_at: Integer
97
132
  attr_reader expires_at: Integer?
98
133
  attr_reader completed_at: Integer?
134
+ attr_reader paid_late_at: Integer?
99
135
  end
100
136
 
101
137
  class InvoiceListItem < Model
@@ -112,6 +148,7 @@ module Paymos
112
148
  attr_reader created_at: Integer
113
149
  attr_reader expires_at: Integer?
114
150
  attr_reader completed_at: Integer?
151
+ attr_reader paid_late_at: Integer?
115
152
  end
116
153
 
117
154
  class Withdrawal < Model
@@ -254,7 +291,7 @@ module Paymos
254
291
  end
255
292
 
256
293
  class Invoices
257
- def create: (project_id: String, amount: String, currency: String, external_order_id: String, ?network: String?, ?customer_fee_percent: Integer?, ?client_id: String?) -> Invoice
294
+ def create: (project_id: String, amount: String, currency: String, external_order_id: String, ?network: String?, ?customer_fee_percent: Integer?, ?client_id: String?, ?products: Array[InvoiceProduct | Hash[Symbol | String, untyped]]?) -> Invoice
258
295
  def get: (String invoice_id) -> Invoice
259
296
  def list: (?limit: Integer?, ?cursor: String?, ?status: String | Array[String]?, ?external_order_id: String?, ?project_id: String?, ?created_from: Integer?, ?created_to: Integer?) -> Page[InvoiceListItem]
260
297
  def cancel: (String invoice_id, reason: String) -> Invoice
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paymos
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paymos Labs
@@ -33,6 +33,7 @@ files:
33
33
  - CHANGELOG.md
34
34
  - LICENSE
35
35
  - README.md
36
+ - conformance/contract.json
36
37
  - lib/paymos.rb
37
38
  - lib/paymos/client.rb
38
39
  - lib/paymos/errors.rb