@hackthedev/dsync-pay 1.0.5 → 1.0.6

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.
Files changed (2) hide show
  1. package/index.mjs +60 -9
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -30,6 +30,7 @@ export default class dSyncPay {
30
30
  this.domain = domain.endsWith('/') ? domain.slice(0, -1) : domain;
31
31
  this.basePath = basePath;
32
32
  this.redirects = redirects;
33
+ this.metadataCache = new Map();
33
34
 
34
35
  this.callbacks = {
35
36
  onPaymentCreated,
@@ -251,6 +252,14 @@ export default class dSyncPay {
251
252
  rawResponse: response
252
253
  };
253
254
 
255
+ // save metadata in cache
256
+ this.parent.metadataCache.set(response.id, metadata);
257
+
258
+ // auto cleanup after 1 hour if never verified
259
+ setTimeout(() => {
260
+ this.parent.metadataCache.delete(response.id);
261
+ }, 60 * 60 * 1000);
262
+
254
263
  this.parent.emit('onPaymentCreated', result);
255
264
  return result;
256
265
  } catch (error) {
@@ -306,6 +315,9 @@ export default class dSyncPay {
306
315
  amount = parseFloat(purchaseUnit.amount.value);
307
316
  }
308
317
 
318
+ // get metadata from cache
319
+ const metadata = this.parent.metadataCache.get(orderId) || {};
320
+
309
321
  const result = {
310
322
  provider: 'paypal',
311
323
  type: 'order',
@@ -314,6 +326,7 @@ export default class dSyncPay {
314
326
  orderId: orderResponse.id,
315
327
  amount: amount,
316
328
  currency: purchaseUnit.payments?.captures?.[0]?.amount?.currency_code || purchaseUnit.amount?.currency_code || 'EUR',
329
+ metadata,
317
330
  rawResponse: orderResponse
318
331
  };
319
332
 
@@ -324,6 +337,10 @@ export default class dSyncPay {
324
337
  } else {
325
338
  await this.parent.emit('onPaymentFailed', result);
326
339
  }
340
+
341
+ // cleanup cache after verify
342
+ this.parent.metadataCache.delete(orderId);
343
+
327
344
  return result;
328
345
  } catch (error) {
329
346
  this.parent.emit('onError', {
@@ -332,6 +349,10 @@ export default class dSyncPay {
332
349
  orderId,
333
350
  error: error.response || error.message
334
351
  });
352
+
353
+ // cleanup cache after verify
354
+ this.parent.metadataCache.delete(orderId);
355
+
335
356
  throw error;
336
357
  }
337
358
  }
@@ -480,6 +501,14 @@ export default class dSyncPay {
480
501
  rawResponse: response
481
502
  };
482
503
 
504
+ // save metadata in cache
505
+ this.parent.metadataCache.set(response.id, metadata);
506
+
507
+ // auto cleanup after 1 hour if never verified
508
+ setTimeout(() => {
509
+ this.parent.metadataCache.delete(response.id);
510
+ }, 60 * 60 * 1000);
511
+
483
512
  this.parent.emit('onSubscriptionCreated', result);
484
513
  return result;
485
514
  } catch (error) {
@@ -505,6 +534,9 @@ export default class dSyncPay {
505
534
  }
506
535
  );
507
536
 
537
+ // get metadata from cache
538
+ const metadata = this.parent.metadataCache.get(subscriptionId) || {};
539
+
508
540
  const result = {
509
541
  provider: 'paypal',
510
542
  type: 'subscription',
@@ -512,15 +544,19 @@ export default class dSyncPay {
512
544
  subscriptionId: response.id,
513
545
  planId: response.plan_id,
514
546
  customId: response.custom_id,
547
+ metadata,
515
548
  rawResponse: response
516
549
  };
517
550
 
518
551
  if (response.status === 'ACTIVE') {
519
- this.parent.emit('onSubscriptionActivated', result);
552
+ await this.parent.emit('onSubscriptionActivated', result);
520
553
  } else if (response.status === 'CANCELLED') {
521
- this.parent.emit('onSubscriptionCancelled', result);
554
+ await this.parent.emit('onSubscriptionCancelled', result);
522
555
  }
523
556
 
557
+ // cleanup cache after verify
558
+ this.parent.metadataCache.delete(subscriptionId);
559
+
524
560
  return result;
525
561
  } catch (error) {
526
562
  this.parent.emit('onError', {
@@ -529,6 +565,10 @@ export default class dSyncPay {
529
565
  subscriptionId,
530
566
  error: error.response || error.message
531
567
  });
568
+
569
+ // cleanup cache after verify
570
+ this.parent.metadataCache.delete(subscriptionId);
571
+
532
572
  throw error;
533
573
  }
534
574
  }
@@ -549,15 +589,22 @@ export default class dSyncPay {
549
589
  }
550
590
  );
551
591
 
592
+ // get metadata from cache
593
+ const metadata = this.parent.metadataCache.get(subscriptionId) || {};
594
+
552
595
  const result = {
553
596
  provider: 'paypal',
554
597
  type: 'subscription',
555
598
  subscriptionId,
556
599
  status: 'CANCELLED',
557
- reason
600
+ reason,
601
+ metadata
558
602
  };
559
603
 
560
- this.parent.emit('onSubscriptionCancelled', result);
604
+ // cleanup cache
605
+ this.parent.metadataCache.delete(subscriptionId);
606
+
607
+ await this.parent.emit('onSubscriptionCancelled', result);
561
608
  return result;
562
609
  } catch (error) {
563
610
  this.parent.emit('onError', {
@@ -566,6 +613,10 @@ export default class dSyncPay {
566
613
  subscriptionId,
567
614
  error: error.response || error.message
568
615
  });
616
+
617
+ // cleanup cache on error too
618
+ this.parent.metadataCache.delete(subscriptionId);
619
+
569
620
  throw error;
570
621
  }
571
622
  }
@@ -675,11 +726,11 @@ export default class dSyncPay {
675
726
  };
676
727
 
677
728
  if (latestStatus === 'COMPLETED') {
678
- this.parent.emit('onPaymentCompleted', result);
729
+ await this.parent.emit('onPaymentCompleted', result);
679
730
  } else if (latestStatus === 'CANCELED') {
680
- this.parent.emit('onPaymentCancelled', result);
731
+ await this.parent.emit('onPaymentCancelled', result);
681
732
  } else if (latestStatus === 'EXPIRED' || latestStatus === 'UNRESOLVED') {
682
- this.parent.emit('onPaymentFailed', result);
733
+ await this.parent.emit('onPaymentFailed', result);
683
734
  }
684
735
 
685
736
  return result;
@@ -724,7 +775,7 @@ export default class dSyncPay {
724
775
  });
725
776
  }
726
777
 
727
- registerRoutes(basePath = '/payments') {
778
+ registerRoutes(basePath = '/payments') {
728
779
  if (this.paypal) {
729
780
  this.app.get(`${basePath}/paypal/verify`, async (req, res) => {
730
781
  try {
@@ -818,4 +869,4 @@ export default class dSyncPay {
818
869
 
819
870
  return this;
820
871
  }
821
- }
872
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackthedev/dsync-pay",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "As another part of the dSync library family this library is responsible for payment handling currently supporting PayPal and Coinbase Crypto payments. Its works independently and without any percentage cuts by using your own API keys.",
5
5
  "homepage": "https://github.com/NETWORK-Z-Dev/dSyncPay#readme",
6
6
  "bugs": {