@waffo/waffo-node 2.4.0 → 2.5.1
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/README.md +12 -6
- package/dist/index.d.mts +252 -24
- package/dist/index.d.ts +252 -24
- package/dist/index.js +63 -25
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +63 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -92,18 +92,18 @@ var WaffoUnknownStatusError = class _WaffoUnknownStatusError extends Error {
|
|
|
92
92
|
// src/utils/rsa-utils.ts
|
|
93
93
|
var ALGORITHM = "RSA-SHA256";
|
|
94
94
|
var KEY_SIZE = 2048;
|
|
95
|
-
|
|
95
|
+
var PKCS8_PEM_HEADER = "-----BEGIN PRIVATE KEY-----";
|
|
96
|
+
var PEM_HEADER_PREFIX = "-----BEGIN ";
|
|
97
|
+
var INVALID_PRIVATE_KEY_FORMAT_MESSAGE = "Invalid private key format. Please provide an unencrypted PKCS#8 private key.";
|
|
98
|
+
function sign(data, privateKeyInput) {
|
|
96
99
|
try {
|
|
97
|
-
const
|
|
100
|
+
const privateKey = parsePrivateKey(privateKeyInput);
|
|
98
101
|
const signer = crypto.createSign(ALGORITHM);
|
|
99
102
|
signer.update(data, "utf8");
|
|
100
|
-
const signature = signer.sign(
|
|
101
|
-
key: privateKeyBuffer,
|
|
102
|
-
format: "der",
|
|
103
|
-
type: "pkcs8"
|
|
104
|
-
});
|
|
103
|
+
const signature = signer.sign(privateKey);
|
|
105
104
|
return signature.toString("base64");
|
|
106
105
|
} catch (error) {
|
|
106
|
+
if (error instanceof WaffoError) throw error;
|
|
107
107
|
throw new WaffoError(
|
|
108
108
|
WaffoErrorCode.SIGN_FAILED,
|
|
109
109
|
`Failed to sign data: ${error instanceof Error ? error.message : String(error)}`,
|
|
@@ -137,29 +137,43 @@ function validatePrivateKey(base64PrivateKey) {
|
|
|
137
137
|
);
|
|
138
138
|
}
|
|
139
139
|
try {
|
|
140
|
-
|
|
141
|
-
crypto.createPrivateKey({
|
|
142
|
-
key: keyBuffer,
|
|
143
|
-
format: "der",
|
|
144
|
-
type: "pkcs8"
|
|
145
|
-
});
|
|
140
|
+
parsePrivateKey(base64PrivateKey);
|
|
146
141
|
} catch (error) {
|
|
147
142
|
if (error instanceof WaffoError) throw error;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
throw new WaffoError(
|
|
144
|
+
WaffoErrorCode.INVALID_PRIVATE_KEY,
|
|
145
|
+
INVALID_PRIVATE_KEY_FORMAT_MESSAGE,
|
|
146
|
+
error instanceof Error ? error : void 0
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function parsePrivateKey(privateKeyInput) {
|
|
151
|
+
try {
|
|
152
|
+
const trimmed = privateKeyInput.trim();
|
|
153
|
+
const key = trimmed.startsWith(PKCS8_PEM_HEADER) ? crypto.createPrivateKey(trimmed) : parseDerPrivateKey(trimmed);
|
|
154
|
+
if (key.asymmetricKeyType !== "rsa") {
|
|
155
|
+
throw new Error("private key is not RSA");
|
|
155
156
|
}
|
|
157
|
+
return key;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (error instanceof WaffoError) throw error;
|
|
156
160
|
throw new WaffoError(
|
|
157
161
|
WaffoErrorCode.INVALID_PRIVATE_KEY,
|
|
158
|
-
|
|
162
|
+
INVALID_PRIVATE_KEY_FORMAT_MESSAGE,
|
|
159
163
|
error instanceof Error ? error : void 0
|
|
160
164
|
);
|
|
161
165
|
}
|
|
162
166
|
}
|
|
167
|
+
function parseDerPrivateKey(trimmedPrivateKey) {
|
|
168
|
+
if (trimmedPrivateKey.startsWith(PEM_HEADER_PREFIX)) {
|
|
169
|
+
throw new Error("unsupported PEM private key");
|
|
170
|
+
}
|
|
171
|
+
return crypto.createPrivateKey({
|
|
172
|
+
key: Buffer.from(trimmedPrivateKey, "base64"),
|
|
173
|
+
format: "der",
|
|
174
|
+
type: "pkcs8"
|
|
175
|
+
});
|
|
176
|
+
}
|
|
163
177
|
function validatePublicKey(base64PublicKey) {
|
|
164
178
|
if (!base64PublicKey || base64PublicKey.trim() === "") {
|
|
165
179
|
throw new WaffoError(
|
|
@@ -441,7 +455,7 @@ var HEADER_SDK_VERSION = "x-sdk-version";
|
|
|
441
455
|
var HEADER_CONTENT_TYPE = "content-type";
|
|
442
456
|
var CONTENT_TYPE_JSON = "application/json";
|
|
443
457
|
var API_VERSION = "1.0.0";
|
|
444
|
-
var SDK_VERSION = "waffo-node/2.
|
|
458
|
+
var SDK_VERSION = "waffo-node/2.5.1";
|
|
445
459
|
var WaffoHttpClient = class {
|
|
446
460
|
baseUrl;
|
|
447
461
|
apiKey;
|
|
@@ -453,6 +467,8 @@ var WaffoHttpClient = class {
|
|
|
453
467
|
logger;
|
|
454
468
|
httpTransport;
|
|
455
469
|
constructor(config) {
|
|
470
|
+
RsaUtils.validatePrivateKey(config.privateKey);
|
|
471
|
+
RsaUtils.validatePublicKey(config.waffoPublicKey);
|
|
456
472
|
this.baseUrl = EnvironmentBaseUrl[config.environment];
|
|
457
473
|
this.apiKey = config.apiKey;
|
|
458
474
|
this.privateKey = config.privateKey;
|
|
@@ -591,7 +607,7 @@ var WaffoHttpClient = class {
|
|
|
591
607
|
isNetworkError(error) {
|
|
592
608
|
if (!(error instanceof Error)) return false;
|
|
593
609
|
const message = error.message.toLowerCase();
|
|
594
|
-
return error.name === "AbortError" || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("network") || message.includes("socket") || message.includes("connection");
|
|
610
|
+
return error.name === "AbortError" || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("fetch failed") || message.includes("network") || message.includes("socket") || message.includes("connection");
|
|
595
611
|
}
|
|
596
612
|
};
|
|
597
613
|
|
|
@@ -880,6 +896,9 @@ var OrderResource = class _OrderResource extends BaseResource {
|
|
|
880
896
|
* @throws WaffoUnknownStatusError for network errors
|
|
881
897
|
*/
|
|
882
898
|
async capture(params, options) {
|
|
899
|
+
if (!params.captureRequestedAt) {
|
|
900
|
+
params.captureRequestedAt = getCurrentTimestamp();
|
|
901
|
+
}
|
|
883
902
|
return this.httpClient.post(
|
|
884
903
|
this.getPath(_OrderResource.PATH_CAPTURE),
|
|
885
904
|
params,
|
|
@@ -894,6 +913,7 @@ var SubscriptionResource = class _SubscriptionResource extends BaseResource {
|
|
|
894
913
|
static PATH_INQUIRY = "/inquiry";
|
|
895
914
|
static PATH_CANCEL = "/cancel";
|
|
896
915
|
static PATH_MANAGE = "/manage";
|
|
916
|
+
static PATH_UPDATE = "/update";
|
|
897
917
|
static PATH_CHANGE = "/change";
|
|
898
918
|
static PATH_CHANGE_INQUIRY = "/change/inquiry";
|
|
899
919
|
/**
|
|
@@ -962,6 +982,24 @@ var SubscriptionResource = class _SubscriptionResource extends BaseResource {
|
|
|
962
982
|
options
|
|
963
983
|
);
|
|
964
984
|
}
|
|
985
|
+
/**
|
|
986
|
+
* Updates an existing subscription amount or scheduled period amounts.
|
|
987
|
+
*
|
|
988
|
+
* Provide either subscriptionRequest or subscriptionId. At least one of amount
|
|
989
|
+
* or productInfo.scheduledAmounts must be provided.
|
|
990
|
+
*
|
|
991
|
+
* @param params - Subscription update parameters
|
|
992
|
+
* @param options - Optional request options
|
|
993
|
+
* @returns API response with update result
|
|
994
|
+
* @throws WaffoUnknownStatusError for network errors
|
|
995
|
+
*/
|
|
996
|
+
async update(params, options) {
|
|
997
|
+
return this.httpClient.post(
|
|
998
|
+
this.getPath(_SubscriptionResource.PATH_UPDATE),
|
|
999
|
+
params,
|
|
1000
|
+
options
|
|
1001
|
+
);
|
|
1002
|
+
}
|
|
965
1003
|
/**
|
|
966
1004
|
* Changes (upgrades/downgrades) an existing subscription.
|
|
967
1005
|
*
|
|
@@ -1128,8 +1166,8 @@ var Waffo = class _Waffo {
|
|
|
1128
1166
|
* @param config - SDK configuration
|
|
1129
1167
|
*/
|
|
1130
1168
|
constructor(config) {
|
|
1131
|
-
this.config = config;
|
|
1132
|
-
this.httpClient = new WaffoHttpClient(config);
|
|
1169
|
+
this.config = createWaffoConfig(config);
|
|
1170
|
+
this.httpClient = new WaffoHttpClient(this.config);
|
|
1133
1171
|
}
|
|
1134
1172
|
/**
|
|
1135
1173
|
* Creates a Waffo SDK instance from environment variables.
|