@diviswap/sdk 1.8.1 → 1.9.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/dist/index.mjs CHANGED
@@ -246,12 +246,28 @@ var PayeesModule = class {
246
246
  constructor(client) {
247
247
  this.client = client;
248
248
  }
249
+ /**
250
+ * Validate ABA routing number checksum
251
+ */
252
+ validateRoutingNumber(routing) {
253
+ if (routing.length !== 9 || !/^\d{9}$/.test(routing)) {
254
+ return false;
255
+ }
256
+ const d = routing.split("").map(Number);
257
+ const checksum = 3 * (d[0] + d[3] + d[6]) + 7 * (d[1] + d[4] + d[7]) + 1 * (d[2] + d[5] + d[8]);
258
+ return checksum % 10 === 0;
259
+ }
260
+ /**
261
+ * Validate account number format
262
+ */
263
+ validateAccountNumber(account) {
264
+ return /^\d{4,17}$/.test(account);
265
+ }
249
266
  /**
250
267
  * Create a new payee (bank account)
251
268
  *
252
269
  * @example
253
270
  * ```typescript
254
- * // Bank account
255
271
  * const bankAccount = await diviswap.payees.create({
256
272
  * nickname: 'My Checking',
257
273
  * accountNumber: '123456789',
@@ -259,54 +275,32 @@ var PayeesModule = class {
259
275
  * accountType: 'checking',
260
276
  * setAsDefault: true
261
277
  * });
262
- *
263
- * // Debit card
264
- * const debitCard = await diviswap.payees.create({
265
- * nickname: 'My Debit Card',
266
- * accountType: 'debit_card',
267
- * debitCard: {
268
- * number: '4111111111111111',
269
- * expirationMonth: '12',
270
- * expirationYear: '2025',
271
- * cvv: '123'
272
- * },
273
- * setAsDefault: false
274
- * });
275
278
  * ```
276
279
  */
277
280
  async create(data) {
278
281
  try {
279
- let response;
280
- if (data.accountType === "debit_card") {
281
- if (!data.debitCard) {
282
- throw new Error(
283
- "Debit card information is required for debit_card account type"
284
- );
285
- }
286
- response = await this.client.post("/api/v1/payees", {
287
- name: data.nickname,
288
- debit_card: {
289
- number: data.debitCard.number,
290
- expiration_month: data.debitCard.expirationMonth,
291
- expiration_year: data.debitCard.expirationYear,
292
- cvv: data.debitCard.cvv
293
- },
294
- set_as_default: data.setAsDefault || false
295
- });
296
- } else {
297
- if (!data.accountNumber || !data.routingNumber) {
298
- throw new Error(
299
- "Account number and routing number are required for bank accounts"
300
- );
301
- }
302
- response = await this.client.post("/api/v1/payees", {
303
- name: data.nickname,
304
- account_number: data.accountNumber,
305
- routing_number: data.routingNumber,
306
- type: (data.accountType || "checking").toUpperCase(),
307
- set_as_default: data.setAsDefault || false
308
- });
282
+ if (!data.accountNumber || !data.routingNumber) {
283
+ throw new Error(
284
+ "Account number and routing number are required for bank accounts"
285
+ );
286
+ }
287
+ if (!this.validateRoutingNumber(data.routingNumber)) {
288
+ throw new Error(
289
+ "Invalid routing number. Please provide a valid 9-digit ABA routing number."
290
+ );
291
+ }
292
+ if (!this.validateAccountNumber(data.accountNumber)) {
293
+ throw new Error(
294
+ "Invalid account number. Must be 4-17 digits with no letters or special characters."
295
+ );
309
296
  }
297
+ const response = await this.client.post("/api/v1/payees", {
298
+ name: data.nickname,
299
+ account_number: data.accountNumber,
300
+ routing_number: data.routingNumber,
301
+ type: (data.accountType || "checking").toUpperCase(),
302
+ set_as_default: data.setAsDefault || false
303
+ });
310
304
  return this.transformPayee(response);
311
305
  } catch (error) {
312
306
  throw new Error(`Failed to create payee: ${error.message}`);