jamsrpay 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/LICENSE +21 -0
- data/README.md +45 -0
- data/jamsrpay.gemspec +23 -0
- data/lib/jamsrpay/client.rb +43 -0
- data/lib/jamsrpay/config.rb +85 -0
- data/lib/jamsrpay/errors.rb +150 -0
- data/lib/jamsrpay/http_client.rb +242 -0
- data/lib/jamsrpay/resources/assets.rb +31 -0
- data/lib/jamsrpay/resources/invoices.rb +73 -0
- data/lib/jamsrpay/resources/payment_intents.rb +49 -0
- data/lib/jamsrpay/resources/store_assets.rb +22 -0
- data/lib/jamsrpay/sse.rb +85 -0
- data/lib/jamsrpay/types.rb +89 -0
- data/lib/jamsrpay/version.rb +5 -0
- data/lib/jamsrpay/webhooks.rb +152 -0
- data/lib/jamsrpay.rb +14 -0
- metadata +65 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6b15ae6e26f42f6406e06a7c084b8768cffdd817da0b865f5426a9a57985407c
|
|
4
|
+
data.tar.gz: cdcf29d1de71f17093a7fbb2f5e5620bf97752348891373fddfd3e2f83545ff8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3bf7284f06ffb0c789c5503934382f9b18733b2d92d223510e0287997c89830fad41965df870926e14aaa21eb8e82ceb9737f89f4c45708c2bb6f314fb503648
|
|
7
|
+
data.tar.gz: 39a94eaea76c3ceb981364cfd2ff7c2e8823c0c6bbf16bcacdb317a6813680579ceb2c8d362f572dc3d9d6ea44d551fba3e028ae691b66bf52e1ba558a407e40
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JamsrPay
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# jamsrpay (Ruby SDK)
|
|
2
|
+
|
|
3
|
+
Official zero-dependency Ruby SDK for the **JamsrPay** cryptocurrency payment gateway (`Ruby >= 2.6`).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem "jamsrpay"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
require "jamsrpay"
|
|
15
|
+
|
|
16
|
+
client = JamsrPay::Client.new(api_key: ENV.fetch("JAMSRPAY_API_KEY"))
|
|
17
|
+
|
|
18
|
+
intent = client.payment_intents.create(
|
|
19
|
+
pricing_amount: "49.99",
|
|
20
|
+
pricing_currency: JamsrPay::FiatCurrencyId::USD,
|
|
21
|
+
payment_title: "Order #1042"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
invoice = client.invoices.create_for_payment(
|
|
25
|
+
intent["id"],
|
|
26
|
+
asset_id: JamsrPay::AssetId::USDT,
|
|
27
|
+
network_id: JamsrPay::NetworkId::TRON_MAINNET
|
|
28
|
+
)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Webhook Verification
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
event = JamsrPay::Webhooks.construct_event(
|
|
35
|
+
raw_body: request.raw_post,
|
|
36
|
+
headers_or_signature: request.headers,
|
|
37
|
+
secret: ENV.fetch("JAMSRPAY_WEBHOOK_SECRET")
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Running Tests
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
ruby -Ilib -Itest test/run_all.rb
|
|
45
|
+
```
|
data/jamsrpay.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/jamsrpay/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "jamsrpay"
|
|
7
|
+
spec.version = JamsrPay::VERSION
|
|
8
|
+
spec.authors = ["JamsrPay"]
|
|
9
|
+
spec.email = ["dev@jamsrpay.cloud"]
|
|
10
|
+
spec.summary = "Official zero-dependency Ruby SDK for the JamsrPay cryptocurrency payment gateway."
|
|
11
|
+
spec.homepage = "https://jamsrpay.cloud"
|
|
12
|
+
spec.license = "MIT"
|
|
13
|
+
spec.required_ruby_version = ">= 2.6.0"
|
|
14
|
+
spec.metadata = {
|
|
15
|
+
"homepage_uri" => "https://jamsrpay.cloud",
|
|
16
|
+
"source_code_uri" => "https://github.com/JamsrPay/sdks/tree/main/packages/ruby",
|
|
17
|
+
"documentation_uri" => "https://docs.jamsrpay.cloud",
|
|
18
|
+
"bug_tracker_uri" => "https://github.com/JamsrPay/sdks/issues",
|
|
19
|
+
"rubygems_mfa_required" => "true"
|
|
20
|
+
}
|
|
21
|
+
spec.files = Dir.glob("lib/**/*.rb") + %w[README.md LICENSE jamsrpay.gemspec]
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
23
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
class Client
|
|
5
|
+
attr_reader :environment, :base_url, :invoices, :payment_intents, :assets, :store_assets
|
|
6
|
+
|
|
7
|
+
def initialize(
|
|
8
|
+
api_key: nil,
|
|
9
|
+
environment: nil,
|
|
10
|
+
base_url: nil,
|
|
11
|
+
timeout_seconds: Config::DEFAULT_TIMEOUT_SECONDS,
|
|
12
|
+
max_retries: Config::DEFAULT_MAX_RETRIES,
|
|
13
|
+
auto_idempotency: true,
|
|
14
|
+
default_headers: nil,
|
|
15
|
+
user_agent: nil,
|
|
16
|
+
transport: nil,
|
|
17
|
+
sleep_fn: nil
|
|
18
|
+
)
|
|
19
|
+
resolved = Config.resolve(
|
|
20
|
+
api_key: api_key,
|
|
21
|
+
environment: environment,
|
|
22
|
+
base_url: base_url,
|
|
23
|
+
timeout_seconds: timeout_seconds,
|
|
24
|
+
max_retries: max_retries,
|
|
25
|
+
auto_idempotency: auto_idempotency,
|
|
26
|
+
default_headers: default_headers,
|
|
27
|
+
user_agent: user_agent
|
|
28
|
+
)
|
|
29
|
+
@environment = resolved[:environment]
|
|
30
|
+
@base_url = resolved[:base_url]
|
|
31
|
+
|
|
32
|
+
http = HttpClient.new(resolved, transport: transport, sleep_fn: sleep_fn)
|
|
33
|
+
@invoices = Resources::Invoices.new(http)
|
|
34
|
+
@payment_intents = Resources::PaymentIntents.new(http)
|
|
35
|
+
@assets = Resources::Assets.new(http)
|
|
36
|
+
@store_assets = Resources::StoreAssets.new(http)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def webhooks
|
|
40
|
+
JamsrPay::Webhooks
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module Config
|
|
5
|
+
ENVIRONMENT_BASE_URLS = {
|
|
6
|
+
"production" => "https://api.jamsrpay.cloud",
|
|
7
|
+
"sandbox" => "https://api-sandbox.jamsrpay.cloud"
|
|
8
|
+
}.freeze
|
|
9
|
+
|
|
10
|
+
LIVE_API_KEY_PREFIX = "apisec_live_"
|
|
11
|
+
TEST_API_KEY_PREFIX = "apisec_test_"
|
|
12
|
+
|
|
13
|
+
DEFAULT_TIMEOUT_SECONDS = 30.0
|
|
14
|
+
DEFAULT_MAX_RETRIES = 2
|
|
15
|
+
|
|
16
|
+
def self.infer_environment_from_api_key(api_key)
|
|
17
|
+
return nil if api_key.nil?
|
|
18
|
+
return "production" if api_key.start_with?(LIVE_API_KEY_PREFIX)
|
|
19
|
+
return "sandbox" if api_key.start_with?(TEST_API_KEY_PREFIX)
|
|
20
|
+
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.resolve(
|
|
25
|
+
api_key: nil,
|
|
26
|
+
environment: nil,
|
|
27
|
+
base_url: nil,
|
|
28
|
+
timeout_seconds: DEFAULT_TIMEOUT_SECONDS,
|
|
29
|
+
max_retries: DEFAULT_MAX_RETRIES,
|
|
30
|
+
auto_idempotency: true,
|
|
31
|
+
default_headers: nil,
|
|
32
|
+
user_agent: nil
|
|
33
|
+
)
|
|
34
|
+
cleaned_key = nil
|
|
35
|
+
unless api_key.nil?
|
|
36
|
+
trimmed = api_key.to_s.strip
|
|
37
|
+
if trimmed.empty? && api_key.to_s != ""
|
|
38
|
+
raise ConfigError, "api_key cannot be empty or whitespace"
|
|
39
|
+
end
|
|
40
|
+
cleaned_key = trimmed unless trimmed.empty?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
env_str = environment.nil? ? nil : environment.to_s
|
|
44
|
+
if !env_str.nil? && !ENVIRONMENT_BASE_URLS.key?(env_str)
|
|
45
|
+
raise ConfigError, "Unsupported environment '#{env_str}'. Expected 'production' or 'sandbox'."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
cleaned_base_url = nil
|
|
49
|
+
unless base_url.nil?
|
|
50
|
+
cleaned_base_url = base_url.to_s.strip.sub(%r{/+\z}, "")
|
|
51
|
+
raise ConfigError, "base_url cannot be empty or whitespace" if cleaned_base_url.empty?
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
inferred = infer_environment_from_api_key(cleaned_key)
|
|
55
|
+
|
|
56
|
+
if !inferred.nil? && !env_str.nil? && inferred != env_str && cleaned_base_url.nil?
|
|
57
|
+
raise ConfigError, "API key prefix implies '#{inferred}', which conflicts with explicit '#{env_str}'."
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
if !cleaned_key.nil? && inferred.nil? && env_str.nil? && cleaned_base_url.nil?
|
|
61
|
+
raise ConfigError, "Unrecognized api_key prefix. Expected apisec_live_* or apisec_test_*, or set environment/base_url."
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
resolved_env = env_str || inferred || "production"
|
|
65
|
+
resolved_base_url = cleaned_base_url || ENVIRONMENT_BASE_URLS.fetch(resolved_env)
|
|
66
|
+
|
|
67
|
+
timeout_val = Float(timeout_seconds)
|
|
68
|
+
raise ConfigError, "timeout_seconds must be positive" unless timeout_val.finite? && timeout_val.positive?
|
|
69
|
+
|
|
70
|
+
retries_val = Integer(max_retries)
|
|
71
|
+
raise ConfigError, "max_retries must be non-negative" if retries_val.negative?
|
|
72
|
+
|
|
73
|
+
{
|
|
74
|
+
api_key: cleaned_key,
|
|
75
|
+
environment: resolved_env,
|
|
76
|
+
base_url: resolved_base_url,
|
|
77
|
+
timeout_seconds: timeout_val,
|
|
78
|
+
max_retries: retries_val,
|
|
79
|
+
auto_idempotency: !!auto_idempotency,
|
|
80
|
+
default_headers: (default_headers || {}).transform_keys(&:to_s).transform_values(&:to_s),
|
|
81
|
+
user_agent: user_agent || "jamsrpay-ruby/#{JamsrPay::VERSION}"
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module JamsrPay
|
|
6
|
+
class Error < StandardError
|
|
7
|
+
attr_reader :code
|
|
8
|
+
|
|
9
|
+
def initialize(message, code: "JAMSRPAY_ERROR")
|
|
10
|
+
super(message)
|
|
11
|
+
@code = code
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class ConfigError < Error
|
|
16
|
+
def initialize(message, code: "CONFIG_ERROR")
|
|
17
|
+
super(message, code: code)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class NetworkError < Error
|
|
22
|
+
attr_reader :is_timeout
|
|
23
|
+
|
|
24
|
+
def initialize(message, is_timeout: false)
|
|
25
|
+
super(message, code: is_timeout ? "REQUEST_TIMEOUT" : "NETWORK_ERROR")
|
|
26
|
+
@is_timeout = is_timeout
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
class WebhookError < Error
|
|
31
|
+
SIGNATURE_MISSING = "WEBHOOK_SIGNATURE_MISSING"
|
|
32
|
+
SIGNATURE_INVALID_FORMAT = "WEBHOOK_SIGNATURE_INVALID_FORMAT"
|
|
33
|
+
TIMESTAMP_EXPIRED = "WEBHOOK_TIMESTAMP_EXPIRED"
|
|
34
|
+
SIGNATURE_MISMATCH = "WEBHOOK_SIGNATURE_MISMATCH"
|
|
35
|
+
INVALID_PAYLOAD = "WEBHOOK_INVALID_PAYLOAD"
|
|
36
|
+
|
|
37
|
+
def initialize(message, code:)
|
|
38
|
+
super(message, code: code)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class ApiError < Error
|
|
43
|
+
attr_reader :status, :title, :detail, :type, :field, :request_id, :retryable, :retry_after_seconds, :raw_body
|
|
44
|
+
|
|
45
|
+
def initialize(
|
|
46
|
+
message:,
|
|
47
|
+
status:,
|
|
48
|
+
code:,
|
|
49
|
+
title: nil,
|
|
50
|
+
detail: nil,
|
|
51
|
+
type: nil,
|
|
52
|
+
field: nil,
|
|
53
|
+
request_id: nil,
|
|
54
|
+
retryable: nil,
|
|
55
|
+
retry_after_seconds: nil,
|
|
56
|
+
raw_body: nil
|
|
57
|
+
)
|
|
58
|
+
super(message, code: code)
|
|
59
|
+
@status = status
|
|
60
|
+
@title = title
|
|
61
|
+
@detail = detail
|
|
62
|
+
@type = type
|
|
63
|
+
@field = field
|
|
64
|
+
@request_id = request_id
|
|
65
|
+
@retryable = retryable.nil? ? self.class.retryable_status?(status) : retryable
|
|
66
|
+
@retry_after_seconds = retry_after_seconds
|
|
67
|
+
@raw_body = raw_body
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def self.retryable_status?(status)
|
|
71
|
+
status == 409 || status == 429 || status >= 500
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.parse_retry_after_seconds(header)
|
|
75
|
+
return nil if header.nil?
|
|
76
|
+
|
|
77
|
+
trimmed = header.to_s.strip
|
|
78
|
+
return nil if trimmed.empty?
|
|
79
|
+
return trimmed.to_i if trimmed.match?(/\A\d+\z/)
|
|
80
|
+
|
|
81
|
+
begin
|
|
82
|
+
parsed = Time.httpdate(trimmed)
|
|
83
|
+
delta = (parsed - Time.now.utc).ceil
|
|
84
|
+
delta.negative? ? 0 : delta
|
|
85
|
+
rescue ArgumentError
|
|
86
|
+
nil
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.from_response(http_status:, headers:, body:)
|
|
91
|
+
hdr_req_id = non_empty_str(header_ci(headers, "x-request-id"))
|
|
92
|
+
retry_after = parse_retry_after_seconds(header_ci(headers, "retry-after"))
|
|
93
|
+
|
|
94
|
+
if body.is_a?(Hash)
|
|
95
|
+
status = body["status"].is_a?(Integer) ? body["status"] : http_status
|
|
96
|
+
code = non_empty_str(body["code"]) || "HTTP_#{status}"
|
|
97
|
+
title = non_empty_str(body["title"])
|
|
98
|
+
detail = non_empty_str(body["detail"])
|
|
99
|
+
body_msg = non_empty_str(body["message"])
|
|
100
|
+
type_uri = non_empty_str(body["type"])
|
|
101
|
+
field = non_empty_str(body["field"])
|
|
102
|
+
request_id = non_empty_str(body["request_id"]) || hdr_req_id
|
|
103
|
+
message = detail || body_msg || title || "JamsrPay API error (#{status})"
|
|
104
|
+
|
|
105
|
+
return new(
|
|
106
|
+
message: message,
|
|
107
|
+
status: status,
|
|
108
|
+
code: code,
|
|
109
|
+
title: title,
|
|
110
|
+
detail: detail,
|
|
111
|
+
type: type_uri,
|
|
112
|
+
field: field,
|
|
113
|
+
request_id: request_id,
|
|
114
|
+
retryable: retryable_status?(status),
|
|
115
|
+
retry_after_seconds: retry_after,
|
|
116
|
+
raw_body: body
|
|
117
|
+
)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
text = non_empty_str(body)
|
|
121
|
+
new(
|
|
122
|
+
message: text || "JamsrPay API error (#{http_status})",
|
|
123
|
+
status: http_status,
|
|
124
|
+
code: "HTTP_#{http_status}",
|
|
125
|
+
request_id: hdr_req_id,
|
|
126
|
+
retryable: retryable_status?(http_status),
|
|
127
|
+
retry_after_seconds: retry_after,
|
|
128
|
+
raw_body: body
|
|
129
|
+
)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def self.header_ci(headers, target)
|
|
133
|
+
return nil unless headers.is_a?(Hash)
|
|
134
|
+
|
|
135
|
+
lower = target.downcase
|
|
136
|
+
headers.each do |k, v|
|
|
137
|
+
next unless k.to_s.downcase == lower
|
|
138
|
+
return v.is_a?(Array) ? v.first : v
|
|
139
|
+
end
|
|
140
|
+
nil
|
|
141
|
+
end
|
|
142
|
+
private_class_method :header_ci
|
|
143
|
+
|
|
144
|
+
def self.non_empty_str(val)
|
|
145
|
+
return nil unless val.is_a?(String)
|
|
146
|
+
val.strip.empty? ? nil : val
|
|
147
|
+
end
|
|
148
|
+
private_class_method :non_empty_str
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module JamsrPay
|
|
9
|
+
class HttpClient
|
|
10
|
+
MAX_IDEMPOTENCY_KEY_LEN = 256
|
|
11
|
+
MAX_RETRY_DELAY_SECONDS = 30.0
|
|
12
|
+
BASE_RETRY_DELAY_SECONDS = 0.25
|
|
13
|
+
|
|
14
|
+
HttpResponse = Struct.new(:status, :headers, :body, keyword_init: true)
|
|
15
|
+
|
|
16
|
+
def initialize(config, transport: nil, sleep_fn: nil)
|
|
17
|
+
@config = config
|
|
18
|
+
@transport = transport
|
|
19
|
+
@sleep_fn = sleep_fn || Kernel.method(:sleep)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.require_path_param(value, name)
|
|
23
|
+
trimmed = value.to_s.strip
|
|
24
|
+
raise ConfigError, "#{name} is required and cannot be empty" if trimmed.empty?
|
|
25
|
+
|
|
26
|
+
URI.encode_www_form_component(trimmed).gsub("+", "%20")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def request_json(
|
|
30
|
+
method:,
|
|
31
|
+
path:,
|
|
32
|
+
body: nil,
|
|
33
|
+
requires_auth: false,
|
|
34
|
+
supports_idempotency: false,
|
|
35
|
+
options: {}
|
|
36
|
+
)
|
|
37
|
+
opts = options || {}
|
|
38
|
+
headers, url, serialized, idem_key, timeout_s = prepare_request(
|
|
39
|
+
method: method,
|
|
40
|
+
path: path,
|
|
41
|
+
body: body,
|
|
42
|
+
accept: "application/json",
|
|
43
|
+
requires_auth: requires_auth,
|
|
44
|
+
supports_idempotency: supports_idempotency,
|
|
45
|
+
options: opts
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
can_retry = method == "GET" || !idem_key.nil?
|
|
49
|
+
max_attempts = can_retry ? (1 + @config[:max_retries]) : 1
|
|
50
|
+
|
|
51
|
+
max_attempts.times do |attempt|
|
|
52
|
+
begin
|
|
53
|
+
resp = send_once(method: method, url: url, headers: headers, body: serialized, timeout_s: timeout_s)
|
|
54
|
+
rescue NetworkError
|
|
55
|
+
if can_retry && attempt + 1 < max_attempts
|
|
56
|
+
@sleep_fn.call(retry_delay(attempt, nil))
|
|
57
|
+
next
|
|
58
|
+
end
|
|
59
|
+
raise
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
if resp.status >= 200 && resp.status < 300
|
|
63
|
+
return nil if resp.status == 204 || resp.body.to_s.strip.empty?
|
|
64
|
+
|
|
65
|
+
begin
|
|
66
|
+
return JSON.parse(resp.body)
|
|
67
|
+
rescue JSON::ParserError => e
|
|
68
|
+
raise NetworkError, "Failed parsing JSON from #{method} #{path}: #{e.message}"
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
api_err = ApiError.from_response(
|
|
73
|
+
http_status: resp.status,
|
|
74
|
+
headers: resp.headers,
|
|
75
|
+
body: decode_error_body(resp.body)
|
|
76
|
+
)
|
|
77
|
+
if can_retry && attempt + 1 < max_attempts && ApiError.retryable_status?(resp.status)
|
|
78
|
+
@sleep_fn.call(retry_delay(attempt, api_err.retry_after_seconds))
|
|
79
|
+
next
|
|
80
|
+
end
|
|
81
|
+
raise api_err
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def stream_sse(path:, options: {}, &block)
|
|
86
|
+
headers, url, _serialized, _idem, timeout_s = prepare_request(
|
|
87
|
+
method: "GET",
|
|
88
|
+
path: path,
|
|
89
|
+
body: nil,
|
|
90
|
+
accept: "text/event-stream",
|
|
91
|
+
requires_auth: false,
|
|
92
|
+
supports_idempotency: false,
|
|
93
|
+
options: options || {}
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
parser = SseParser.new
|
|
97
|
+
err_chunks = +""
|
|
98
|
+
stopped = false
|
|
99
|
+
|
|
100
|
+
meta = stream_once(url: url, headers: headers, timeout_s: timeout_s) do |chunk|
|
|
101
|
+
err_chunks << chunk
|
|
102
|
+
next if stopped
|
|
103
|
+
|
|
104
|
+
parser.feed(chunk).each do |event|
|
|
105
|
+
result = block.call(event)
|
|
106
|
+
if result == :stop
|
|
107
|
+
stopped = true
|
|
108
|
+
break
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
if meta[:status] < 200 || meta[:status] >= 300
|
|
114
|
+
raise ApiError.from_response(
|
|
115
|
+
http_status: meta[:status],
|
|
116
|
+
headers: meta[:headers],
|
|
117
|
+
body: decode_error_body(err_chunks)
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
return if stopped
|
|
122
|
+
|
|
123
|
+
parser.flush.each do |event|
|
|
124
|
+
break if block.call(event) == :stop
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
def prepare_request(method:, path:, body:, accept:, requires_auth:, supports_idempotency:, options:)
|
|
131
|
+
if requires_auth && @config[:api_key].nil?
|
|
132
|
+
raise ConfigError, "An API key is required for #{method} #{path}"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
timeout_s = options.key?(:timeout_seconds) && !options[:timeout_seconds].nil? ? Float(options[:timeout_seconds]) : @config[:timeout_seconds]
|
|
136
|
+
raise ConfigError, "timeout_seconds must be positive" unless timeout_s.finite? && timeout_s.positive?
|
|
137
|
+
|
|
138
|
+
idem_key = nil
|
|
139
|
+
if options.key?(:idempotency_key) && !options[:idempotency_key].nil?
|
|
140
|
+
explicit = options[:idempotency_key].to_s
|
|
141
|
+
if explicit.empty? || explicit.bytesize > MAX_IDEMPOTENCY_KEY_LEN
|
|
142
|
+
raise ConfigError, "idempotency_key must be between 1 and #{MAX_IDEMPOTENCY_KEY_LEN} characters"
|
|
143
|
+
end
|
|
144
|
+
idem_key = explicit
|
|
145
|
+
elsif supports_idempotency && @config[:auto_idempotency]
|
|
146
|
+
idem_key = SecureRandom.uuid
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
headers = @config[:default_headers].dup
|
|
150
|
+
headers["Accept"] = accept
|
|
151
|
+
headers["User-Agent"] = @config[:user_agent]
|
|
152
|
+
headers["x-api-key"] = @config[:api_key] unless @config[:api_key].nil?
|
|
153
|
+
headers["Idempotency-Key"] = idem_key unless idem_key.nil?
|
|
154
|
+
if options[:headers].is_a?(Hash)
|
|
155
|
+
options[:headers].each { |k, v| headers[k.to_s] = v.to_s }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
serialized = nil
|
|
159
|
+
unless body.nil?
|
|
160
|
+
headers["Content-Type"] = "application/json"
|
|
161
|
+
serialized = JSON.generate(body)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
normalized_path = path.start_with?("/") ? path : "/#{path}"
|
|
165
|
+
[headers, "#{@config[:base_url]}#{normalized_path}", serialized, idem_key, timeout_s]
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def send_once(method:, url:, headers:, body:, timeout_s:)
|
|
169
|
+
if @transport
|
|
170
|
+
return @transport.send(method: method, url: url, headers: headers, body: body, timeout_seconds: timeout_s)
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
uri = URI.parse(url)
|
|
174
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
175
|
+
http.use_ssl = uri.scheme == "https"
|
|
176
|
+
http.open_timeout = [timeout_s, 10.0].min
|
|
177
|
+
http.read_timeout = timeout_s
|
|
178
|
+
|
|
179
|
+
req_class = method == "POST" ? Net::HTTP::Post : Net::HTTP::Get
|
|
180
|
+
req = req_class.new(uri.request_uri, headers)
|
|
181
|
+
req.body = body unless body.nil?
|
|
182
|
+
|
|
183
|
+
resp = http.request(req)
|
|
184
|
+
resp_headers = {}
|
|
185
|
+
resp.each_header { |k, v| resp_headers[k] = v }
|
|
186
|
+
HttpResponse.new(status: resp.code.to_i, headers: resp_headers, body: resp.body.to_s)
|
|
187
|
+
rescue Net::OpenTimeout, Net::ReadTimeout, Timeout::Error => e
|
|
188
|
+
raise NetworkError.new("Request timed out (#{method} #{url}): #{e.message}", is_timeout: true)
|
|
189
|
+
rescue StandardError => e
|
|
190
|
+
raise e if e.is_a?(Error)
|
|
191
|
+
|
|
192
|
+
raise NetworkError.new("Network request failed (#{method} #{url}): #{e.message}", is_timeout: false)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def stream_once(url:, headers:, timeout_s:, &on_chunk)
|
|
196
|
+
if @transport
|
|
197
|
+
return @transport.stream(url: url, headers: headers, timeout_seconds: timeout_s, &on_chunk)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
uri = URI.parse(url)
|
|
201
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
202
|
+
http.use_ssl = uri.scheme == "https"
|
|
203
|
+
http.open_timeout = [timeout_s, 10.0].min
|
|
204
|
+
http.read_timeout = timeout_s
|
|
205
|
+
|
|
206
|
+
req = Net::HTTP::Get.new(uri.request_uri, headers)
|
|
207
|
+
status = 0
|
|
208
|
+
resp_headers = {}
|
|
209
|
+
|
|
210
|
+
http.request(req) do |resp|
|
|
211
|
+
status = resp.code.to_i
|
|
212
|
+
resp.each_header { |k, v| resp_headers[k] = v }
|
|
213
|
+
resp.read_body { |chunk| on_chunk.call(chunk) }
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
{ status: status, headers: resp_headers }
|
|
217
|
+
rescue Net::OpenTimeout, Net::ReadTimeout, Timeout::Error => e
|
|
218
|
+
raise NetworkError.new("SSE timed out (#{url}): #{e.message}", is_timeout: true)
|
|
219
|
+
rescue StandardError => e
|
|
220
|
+
raise e if e.is_a?(Error)
|
|
221
|
+
|
|
222
|
+
raise NetworkError.new("SSE request failed (#{url}): #{e.message}", is_timeout: false)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def decode_error_body(raw)
|
|
226
|
+
return nil if raw.nil? || raw.to_s.strip.empty?
|
|
227
|
+
|
|
228
|
+
JSON.parse(raw)
|
|
229
|
+
rescue JSON::ParserError
|
|
230
|
+
raw
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def retry_delay(attempt, retry_after_seconds)
|
|
234
|
+
if !retry_after_seconds.nil? && retry_after_seconds >= 0
|
|
235
|
+
return [retry_after_seconds.to_f, MAX_RETRY_DELAY_SECONDS].min
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
exp = [BASE_RETRY_DELAY_SECONDS * (2**attempt), MAX_RETRY_DELAY_SECONDS].min
|
|
239
|
+
[exp + rand * 0.1, MAX_RETRY_DELAY_SECONDS].min
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module Resources
|
|
5
|
+
class Assets
|
|
6
|
+
def initialize(http)
|
|
7
|
+
@http = http
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def list(options: {})
|
|
11
|
+
resp = @http.request_json(
|
|
12
|
+
method: "GET",
|
|
13
|
+
path: "/v1/assets",
|
|
14
|
+
requires_auth: false,
|
|
15
|
+
options: options
|
|
16
|
+
)
|
|
17
|
+
resp.is_a?(Hash) && resp["assets"].is_a?(Array) ? resp["assets"] : []
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get(asset_id, options: {})
|
|
21
|
+
id = HttpClient.require_path_param(asset_id, "asset_id")
|
|
22
|
+
@http.request_json(
|
|
23
|
+
method: "GET",
|
|
24
|
+
path: "/v1/assets/#{id}",
|
|
25
|
+
requires_auth: false,
|
|
26
|
+
options: options
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module Resources
|
|
5
|
+
class Invoices
|
|
6
|
+
def initialize(http)
|
|
7
|
+
@http = http
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def create(
|
|
11
|
+
pricing_amount:,
|
|
12
|
+
pricing_currency:,
|
|
13
|
+
asset_id:,
|
|
14
|
+
network_id:,
|
|
15
|
+
redirect_url: nil,
|
|
16
|
+
external_id: nil,
|
|
17
|
+
options: {}
|
|
18
|
+
)
|
|
19
|
+
payload = {
|
|
20
|
+
pricing_amount: pricing_amount,
|
|
21
|
+
pricing_currency: pricing_currency,
|
|
22
|
+
asset_id: asset_id,
|
|
23
|
+
network_id: network_id
|
|
24
|
+
}
|
|
25
|
+
payload[:redirect_url] = redirect_url unless redirect_url.nil?
|
|
26
|
+
payload[:external_id] = external_id unless external_id.nil?
|
|
27
|
+
|
|
28
|
+
@http.request_json(
|
|
29
|
+
method: "POST",
|
|
30
|
+
path: "/v1/invoices",
|
|
31
|
+
body: payload,
|
|
32
|
+
requires_auth: true,
|
|
33
|
+
supports_idempotency: true,
|
|
34
|
+
options: options
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def create_for_payment(payment_intent_id, asset_id:, network_id:, options: {})
|
|
39
|
+
id = HttpClient.require_path_param(payment_intent_id, "payment_intent_id")
|
|
40
|
+
@http.request_json(
|
|
41
|
+
method: "POST",
|
|
42
|
+
path: "/v1/payment-intents/#{id}/invoices",
|
|
43
|
+
body: { asset_id: asset_id, network_id: network_id },
|
|
44
|
+
requires_auth: false,
|
|
45
|
+
supports_idempotency: true,
|
|
46
|
+
options: options
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def get(invoice_id, options: {})
|
|
51
|
+
id = HttpClient.require_path_param(invoice_id, "invoice_id")
|
|
52
|
+
@http.request_json(
|
|
53
|
+
method: "GET",
|
|
54
|
+
path: "/v1/invoices/#{id}",
|
|
55
|
+
requires_auth: false,
|
|
56
|
+
options: options
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def stream_events(invoice_id, options: {}, &block)
|
|
61
|
+
id = HttpClient.require_path_param(invoice_id, "invoice_id")
|
|
62
|
+
if block
|
|
63
|
+
@http.stream_sse(path: "/v1/invoices/#{id}/events", options: options, &block)
|
|
64
|
+
return nil
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
collected = []
|
|
68
|
+
@http.stream_sse(path: "/v1/invoices/#{id}/events", options: options) { |ev| collected << ev }
|
|
69
|
+
collected
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module Resources
|
|
5
|
+
class PaymentIntents
|
|
6
|
+
def initialize(http)
|
|
7
|
+
@http = http
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def create(
|
|
11
|
+
pricing_amount:,
|
|
12
|
+
pricing_currency:,
|
|
13
|
+
external_id: nil,
|
|
14
|
+
redirect_url: nil,
|
|
15
|
+
payment_title: nil,
|
|
16
|
+
description: nil,
|
|
17
|
+
options: {}
|
|
18
|
+
)
|
|
19
|
+
payload = {
|
|
20
|
+
pricing_amount: pricing_amount,
|
|
21
|
+
pricing_currency: pricing_currency
|
|
22
|
+
}
|
|
23
|
+
payload[:external_id] = external_id unless external_id.nil?
|
|
24
|
+
payload[:redirect_url] = redirect_url unless redirect_url.nil?
|
|
25
|
+
payload[:payment_title] = payment_title unless payment_title.nil?
|
|
26
|
+
payload[:description] = description unless description.nil?
|
|
27
|
+
|
|
28
|
+
@http.request_json(
|
|
29
|
+
method: "POST",
|
|
30
|
+
path: "/v1/payment-intents",
|
|
31
|
+
body: payload,
|
|
32
|
+
requires_auth: true,
|
|
33
|
+
supports_idempotency: true,
|
|
34
|
+
options: options
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def get(payment_intent_id, options: {})
|
|
39
|
+
id = HttpClient.require_path_param(payment_intent_id, "payment_intent_id")
|
|
40
|
+
@http.request_json(
|
|
41
|
+
method: "GET",
|
|
42
|
+
path: "/v1/payment-intents/#{id}",
|
|
43
|
+
requires_auth: false,
|
|
44
|
+
options: options
|
|
45
|
+
)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module Resources
|
|
5
|
+
class StoreAssets
|
|
6
|
+
def initialize(http)
|
|
7
|
+
@http = http
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def list(store_id, options: {})
|
|
11
|
+
id = HttpClient.require_path_param(store_id, "store_id")
|
|
12
|
+
resp = @http.request_json(
|
|
13
|
+
method: "GET",
|
|
14
|
+
path: "/v1/store-assets/#{id}",
|
|
15
|
+
requires_auth: false,
|
|
16
|
+
options: options
|
|
17
|
+
)
|
|
18
|
+
resp.is_a?(Hash) && resp["store_assets"].is_a?(Array) ? resp["store_assets"] : []
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/lib/jamsrpay/sse.rb
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module JamsrPay
|
|
6
|
+
class SseParser
|
|
7
|
+
def initialize
|
|
8
|
+
@buffer = +""
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def feed(chunk)
|
|
12
|
+
@buffer << chunk.to_s.dup.force_encoding(Encoding::UTF_8)
|
|
13
|
+
events = []
|
|
14
|
+
|
|
15
|
+
while (boundary = find_boundary(@buffer))
|
|
16
|
+
idx, sep_len = boundary
|
|
17
|
+
raw_block = @buffer.byteslice(0, idx)
|
|
18
|
+
@buffer = @buffer.byteslice(idx + sep_len, @buffer.bytesize - idx - sep_len) || +""
|
|
19
|
+
decoded = parse_block(raw_block)
|
|
20
|
+
events << decoded unless decoded.nil?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
events
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def flush
|
|
27
|
+
trailing = @buffer.strip
|
|
28
|
+
@buffer = +""
|
|
29
|
+
return [] if trailing.empty?
|
|
30
|
+
|
|
31
|
+
decoded = parse_block(trailing)
|
|
32
|
+
decoded.nil? ? [] : [decoded]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def find_boundary(buf)
|
|
38
|
+
crlf = buf.index("\r\n\r\n")
|
|
39
|
+
lf = buf.index("\n\n")
|
|
40
|
+
return nil if crlf.nil? && lf.nil?
|
|
41
|
+
return [crlf, 4] if !crlf.nil? && (lf.nil? || crlf < lf)
|
|
42
|
+
|
|
43
|
+
[lf, 2]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def parse_block(block)
|
|
47
|
+
normalized = block.gsub("\r\n", "\n").gsub("\r", "\n")
|
|
48
|
+
event_name = +""
|
|
49
|
+
data_lines = []
|
|
50
|
+
|
|
51
|
+
normalized.split("\n", -1).each do |line|
|
|
52
|
+
next if line.empty? || line.start_with?(":")
|
|
53
|
+
|
|
54
|
+
colon = line.index(":")
|
|
55
|
+
if colon.nil?
|
|
56
|
+
field = line
|
|
57
|
+
value = ""
|
|
58
|
+
else
|
|
59
|
+
field = line.slice(0, colon)
|
|
60
|
+
value = line.slice(colon + 1, line.length - colon - 1) || ""
|
|
61
|
+
value = value.slice(1, value.length - 1) if value.start_with?(" ")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
if field == "event"
|
|
65
|
+
event_name = value
|
|
66
|
+
elsif field == "data"
|
|
67
|
+
data_lines << value
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
return nil if data_lines.empty?
|
|
72
|
+
|
|
73
|
+
name = event_name.empty? ? "message" : event_name
|
|
74
|
+
return nil unless name == "invoice.update" || name == "message"
|
|
75
|
+
|
|
76
|
+
payload = data_lines.join("\n")
|
|
77
|
+
parsed = JSON.parse(payload)
|
|
78
|
+
raise NetworkError, "Expected JSON object in SSE event frame" unless parsed.is_a?(Hash)
|
|
79
|
+
|
|
80
|
+
parsed
|
|
81
|
+
rescue JSON::ParserError => e
|
|
82
|
+
raise NetworkError, "Invalid JSON in SSE event frame: #{e.message}"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JamsrPay
|
|
4
|
+
module AssetId
|
|
5
|
+
TRX = "TRX"
|
|
6
|
+
USDT = "USDT"
|
|
7
|
+
BNB = "BNB"
|
|
8
|
+
USDC = "USDC"
|
|
9
|
+
DAI = "DAI"
|
|
10
|
+
EURC = "EURC"
|
|
11
|
+
ETH = "ETH"
|
|
12
|
+
POL = "POL"
|
|
13
|
+
BTC = "BTC"
|
|
14
|
+
LTC = "LTC"
|
|
15
|
+
SOL = "SOL"
|
|
16
|
+
ALL = [TRX, USDT, BNB, USDC, DAI, EURC, ETH, POL, BTC, LTC, SOL].freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
module FiatCurrencyId
|
|
20
|
+
USD = "USD"
|
|
21
|
+
EUR = "EUR"
|
|
22
|
+
GBP = "GBP"
|
|
23
|
+
INR = "INR"
|
|
24
|
+
CAD = "CAD"
|
|
25
|
+
CHF = "CHF"
|
|
26
|
+
HKD = "HKD"
|
|
27
|
+
ILS = "ILS"
|
|
28
|
+
JPY = "JPY"
|
|
29
|
+
PHP = "PHP"
|
|
30
|
+
ALL = [USD, EUR, GBP, INR, CAD, CHF, HKD, ILS, JPY, PHP].freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module NetworkId
|
|
34
|
+
TRON_MAINNET = "TRON_MAINNET"
|
|
35
|
+
TRON_NILE = "TRON_NILE"
|
|
36
|
+
BSC_MAINNET = "BSC_MAINNET"
|
|
37
|
+
BSC_TESTNET = "BSC_TESTNET"
|
|
38
|
+
ETH_MAINNET = "ETH_MAINNET"
|
|
39
|
+
ETH_SEPOLIA = "ETH_SEPOLIA"
|
|
40
|
+
POL_MAINNET = "POL_MAINNET"
|
|
41
|
+
POL_AMOY = "POL_AMOY"
|
|
42
|
+
BTC_MAINNET = "BTC_MAINNET"
|
|
43
|
+
BTC_TESTNET = "BTC_TESTNET"
|
|
44
|
+
LTC_MAINNET = "LTC_MAINNET"
|
|
45
|
+
LTC_TESTNET = "LTC_TESTNET"
|
|
46
|
+
SOL_MAINNET = "SOL_MAINNET"
|
|
47
|
+
SOL_DEVNET = "SOL_DEVNET"
|
|
48
|
+
ALL = [
|
|
49
|
+
TRON_MAINNET, TRON_NILE, BSC_MAINNET, BSC_TESTNET, ETH_MAINNET, ETH_SEPOLIA,
|
|
50
|
+
POL_MAINNET, POL_AMOY, BTC_MAINNET, BTC_TESTNET, LTC_MAINNET, LTC_TESTNET,
|
|
51
|
+
SOL_MAINNET, SOL_DEVNET
|
|
52
|
+
].freeze
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
module InvoiceStatus
|
|
56
|
+
AWAITING_PAYMENT = "AWAITING_PAYMENT"
|
|
57
|
+
PAID = "PAID"
|
|
58
|
+
CONFIRMING = "CONFIRMING"
|
|
59
|
+
EXPIRED = "EXPIRED"
|
|
60
|
+
ALL = [AWAITING_PAYMENT, PAID, CONFIRMING, EXPIRED].freeze
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
module InvoiceAdditionalStatus
|
|
64
|
+
OVERPAID = "OVERPAID"
|
|
65
|
+
PARTIALLY_PAID = "PARTIALLY_PAID"
|
|
66
|
+
MARKED_PAID = "MARKED_PAID"
|
|
67
|
+
ALL = [OVERPAID, PARTIALLY_PAID, MARKED_PAID].freeze
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
module PaymentIntentStatus
|
|
71
|
+
AWAITING_PAYMENT = "AWAITING_PAYMENT"
|
|
72
|
+
SUCCEEDED = "SUCCEEDED"
|
|
73
|
+
EXPIRED = "EXPIRED"
|
|
74
|
+
ALL = [AWAITING_PAYMENT, SUCCEEDED, EXPIRED].freeze
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
module AssetType
|
|
78
|
+
NATIVE = "NATIVE"
|
|
79
|
+
TOKEN = "TOKEN"
|
|
80
|
+
ALL = [NATIVE, TOKEN].freeze
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
module AssetStatus
|
|
84
|
+
ACTIVE = "ACTIVE"
|
|
85
|
+
DISABLED = "DISABLED"
|
|
86
|
+
DEPRECATED = "DEPRECATED"
|
|
87
|
+
ALL = [ACTIVE, DISABLED, DEPRECATED].freeze
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "openssl"
|
|
5
|
+
|
|
6
|
+
module JamsrPay
|
|
7
|
+
module Webhooks
|
|
8
|
+
DEFAULT_TOLERANCE_SECONDS = 300
|
|
9
|
+
VALID_EVENT_TYPES = %w[
|
|
10
|
+
invoice.created
|
|
11
|
+
invoice.payment_detected
|
|
12
|
+
invoice.confirmation_started
|
|
13
|
+
invoice.paid
|
|
14
|
+
invoice.expired
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def extract_signature_header(headers_or_signature)
|
|
20
|
+
return headers_or_signature.strip if headers_or_signature.is_a?(String)
|
|
21
|
+
return "" unless headers_or_signature.is_a?(Hash)
|
|
22
|
+
|
|
23
|
+
headers_or_signature.each do |k, v|
|
|
24
|
+
next unless k.to_s.downcase == "webhook-signature"
|
|
25
|
+
|
|
26
|
+
return v.strip if v.is_a?(String)
|
|
27
|
+
return v.first.strip if v.is_a?(Array) && v.first.is_a?(String)
|
|
28
|
+
end
|
|
29
|
+
""
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def parse_signature_header(header_value)
|
|
33
|
+
trimmed = header_value.to_s.strip
|
|
34
|
+
raise WebhookError.new("Missing Webhook-Signature header.", code: WebhookError::SIGNATURE_MISSING) if trimmed.empty?
|
|
35
|
+
|
|
36
|
+
timestamp = nil
|
|
37
|
+
signatures = []
|
|
38
|
+
|
|
39
|
+
trimmed.split(",").each do |part|
|
|
40
|
+
eq = part.index("=")
|
|
41
|
+
next if eq.nil? || eq <= 0
|
|
42
|
+
|
|
43
|
+
key = part.slice(0, eq).strip
|
|
44
|
+
val = part.slice(eq + 1, part.length - eq - 1).to_s.strip
|
|
45
|
+
if key == "t" && val.match?(/\A\d+\z/)
|
|
46
|
+
timestamp = val.to_i
|
|
47
|
+
elsif key == "v1" && val.match?(/\A[0-9a-fA-F]{64}\z/)
|
|
48
|
+
signatures << val.downcase
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if timestamp.nil? || signatures.empty?
|
|
53
|
+
raise WebhookError.new(
|
|
54
|
+
"Malformed Webhook-Signature header. Expected `t={unix_timestamp},v1={sha256_hex}`.",
|
|
55
|
+
code: WebhookError::SIGNATURE_INVALID_FORMAT
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
{ timestamp: timestamp, signatures: signatures }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def sign_payload(raw_body:, secret:, timestamp: nil)
|
|
63
|
+
raise ConfigError, "Webhook secret cannot be empty" if secret.to_s.empty?
|
|
64
|
+
|
|
65
|
+
ts = timestamp.nil? ? Time.now.to_i : Integer(timestamp)
|
|
66
|
+
payload = "#{ts}.".b + raw_body.to_s.b
|
|
67
|
+
digest = OpenSSL::HMAC.hexdigest("SHA256", secret.b, payload)
|
|
68
|
+
"t=#{ts},v1=#{digest}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def secure_compare(a, b)
|
|
72
|
+
return false unless a.bytesize == b.bytesize
|
|
73
|
+
if OpenSSL.respond_to?(:fixed_length_secure_compare)
|
|
74
|
+
return OpenSSL.fixed_length_secure_compare(a, b)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
l = a.unpack("C*")
|
|
78
|
+
res = 0
|
|
79
|
+
b.each_byte { |byte| res |= byte ^ l.shift }
|
|
80
|
+
res.zero?
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def verify_signature(
|
|
84
|
+
raw_body:,
|
|
85
|
+
headers_or_signature:,
|
|
86
|
+
secret:,
|
|
87
|
+
tolerance_seconds: DEFAULT_TOLERANCE_SECONDS,
|
|
88
|
+
now_timestamp: nil
|
|
89
|
+
)
|
|
90
|
+
raise ConfigError, "Webhook secret cannot be empty" if secret.to_s.empty?
|
|
91
|
+
|
|
92
|
+
tol = Integer(tolerance_seconds)
|
|
93
|
+
raise ConfigError, "tolerance_seconds cannot be negative" if tol.negative?
|
|
94
|
+
|
|
95
|
+
header_val = extract_signature_header(headers_or_signature)
|
|
96
|
+
parsed = parse_signature_header(header_val)
|
|
97
|
+
|
|
98
|
+
if tol.positive?
|
|
99
|
+
now_ts = now_timestamp.nil? ? Time.now.to_i : Integer(now_timestamp)
|
|
100
|
+
if (now_ts - parsed[:timestamp]).abs > tol
|
|
101
|
+
raise WebhookError.new(
|
|
102
|
+
"Webhook timestamp #{parsed[:timestamp]} exceeds #{tol}s tolerance window (now=#{now_ts}).",
|
|
103
|
+
code: WebhookError::TIMESTAMP_EXPIRED
|
|
104
|
+
)
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
payload = "#{parsed[:timestamp]}.".b + raw_body.to_s.b
|
|
109
|
+
expected = OpenSSL::HMAC.hexdigest("SHA256", secret.b, payload)
|
|
110
|
+
|
|
111
|
+
parsed[:signatures].each do |candidate|
|
|
112
|
+
return parsed if secure_compare(expected, candidate)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
raise WebhookError.new(
|
|
116
|
+
"Webhook signature verification failed for all provided v1 signatures.",
|
|
117
|
+
code: WebhookError::SIGNATURE_MISMATCH
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def construct_event(
|
|
122
|
+
raw_body:,
|
|
123
|
+
headers_or_signature:,
|
|
124
|
+
secret:,
|
|
125
|
+
tolerance_seconds: DEFAULT_TOLERANCE_SECONDS,
|
|
126
|
+
now_timestamp: nil
|
|
127
|
+
)
|
|
128
|
+
verify_signature(
|
|
129
|
+
raw_body: raw_body,
|
|
130
|
+
headers_or_signature: headers_or_signature,
|
|
131
|
+
secret: secret,
|
|
132
|
+
tolerance_seconds: tolerance_seconds,
|
|
133
|
+
now_timestamp: now_timestamp
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
begin
|
|
137
|
+
parsed = JSON.parse(raw_body.to_s)
|
|
138
|
+
rescue JSON::ParserError => e
|
|
139
|
+
raise WebhookError.new("Webhook body is not valid JSON: #{e.message}", code: WebhookError::INVALID_PAYLOAD)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
unless parsed.is_a?(Hash) && VALID_EVENT_TYPES.include?(parsed["type"]) && parsed["data"].is_a?(Hash)
|
|
143
|
+
raise WebhookError.new(
|
|
144
|
+
"Webhook JSON payload does not match expected `{ type, data }` event contract.",
|
|
145
|
+
code: WebhookError::INVALID_PAYLOAD
|
|
146
|
+
)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
parsed
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
data/lib/jamsrpay.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "jamsrpay/version"
|
|
4
|
+
require_relative "jamsrpay/types"
|
|
5
|
+
require_relative "jamsrpay/errors"
|
|
6
|
+
require_relative "jamsrpay/config"
|
|
7
|
+
require_relative "jamsrpay/sse"
|
|
8
|
+
require_relative "jamsrpay/http_client"
|
|
9
|
+
require_relative "jamsrpay/webhooks"
|
|
10
|
+
require_relative "jamsrpay/resources/invoices"
|
|
11
|
+
require_relative "jamsrpay/resources/payment_intents"
|
|
12
|
+
require_relative "jamsrpay/resources/assets"
|
|
13
|
+
require_relative "jamsrpay/resources/store_assets"
|
|
14
|
+
require_relative "jamsrpay/client"
|
metadata
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jamsrpay
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- JamsrPay
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- dev@jamsrpay.cloud
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- jamsrpay.gemspec
|
|
23
|
+
- lib/jamsrpay.rb
|
|
24
|
+
- lib/jamsrpay/client.rb
|
|
25
|
+
- lib/jamsrpay/config.rb
|
|
26
|
+
- lib/jamsrpay/errors.rb
|
|
27
|
+
- lib/jamsrpay/http_client.rb
|
|
28
|
+
- lib/jamsrpay/resources/assets.rb
|
|
29
|
+
- lib/jamsrpay/resources/invoices.rb
|
|
30
|
+
- lib/jamsrpay/resources/payment_intents.rb
|
|
31
|
+
- lib/jamsrpay/resources/store_assets.rb
|
|
32
|
+
- lib/jamsrpay/sse.rb
|
|
33
|
+
- lib/jamsrpay/types.rb
|
|
34
|
+
- lib/jamsrpay/version.rb
|
|
35
|
+
- lib/jamsrpay/webhooks.rb
|
|
36
|
+
homepage: https://jamsrpay.cloud
|
|
37
|
+
licenses:
|
|
38
|
+
- MIT
|
|
39
|
+
metadata:
|
|
40
|
+
homepage_uri: https://jamsrpay.cloud
|
|
41
|
+
source_code_uri: https://github.com/JamsrPay/sdks/tree/main/packages/ruby
|
|
42
|
+
documentation_uri: https://docs.jamsrpay.cloud
|
|
43
|
+
bug_tracker_uri: https://github.com/JamsrPay/sdks/issues
|
|
44
|
+
rubygems_mfa_required: 'true'
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
require_paths:
|
|
48
|
+
- lib
|
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 2.6.0
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 3.5.22
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: Official zero-dependency Ruby SDK for the JamsrPay cryptocurrency payment
|
|
64
|
+
gateway.
|
|
65
|
+
test_files: []
|