@caspay/sdk 1.0.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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Browser bundle entry point
3
+ * Exposes CasPay SDK to window.CasPay for script tag usage
4
+ *
5
+ * @example
6
+ * ```html
7
+ * <script src="https://cdn.jsdelivr.net/npm/@caspay/sdk/dist/caspay.min.js"></script>
8
+ * <script>
9
+ * const caspay = new CasPay({
10
+ * apiKey: 'cp_live_...',
11
+ * merchantId: 'MERCH_...'
12
+ * });
13
+ * </script>
14
+ * ```
15
+ */
16
+ import CasPay from './index';
17
+ export default CasPay;
@@ -0,0 +1 @@
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).CasPay=e()}(this,function(){"use strict";class t{constructor(t){if(this.SDK_VERSION="1.0.0",this.baseUrl=t.baseUrl||"https://api.caspay.link",this.apiKey=t.apiKey,this.merchantId=t.merchantId,!this.apiKey)throw new Error("CasPay SDK: apiKey is required");if(!this.merchantId)throw new Error("CasPay SDK: merchantId is required")}async request(t,e,r){const s=`${this.baseUrl}${e}`;try{const e=await fetch(s,{method:t,headers:{"Content-Type":"application/json","X-CasPay-Key":this.apiKey,"X-CasPay-SDK-Version":this.SDK_VERSION,"User-Agent":`CasPay-SDK-JS/${this.SDK_VERSION}`},body:r?JSON.stringify(r):void 0}),i=await e.json();if(!e.ok){throw{error:i.error||"Request failed",code:i.code||"UNKNOWN_ERROR",status:e.status}}return i}catch(t){if(t.error&&t.code)throw t;throw{error:t.message||"Network error",code:"NETWORK_ERROR",status:0}}}getMerchantId(){return this.merchantId}}class e{constructor(t){this.client=t}async create(t){if(!t.senderAddress)throw{error:"senderAddress is required",code:"INVALID_PARAMS",status:400};if(!t.amount||t.amount<=0)throw{error:"amount must be greater than 0",code:"INVALID_PARAMS",status:400};if(!t.productId&&!t.subscriptionPlanId)throw{error:"Either productId or subscriptionPlanId is required",code:"INVALID_PARAMS",status:400};const e={merchant_id:this.client.getMerchantId(),sender_address:t.senderAddress,transaction_hash:t.transactionHash||`mock_tx_${Date.now()}`,product_id:t.productId,subscription_plan_id:t.subscriptionPlanId,amount:t.amount,currency:t.currency||"USD"};return this.client.request("POST","/api/v1/payments/record",e)}}class r{constructor(r){this.client=new t(r),this.payments=new e(this.client)}static get version(){return"1.0.0"}}return"undefined"!=typeof window&&(window.CasPay=r),r});
@@ -0,0 +1,14 @@
1
+ import type { CasPayConfig } from '../types';
2
+ /**
3
+ * HTTP Client for CasPay API
4
+ * Handles all API requests with authentication and error handling
5
+ */
6
+ export declare class HttpClient {
7
+ private baseUrl;
8
+ private apiKey;
9
+ private merchantId;
10
+ private readonly SDK_VERSION;
11
+ constructor(config: CasPayConfig);
12
+ request<T>(method: string, path: string, body?: Record<string, any>): Promise<T>;
13
+ getMerchantId(): string;
14
+ }
@@ -0,0 +1,38 @@
1
+ import { Payments } from './resources/payments';
2
+ import type { CasPayConfig } from './types';
3
+ /**
4
+ * CasPay SDK
5
+ * Official JavaScript/TypeScript SDK for CasPay payment gateway
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import CasPay from '@caspay/sdk';
10
+ *
11
+ * const caspay = new CasPay({
12
+ * apiKey: 'cp_live_...',
13
+ * merchantId: 'MERCH_...'
14
+ * });
15
+ *
16
+ * const payment = await caspay.payments.create({
17
+ * senderAddress: '0x123...',
18
+ * productId: 'prod_abc',
19
+ * amount: 100
20
+ * });
21
+ * ```
22
+ */
23
+ export default class CasPay {
24
+ /** Payments resource for creating and managing payments */
25
+ payments: Payments;
26
+ private client;
27
+ /**
28
+ * Create a new CasPay SDK instance
29
+ * @param config - SDK configuration
30
+ */
31
+ constructor(config: CasPayConfig);
32
+ /**
33
+ * Get SDK version
34
+ */
35
+ static get version(): string;
36
+ }
37
+ export { CasPay };
38
+ export * from './types';
@@ -0,0 +1,160 @@
1
+ /**
2
+ * HTTP Client for CasPay API
3
+ * Handles all API requests with authentication and error handling
4
+ */
5
+ class HttpClient {
6
+ constructor(config) {
7
+ this.SDK_VERSION = '1.0.0';
8
+ // Use production API by default
9
+ this.baseUrl = config.baseUrl || 'https://api.caspay.link';
10
+ this.apiKey = config.apiKey;
11
+ this.merchantId = config.merchantId;
12
+ // Validate required config
13
+ if (!this.apiKey) {
14
+ throw new Error('CasPay SDK: apiKey is required');
15
+ }
16
+ if (!this.merchantId) {
17
+ throw new Error('CasPay SDK: merchantId is required');
18
+ }
19
+ }
20
+ async request(method, path, body) {
21
+ const url = `${this.baseUrl}${path}`;
22
+ try {
23
+ const response = await fetch(url, {
24
+ method,
25
+ headers: {
26
+ 'Content-Type': 'application/json',
27
+ 'X-CasPay-Key': this.apiKey,
28
+ 'X-CasPay-SDK-Version': this.SDK_VERSION,
29
+ 'User-Agent': `CasPay-SDK-JS/${this.SDK_VERSION}`,
30
+ },
31
+ body: body ? JSON.stringify(body) : undefined,
32
+ });
33
+ const data = await response.json();
34
+ if (!response.ok) {
35
+ const error = {
36
+ error: data.error || 'Request failed',
37
+ code: data.code || 'UNKNOWN_ERROR',
38
+ status: response.status,
39
+ };
40
+ throw error;
41
+ }
42
+ return data;
43
+ }
44
+ catch (error) {
45
+ // Re-throw CasPay errors
46
+ if (error.error && error.code) {
47
+ throw error;
48
+ }
49
+ // Wrap network errors
50
+ throw {
51
+ error: error.message || 'Network error',
52
+ code: 'NETWORK_ERROR',
53
+ status: 0,
54
+ };
55
+ }
56
+ }
57
+ getMerchantId() {
58
+ return this.merchantId;
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Payments Resource
64
+ * Handle payment creation and verification
65
+ */
66
+ class Payments {
67
+ constructor(client) {
68
+ this.client = client;
69
+ }
70
+ /**
71
+ * Create a new payment record
72
+ *
73
+ * @param params - Payment parameters
74
+ * @returns Payment response with transaction details
75
+ * @throws {CasPayError} If payment creation fails
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const payment = await caspay.payments.create({
80
+ * senderAddress: '0x123...',
81
+ * productId: 'prod_abc123',
82
+ * amount: 100,
83
+ * currency: 'USD'
84
+ * });
85
+ * ```
86
+ */
87
+ async create(params) {
88
+ // Validate required fields
89
+ if (!params.senderAddress) {
90
+ throw {
91
+ error: 'senderAddress is required',
92
+ code: 'INVALID_PARAMS',
93
+ status: 400,
94
+ };
95
+ }
96
+ if (!params.amount || params.amount <= 0) {
97
+ throw {
98
+ error: 'amount must be greater than 0',
99
+ code: 'INVALID_PARAMS',
100
+ status: 400,
101
+ };
102
+ }
103
+ if (!params.productId && !params.subscriptionPlanId) {
104
+ throw {
105
+ error: 'Either productId or subscriptionPlanId is required',
106
+ code: 'INVALID_PARAMS',
107
+ status: 400,
108
+ };
109
+ }
110
+ const payload = {
111
+ merchant_id: this.client.getMerchantId(),
112
+ sender_address: params.senderAddress,
113
+ transaction_hash: params.transactionHash || `mock_tx_${Date.now()}`,
114
+ product_id: params.productId,
115
+ subscription_plan_id: params.subscriptionPlanId,
116
+ amount: params.amount,
117
+ currency: params.currency || 'USD',
118
+ };
119
+ return this.client.request('POST', '/api/v1/payments/record', payload);
120
+ }
121
+ }
122
+
123
+ /**
124
+ * CasPay SDK
125
+ * Official JavaScript/TypeScript SDK for CasPay payment gateway
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * import CasPay from '@caspay/sdk';
130
+ *
131
+ * const caspay = new CasPay({
132
+ * apiKey: 'cp_live_...',
133
+ * merchantId: 'MERCH_...'
134
+ * });
135
+ *
136
+ * const payment = await caspay.payments.create({
137
+ * senderAddress: '0x123...',
138
+ * productId: 'prod_abc',
139
+ * amount: 100
140
+ * });
141
+ * ```
142
+ */
143
+ class CasPay {
144
+ /**
145
+ * Create a new CasPay SDK instance
146
+ * @param config - SDK configuration
147
+ */
148
+ constructor(config) {
149
+ this.client = new HttpClient(config);
150
+ this.payments = new Payments(this.client);
151
+ }
152
+ /**
153
+ * Get SDK version
154
+ */
155
+ static get version() {
156
+ return '1.0.0';
157
+ }
158
+ }
159
+
160
+ export { CasPay, CasPay as default };
package/dist/index.js ADDED
@@ -0,0 +1,165 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * HTTP Client for CasPay API
7
+ * Handles all API requests with authentication and error handling
8
+ */
9
+ class HttpClient {
10
+ constructor(config) {
11
+ this.SDK_VERSION = '1.0.0';
12
+ // Use production API by default
13
+ this.baseUrl = config.baseUrl || 'https://api.caspay.link';
14
+ this.apiKey = config.apiKey;
15
+ this.merchantId = config.merchantId;
16
+ // Validate required config
17
+ if (!this.apiKey) {
18
+ throw new Error('CasPay SDK: apiKey is required');
19
+ }
20
+ if (!this.merchantId) {
21
+ throw new Error('CasPay SDK: merchantId is required');
22
+ }
23
+ }
24
+ async request(method, path, body) {
25
+ const url = `${this.baseUrl}${path}`;
26
+ try {
27
+ const response = await fetch(url, {
28
+ method,
29
+ headers: {
30
+ 'Content-Type': 'application/json',
31
+ 'X-CasPay-Key': this.apiKey,
32
+ 'X-CasPay-SDK-Version': this.SDK_VERSION,
33
+ 'User-Agent': `CasPay-SDK-JS/${this.SDK_VERSION}`,
34
+ },
35
+ body: body ? JSON.stringify(body) : undefined,
36
+ });
37
+ const data = await response.json();
38
+ if (!response.ok) {
39
+ const error = {
40
+ error: data.error || 'Request failed',
41
+ code: data.code || 'UNKNOWN_ERROR',
42
+ status: response.status,
43
+ };
44
+ throw error;
45
+ }
46
+ return data;
47
+ }
48
+ catch (error) {
49
+ // Re-throw CasPay errors
50
+ if (error.error && error.code) {
51
+ throw error;
52
+ }
53
+ // Wrap network errors
54
+ throw {
55
+ error: error.message || 'Network error',
56
+ code: 'NETWORK_ERROR',
57
+ status: 0,
58
+ };
59
+ }
60
+ }
61
+ getMerchantId() {
62
+ return this.merchantId;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Payments Resource
68
+ * Handle payment creation and verification
69
+ */
70
+ class Payments {
71
+ constructor(client) {
72
+ this.client = client;
73
+ }
74
+ /**
75
+ * Create a new payment record
76
+ *
77
+ * @param params - Payment parameters
78
+ * @returns Payment response with transaction details
79
+ * @throws {CasPayError} If payment creation fails
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * const payment = await caspay.payments.create({
84
+ * senderAddress: '0x123...',
85
+ * productId: 'prod_abc123',
86
+ * amount: 100,
87
+ * currency: 'USD'
88
+ * });
89
+ * ```
90
+ */
91
+ async create(params) {
92
+ // Validate required fields
93
+ if (!params.senderAddress) {
94
+ throw {
95
+ error: 'senderAddress is required',
96
+ code: 'INVALID_PARAMS',
97
+ status: 400,
98
+ };
99
+ }
100
+ if (!params.amount || params.amount <= 0) {
101
+ throw {
102
+ error: 'amount must be greater than 0',
103
+ code: 'INVALID_PARAMS',
104
+ status: 400,
105
+ };
106
+ }
107
+ if (!params.productId && !params.subscriptionPlanId) {
108
+ throw {
109
+ error: 'Either productId or subscriptionPlanId is required',
110
+ code: 'INVALID_PARAMS',
111
+ status: 400,
112
+ };
113
+ }
114
+ const payload = {
115
+ merchant_id: this.client.getMerchantId(),
116
+ sender_address: params.senderAddress,
117
+ transaction_hash: params.transactionHash || `mock_tx_${Date.now()}`,
118
+ product_id: params.productId,
119
+ subscription_plan_id: params.subscriptionPlanId,
120
+ amount: params.amount,
121
+ currency: params.currency || 'USD',
122
+ };
123
+ return this.client.request('POST', '/api/v1/payments/record', payload);
124
+ }
125
+ }
126
+
127
+ /**
128
+ * CasPay SDK
129
+ * Official JavaScript/TypeScript SDK for CasPay payment gateway
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * import CasPay from '@caspay/sdk';
134
+ *
135
+ * const caspay = new CasPay({
136
+ * apiKey: 'cp_live_...',
137
+ * merchantId: 'MERCH_...'
138
+ * });
139
+ *
140
+ * const payment = await caspay.payments.create({
141
+ * senderAddress: '0x123...',
142
+ * productId: 'prod_abc',
143
+ * amount: 100
144
+ * });
145
+ * ```
146
+ */
147
+ class CasPay {
148
+ /**
149
+ * Create a new CasPay SDK instance
150
+ * @param config - SDK configuration
151
+ */
152
+ constructor(config) {
153
+ this.client = new HttpClient(config);
154
+ this.payments = new Payments(this.client);
155
+ }
156
+ /**
157
+ * Get SDK version
158
+ */
159
+ static get version() {
160
+ return '1.0.0';
161
+ }
162
+ }
163
+
164
+ exports.CasPay = CasPay;
165
+ exports.default = CasPay;
@@ -0,0 +1,28 @@
1
+ import type { HttpClient } from '../core/client';
2
+ import type { PaymentCreateParams, PaymentResponse } from '../types';
3
+ /**
4
+ * Payments Resource
5
+ * Handle payment creation and verification
6
+ */
7
+ export declare class Payments {
8
+ private client;
9
+ constructor(client: HttpClient);
10
+ /**
11
+ * Create a new payment record
12
+ *
13
+ * @param params - Payment parameters
14
+ * @returns Payment response with transaction details
15
+ * @throws {CasPayError} If payment creation fails
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const payment = await caspay.payments.create({
20
+ * senderAddress: '0x123...',
21
+ * productId: 'prod_abc123',
22
+ * amount: 100,
23
+ * currency: 'USD'
24
+ * });
25
+ * ```
26
+ */
27
+ create(params: PaymentCreateParams): Promise<PaymentResponse>;
28
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * CasPay SDK Configuration
3
+ */
4
+ export interface CasPayConfig {
5
+ /** Your CasPay API key (starts with cp_live_ or cp_test_) */
6
+ apiKey: string;
7
+ /** Your merchant ID (starts with MERCH_) */
8
+ merchantId: string;
9
+ /** Optional: Override API base URL (default: https://api.caspay.link) */
10
+ baseUrl?: string;
11
+ }
12
+ /**
13
+ * Payment Creation Parameters
14
+ */
15
+ export interface PaymentCreateParams {
16
+ /** Sender's Casper wallet address */
17
+ senderAddress: string;
18
+ /** Optional: Casper transaction hash (auto-generated in mock mode) */
19
+ transactionHash?: string;
20
+ /** Product ID for one-time payments */
21
+ productId?: string;
22
+ /** Subscription plan ID for recurring payments */
23
+ subscriptionPlanId?: string;
24
+ /** Payment amount */
25
+ amount: number;
26
+ /** Currency code (default: USD) */
27
+ currency?: string;
28
+ }
29
+ /**
30
+ * Subscription Creation Parameters
31
+ */
32
+ export interface SubscriptionCreateParams {
33
+ /** Subscriber's Casper wallet address */
34
+ senderAddress: string;
35
+ /** Optional: Casper transaction hash */
36
+ transactionHash?: string;
37
+ /** Subscription plan ID */
38
+ planId: string;
39
+ /** Payment amount */
40
+ amount: number;
41
+ /** Currency code (default: USD) */
42
+ currency?: string;
43
+ }
44
+ /**
45
+ * Payment Response
46
+ */
47
+ export interface PaymentResponse {
48
+ success: boolean;
49
+ payment: {
50
+ id: string;
51
+ transaction_hash: string;
52
+ amount: number;
53
+ token: string;
54
+ status: string;
55
+ invoice_number: string;
56
+ created_at: string;
57
+ };
58
+ verification?: {
59
+ verified: boolean;
60
+ transaction_hash: string;
61
+ amount: number;
62
+ };
63
+ duplicate?: boolean;
64
+ responseTime?: string;
65
+ }
66
+ /**
67
+ * Subscription Response
68
+ */
69
+ export interface SubscriptionResponse {
70
+ success: boolean;
71
+ payment: {
72
+ id: string;
73
+ subscription_plan_id: string;
74
+ amount: number;
75
+ status: string;
76
+ };
77
+ subscription_id?: string;
78
+ }
79
+ /**
80
+ * API Error Response
81
+ */
82
+ export interface CasPayError {
83
+ error: string;
84
+ code: string;
85
+ status?: number;
86
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@caspay/sdk",
3
+ "version": "1.0.0",
4
+ "description": "Official CasPay JavaScript/TypeScript SDK - Accept crypto payments with Casper blockchain",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.esm.js",
7
+ "browser": "dist/caspay.min.js",
8
+ "types": "dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "scripts": {
15
+ "build": "rollup -c",
16
+ "dev": "rollup -c -w",
17
+ "prepublishOnly": "npm run build",
18
+ "test": "echo \"Error: no test specified\" && exit 0"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/caspay/sdk.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/caspay/sdk/issues"
26
+ },
27
+ "homepage": "https://caspay.link",
28
+ "keywords": [
29
+ "caspay",
30
+ "payment",
31
+ "casper",
32
+ "blockchain",
33
+ "cryptocurrency",
34
+ "subscription",
35
+ "payment-gateway",
36
+ "web3",
37
+ "crypto-payments"
38
+ ],
39
+ "author": "CasPay <support@caspay.link>",
40
+ "license": "MIT",
41
+ "engines": {
42
+ "node": ">=14.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@rollup/plugin-commonjs": "^25.0.7",
46
+ "@rollup/plugin-node-resolve": "^15.2.3",
47
+ "@rollup/plugin-typescript": "^11.1.5",
48
+ "rollup": "^4.9.1",
49
+ "rollup-plugin-terser": "^7.0.2",
50
+ "tslib": "^2.8.1",
51
+ "typescript": "^5.3.3"
52
+ }
53
+ }