@caspay/sdk 1.1.1 → 1.1.3

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.
@@ -7,4 +7,5 @@ export declare class HttpClient {
7
7
  request<T>(method: string, path: string, body?: Record<string, any>): Promise<T>;
8
8
  getMerchantId(): string;
9
9
  getBaseUrl(): string;
10
+ validateApiKey(): Promise<void>;
10
11
  }
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { PublicKey, ExecutableDeployItem, TransferDeployItem, DeployHeader, Deploy } from 'casper-js-sdk';
2
2
 
3
- const SDK_VERSION = '1.1.1';
3
+ const SDK_VERSION = '1.1.3';
4
4
 
5
5
  class HttpClient {
6
6
  constructor(config) {
@@ -55,6 +55,36 @@ class HttpClient {
55
55
  getBaseUrl() {
56
56
  return this.baseUrl;
57
57
  }
58
+ async validateApiKey() {
59
+ const url = `${this.baseUrl}/v1/validate-key`;
60
+ try {
61
+ const response = await fetch(url, {
62
+ method: 'GET',
63
+ headers: {
64
+ 'X-CasPay-Key': this.apiKey,
65
+ 'X-CasPay-SDK-Version': SDK_VERSION,
66
+ },
67
+ });
68
+ const data = await response.json();
69
+ if (!response.ok || !data.valid) {
70
+ throw {
71
+ error: data.error || 'Invalid API key',
72
+ code: 'INVALID_API_KEY',
73
+ status: response.status,
74
+ };
75
+ }
76
+ }
77
+ catch (error) {
78
+ if (error.code === 'INVALID_API_KEY') {
79
+ throw error;
80
+ }
81
+ throw {
82
+ error: error.message || 'API key validation failed',
83
+ code: 'VALIDATION_ERROR',
84
+ status: 0,
85
+ };
86
+ }
87
+ }
58
88
  }
59
89
 
60
90
  const CSPR_TO_MOTES = 1000000000;
@@ -237,6 +267,7 @@ class Payments {
237
267
  };
238
268
  }
239
269
  try {
270
+ await this.client.validateApiKey();
240
271
  const isConnected = await this.wallet.isConnected();
241
272
  if (!isConnected) {
242
273
  await this.wallet.connect();
@@ -270,10 +301,11 @@ class Payments {
270
301
  };
271
302
  }
272
303
  catch (recordError) {
273
- return {
274
- success: true,
275
- transactionHash: transferResult.deployHash,
304
+ throw {
276
305
  error: `Payment transferred but recording failed: ${recordError.error || recordError.message}`,
306
+ code: 'RECORDING_FAILED',
307
+ status: recordError.status || 500,
308
+ transactionHash: transferResult.deployHash,
277
309
  };
278
310
  }
279
311
  }
@@ -300,7 +332,7 @@ class Subscriptions {
300
332
  };
301
333
  }
302
334
  const merchantId = this.client.getMerchantId();
303
- let url = `/v1/subscriptions/check/?merchant_id=${merchantId}&subscriber=${params.subscriberAddress}`;
335
+ let url = `/v1/subscriptions/check?merchant_id=${merchantId}&subscriber=${params.subscriberAddress}`;
304
336
  if (params.planId) {
305
337
  url += `&plan_id=${params.planId}`;
306
338
  }
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var casperJsSdk = require('casper-js-sdk');
6
6
 
7
- const SDK_VERSION = '1.1.1';
7
+ const SDK_VERSION = '1.1.3';
8
8
 
9
9
  class HttpClient {
10
10
  constructor(config) {
@@ -59,6 +59,36 @@ class HttpClient {
59
59
  getBaseUrl() {
60
60
  return this.baseUrl;
61
61
  }
62
+ async validateApiKey() {
63
+ const url = `${this.baseUrl}/v1/validate-key`;
64
+ try {
65
+ const response = await fetch(url, {
66
+ method: 'GET',
67
+ headers: {
68
+ 'X-CasPay-Key': this.apiKey,
69
+ 'X-CasPay-SDK-Version': SDK_VERSION,
70
+ },
71
+ });
72
+ const data = await response.json();
73
+ if (!response.ok || !data.valid) {
74
+ throw {
75
+ error: data.error || 'Invalid API key',
76
+ code: 'INVALID_API_KEY',
77
+ status: response.status,
78
+ };
79
+ }
80
+ }
81
+ catch (error) {
82
+ if (error.code === 'INVALID_API_KEY') {
83
+ throw error;
84
+ }
85
+ throw {
86
+ error: error.message || 'API key validation failed',
87
+ code: 'VALIDATION_ERROR',
88
+ status: 0,
89
+ };
90
+ }
91
+ }
62
92
  }
63
93
 
64
94
  const CSPR_TO_MOTES = 1000000000;
@@ -241,6 +271,7 @@ class Payments {
241
271
  };
242
272
  }
243
273
  try {
274
+ await this.client.validateApiKey();
244
275
  const isConnected = await this.wallet.isConnected();
245
276
  if (!isConnected) {
246
277
  await this.wallet.connect();
@@ -274,10 +305,11 @@ class Payments {
274
305
  };
275
306
  }
276
307
  catch (recordError) {
277
- return {
278
- success: true,
279
- transactionHash: transferResult.deployHash,
308
+ throw {
280
309
  error: `Payment transferred but recording failed: ${recordError.error || recordError.message}`,
310
+ code: 'RECORDING_FAILED',
311
+ status: recordError.status || 500,
312
+ transactionHash: transferResult.deployHash,
281
313
  };
282
314
  }
283
315
  }
@@ -304,7 +336,7 @@ class Subscriptions {
304
336
  };
305
337
  }
306
338
  const merchantId = this.client.getMerchantId();
307
- let url = `/v1/subscriptions/check/?merchant_id=${merchantId}&subscriber=${params.subscriberAddress}`;
339
+ let url = `/v1/subscriptions/check?merchant_id=${merchantId}&subscriber=${params.subscriberAddress}`;
308
340
  if (params.planId) {
309
341
  url += `&plan_id=${params.planId}`;
310
342
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.1.1";
1
+ export declare const SDK_VERSION = "1.1.3";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caspay/sdk",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Official CasPay JavaScript/TypeScript SDK - Accept crypto payments with Casper blockchain",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",