@profullstack/coinpay 0.1.0 → 0.3.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.
- package/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +781 -143
- package/bin/coinpay.js +38 -14
- package/package.json +29 -12
- package/src/client.d.ts +266 -0
- package/src/client.js +170 -23
- package/src/index.d.ts +37 -0
- package/src/index.js +33 -1
- package/src/payments.d.ts +153 -0
- package/src/payments.js +78 -18
- package/src/webhooks.d.ts +134 -0
package/src/client.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Main client class for interacting with the CoinPay API
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
const DEFAULT_BASE_URL = 'https://
|
|
6
|
+
const DEFAULT_BASE_URL = 'https://coinpayportal.com/api';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* CoinPay API Client
|
|
@@ -17,7 +17,7 @@ export class CoinPayClient {
|
|
|
17
17
|
* Create a new CoinPay client
|
|
18
18
|
* @param {Object} options - Client options
|
|
19
19
|
* @param {string} options.apiKey - Your CoinPay API key
|
|
20
|
-
* @param {string} [options.baseUrl] - API base URL (default: https://
|
|
20
|
+
* @param {string} [options.baseUrl] - API base URL (default: https://coinpayportal.com/api)
|
|
21
21
|
* @param {number} [options.timeout] - Request timeout in ms (default: 30000)
|
|
22
22
|
*/
|
|
23
23
|
constructor({ apiKey, baseUrl = DEFAULT_BASE_URL, timeout = 30000 }) {
|
|
@@ -47,7 +47,7 @@ export class CoinPayClient {
|
|
|
47
47
|
signal: controller.signal,
|
|
48
48
|
headers: {
|
|
49
49
|
'Content-Type': 'application/json',
|
|
50
|
-
'
|
|
50
|
+
'Authorization': `Bearer ${this.#apiKey}`,
|
|
51
51
|
...options.headers,
|
|
52
52
|
},
|
|
53
53
|
});
|
|
@@ -74,48 +74,142 @@ export class CoinPayClient {
|
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* Create a new payment
|
|
77
|
+
*
|
|
78
|
+
* This is the primary method for merchants to create payment requests.
|
|
79
|
+
* When a customer needs to pay, call this method to generate a unique
|
|
80
|
+
* payment address and QR code.
|
|
81
|
+
*
|
|
77
82
|
* @param {Object} params - Payment parameters
|
|
78
|
-
* @param {string} params.businessId - Business ID
|
|
79
|
-
* @param {number} params.amount - Amount in fiat currency
|
|
80
|
-
* @param {string} params.currency - Fiat currency code (USD, EUR, etc.)
|
|
81
|
-
* @param {string} params.
|
|
82
|
-
* @param {string} [params.description] - Payment description
|
|
83
|
-
* @param {
|
|
84
|
-
* @
|
|
85
|
-
*
|
|
83
|
+
* @param {string} params.businessId - Business ID (from your CoinPay dashboard)
|
|
84
|
+
* @param {number} params.amount - Amount in fiat currency (e.g., 100.00)
|
|
85
|
+
* @param {string} [params.currency='USD'] - Fiat currency code (USD, EUR, etc.)
|
|
86
|
+
* @param {string} params.blockchain - Blockchain/cryptocurrency (BTC, ETH, SOL, POL, BCH, USDC_ETH, USDC_POL, USDC_SOL)
|
|
87
|
+
* @param {string} [params.description] - Payment description (shown to customer)
|
|
88
|
+
* @param {Object} [params.metadata] - Custom metadata (e.g., { orderId: 'ORD-123', customerId: 'CUST-456' })
|
|
89
|
+
* @returns {Promise<Object>} Created payment with address and QR code
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Create a $50 payment in Bitcoin
|
|
93
|
+
* const payment = await client.createPayment({
|
|
94
|
+
* businessId: 'your-business-id',
|
|
95
|
+
* amount: 50.00,
|
|
96
|
+
* currency: 'USD',
|
|
97
|
+
* blockchain: 'BTC',
|
|
98
|
+
* description: 'Order #12345',
|
|
99
|
+
* metadata: { orderId: '12345', customerEmail: 'customer@example.com' }
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* // Display payment.payment_address and payment.qr_code to customer
|
|
86
103
|
*/
|
|
87
104
|
async createPayment({
|
|
88
105
|
businessId,
|
|
89
106
|
amount,
|
|
90
|
-
currency,
|
|
91
|
-
|
|
107
|
+
currency = 'USD',
|
|
108
|
+
blockchain,
|
|
92
109
|
description,
|
|
93
110
|
metadata,
|
|
94
|
-
callbackUrl,
|
|
95
111
|
}) {
|
|
112
|
+
// Map to API field names (snake_case)
|
|
96
113
|
return this.request('/payments/create', {
|
|
97
114
|
method: 'POST',
|
|
98
115
|
body: JSON.stringify({
|
|
99
|
-
businessId,
|
|
116
|
+
business_id: businessId,
|
|
100
117
|
amount,
|
|
101
118
|
currency,
|
|
102
|
-
|
|
119
|
+
blockchain: blockchain?.toUpperCase(),
|
|
103
120
|
description,
|
|
104
121
|
metadata,
|
|
105
|
-
callbackUrl,
|
|
106
122
|
}),
|
|
107
123
|
});
|
|
108
124
|
}
|
|
109
125
|
|
|
110
126
|
/**
|
|
111
127
|
* Get payment by ID
|
|
128
|
+
*
|
|
129
|
+
* Use this to check the current status of a payment. You can poll this
|
|
130
|
+
* endpoint to wait for payment completion, or use webhooks for real-time
|
|
131
|
+
* notifications.
|
|
132
|
+
*
|
|
112
133
|
* @param {string} paymentId - Payment ID
|
|
113
|
-
* @returns {Promise<Object>} Payment details
|
|
134
|
+
* @returns {Promise<Object>} Payment details including status
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const result = await client.getPayment('pay_abc123');
|
|
138
|
+
* console.log(result.payment.status); // 'pending', 'confirmed', 'forwarded', etc.
|
|
114
139
|
*/
|
|
115
140
|
async getPayment(paymentId) {
|
|
116
141
|
return this.request(`/payments/${paymentId}`);
|
|
117
142
|
}
|
|
118
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Wait for payment to reach a terminal status
|
|
146
|
+
*
|
|
147
|
+
* Polls the payment status until it reaches a terminal state (confirmed,
|
|
148
|
+
* forwarded, expired, or failed). Useful for simple integrations that
|
|
149
|
+
* don't use webhooks.
|
|
150
|
+
*
|
|
151
|
+
* For production use, webhooks are recommended over polling.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} paymentId - Payment ID
|
|
154
|
+
* @param {Object} [options] - Polling options
|
|
155
|
+
* @param {number} [options.interval=5000] - Polling interval in ms (default: 5 seconds)
|
|
156
|
+
* @param {number} [options.timeout=3600000] - Maximum wait time in ms (default: 1 hour)
|
|
157
|
+
* @param {string[]} [options.targetStatuses] - Statuses to wait for (default: ['confirmed', 'forwarded', 'expired', 'failed'])
|
|
158
|
+
* @param {Function} [options.onStatusChange] - Callback when status changes
|
|
159
|
+
* @returns {Promise<Object>} Final payment details
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* // Simple usage - wait for payment to complete
|
|
163
|
+
* const payment = await client.waitForPayment('pay_abc123');
|
|
164
|
+
* if (payment.payment.status === 'confirmed' || payment.payment.status === 'forwarded') {
|
|
165
|
+
* console.log('Payment successful!');
|
|
166
|
+
* }
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* // With status change callback
|
|
170
|
+
* const payment = await client.waitForPayment('pay_abc123', {
|
|
171
|
+
* interval: 3000,
|
|
172
|
+
* timeout: 600000, // 10 minutes
|
|
173
|
+
* onStatusChange: (status, payment) => {
|
|
174
|
+
* console.log(`Payment status: ${status}`);
|
|
175
|
+
* }
|
|
176
|
+
* });
|
|
177
|
+
*/
|
|
178
|
+
async waitForPayment(paymentId, options = {}) {
|
|
179
|
+
const {
|
|
180
|
+
interval = 5000,
|
|
181
|
+
timeout = 3600000,
|
|
182
|
+
targetStatuses = ['confirmed', 'forwarded', 'expired', 'failed'],
|
|
183
|
+
onStatusChange,
|
|
184
|
+
} = options;
|
|
185
|
+
|
|
186
|
+
const startTime = Date.now();
|
|
187
|
+
let lastStatus = null;
|
|
188
|
+
|
|
189
|
+
while (Date.now() - startTime < timeout) {
|
|
190
|
+
const result = await this.getPayment(paymentId);
|
|
191
|
+
const currentStatus = result.payment?.status;
|
|
192
|
+
|
|
193
|
+
// Notify on status change
|
|
194
|
+
if (currentStatus !== lastStatus) {
|
|
195
|
+
if (onStatusChange && lastStatus !== null) {
|
|
196
|
+
onStatusChange(currentStatus, result.payment);
|
|
197
|
+
}
|
|
198
|
+
lastStatus = currentStatus;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Check if we've reached a target status
|
|
202
|
+
if (targetStatuses.includes(currentStatus)) {
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Wait before next poll
|
|
207
|
+
await new Promise(resolve => setTimeout(resolve, interval));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
throw new Error(`Payment status check timed out after ${timeout}ms`);
|
|
211
|
+
}
|
|
212
|
+
|
|
119
213
|
/**
|
|
120
214
|
* List payments for a business
|
|
121
215
|
* @param {Object} params - Query parameters
|
|
@@ -140,13 +234,66 @@ export class CoinPayClient {
|
|
|
140
234
|
}
|
|
141
235
|
|
|
142
236
|
/**
|
|
143
|
-
* Get payment QR code
|
|
237
|
+
* Get payment QR code URL
|
|
238
|
+
*
|
|
239
|
+
* Returns the URL to the QR code image endpoint. The endpoint returns
|
|
240
|
+
* binary PNG image data that can be used directly in an <img> tag.
|
|
241
|
+
*
|
|
144
242
|
* @param {string} paymentId - Payment ID
|
|
145
|
-
* @
|
|
146
|
-
*
|
|
243
|
+
* @returns {string} URL to the QR code image
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* // Get QR code URL for use in HTML
|
|
247
|
+
* const qrUrl = client.getPaymentQRUrl('pay_abc123');
|
|
248
|
+
* // Use in HTML: <img src={qrUrl} alt="Payment QR Code" />
|
|
147
249
|
*/
|
|
148
|
-
|
|
149
|
-
return this
|
|
250
|
+
getPaymentQRUrl(paymentId) {
|
|
251
|
+
return `${this.#baseUrl}/payments/${paymentId}/qr`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Get payment QR code as binary image data
|
|
256
|
+
*
|
|
257
|
+
* Fetches the QR code image as binary data (ArrayBuffer).
|
|
258
|
+
* Useful for server-side processing or saving to file.
|
|
259
|
+
*
|
|
260
|
+
* @param {string} paymentId - Payment ID
|
|
261
|
+
* @returns {Promise<ArrayBuffer>} QR code image as binary data
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* // Get QR code as binary data
|
|
265
|
+
* const imageData = await client.getPaymentQR('pay_abc123');
|
|
266
|
+
* // Save to file (Node.js)
|
|
267
|
+
* fs.writeFileSync('qr.png', Buffer.from(imageData));
|
|
268
|
+
*/
|
|
269
|
+
async getPaymentQR(paymentId) {
|
|
270
|
+
const url = `${this.#baseUrl}/payments/${paymentId}/qr`;
|
|
271
|
+
const controller = new AbortController();
|
|
272
|
+
const timeoutId = setTimeout(() => controller.abort(), this.#timeout);
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
const response = await fetch(url, {
|
|
276
|
+
signal: controller.signal,
|
|
277
|
+
headers: {
|
|
278
|
+
'Authorization': `Bearer ${this.#apiKey}`,
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
if (!response.ok) {
|
|
283
|
+
const error = new Error(`HTTP ${response.status}`);
|
|
284
|
+
error.status = response.status;
|
|
285
|
+
throw error;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return response.arrayBuffer();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error.name === 'AbortError') {
|
|
291
|
+
throw new Error(`Request timeout after ${this.#timeout}ms`);
|
|
292
|
+
}
|
|
293
|
+
throw error;
|
|
294
|
+
} finally {
|
|
295
|
+
clearTimeout(timeoutId);
|
|
296
|
+
}
|
|
150
297
|
}
|
|
151
298
|
|
|
152
299
|
/**
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @profullstack/coinpay - CoinPay SDK
|
|
3
|
+
* Cryptocurrency payment integration for Node.js
|
|
4
|
+
*
|
|
5
|
+
* @module @profullstack/coinpay
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { CoinPayClient } from './client.js';
|
|
9
|
+
export type { CoinPayClientOptions, PaymentParams, ListPaymentsParams, WaitForPaymentOptions, CreateBusinessParams } from './client.js';
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
createPayment,
|
|
13
|
+
getPayment,
|
|
14
|
+
listPayments,
|
|
15
|
+
Blockchain,
|
|
16
|
+
Cryptocurrency,
|
|
17
|
+
PaymentStatus,
|
|
18
|
+
FiatCurrency,
|
|
19
|
+
} from './payments.js';
|
|
20
|
+
export type { CreatePaymentParams, GetPaymentParams, ListPaymentsFnParams } from './payments.js';
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
verifyWebhookSignature,
|
|
24
|
+
generateWebhookSignature,
|
|
25
|
+
parseWebhookPayload,
|
|
26
|
+
createWebhookHandler,
|
|
27
|
+
WebhookEvent,
|
|
28
|
+
} from './webhooks.js';
|
|
29
|
+
export type {
|
|
30
|
+
VerifyWebhookParams,
|
|
31
|
+
GenerateWebhookParams,
|
|
32
|
+
WebhookHandlerOptions,
|
|
33
|
+
ParsedWebhookEvent,
|
|
34
|
+
} from './webhooks.js';
|
|
35
|
+
|
|
36
|
+
import { CoinPayClient } from './client.js';
|
|
37
|
+
export default CoinPayClient;
|
package/src/index.js
CHANGED
|
@@ -3,10 +3,31 @@
|
|
|
3
3
|
* Cryptocurrency payment integration for Node.js
|
|
4
4
|
*
|
|
5
5
|
* @module @profullstack/coinpay
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Quick start
|
|
9
|
+
* import { CoinPayClient, Blockchain } from '@profullstack/coinpay';
|
|
10
|
+
*
|
|
11
|
+
* const client = new CoinPayClient({ apiKey: 'cp_live_xxxxx' });
|
|
12
|
+
*
|
|
13
|
+
* const payment = await client.createPayment({
|
|
14
|
+
* businessId: 'your-business-id',
|
|
15
|
+
* amount: 100,
|
|
16
|
+
* blockchain: Blockchain.BTC,
|
|
17
|
+
* description: 'Order #12345'
|
|
18
|
+
* });
|
|
6
19
|
*/
|
|
7
20
|
|
|
8
21
|
import { CoinPayClient } from './client.js';
|
|
9
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
createPayment,
|
|
24
|
+
getPayment,
|
|
25
|
+
listPayments,
|
|
26
|
+
Blockchain,
|
|
27
|
+
Cryptocurrency,
|
|
28
|
+
PaymentStatus,
|
|
29
|
+
FiatCurrency,
|
|
30
|
+
} from './payments.js';
|
|
10
31
|
import {
|
|
11
32
|
verifyWebhookSignature,
|
|
12
33
|
generateWebhookSignature,
|
|
@@ -16,10 +37,21 @@ import {
|
|
|
16
37
|
} from './webhooks.js';
|
|
17
38
|
|
|
18
39
|
export {
|
|
40
|
+
// Client
|
|
19
41
|
CoinPayClient,
|
|
42
|
+
|
|
43
|
+
// Payment functions
|
|
20
44
|
createPayment,
|
|
21
45
|
getPayment,
|
|
22
46
|
listPayments,
|
|
47
|
+
|
|
48
|
+
// Constants
|
|
49
|
+
Blockchain,
|
|
50
|
+
Cryptocurrency, // Deprecated, use Blockchain
|
|
51
|
+
PaymentStatus,
|
|
52
|
+
FiatCurrency,
|
|
53
|
+
|
|
54
|
+
// Webhook utilities
|
|
23
55
|
verifyWebhookSignature,
|
|
24
56
|
generateWebhookSignature,
|
|
25
57
|
parseWebhookPayload,
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment utilities for CoinPay SDK
|
|
3
|
+
*
|
|
4
|
+
* Standalone functions for creating and managing payments without
|
|
5
|
+
* manually instantiating a `CoinPayClient`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { CoinPayClient } from './client.js';
|
|
9
|
+
|
|
10
|
+
/** Parameters for the standalone `createPayment` function */
|
|
11
|
+
export interface CreatePaymentParams {
|
|
12
|
+
/** API key — required if `client` is not provided */
|
|
13
|
+
apiKey?: string;
|
|
14
|
+
/** Existing CoinPayClient instance (takes precedence over `apiKey`) */
|
|
15
|
+
client?: CoinPayClient;
|
|
16
|
+
/** Business ID from your CoinPay dashboard */
|
|
17
|
+
businessId: string;
|
|
18
|
+
/** Amount in fiat currency */
|
|
19
|
+
amount: number;
|
|
20
|
+
/** Fiat currency code (default: `'USD'`) */
|
|
21
|
+
currency?: string;
|
|
22
|
+
/** Blockchain code: `BTC`, `ETH`, `SOL`, `POL`, `BCH`, `USDC_ETH`, `USDC_POL`, `USDC_SOL` */
|
|
23
|
+
blockchain: string;
|
|
24
|
+
/** Payment description shown to the customer */
|
|
25
|
+
description?: string;
|
|
26
|
+
/** Custom metadata for your records */
|
|
27
|
+
metadata?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Parameters for the standalone `getPayment` function */
|
|
31
|
+
export interface GetPaymentParams {
|
|
32
|
+
/** API key — required if `client` is not provided */
|
|
33
|
+
apiKey?: string;
|
|
34
|
+
/** Existing CoinPayClient instance */
|
|
35
|
+
client?: CoinPayClient;
|
|
36
|
+
/** Payment ID to look up */
|
|
37
|
+
paymentId: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Parameters for the standalone `listPayments` function */
|
|
41
|
+
export interface ListPaymentsFnParams {
|
|
42
|
+
/** API key — required if `client` is not provided */
|
|
43
|
+
apiKey?: string;
|
|
44
|
+
/** Existing CoinPayClient instance */
|
|
45
|
+
client?: CoinPayClient;
|
|
46
|
+
/** Business ID */
|
|
47
|
+
businessId: string;
|
|
48
|
+
/** Filter by payment status */
|
|
49
|
+
status?: string;
|
|
50
|
+
/** Number of results (default: `20`) */
|
|
51
|
+
limit?: number;
|
|
52
|
+
/** Pagination offset */
|
|
53
|
+
offset?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a payment using an API key or existing client.
|
|
58
|
+
*
|
|
59
|
+
* Convenience wrapper — for multiple operations, prefer creating a
|
|
60
|
+
* `CoinPayClient` instance and reusing it.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { createPayment, Blockchain } from '@profullstack/coinpay';
|
|
65
|
+
*
|
|
66
|
+
* const result = await createPayment({
|
|
67
|
+
* apiKey: 'cp_live_xxxxx',
|
|
68
|
+
* businessId: 'biz_123',
|
|
69
|
+
* amount: 50,
|
|
70
|
+
* blockchain: Blockchain.BTC,
|
|
71
|
+
* });
|
|
72
|
+
* console.log(result.payment.payment_address);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function createPayment(params: CreatePaymentParams): Promise<Record<string, unknown>>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Get a payment by ID.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const result = await getPayment({
|
|
83
|
+
* apiKey: 'cp_live_xxxxx',
|
|
84
|
+
* paymentId: 'pay_abc123',
|
|
85
|
+
* });
|
|
86
|
+
* console.log(result.payment.status);
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export function getPayment(params: GetPaymentParams): Promise<Record<string, unknown>>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* List payments for a business.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* const result = await listPayments({
|
|
97
|
+
* apiKey: 'cp_live_xxxxx',
|
|
98
|
+
* businessId: 'biz_123',
|
|
99
|
+
* status: 'completed',
|
|
100
|
+
* limit: 10,
|
|
101
|
+
* });
|
|
102
|
+
* console.log(result.payments);
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function listPayments(params: ListPaymentsFnParams): Promise<Record<string, unknown>>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Supported blockchains/cryptocurrencies.
|
|
109
|
+
*
|
|
110
|
+
* Use these constants when creating payments to avoid typos.
|
|
111
|
+
*/
|
|
112
|
+
export declare const Blockchain: {
|
|
113
|
+
/** Bitcoin */
|
|
114
|
+
readonly BTC: 'BTC';
|
|
115
|
+
/** Bitcoin Cash */
|
|
116
|
+
readonly BCH: 'BCH';
|
|
117
|
+
/** Ethereum */
|
|
118
|
+
readonly ETH: 'ETH';
|
|
119
|
+
/** Polygon (POL) */
|
|
120
|
+
readonly POL: 'POL';
|
|
121
|
+
/** Solana */
|
|
122
|
+
readonly SOL: 'SOL';
|
|
123
|
+
/** USDC on Ethereum */
|
|
124
|
+
readonly USDC_ETH: 'USDC_ETH';
|
|
125
|
+
/** USDC on Polygon */
|
|
126
|
+
readonly USDC_POL: 'USDC_POL';
|
|
127
|
+
/** USDC on Solana */
|
|
128
|
+
readonly USDC_SOL: 'USDC_SOL';
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @deprecated Use `Blockchain` instead.
|
|
133
|
+
*/
|
|
134
|
+
export declare const Cryptocurrency: typeof Blockchain;
|
|
135
|
+
|
|
136
|
+
/** Payment status constants */
|
|
137
|
+
export declare const PaymentStatus: {
|
|
138
|
+
readonly PENDING: 'pending';
|
|
139
|
+
readonly CONFIRMING: 'confirming';
|
|
140
|
+
readonly COMPLETED: 'completed';
|
|
141
|
+
readonly EXPIRED: 'expired';
|
|
142
|
+
readonly FAILED: 'failed';
|
|
143
|
+
readonly REFUNDED: 'refunded';
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** Supported fiat currencies */
|
|
147
|
+
export declare const FiatCurrency: {
|
|
148
|
+
readonly USD: 'USD';
|
|
149
|
+
readonly EUR: 'EUR';
|
|
150
|
+
readonly GBP: 'GBP';
|
|
151
|
+
readonly CAD: 'CAD';
|
|
152
|
+
readonly AUD: 'AUD';
|
|
153
|
+
};
|
package/src/payments.js
CHANGED
|
@@ -1,33 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Payment utilities for CoinPay SDK
|
|
3
|
+
*
|
|
4
|
+
* This module provides helper functions for creating and managing cryptocurrency payments.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // Quick payment creation
|
|
8
|
+
* import { createPayment } from '@coinpay/sdk';
|
|
9
|
+
*
|
|
10
|
+
* const payment = await createPayment({
|
|
11
|
+
* apiKey: 'cp_live_xxxxx',
|
|
12
|
+
* businessId: 'your-business-id',
|
|
13
|
+
* amount: 100.00,
|
|
14
|
+
* blockchain: 'ETH',
|
|
15
|
+
* description: 'Order #12345'
|
|
16
|
+
* });
|
|
3
17
|
*/
|
|
4
18
|
|
|
5
19
|
import { CoinPayClient } from './client.js';
|
|
6
20
|
|
|
7
21
|
/**
|
|
8
22
|
* Create a payment using a client instance or API key
|
|
23
|
+
*
|
|
24
|
+
* This is a convenience function for creating payments without manually
|
|
25
|
+
* instantiating a client. For multiple operations, create a client instance
|
|
26
|
+
* and reuse it.
|
|
27
|
+
*
|
|
9
28
|
* @param {Object} params - Payment parameters
|
|
10
|
-
* @param {string} params.apiKey - API key (if not using client)
|
|
11
|
-
* @param {CoinPayClient} [params.client] - Existing client instance
|
|
12
|
-
* @param {string} params.businessId - Business ID
|
|
13
|
-
* @param {number} params.amount - Amount in fiat currency
|
|
14
|
-
* @param {string} params.currency - Fiat currency code
|
|
15
|
-
* @param {string} params.
|
|
16
|
-
* @param {string} [params.description] - Payment description
|
|
17
|
-
* @param {
|
|
18
|
-
* @
|
|
19
|
-
*
|
|
29
|
+
* @param {string} params.apiKey - API key (required if not using client)
|
|
30
|
+
* @param {CoinPayClient} [params.client] - Existing client instance (optional)
|
|
31
|
+
* @param {string} params.businessId - Business ID from your CoinPay dashboard
|
|
32
|
+
* @param {number} params.amount - Amount in fiat currency (e.g., 100.00)
|
|
33
|
+
* @param {string} [params.currency='USD'] - Fiat currency code (USD, EUR, etc.)
|
|
34
|
+
* @param {string} params.blockchain - Blockchain to use (BTC, ETH, SOL, POL, BCH, USDC_ETH, USDC_POL, USDC_SOL)
|
|
35
|
+
* @param {string} [params.description] - Payment description shown to customer
|
|
36
|
+
* @param {Object} [params.metadata] - Custom metadata for your records
|
|
37
|
+
* @returns {Promise<Object>} Created payment with address and QR code
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* // Create a Bitcoin payment
|
|
41
|
+
* const payment = await createPayment({
|
|
42
|
+
* apiKey: 'cp_live_xxxxx',
|
|
43
|
+
* businessId: 'biz_123',
|
|
44
|
+
* amount: 50.00,
|
|
45
|
+
* currency: 'USD',
|
|
46
|
+
* blockchain: 'BTC',
|
|
47
|
+
* description: 'Premium subscription',
|
|
48
|
+
* metadata: { userId: 'user_456', plan: 'premium' }
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* console.log('Payment address:', payment.payment.payment_address);
|
|
52
|
+
* console.log('Amount in BTC:', payment.payment.crypto_amount);
|
|
20
53
|
*/
|
|
21
54
|
export async function createPayment({
|
|
22
55
|
apiKey,
|
|
23
56
|
client,
|
|
24
57
|
businessId,
|
|
25
58
|
amount,
|
|
26
|
-
currency,
|
|
27
|
-
|
|
59
|
+
currency = 'USD',
|
|
60
|
+
blockchain,
|
|
28
61
|
description,
|
|
29
62
|
metadata,
|
|
30
|
-
callbackUrl,
|
|
31
63
|
}) {
|
|
32
64
|
const coinpay = client || new CoinPayClient({ apiKey });
|
|
33
65
|
|
|
@@ -35,10 +67,9 @@ export async function createPayment({
|
|
|
35
67
|
businessId,
|
|
36
68
|
amount,
|
|
37
69
|
currency,
|
|
38
|
-
|
|
70
|
+
blockchain,
|
|
39
71
|
description,
|
|
40
72
|
metadata,
|
|
41
|
-
callbackUrl,
|
|
42
73
|
});
|
|
43
74
|
}
|
|
44
75
|
|
|
@@ -91,16 +122,44 @@ export const PaymentStatus = {
|
|
|
91
122
|
};
|
|
92
123
|
|
|
93
124
|
/**
|
|
94
|
-
* Supported cryptocurrencies
|
|
125
|
+
* Supported blockchains/cryptocurrencies
|
|
126
|
+
*
|
|
127
|
+
* Use these constants when creating payments to ensure valid blockchain values.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* import { Blockchain, createPayment } from '@coinpay/sdk';
|
|
131
|
+
*
|
|
132
|
+
* const payment = await createPayment({
|
|
133
|
+
* apiKey: 'cp_live_xxxxx',
|
|
134
|
+
* businessId: 'biz_123',
|
|
135
|
+
* amount: 100,
|
|
136
|
+
* blockchain: Blockchain.ETH
|
|
137
|
+
* });
|
|
95
138
|
*/
|
|
96
|
-
export const
|
|
139
|
+
export const Blockchain = {
|
|
140
|
+
/** Bitcoin */
|
|
97
141
|
BTC: 'BTC',
|
|
142
|
+
/** Bitcoin Cash */
|
|
98
143
|
BCH: 'BCH',
|
|
144
|
+
/** Ethereum */
|
|
99
145
|
ETH: 'ETH',
|
|
100
|
-
|
|
146
|
+
/** Polygon (POL) */
|
|
147
|
+
POL: 'POL',
|
|
148
|
+
/** Solana */
|
|
101
149
|
SOL: 'SOL',
|
|
150
|
+
/** USDC on Ethereum */
|
|
151
|
+
USDC_ETH: 'USDC_ETH',
|
|
152
|
+
/** USDC on Polygon */
|
|
153
|
+
USDC_POL: 'USDC_POL',
|
|
154
|
+
/** USDC on Solana */
|
|
155
|
+
USDC_SOL: 'USDC_SOL',
|
|
102
156
|
};
|
|
103
157
|
|
|
158
|
+
/**
|
|
159
|
+
* @deprecated Use Blockchain instead
|
|
160
|
+
*/
|
|
161
|
+
export const Cryptocurrency = Blockchain;
|
|
162
|
+
|
|
104
163
|
/**
|
|
105
164
|
* Supported fiat currencies
|
|
106
165
|
*/
|
|
@@ -117,6 +176,7 @@ export default {
|
|
|
117
176
|
getPayment,
|
|
118
177
|
listPayments,
|
|
119
178
|
PaymentStatus,
|
|
179
|
+
Blockchain,
|
|
120
180
|
Cryptocurrency,
|
|
121
181
|
FiatCurrency,
|
|
122
182
|
};
|