nowpayments-ruby 1.0.0 → 1.0.5
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 +4 -4
- data/LICENSE +21 -21
- data/README.md +514 -514
- data/lib/nowpayments/client.rb +295 -271
- data/lib/nowpayments/constants.rb +27 -27
- data/lib/nowpayments/error.rb +21 -21
- data/lib/nowpayments/helpers.rb +44 -44
- data/lib/nowpayments/http.rb +94 -94
- data/lib/nowpayments/ipn.rb +72 -63
- data/lib/nowpayments/version.rb +5 -5
- data/lib/nowpayments.rb +20 -22
- metadata +13 -10
data/README.md
CHANGED
|
@@ -1,514 +1,514 @@
|
|
|
1
|
-
# nowpayments-ruby
|
|
2
|
-
|
|
3
|
-
<p align="center">
|
|
4
|
-
<img src="https://img.shields.io/gem/v/nowpayments?color=6366f1&style=for-the-badge&logo=ruby" alt="gem version" />
|
|
5
|
-
<img src="https://img.shields.io/badge/license-MIT-22c55e?style=for-the-badge" alt="license" />
|
|
6
|
-
<img src="https://img.shields.io/badge/Ruby-2.7+-cc342d?style=for-the-badge&logo=ruby" alt="Ruby" />
|
|
7
|
-
</p>
|
|
8
|
-
|
|
9
|
-
<h1 align="center">nowpayments-ruby</h1>
|
|
10
|
-
<p align="center">
|
|
11
|
-
<strong>Full-featured Ruby SDK for NOWPayments</strong><br/>
|
|
12
|
-
Accept 300+ cryptocurrencies with auto-conversion to your wallet
|
|
13
|
-
</p>
|
|
14
|
-
|
|
15
|
-
<p align="center">
|
|
16
|
-
<a href="#-quick-start">Quick Start</a> •
|
|
17
|
-
<a href="#-features">Features</a> •
|
|
18
|
-
<a href="#-examples">Examples</a> •
|
|
19
|
-
<a href="#-api-reference">API</a> •
|
|
20
|
-
<a href="#-links">Links</a>
|
|
21
|
-
</p>
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
## ✨ Features
|
|
26
|
-
|
|
27
|
-
| Feature | Support |
|
|
28
|
-
|---------|---------|
|
|
29
|
-
| **Payments** | Create, status, list, update estimate |
|
|
30
|
-
| **Invoices** | Create invoice + redirect flow |
|
|
31
|
-
| **Payouts** | Mass payout, verify 2FA, cancel scheduled |
|
|
32
|
-
| **Fiat Payouts** | Currencies, payment methods, list |
|
|
33
|
-
| **Subscriptions** | Plans, recurring payments, cancel |
|
|
34
|
-
| **Custody** | Sub-partners, transfers, deposit, write-off |
|
|
35
|
-
| **Conversions** | In-custody currency conversion |
|
|
36
|
-
| **IPN Webhooks** | HMAC signature verification |
|
|
37
|
-
| **Helpers** | `payment_complete?`, `status_label`, etc. |
|
|
38
|
-
|
|
39
|
-
---
|
|
40
|
-
|
|
41
|
-
## 🚀 Quick Start
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
gem install nowpayments-ruby
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Or add to your Gemfile:
|
|
48
|
-
|
|
49
|
-
```ruby
|
|
50
|
-
gem "nowpayments-ruby"
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
```ruby
|
|
54
|
-
require "nowpayments"
|
|
55
|
-
|
|
56
|
-
np = NowPayments::Client.new(
|
|
57
|
-
api_key: ENV["NOWPAYMENTS_API_KEY"],
|
|
58
|
-
sandbox: true # false for production
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
# Create payment
|
|
62
|
-
payment = np.create_payment(
|
|
63
|
-
price_amount: 29.99,
|
|
64
|
-
price_currency: "usd",
|
|
65
|
-
pay_currency: "btc",
|
|
66
|
-
order_id: "order-123"
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
puts "Pay #{payment["pay_amount"]} #{(payment["pay_currency"] || "").to_s.upcase} → #{payment["pay_address"]}"
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
---
|
|
73
|
-
|
|
74
|
-
## ⚙️ Config
|
|
75
|
-
|
|
76
|
-
| Option | Required | Default | Description |
|
|
77
|
-
|--------|----------|---------|-------------|
|
|
78
|
-
| `api_key` | Yes | — | From [Dashboard](https://account.nowpayments.io) |
|
|
79
|
-
| `sandbox` | No | `false` | Use sandbox API |
|
|
80
|
-
| `timeout` | No | `30000` | Request timeout (ms) |
|
|
81
|
-
| `ipn_secret` | No | — | For webhook verification |
|
|
82
|
-
| `base_url` | No | — | Override API URL |
|
|
83
|
-
|
|
84
|
-
---
|
|
85
|
-
|
|
86
|
-
## 📁 Examples
|
|
87
|
-
|
|
88
|
-
All examples use the same setup. Set `NOWPAYMENTS_API_KEY` before running:
|
|
89
|
-
|
|
90
|
-
```bash
|
|
91
|
-
export NOWPAYMENTS_API_KEY=your_api_key
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
| File | Description | Run |
|
|
95
|
-
|------|-------------|-----|
|
|
96
|
-
| `01_create_payment.rb` | Create payment, show address to customer | `ruby examples/01_create_payment.rb` |
|
|
97
|
-
| `02_check_status.rb` | Check payment status + helper labels | `ruby examples/02_check_status.rb PAYMENT_ID` |
|
|
98
|
-
| `03_list_payments.rb` | List payments with filters | `ruby examples/03_list_payments.rb` |
|
|
99
|
-
| `04_create_invoice.rb` | Create invoice (redirect flow) | `ruby examples/04_create_invoice.rb` |
|
|
100
|
-
| `05_estimate_and_min_amount.rb` | Price estimate + minimum amount | `ruby examples/05_estimate_and_min_amount.rb` |
|
|
101
|
-
| `06_get_currencies.rb` | Get available currencies | `ruby examples/06_get_currencies.rb` |
|
|
102
|
-
| `07_payout_flow.rb` | Payout: validate → create → verify | `ruby examples/07_payout_flow.rb` |
|
|
103
|
-
| `08_subscription.rb` | Subscription plans + create subscription | `ruby examples/08_subscription.rb` |
|
|
104
|
-
| `09_ipn_webhook.rb` | IPN signature verification demo | `ruby examples/09_ipn_webhook.rb` |
|
|
105
|
-
| `10_custody_and_balance.rb` | Balance, sub-partners, deposit payment | `ruby examples/10_custody_and_balance.rb` |
|
|
106
|
-
| `11_conversions.rb` | Create conversion (custody) | `ruby examples/11_conversions.rb` |
|
|
107
|
-
|
|
108
|
-
### Example: Create Payment
|
|
109
|
-
|
|
110
|
-
```ruby
|
|
111
|
-
# examples/01_create_payment.rb
|
|
112
|
-
require "nowpayments"
|
|
113
|
-
|
|
114
|
-
np = NowPayments::Client.new(api_key: ENV["NOWPAYMENTS_API_KEY"], sandbox: true)
|
|
115
|
-
|
|
116
|
-
payment = np.create_payment(
|
|
117
|
-
price_amount: 29.99,
|
|
118
|
-
price_currency: "usd",
|
|
119
|
-
pay_currency: "btc",
|
|
120
|
-
order_id: "order-#{Time.now.to_i}",
|
|
121
|
-
order_description: "Premium Plan",
|
|
122
|
-
ipn_callback_url: "https://yoursite.com/webhook"
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
puts "Payment created: #{payment["payment_id"]}"
|
|
126
|
-
puts "Pay #{payment["pay_amount"]} #{payment["pay_currency"].to_s.upcase} to:"
|
|
127
|
-
puts payment["pay_address"]
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### Example: Check Payment Status
|
|
131
|
-
|
|
132
|
-
```ruby
|
|
133
|
-
# examples/02_check_status.rb
|
|
134
|
-
payment_id = ARGV[0] || "PASTE_PAYMENT_ID_HERE"
|
|
135
|
-
payment = np.get_payment_status(payment_id)
|
|
136
|
-
|
|
137
|
-
puts "Status: #{NowPayments::Helpers.status_label(payment["payment_status"])}"
|
|
138
|
-
puts "Amount: #{payment["pay_amount"]} #{payment["pay_currency"]}"
|
|
139
|
-
|
|
140
|
-
if NowPayments::Helpers.payment_complete?(payment["payment_status"])
|
|
141
|
-
puts "✅ Payment done! Fulfill the order."
|
|
142
|
-
else
|
|
143
|
-
puts "⏳ Waiting for payment..."
|
|
144
|
-
end
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Example: List Payments
|
|
148
|
-
|
|
149
|
-
```ruby
|
|
150
|
-
# examples/03_list_payments.rb
|
|
151
|
-
result = np.get_payments(
|
|
152
|
-
limit: 5,
|
|
153
|
-
page: 0,
|
|
154
|
-
sortBy: "created_at",
|
|
155
|
-
orderBy: "desc",
|
|
156
|
-
dateFrom: "2024-01-01",
|
|
157
|
-
dateTo: "2024-12-31"
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
puts "Total: #{result["total"]}"
|
|
161
|
-
puts "Page: #{result["page"] + 1} of #{result["pagesCount"]}"
|
|
162
|
-
result["data"].each_with_index do |p, i|
|
|
163
|
-
puts "#{i + 1}. #{p["payment_id"]} | #{p["payment_status"]} | #{p["price_amount"]} #{p["price_currency"]}"
|
|
164
|
-
end
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### Example: Create Invoice
|
|
168
|
-
|
|
169
|
-
```ruby
|
|
170
|
-
# examples/04_create_invoice.rb
|
|
171
|
-
invoice = np.create_invoice(
|
|
172
|
-
price_amount: 49.99,
|
|
173
|
-
price_currency: "usd",
|
|
174
|
-
pay_currency: "btc",
|
|
175
|
-
order_id: "inv-#{Time.now.to_i}",
|
|
176
|
-
order_description: "Premium subscription",
|
|
177
|
-
success_url: "https://yoursite.com/success",
|
|
178
|
-
cancel_url: "https://yoursite.com/cancel"
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
puts "Redirect customer to: #{invoice["invoice_url"]}"
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### Example: Estimate & Min Amount
|
|
185
|
-
|
|
186
|
-
```ruby
|
|
187
|
-
# examples/05_estimate_and_min_amount.rb
|
|
188
|
-
estimate = np.get_estimate_price(
|
|
189
|
-
amount: 100,
|
|
190
|
-
currency_from: "usd",
|
|
191
|
-
currency_to: "btc"
|
|
192
|
-
)
|
|
193
|
-
puts "100 USD ≈ #{estimate["estimated_amount"]} BTC"
|
|
194
|
-
|
|
195
|
-
min = np.get_min_amount(
|
|
196
|
-
currency_from: "usd",
|
|
197
|
-
currency_to: "btc",
|
|
198
|
-
fiat_equivalent: "usd"
|
|
199
|
-
)
|
|
200
|
-
puts "Min amount: #{min["min_amount"]} BTC (≈ #{min["fiat_equivalent"]} USD)"
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
### Example: Get Currencies
|
|
204
|
-
|
|
205
|
-
```ruby
|
|
206
|
-
# examples/06_get_currencies.rb
|
|
207
|
-
currencies = np.get_currencies["currencies"]
|
|
208
|
-
puts "Supported: #{currencies.first(15).join(", ")}..."
|
|
209
|
-
puts "Total: #{currencies.length}"
|
|
210
|
-
|
|
211
|
-
btc_info = np.get_currency("btc")
|
|
212
|
-
puts "BTC info: #{btc_info}"
|
|
213
|
-
|
|
214
|
-
full = np.get_full_currencies["currencies"]
|
|
215
|
-
btc_full = full.find { |c| c["code"]&.downcase == "btc" }
|
|
216
|
-
puts "BTC full: #{btc_full}"
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
### Example: Payout Flow (JWT required)
|
|
220
|
-
|
|
221
|
-
```ruby
|
|
222
|
-
# examples/07_payout_flow.rb
|
|
223
|
-
# Env: NOWPAYMENTS_API_KEY, EMAIL, PASSWORD, PAYOUT_ADDRESS, VERIFICATION_CODE?
|
|
224
|
-
|
|
225
|
-
# 1. Validate address
|
|
226
|
-
np.validate_payout_address(address: payout_address, currency: "btc")
|
|
227
|
-
|
|
228
|
-
# 2. Get JWT
|
|
229
|
-
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
230
|
-
token = auth["token"]
|
|
231
|
-
|
|
232
|
-
# 3. Create payout
|
|
233
|
-
payout = np.create_payout(
|
|
234
|
-
{
|
|
235
|
-
withdrawals: [{ address: payout_address, currency: "btc", amount: 0.0001 }],
|
|
236
|
-
ipn_callback_url: "https://yoursite.com/payout-webhook"
|
|
237
|
-
},
|
|
238
|
-
token
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
# 4. Verify (2FA code from email)
|
|
242
|
-
np.verify_payout(payout["id"], ENV["VERIFICATION_CODE"], token)
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
### Example: Subscriptions
|
|
246
|
-
|
|
247
|
-
```ruby
|
|
248
|
-
# examples/08_subscription.rb
|
|
249
|
-
plans = np.get_subscription_plans
|
|
250
|
-
puts "Plans: #{plans["result"]&.length || 0}"
|
|
251
|
-
|
|
252
|
-
plan = plans["result"]&.first
|
|
253
|
-
if plan
|
|
254
|
-
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
255
|
-
sub = np.create_subscription(
|
|
256
|
-
{ subscription_plan_id: plan["id"], email: "customer@example.com" },
|
|
257
|
-
auth["token"]
|
|
258
|
-
)
|
|
259
|
-
puts "Subscription: #{sub["result"]}"
|
|
260
|
-
end
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
### Example: IPN Webhook
|
|
264
|
-
|
|
265
|
-
```ruby
|
|
266
|
-
# examples/09_ipn_webhook.rb
|
|
267
|
-
# In Sinatra/Rails: verify before processing
|
|
268
|
-
|
|
269
|
-
np = NowPayments::Client.new(api_key: "...", ipn_secret: ENV["IPN_SECRET"])
|
|
270
|
-
|
|
271
|
-
# In your webhook handler:
|
|
272
|
-
# if np.verify_ipn(request.body.read, request.env["HTTP_X_NOWPAYMENTS_SIG"])
|
|
273
|
-
# payload = JSON.parse(request.body.read)
|
|
274
|
-
# if NowPayments::Helpers.payment_complete?(payload["payment_status"])
|
|
275
|
-
# # Fulfill order
|
|
276
|
-
# end
|
|
277
|
-
# end
|
|
278
|
-
|
|
279
|
-
# Standalone verification
|
|
280
|
-
valid = NowPayments::IPN.verify_signature(payload, signature, ipn_secret)
|
|
281
|
-
NowPayments::IPN.create_signature(payload, ipn_secret) # For testing
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
### Example: Custody & Balance
|
|
285
|
-
|
|
286
|
-
```ruby
|
|
287
|
-
# examples/10_custody_and_balance.rb
|
|
288
|
-
balance = np.get_balance
|
|
289
|
-
puts "Balance: #{balance}"
|
|
290
|
-
|
|
291
|
-
partners = np.get_sub_partners
|
|
292
|
-
puts "Sub-partners: #{partners}"
|
|
293
|
-
|
|
294
|
-
# Deposit with payment (JWT required)
|
|
295
|
-
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
296
|
-
result = np.create_sub_partner_payment(
|
|
297
|
-
{ currency: "trx", amount: 50, sub_partner_id: ENV["SUB_PARTNER_ID"] },
|
|
298
|
-
auth["token"]
|
|
299
|
-
)
|
|
300
|
-
puts "Deposit: #{result["result"]["pay_address"]} #{result["result"]["pay_amount"]} TRX"
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
### Example: Conversions (JWT required)
|
|
304
|
-
|
|
305
|
-
```ruby
|
|
306
|
-
# examples/11_conversions.rb
|
|
307
|
-
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
308
|
-
token = auth["token"]
|
|
309
|
-
|
|
310
|
-
conv = np.create_conversion(
|
|
311
|
-
{ amount: 0.001, from_currency: "btc", to_currency: "usd" },
|
|
312
|
-
token
|
|
313
|
-
)
|
|
314
|
-
|
|
315
|
-
if conv["deposit_id"]
|
|
316
|
-
status = np.get_conversion_status(conv["deposit_id"], token)
|
|
317
|
-
puts "Status: #{status}"
|
|
318
|
-
end
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
---
|
|
322
|
-
|
|
323
|
-
## 📖 API Reference
|
|
324
|
-
|
|
325
|
-
### Auth & Status
|
|
326
|
-
|
|
327
|
-
```ruby
|
|
328
|
-
np.get_status
|
|
329
|
-
auth = np.get_auth_token("your@email.com", "password")
|
|
330
|
-
token = auth["token"]
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
### Currencies
|
|
334
|
-
|
|
335
|
-
```ruby
|
|
336
|
-
np.get_currencies
|
|
337
|
-
np.get_currencies(true) # fixed rate
|
|
338
|
-
np.get_full_currencies
|
|
339
|
-
np.get_merchant_coins
|
|
340
|
-
np.get_currency("btc")
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
### Payments
|
|
344
|
-
|
|
345
|
-
```ruby
|
|
346
|
-
payment = np.create_payment(
|
|
347
|
-
price_amount: 29.99,
|
|
348
|
-
price_currency: "usd",
|
|
349
|
-
pay_currency: "btc",
|
|
350
|
-
order_id: "order-123",
|
|
351
|
-
ipn_callback_url: "https://yoursite.com/webhook",
|
|
352
|
-
is_fixed_rate: true
|
|
353
|
-
)
|
|
354
|
-
|
|
355
|
-
np.get_payment_status(payment_id)
|
|
356
|
-
np.get_payments(limit: 10, page: 0, sortBy: "created_at", orderBy: "desc")
|
|
357
|
-
np.update_payment_estimate(payment_id)
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
### Estimate & Min Amount
|
|
361
|
-
|
|
362
|
-
```ruby
|
|
363
|
-
np.get_estimate_price(amount: 100, currency_from: "usd", currency_to: "btc")
|
|
364
|
-
np.get_min_amount(currency_from: "usd", currency_to: "btc", fiat_equivalent: "usd")
|
|
365
|
-
```
|
|
366
|
-
|
|
367
|
-
### Invoices
|
|
368
|
-
|
|
369
|
-
```ruby
|
|
370
|
-
invoice = np.create_invoice(
|
|
371
|
-
price_amount: 49.99,
|
|
372
|
-
price_currency: "usd",
|
|
373
|
-
order_id: "inv-001",
|
|
374
|
-
success_url: "https://yoursite.com/success",
|
|
375
|
-
cancel_url: "https://yoursite.com/cancel"
|
|
376
|
-
)
|
|
377
|
-
# invoice["invoice_url"] → redirect customer
|
|
378
|
-
|
|
379
|
-
np.create_invoice_payment(iid: invoice_id, pay_currency: "btc")
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
### Payouts (JWT required)
|
|
383
|
-
|
|
384
|
-
```ruby
|
|
385
|
-
np.validate_payout_address(address: "0x...", currency: "eth")
|
|
386
|
-
|
|
387
|
-
batch = np.create_payout(
|
|
388
|
-
{
|
|
389
|
-
ipn_callback_url: "https://yoursite.com/payout-webhook",
|
|
390
|
-
withdrawals: [
|
|
391
|
-
{ address: "TEmGw...", currency: "trx", amount: 200 },
|
|
392
|
-
{ address: "0x1EB...", currency: "eth", amount: 0.1 }
|
|
393
|
-
]
|
|
394
|
-
},
|
|
395
|
-
token
|
|
396
|
-
)
|
|
397
|
-
|
|
398
|
-
np.verify_payout(batch["id"], "123456", token)
|
|
399
|
-
np.cancel_payout(payout_id, token)
|
|
400
|
-
np.get_payout_status(payout_id, token)
|
|
401
|
-
np.get_payouts(batch_id: "...", limit: 10, page: 0)
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
### Fiat Payouts
|
|
405
|
-
|
|
406
|
-
```ruby
|
|
407
|
-
np.get_fiat_payouts_crypto_currencies({ provider: "transfi" }, token)
|
|
408
|
-
np.get_fiat_payouts_payment_methods({ provider: "transfi", currency: "usd" }, token)
|
|
409
|
-
np.get_fiat_payouts({ status: "FINISHED", limit: 10 }, token)
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
### Balance
|
|
413
|
-
|
|
414
|
-
```ruby
|
|
415
|
-
np.get_balance(token)
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
### Subscriptions
|
|
419
|
-
|
|
420
|
-
```ruby
|
|
421
|
-
np.get_subscription_plans(limit: 10, offset: 0)
|
|
422
|
-
np.get_subscription_plan(id)
|
|
423
|
-
np.update_subscription_plan(id, amount: 9.99, interval_day: "30")
|
|
424
|
-
np.create_subscription({ subscription_plan_id: 76215585, email: "user@example.com" }, token)
|
|
425
|
-
np.get_subscriptions(status: "PAID", limit: 10)
|
|
426
|
-
np.get_subscription(id)
|
|
427
|
-
np.delete_subscription(id, token)
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
### Custody / Sub-partners (JWT required)
|
|
431
|
-
|
|
432
|
-
```ruby
|
|
433
|
-
np.create_sub_partner("user-123", token)
|
|
434
|
-
np.create_sub_partner_payment({ currency: "trx", amount: 50, sub_partner_id: "1631380403" }, token)
|
|
435
|
-
np.get_sub_partners({ offset: 0, limit: 10 }, token)
|
|
436
|
-
np.get_sub_partner_balance(sub_partner_id)
|
|
437
|
-
np.create_transfer({ currency: "trx", amount: 0.3, from_id: 111, to_id: 222 }, token)
|
|
438
|
-
np.deposit({ currency: "trx", amount: 0.5, sub_partner_id: "111" }, token)
|
|
439
|
-
np.write_off({ currency: "trx", amount: 0.3, sub_partner_id: "111" }, token)
|
|
440
|
-
np.get_transfers({ status: "FINISHED", limit: 10 }, token)
|
|
441
|
-
np.get_transfer(id, token)
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
### Conversions (JWT required)
|
|
445
|
-
|
|
446
|
-
```ruby
|
|
447
|
-
np.create_conversion({ amount: 50, from_currency: "usdttrc20", to_currency: "usdterc20" }, token)
|
|
448
|
-
np.get_conversion_status(conversion_id, token)
|
|
449
|
-
np.get_conversions({ status: "FINISHED", limit: 10 }, token)
|
|
450
|
-
```
|
|
451
|
-
|
|
452
|
-
### IPN / Webhooks
|
|
453
|
-
|
|
454
|
-
```ruby
|
|
455
|
-
np = NowPayments::Client.new(api_key: "...", ipn_secret: "SECRET")
|
|
456
|
-
|
|
457
|
-
# In webhook handler (Sinatra/Rails)
|
|
458
|
-
if np.verify_ipn(request.body.read, request.env["HTTP_X_NOWPAYMENTS_SIG"])
|
|
459
|
-
payload = JSON.parse(request.body.read)
|
|
460
|
-
# Process payment
|
|
461
|
-
end
|
|
462
|
-
|
|
463
|
-
# Standalone
|
|
464
|
-
NowPayments.verify_ipn_signature(payload, signature, ipn_secret)
|
|
465
|
-
NowPayments.create_ipn_signature(payload, ipn_secret) # For testing
|
|
466
|
-
NowPayments::IPN.verify_signature(payload, signature, ipn_secret)
|
|
467
|
-
NowPayments::IPN.create_signature(payload, ipn_secret)
|
|
468
|
-
```
|
|
469
|
-
|
|
470
|
-
### Helper functions
|
|
471
|
-
|
|
472
|
-
```ruby
|
|
473
|
-
NowPayments::Helpers.payment_complete?(payment["payment_status"])
|
|
474
|
-
NowPayments::Helpers.payment_pending?(payment["payment_status"])
|
|
475
|
-
NowPayments::Helpers.status_label(payment["payment_status"])
|
|
476
|
-
NowPayments::Helpers.payment_summary(payment)
|
|
477
|
-
|
|
478
|
-
# Constants
|
|
479
|
-
NowPayments::PAYMENT_STATUSES
|
|
480
|
-
NowPayments::PAYMENT_DONE_STATUSES
|
|
481
|
-
NowPayments::PAYMENT_PENDING_STATUSES
|
|
482
|
-
NowPayments::PAYMENT_STATUS_LABELS
|
|
483
|
-
```
|
|
484
|
-
|
|
485
|
-
### Error handling
|
|
486
|
-
|
|
487
|
-
```ruby
|
|
488
|
-
begin
|
|
489
|
-
np.create_payment(...)
|
|
490
|
-
rescue NowPayments::NowPaymentsError => e
|
|
491
|
-
puts e.message
|
|
492
|
-
puts e.status_code
|
|
493
|
-
puts e.code
|
|
494
|
-
puts e.response
|
|
495
|
-
puts e.to_s
|
|
496
|
-
end
|
|
497
|
-
```
|
|
498
|
-
|
|
499
|
-
---
|
|
500
|
-
|
|
501
|
-
## 🔗 Links
|
|
502
|
-
|
|
503
|
-
| Link | URL |
|
|
504
|
-
|------|-----|
|
|
505
|
-
| **API Docs** | [Postman](https://documenter.getpostman.com/view/7907941/2s93JusNJt) |
|
|
506
|
-
| **Sandbox** | [Postman Sandbox](https://documenter.getpostman.com/view/7907941/T1LSCRHC) |
|
|
507
|
-
| **Help** | [nowpayments.io/help](https://nowpayments.io/help/payments/api) |
|
|
508
|
-
| **Dashboard** | [account.nowpayments.io](https://account.nowpayments.io) |
|
|
509
|
-
|
|
510
|
-
---
|
|
511
|
-
|
|
512
|
-
## 📄 License
|
|
513
|
-
|
|
514
|
-
MIT © [Foisalislambd](https://github.com/Foisalislambd)
|
|
1
|
+
# nowpayments-ruby
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://img.shields.io/gem/v/nowpayments?color=6366f1&style=for-the-badge&logo=ruby" alt="gem version" />
|
|
5
|
+
<img src="https://img.shields.io/badge/license-MIT-22c55e?style=for-the-badge" alt="license" />
|
|
6
|
+
<img src="https://img.shields.io/badge/Ruby-2.7+-cc342d?style=for-the-badge&logo=ruby" alt="Ruby" />
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<h1 align="center">nowpayments-ruby</h1>
|
|
10
|
+
<p align="center">
|
|
11
|
+
<strong>Full-featured Ruby SDK for NOWPayments</strong><br/>
|
|
12
|
+
Accept 300+ cryptocurrencies with auto-conversion to your wallet
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<a href="#-quick-start">Quick Start</a> •
|
|
17
|
+
<a href="#-features">Features</a> •
|
|
18
|
+
<a href="#-examples">Examples</a> •
|
|
19
|
+
<a href="#-api-reference">API</a> •
|
|
20
|
+
<a href="#-links">Links</a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## ✨ Features
|
|
26
|
+
|
|
27
|
+
| Feature | Support |
|
|
28
|
+
|---------|---------|
|
|
29
|
+
| **Payments** | Create, status, list, update estimate |
|
|
30
|
+
| **Invoices** | Create invoice + redirect flow |
|
|
31
|
+
| **Payouts** | Mass payout, verify 2FA, cancel scheduled |
|
|
32
|
+
| **Fiat Payouts** | Currencies, payment methods, list |
|
|
33
|
+
| **Subscriptions** | Plans, recurring payments, cancel |
|
|
34
|
+
| **Custody** | Sub-partners, transfers, deposit, write-off |
|
|
35
|
+
| **Conversions** | In-custody currency conversion |
|
|
36
|
+
| **IPN Webhooks** | HMAC signature verification |
|
|
37
|
+
| **Helpers** | `payment_complete?`, `status_label`, etc. |
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
## 🚀 Quick Start
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
gem install nowpayments-ruby
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or add to your Gemfile:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
gem "nowpayments-ruby"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
require "nowpayments"
|
|
55
|
+
|
|
56
|
+
np = NowPayments::Client.new(
|
|
57
|
+
api_key: ENV["NOWPAYMENTS_API_KEY"],
|
|
58
|
+
sandbox: true # false for production
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Create payment
|
|
62
|
+
payment = np.create_payment(
|
|
63
|
+
price_amount: 29.99,
|
|
64
|
+
price_currency: "usd",
|
|
65
|
+
pay_currency: "btc",
|
|
66
|
+
order_id: "order-123"
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
puts "Pay #{payment["pay_amount"]} #{(payment["pay_currency"] || "").to_s.upcase} → #{payment["pay_address"]}"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
## ⚙️ Config
|
|
75
|
+
|
|
76
|
+
| Option | Required | Default | Description |
|
|
77
|
+
|--------|----------|---------|-------------|
|
|
78
|
+
| `api_key` | Yes | — | From [Dashboard](https://account.nowpayments.io) |
|
|
79
|
+
| `sandbox` | No | `false` | Use sandbox API |
|
|
80
|
+
| `timeout` | No | `30000` | Request timeout (ms) |
|
|
81
|
+
| `ipn_secret` | No | — | For webhook verification |
|
|
82
|
+
| `base_url` | No | — | Override API URL |
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## 📁 Examples
|
|
87
|
+
|
|
88
|
+
All examples use the same setup. Set `NOWPAYMENTS_API_KEY` before running:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
export NOWPAYMENTS_API_KEY=your_api_key
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| File | Description | Run |
|
|
95
|
+
|------|-------------|-----|
|
|
96
|
+
| `01_create_payment.rb` | Create payment, show address to customer | `ruby examples/01_create_payment.rb` |
|
|
97
|
+
| `02_check_status.rb` | Check payment status + helper labels | `ruby examples/02_check_status.rb PAYMENT_ID` |
|
|
98
|
+
| `03_list_payments.rb` | List payments with filters | `ruby examples/03_list_payments.rb` |
|
|
99
|
+
| `04_create_invoice.rb` | Create invoice (redirect flow) | `ruby examples/04_create_invoice.rb` |
|
|
100
|
+
| `05_estimate_and_min_amount.rb` | Price estimate + minimum amount | `ruby examples/05_estimate_and_min_amount.rb` |
|
|
101
|
+
| `06_get_currencies.rb` | Get available currencies | `ruby examples/06_get_currencies.rb` |
|
|
102
|
+
| `07_payout_flow.rb` | Payout: validate → create → verify | `ruby examples/07_payout_flow.rb` |
|
|
103
|
+
| `08_subscription.rb` | Subscription plans + create subscription | `ruby examples/08_subscription.rb` |
|
|
104
|
+
| `09_ipn_webhook.rb` | IPN signature verification demo | `ruby examples/09_ipn_webhook.rb` |
|
|
105
|
+
| `10_custody_and_balance.rb` | Balance, sub-partners, deposit payment | `ruby examples/10_custody_and_balance.rb` |
|
|
106
|
+
| `11_conversions.rb` | Create conversion (custody) | `ruby examples/11_conversions.rb` |
|
|
107
|
+
|
|
108
|
+
### Example: Create Payment
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
# examples/01_create_payment.rb
|
|
112
|
+
require "nowpayments"
|
|
113
|
+
|
|
114
|
+
np = NowPayments::Client.new(api_key: ENV["NOWPAYMENTS_API_KEY"], sandbox: true)
|
|
115
|
+
|
|
116
|
+
payment = np.create_payment(
|
|
117
|
+
price_amount: 29.99,
|
|
118
|
+
price_currency: "usd",
|
|
119
|
+
pay_currency: "btc",
|
|
120
|
+
order_id: "order-#{Time.now.to_i}",
|
|
121
|
+
order_description: "Premium Plan",
|
|
122
|
+
ipn_callback_url: "https://yoursite.com/webhook"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
puts "Payment created: #{payment["payment_id"]}"
|
|
126
|
+
puts "Pay #{payment["pay_amount"]} #{payment["pay_currency"].to_s.upcase} to:"
|
|
127
|
+
puts payment["pay_address"]
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Example: Check Payment Status
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
# examples/02_check_status.rb
|
|
134
|
+
payment_id = ARGV[0] || "PASTE_PAYMENT_ID_HERE"
|
|
135
|
+
payment = np.get_payment_status(payment_id)
|
|
136
|
+
|
|
137
|
+
puts "Status: #{NowPayments::Helpers.status_label(payment["payment_status"])}"
|
|
138
|
+
puts "Amount: #{payment["pay_amount"]} #{payment["pay_currency"]}"
|
|
139
|
+
|
|
140
|
+
if NowPayments::Helpers.payment_complete?(payment["payment_status"])
|
|
141
|
+
puts "✅ Payment done! Fulfill the order."
|
|
142
|
+
else
|
|
143
|
+
puts "⏳ Waiting for payment..."
|
|
144
|
+
end
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Example: List Payments
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
# examples/03_list_payments.rb
|
|
151
|
+
result = np.get_payments(
|
|
152
|
+
limit: 5,
|
|
153
|
+
page: 0,
|
|
154
|
+
sortBy: "created_at",
|
|
155
|
+
orderBy: "desc",
|
|
156
|
+
dateFrom: "2024-01-01",
|
|
157
|
+
dateTo: "2024-12-31"
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
puts "Total: #{result["total"]}"
|
|
161
|
+
puts "Page: #{result["page"] + 1} of #{result["pagesCount"]}"
|
|
162
|
+
result["data"].each_with_index do |p, i|
|
|
163
|
+
puts "#{i + 1}. #{p["payment_id"]} | #{p["payment_status"]} | #{p["price_amount"]} #{p["price_currency"]}"
|
|
164
|
+
end
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Example: Create Invoice
|
|
168
|
+
|
|
169
|
+
```ruby
|
|
170
|
+
# examples/04_create_invoice.rb
|
|
171
|
+
invoice = np.create_invoice(
|
|
172
|
+
price_amount: 49.99,
|
|
173
|
+
price_currency: "usd",
|
|
174
|
+
pay_currency: "btc",
|
|
175
|
+
order_id: "inv-#{Time.now.to_i}",
|
|
176
|
+
order_description: "Premium subscription",
|
|
177
|
+
success_url: "https://yoursite.com/success",
|
|
178
|
+
cancel_url: "https://yoursite.com/cancel"
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
puts "Redirect customer to: #{invoice["invoice_url"]}"
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Example: Estimate & Min Amount
|
|
185
|
+
|
|
186
|
+
```ruby
|
|
187
|
+
# examples/05_estimate_and_min_amount.rb
|
|
188
|
+
estimate = np.get_estimate_price(
|
|
189
|
+
amount: 100,
|
|
190
|
+
currency_from: "usd",
|
|
191
|
+
currency_to: "btc"
|
|
192
|
+
)
|
|
193
|
+
puts "100 USD ≈ #{estimate["estimated_amount"]} BTC"
|
|
194
|
+
|
|
195
|
+
min = np.get_min_amount(
|
|
196
|
+
currency_from: "usd",
|
|
197
|
+
currency_to: "btc",
|
|
198
|
+
fiat_equivalent: "usd"
|
|
199
|
+
)
|
|
200
|
+
puts "Min amount: #{min["min_amount"]} BTC (≈ #{min["fiat_equivalent"]} USD)"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Example: Get Currencies
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
# examples/06_get_currencies.rb
|
|
207
|
+
currencies = np.get_currencies["currencies"]
|
|
208
|
+
puts "Supported: #{currencies.first(15).join(", ")}..."
|
|
209
|
+
puts "Total: #{currencies.length}"
|
|
210
|
+
|
|
211
|
+
btc_info = np.get_currency("btc")
|
|
212
|
+
puts "BTC info: #{btc_info}"
|
|
213
|
+
|
|
214
|
+
full = np.get_full_currencies["currencies"]
|
|
215
|
+
btc_full = full.find { |c| c["code"]&.downcase == "btc" }
|
|
216
|
+
puts "BTC full: #{btc_full}"
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Example: Payout Flow (JWT required)
|
|
220
|
+
|
|
221
|
+
```ruby
|
|
222
|
+
# examples/07_payout_flow.rb
|
|
223
|
+
# Env: NOWPAYMENTS_API_KEY, EMAIL, PASSWORD, PAYOUT_ADDRESS, VERIFICATION_CODE?
|
|
224
|
+
|
|
225
|
+
# 1. Validate address
|
|
226
|
+
np.validate_payout_address(address: payout_address, currency: "btc")
|
|
227
|
+
|
|
228
|
+
# 2. Get JWT
|
|
229
|
+
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
230
|
+
token = auth["token"]
|
|
231
|
+
|
|
232
|
+
# 3. Create payout
|
|
233
|
+
payout = np.create_payout(
|
|
234
|
+
{
|
|
235
|
+
withdrawals: [{ address: payout_address, currency: "btc", amount: 0.0001 }],
|
|
236
|
+
ipn_callback_url: "https://yoursite.com/payout-webhook"
|
|
237
|
+
},
|
|
238
|
+
token
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
# 4. Verify (2FA code from email)
|
|
242
|
+
np.verify_payout(payout["id"], ENV["VERIFICATION_CODE"], token)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Example: Subscriptions
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
# examples/08_subscription.rb
|
|
249
|
+
plans = np.get_subscription_plans
|
|
250
|
+
puts "Plans: #{plans["result"]&.length || 0}"
|
|
251
|
+
|
|
252
|
+
plan = plans["result"]&.first
|
|
253
|
+
if plan
|
|
254
|
+
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
255
|
+
sub = np.create_subscription(
|
|
256
|
+
{ subscription_plan_id: plan["id"], email: "customer@example.com" },
|
|
257
|
+
auth["token"]
|
|
258
|
+
)
|
|
259
|
+
puts "Subscription: #{sub["result"]}"
|
|
260
|
+
end
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Example: IPN Webhook
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
# examples/09_ipn_webhook.rb
|
|
267
|
+
# In Sinatra/Rails: verify before processing
|
|
268
|
+
|
|
269
|
+
np = NowPayments::Client.new(api_key: "...", ipn_secret: ENV["IPN_SECRET"])
|
|
270
|
+
|
|
271
|
+
# In your webhook handler:
|
|
272
|
+
# if np.verify_ipn(request.body.read, request.env["HTTP_X_NOWPAYMENTS_SIG"])
|
|
273
|
+
# payload = JSON.parse(request.body.read)
|
|
274
|
+
# if NowPayments::Helpers.payment_complete?(payload["payment_status"])
|
|
275
|
+
# # Fulfill order
|
|
276
|
+
# end
|
|
277
|
+
# end
|
|
278
|
+
|
|
279
|
+
# Standalone verification
|
|
280
|
+
valid = NowPayments::IPN.verify_signature(payload, signature, ipn_secret)
|
|
281
|
+
NowPayments::IPN.create_signature(payload, ipn_secret) # For testing
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Example: Custody & Balance
|
|
285
|
+
|
|
286
|
+
```ruby
|
|
287
|
+
# examples/10_custody_and_balance.rb
|
|
288
|
+
balance = np.get_balance
|
|
289
|
+
puts "Balance: #{balance}"
|
|
290
|
+
|
|
291
|
+
partners = np.get_sub_partners
|
|
292
|
+
puts "Sub-partners: #{partners}"
|
|
293
|
+
|
|
294
|
+
# Deposit with payment (JWT required)
|
|
295
|
+
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
296
|
+
result = np.create_sub_partner_payment(
|
|
297
|
+
{ currency: "trx", amount: 50, sub_partner_id: ENV["SUB_PARTNER_ID"] },
|
|
298
|
+
auth["token"]
|
|
299
|
+
)
|
|
300
|
+
puts "Deposit: #{result["result"]["pay_address"]} #{result["result"]["pay_amount"]} TRX"
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Example: Conversions (JWT required)
|
|
304
|
+
|
|
305
|
+
```ruby
|
|
306
|
+
# examples/11_conversions.rb
|
|
307
|
+
auth = np.get_auth_token(ENV["EMAIL"], ENV["PASSWORD"])
|
|
308
|
+
token = auth["token"]
|
|
309
|
+
|
|
310
|
+
conv = np.create_conversion(
|
|
311
|
+
{ amount: 0.001, from_currency: "btc", to_currency: "usd" },
|
|
312
|
+
token
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
if conv["deposit_id"]
|
|
316
|
+
status = np.get_conversion_status(conv["deposit_id"], token)
|
|
317
|
+
puts "Status: #{status}"
|
|
318
|
+
end
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
## 📖 API Reference
|
|
324
|
+
|
|
325
|
+
### Auth & Status
|
|
326
|
+
|
|
327
|
+
```ruby
|
|
328
|
+
np.get_status
|
|
329
|
+
auth = np.get_auth_token("your@email.com", "password")
|
|
330
|
+
token = auth["token"]
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
### Currencies
|
|
334
|
+
|
|
335
|
+
```ruby
|
|
336
|
+
np.get_currencies
|
|
337
|
+
np.get_currencies(true) # fixed rate
|
|
338
|
+
np.get_full_currencies
|
|
339
|
+
np.get_merchant_coins
|
|
340
|
+
np.get_currency("btc")
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Payments
|
|
344
|
+
|
|
345
|
+
```ruby
|
|
346
|
+
payment = np.create_payment(
|
|
347
|
+
price_amount: 29.99,
|
|
348
|
+
price_currency: "usd",
|
|
349
|
+
pay_currency: "btc",
|
|
350
|
+
order_id: "order-123",
|
|
351
|
+
ipn_callback_url: "https://yoursite.com/webhook",
|
|
352
|
+
is_fixed_rate: true
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
np.get_payment_status(payment_id)
|
|
356
|
+
np.get_payments(limit: 10, page: 0, sortBy: "created_at", orderBy: "desc")
|
|
357
|
+
np.update_payment_estimate(payment_id)
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Estimate & Min Amount
|
|
361
|
+
|
|
362
|
+
```ruby
|
|
363
|
+
np.get_estimate_price(amount: 100, currency_from: "usd", currency_to: "btc")
|
|
364
|
+
np.get_min_amount(currency_from: "usd", currency_to: "btc", fiat_equivalent: "usd")
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### Invoices
|
|
368
|
+
|
|
369
|
+
```ruby
|
|
370
|
+
invoice = np.create_invoice(
|
|
371
|
+
price_amount: 49.99,
|
|
372
|
+
price_currency: "usd",
|
|
373
|
+
order_id: "inv-001",
|
|
374
|
+
success_url: "https://yoursite.com/success",
|
|
375
|
+
cancel_url: "https://yoursite.com/cancel"
|
|
376
|
+
)
|
|
377
|
+
# invoice["invoice_url"] → redirect customer
|
|
378
|
+
|
|
379
|
+
np.create_invoice_payment(iid: invoice_id, pay_currency: "btc")
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Payouts (JWT required)
|
|
383
|
+
|
|
384
|
+
```ruby
|
|
385
|
+
np.validate_payout_address(address: "0x...", currency: "eth")
|
|
386
|
+
|
|
387
|
+
batch = np.create_payout(
|
|
388
|
+
{
|
|
389
|
+
ipn_callback_url: "https://yoursite.com/payout-webhook",
|
|
390
|
+
withdrawals: [
|
|
391
|
+
{ address: "TEmGw...", currency: "trx", amount: 200 },
|
|
392
|
+
{ address: "0x1EB...", currency: "eth", amount: 0.1 }
|
|
393
|
+
]
|
|
394
|
+
},
|
|
395
|
+
token
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
np.verify_payout(batch["id"], "123456", token)
|
|
399
|
+
np.cancel_payout(payout_id, token)
|
|
400
|
+
np.get_payout_status(payout_id, token)
|
|
401
|
+
np.get_payouts(batch_id: "...", limit: 10, page: 0)
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### Fiat Payouts
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
np.get_fiat_payouts_crypto_currencies({ provider: "transfi" }, token)
|
|
408
|
+
np.get_fiat_payouts_payment_methods({ provider: "transfi", currency: "usd" }, token)
|
|
409
|
+
np.get_fiat_payouts({ status: "FINISHED", limit: 10 }, token)
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Balance
|
|
413
|
+
|
|
414
|
+
```ruby
|
|
415
|
+
np.get_balance(token)
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Subscriptions
|
|
419
|
+
|
|
420
|
+
```ruby
|
|
421
|
+
np.get_subscription_plans(limit: 10, offset: 0)
|
|
422
|
+
np.get_subscription_plan(id)
|
|
423
|
+
np.update_subscription_plan(id, amount: 9.99, interval_day: "30")
|
|
424
|
+
np.create_subscription({ subscription_plan_id: 76215585, email: "user@example.com" }, token)
|
|
425
|
+
np.get_subscriptions(status: "PAID", limit: 10)
|
|
426
|
+
np.get_subscription(id)
|
|
427
|
+
np.delete_subscription(id, token)
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### Custody / Sub-partners (JWT required)
|
|
431
|
+
|
|
432
|
+
```ruby
|
|
433
|
+
np.create_sub_partner("user-123", token)
|
|
434
|
+
np.create_sub_partner_payment({ currency: "trx", amount: 50, sub_partner_id: "1631380403" }, token)
|
|
435
|
+
np.get_sub_partners({ offset: 0, limit: 10 }, token)
|
|
436
|
+
np.get_sub_partner_balance(sub_partner_id)
|
|
437
|
+
np.create_transfer({ currency: "trx", amount: 0.3, from_id: 111, to_id: 222 }, token)
|
|
438
|
+
np.deposit({ currency: "trx", amount: 0.5, sub_partner_id: "111" }, token)
|
|
439
|
+
np.write_off({ currency: "trx", amount: 0.3, sub_partner_id: "111" }, token)
|
|
440
|
+
np.get_transfers({ status: "FINISHED", limit: 10 }, token)
|
|
441
|
+
np.get_transfer(id, token)
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
### Conversions (JWT required)
|
|
445
|
+
|
|
446
|
+
```ruby
|
|
447
|
+
np.create_conversion({ amount: 50, from_currency: "usdttrc20", to_currency: "usdterc20" }, token)
|
|
448
|
+
np.get_conversion_status(conversion_id, token)
|
|
449
|
+
np.get_conversions({ status: "FINISHED", limit: 10 }, token)
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### IPN / Webhooks
|
|
453
|
+
|
|
454
|
+
```ruby
|
|
455
|
+
np = NowPayments::Client.new(api_key: "...", ipn_secret: "SECRET")
|
|
456
|
+
|
|
457
|
+
# In webhook handler (Sinatra/Rails)
|
|
458
|
+
if np.verify_ipn(request.body.read, request.env["HTTP_X_NOWPAYMENTS_SIG"])
|
|
459
|
+
payload = JSON.parse(request.body.read)
|
|
460
|
+
# Process payment
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
# Standalone
|
|
464
|
+
NowPayments.verify_ipn_signature(payload, signature, ipn_secret)
|
|
465
|
+
NowPayments.create_ipn_signature(payload, ipn_secret) # For testing
|
|
466
|
+
NowPayments::IPN.verify_signature(payload, signature, ipn_secret)
|
|
467
|
+
NowPayments::IPN.create_signature(payload, ipn_secret)
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### Helper functions
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
NowPayments::Helpers.payment_complete?(payment["payment_status"])
|
|
474
|
+
NowPayments::Helpers.payment_pending?(payment["payment_status"])
|
|
475
|
+
NowPayments::Helpers.status_label(payment["payment_status"])
|
|
476
|
+
NowPayments::Helpers.payment_summary(payment)
|
|
477
|
+
|
|
478
|
+
# Constants
|
|
479
|
+
NowPayments::PAYMENT_STATUSES
|
|
480
|
+
NowPayments::PAYMENT_DONE_STATUSES
|
|
481
|
+
NowPayments::PAYMENT_PENDING_STATUSES
|
|
482
|
+
NowPayments::PAYMENT_STATUS_LABELS
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### Error handling
|
|
486
|
+
|
|
487
|
+
```ruby
|
|
488
|
+
begin
|
|
489
|
+
np.create_payment(...)
|
|
490
|
+
rescue NowPayments::NowPaymentsError => e
|
|
491
|
+
puts e.message
|
|
492
|
+
puts e.status_code
|
|
493
|
+
puts e.code
|
|
494
|
+
puts e.response
|
|
495
|
+
puts e.to_s
|
|
496
|
+
end
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## 🔗 Links
|
|
502
|
+
|
|
503
|
+
| Link | URL |
|
|
504
|
+
|------|-----|
|
|
505
|
+
| **API Docs** | [Postman](https://documenter.getpostman.com/view/7907941/2s93JusNJt) |
|
|
506
|
+
| **Sandbox** | [Postman Sandbox](https://documenter.getpostman.com/view/7907941/T1LSCRHC) |
|
|
507
|
+
| **Help** | [nowpayments.io/help](https://nowpayments.io/help/payments/api) |
|
|
508
|
+
| **Dashboard** | [account.nowpayments.io](https://account.nowpayments.io) |
|
|
509
|
+
|
|
510
|
+
---
|
|
511
|
+
|
|
512
|
+
## 📄 License
|
|
513
|
+
|
|
514
|
+
MIT © [Foisalislambd](https://github.com/Foisalislambd)
|