payment-kit 1.18.10 → 1.18.11
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/api/src/libs/util.ts +52 -2
- package/api/src/routes/checkout-sessions.ts +18 -1
- package/api/tests/libs/util.spec.ts +279 -1
- package/blocklet.yml +2 -2
- package/package.json +4 -4
package/api/src/libs/util.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { joinURL, withQuery, withTrailingSlash } from 'ufo';
|
|
|
11
11
|
import axios from 'axios';
|
|
12
12
|
import dayjs from './dayjs';
|
|
13
13
|
import { blocklet, wallet } from './auth';
|
|
14
|
-
import type { Subscription } from '../store/models';
|
|
14
|
+
import type { PaymentMethod, Subscription } from '../store/models';
|
|
15
15
|
import logger from './logger';
|
|
16
16
|
|
|
17
17
|
export const OCAP_PAYMENT_TX_TYPE = 'fg:t:transfer_v2';
|
|
@@ -73,7 +73,7 @@ export const STRIPE_EVENTS: any[] = [
|
|
|
73
73
|
'refund.updated',
|
|
74
74
|
];
|
|
75
75
|
|
|
76
|
-
const api = axios.create({
|
|
76
|
+
export const api = axios.create({
|
|
77
77
|
timeout: 10 * 1000,
|
|
78
78
|
});
|
|
79
79
|
|
|
@@ -458,3 +458,53 @@ export function getAdminBillingSubscriptionUrl({
|
|
|
458
458
|
export function getCustomerIndexUrl({ locale, userDid }: { locale: string; userDid: string }) {
|
|
459
459
|
return getUrl(withQuery('customer', { locale, ...getConnectQueryParam({ userDid }) }));
|
|
460
460
|
}
|
|
461
|
+
|
|
462
|
+
// Check if user is in blocklist
|
|
463
|
+
export async function isUserInBlocklist(did: string, paymentMethod: PaymentMethod): Promise<boolean> {
|
|
464
|
+
try {
|
|
465
|
+
// Get API host from payment method settings
|
|
466
|
+
// @ts-ignore
|
|
467
|
+
const apiHost = paymentMethod?.settings?.[paymentMethod.type]?.api_host;
|
|
468
|
+
if (!apiHost) {
|
|
469
|
+
logger.warn('No API host found in payment method settings', { paymentMethodId: paymentMethod.id });
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Get user information
|
|
474
|
+
const { user }: { user: any } = await blocklet.getUser(did);
|
|
475
|
+
if (!user) {
|
|
476
|
+
logger.error('User not found', { did });
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
// Get all wallet DIDs associated with the user
|
|
480
|
+
const connectedAccounts = user.connectedAccounts || user.extraConfigs?.connectedAccounts || [];
|
|
481
|
+
const walletDids = connectedAccounts
|
|
482
|
+
.filter((account: any) => account.provider === 'wallet')
|
|
483
|
+
.map((account: any) => account.did);
|
|
484
|
+
|
|
485
|
+
if (!walletDids.length) {
|
|
486
|
+
logger.info('No wallet DIDs found for user', { did });
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const blockedUrl = joinURL(apiHost, '/blocked');
|
|
491
|
+
const blockedResponse = await api.get(blockedUrl);
|
|
492
|
+
const blockedDids = blockedResponse.data || [];
|
|
493
|
+
|
|
494
|
+
if (!Array.isArray(blockedDids) || blockedDids.length === 0) {
|
|
495
|
+
return false;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Check if any of the user's DIDs are in the blocklist
|
|
499
|
+
const isBlocked = walletDids.some((walletDid: string) => blockedDids.includes(walletDid));
|
|
500
|
+
|
|
501
|
+
if (isBlocked) {
|
|
502
|
+
logger.info('User is in blocklist', { did, walletDids, blockedDids });
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
return isBlocked;
|
|
506
|
+
} catch (error) {
|
|
507
|
+
logger.error('Error checking blocklist', { error: error.message, did });
|
|
508
|
+
return false; // Default to allowing payment on error
|
|
509
|
+
}
|
|
510
|
+
}
|
|
@@ -48,7 +48,13 @@ import {
|
|
|
48
48
|
getSubscriptionCreateSetup,
|
|
49
49
|
getSubscriptionTrialSetup,
|
|
50
50
|
} from '../libs/subscription';
|
|
51
|
-
import {
|
|
51
|
+
import {
|
|
52
|
+
CHECKOUT_SESSION_TTL,
|
|
53
|
+
formatAmountPrecisionLimit,
|
|
54
|
+
formatMetadata,
|
|
55
|
+
getDataObjectFromQuery,
|
|
56
|
+
isUserInBlocklist,
|
|
57
|
+
} from '../libs/util';
|
|
52
58
|
import { invoiceQueue } from '../queues/invoice';
|
|
53
59
|
import { paymentQueue } from '../queues/payment';
|
|
54
60
|
import {
|
|
@@ -70,6 +76,7 @@ import { SetupIntent } from '../store/models/setup-intent';
|
|
|
70
76
|
import { Subscription } from '../store/models/subscription';
|
|
71
77
|
import { SubscriptionItem } from '../store/models/subscription-item';
|
|
72
78
|
import { ensureInvoiceForCheckout } from './connect/shared';
|
|
79
|
+
import { CHARGE_SUPPORTED_CHAIN_TYPES } from '../libs/constants';
|
|
73
80
|
|
|
74
81
|
const router = Router();
|
|
75
82
|
|
|
@@ -692,6 +699,16 @@ router.put('/:id/submit', user, ensureCheckoutSessionOpen, async (req, res) => {
|
|
|
692
699
|
});
|
|
693
700
|
}
|
|
694
701
|
|
|
702
|
+
// check if user in block list
|
|
703
|
+
if (CHARGE_SUPPORTED_CHAIN_TYPES.includes(paymentMethod.type)) {
|
|
704
|
+
const inBlock = await isUserInBlocklist(req.user.did, paymentMethod);
|
|
705
|
+
if (inBlock) {
|
|
706
|
+
return res
|
|
707
|
+
.status(403)
|
|
708
|
+
.json({ code: 'PAYMENT_RESTRICTED', error: 'Unable to process your purchase at this time' });
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
695
712
|
await checkoutSession.update({ customer_id: customer.id, customer_did: req.user.did });
|
|
696
713
|
|
|
697
714
|
// payment intent is only created when checkout session is in payment mode
|
|
@@ -11,8 +11,13 @@ import {
|
|
|
11
11
|
getNextRetry,
|
|
12
12
|
tryWithTimeout,
|
|
13
13
|
getSubscriptionNotificationCustomActions,
|
|
14
|
+
isUserInBlocklist,
|
|
15
|
+
api,
|
|
14
16
|
} from '../../src/libs/util';
|
|
15
|
-
import type { Subscription } from '../../src/store/models';
|
|
17
|
+
import type { Subscription, PaymentMethod } from '../../src/store/models';
|
|
18
|
+
|
|
19
|
+
import { blocklet } from '../../src/libs/auth';
|
|
20
|
+
import logger from '../../src/libs/logger';
|
|
16
21
|
|
|
17
22
|
describe('createIdGenerator', () => {
|
|
18
23
|
it('should return a function that generates an ID with the specified prefix and size', () => {
|
|
@@ -375,3 +380,276 @@ describe('getSubscriptionNotificationCustomActions', () => {
|
|
|
375
380
|
});
|
|
376
381
|
});
|
|
377
382
|
});
|
|
383
|
+
|
|
384
|
+
describe('isUserInBlocklist', () => {
|
|
385
|
+
let mockAxios: any;
|
|
386
|
+
let mockBlocklet: any;
|
|
387
|
+
let mockLogger: any;
|
|
388
|
+
|
|
389
|
+
beforeEach(() => {
|
|
390
|
+
mockAxios = jest.spyOn(api, 'get').mockImplementation();
|
|
391
|
+
|
|
392
|
+
mockBlocklet = jest.spyOn(blocklet, 'getUser').mockImplementation();
|
|
393
|
+
|
|
394
|
+
mockLogger = {
|
|
395
|
+
warn: jest.spyOn(logger, 'warn').mockImplementation(),
|
|
396
|
+
info: jest.spyOn(logger, 'info').mockImplementation(),
|
|
397
|
+
error: jest.spyOn(logger, 'error').mockImplementation(),
|
|
398
|
+
};
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
afterEach(() => {
|
|
402
|
+
jest.clearAllMocks();
|
|
403
|
+
jest.resetAllMocks();
|
|
404
|
+
jest.restoreAllMocks();
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('should return true when user DID is in blocklist', async () => {
|
|
408
|
+
// Setup mocks
|
|
409
|
+
const userDid = 'did:user:123';
|
|
410
|
+
const paymentMethod = {
|
|
411
|
+
id: 'pm_123',
|
|
412
|
+
type: 'arcblock',
|
|
413
|
+
settings: {
|
|
414
|
+
arcblock: {
|
|
415
|
+
api_host: 'https://api.example.com',
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
} as PaymentMethod;
|
|
419
|
+
|
|
420
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:user:123', 'did:user:456'] });
|
|
421
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
422
|
+
user: {
|
|
423
|
+
did: userDid,
|
|
424
|
+
connectedAccounts: [{ provider: 'wallet', did: userDid }],
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
// Execute
|
|
429
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
430
|
+
|
|
431
|
+
// Verify
|
|
432
|
+
expect(result).toBe(true);
|
|
433
|
+
expect(mockAxios).toHaveBeenCalledWith('https://api.example.com/blocked');
|
|
434
|
+
expect(mockBlocklet).toHaveBeenCalledWith(userDid);
|
|
435
|
+
expect(mockLogger.info).toHaveBeenCalled();
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('should return true when user wallet DID is in blocklist', async () => {
|
|
439
|
+
// Setup mocks
|
|
440
|
+
const userDid = 'did:user:123';
|
|
441
|
+
const walletDid = 'did:wallet:456';
|
|
442
|
+
const paymentMethod = {
|
|
443
|
+
id: 'pm_123',
|
|
444
|
+
type: 'arcblock',
|
|
445
|
+
settings: {
|
|
446
|
+
arcblock: {
|
|
447
|
+
api_host: 'https://api.example.com',
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
} as PaymentMethod;
|
|
451
|
+
|
|
452
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:wallet:456', 'did:user:789'] });
|
|
453
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
454
|
+
user: {
|
|
455
|
+
did: userDid,
|
|
456
|
+
connectedAccounts: [{ provider: 'wallet', did: walletDid }],
|
|
457
|
+
},
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// Execute
|
|
461
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
462
|
+
|
|
463
|
+
// Verify
|
|
464
|
+
expect(result).toBe(true);
|
|
465
|
+
expect(mockAxios).toHaveBeenCalledWith('https://api.example.com/blocked');
|
|
466
|
+
expect(mockBlocklet).toHaveBeenCalledWith(userDid);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('should return false when user is not in blocklist', async () => {
|
|
470
|
+
// Setup mocks
|
|
471
|
+
const userDid = 'did:user:123';
|
|
472
|
+
const walletDid = 'did:wallet:456';
|
|
473
|
+
const paymentMethod = {
|
|
474
|
+
id: 'pm_123',
|
|
475
|
+
type: 'arcblock',
|
|
476
|
+
settings: {
|
|
477
|
+
arcblock: {
|
|
478
|
+
api_host: 'https://api.example.com',
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
} as PaymentMethod;
|
|
482
|
+
|
|
483
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:user:789', 'did:wallet:789'] });
|
|
484
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
485
|
+
user: {
|
|
486
|
+
did: userDid,
|
|
487
|
+
connectedAccounts: [{ provider: 'wallet', did: walletDid }],
|
|
488
|
+
},
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// Execute
|
|
492
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
493
|
+
|
|
494
|
+
// Verify
|
|
495
|
+
expect(result).toBe(false);
|
|
496
|
+
expect(mockAxios).toHaveBeenCalledWith('https://api.example.com/blocked');
|
|
497
|
+
expect(mockBlocklet).toHaveBeenCalledWith(userDid);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it('should handle API host with trailing slash', async () => {
|
|
501
|
+
// Setup mocks
|
|
502
|
+
const userDid = 'did:user:123';
|
|
503
|
+
const paymentMethod = {
|
|
504
|
+
id: 'pm_123',
|
|
505
|
+
type: 'arcblock',
|
|
506
|
+
settings: {
|
|
507
|
+
arcblock: {
|
|
508
|
+
api_host: 'https://api.example.com/',
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
} as PaymentMethod;
|
|
512
|
+
|
|
513
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:user:789'] });
|
|
514
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
515
|
+
user: {
|
|
516
|
+
did: userDid,
|
|
517
|
+
connectedAccounts: [{ provider: 'wallet', did: userDid }],
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
// Execute
|
|
522
|
+
await isUserInBlocklist(userDid, paymentMethod);
|
|
523
|
+
|
|
524
|
+
// Verify - 确保调用了正确的 URL
|
|
525
|
+
expect(mockAxios).toHaveBeenCalledWith('https://api.example.com/blocked');
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it('should return false when API host is not found', async () => {
|
|
529
|
+
// Setup mocks
|
|
530
|
+
const userDid = 'did:user:123';
|
|
531
|
+
const paymentMethod = {
|
|
532
|
+
id: 'pm_123',
|
|
533
|
+
type: 'arcblock',
|
|
534
|
+
settings: {},
|
|
535
|
+
} as PaymentMethod;
|
|
536
|
+
|
|
537
|
+
// Execute
|
|
538
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
539
|
+
|
|
540
|
+
// Verify
|
|
541
|
+
expect(result).toBe(false);
|
|
542
|
+
expect(mockLogger.warn).toHaveBeenCalled();
|
|
543
|
+
expect(mockAxios).not.toHaveBeenCalled();
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it('should return false when blocklist is empty', async () => {
|
|
547
|
+
// Setup mocks
|
|
548
|
+
const userDid = 'did:user:123';
|
|
549
|
+
const paymentMethod = {
|
|
550
|
+
id: 'pm_123',
|
|
551
|
+
type: 'arcblock',
|
|
552
|
+
settings: {
|
|
553
|
+
arcblock: {
|
|
554
|
+
api_host: 'https://api.example.com',
|
|
555
|
+
},
|
|
556
|
+
},
|
|
557
|
+
} as PaymentMethod;
|
|
558
|
+
|
|
559
|
+
// 先模拟用户信息获取
|
|
560
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
561
|
+
user: {
|
|
562
|
+
did: userDid,
|
|
563
|
+
connectedAccounts: [{ provider: 'wallet', did: userDid }],
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// 再模拟空黑名单
|
|
568
|
+
mockAxios.mockResolvedValueOnce({ data: [] });
|
|
569
|
+
|
|
570
|
+
// Execute
|
|
571
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
572
|
+
|
|
573
|
+
// Verify
|
|
574
|
+
expect(result).toBe(false);
|
|
575
|
+
expect(mockBlocklet).toHaveBeenCalled(); // 现在应该调用了用户信息获取
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
it('should return false when user is not found', async () => {
|
|
579
|
+
// Setup mocks
|
|
580
|
+
const userDid = 'did:user:123';
|
|
581
|
+
const paymentMethod = {
|
|
582
|
+
id: 'pm_123',
|
|
583
|
+
type: 'arcblock',
|
|
584
|
+
settings: {
|
|
585
|
+
arcblock: {
|
|
586
|
+
api_host: 'https://api.example.com',
|
|
587
|
+
},
|
|
588
|
+
},
|
|
589
|
+
} as PaymentMethod;
|
|
590
|
+
|
|
591
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:user:123'] });
|
|
592
|
+
mockBlocklet.mockResolvedValueOnce({ user: null });
|
|
593
|
+
|
|
594
|
+
// Execute
|
|
595
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
596
|
+
|
|
597
|
+
// Verify
|
|
598
|
+
expect(result).toBe(false);
|
|
599
|
+
expect(mockLogger.error).toHaveBeenCalled();
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
it('should return false when API request fails', async () => {
|
|
603
|
+
// Setup mocks
|
|
604
|
+
const userDid = 'did:user:123';
|
|
605
|
+
const paymentMethod = {
|
|
606
|
+
id: 'pm_123',
|
|
607
|
+
type: 'arcblock',
|
|
608
|
+
settings: {
|
|
609
|
+
arcblock: {
|
|
610
|
+
api_host: 'https://api.example.com',
|
|
611
|
+
},
|
|
612
|
+
},
|
|
613
|
+
} as PaymentMethod;
|
|
614
|
+
|
|
615
|
+
mockAxios.mockRejectedValueOnce(new Error('Network error'));
|
|
616
|
+
|
|
617
|
+
// Execute
|
|
618
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
619
|
+
|
|
620
|
+
// Verify
|
|
621
|
+
expect(result).toBe(false);
|
|
622
|
+
expect(mockLogger.error).toHaveBeenCalled();
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it('should handle extraConfigs for connected accounts', async () => {
|
|
626
|
+
// Setup mocks
|
|
627
|
+
const userDid = 'did:user:123';
|
|
628
|
+
const walletDid = 'did:wallet:456';
|
|
629
|
+
const paymentMethod = {
|
|
630
|
+
id: 'pm_123',
|
|
631
|
+
type: 'arcblock',
|
|
632
|
+
settings: {
|
|
633
|
+
arcblock: {
|
|
634
|
+
api_host: 'https://api.example.com',
|
|
635
|
+
},
|
|
636
|
+
},
|
|
637
|
+
} as PaymentMethod;
|
|
638
|
+
|
|
639
|
+
mockAxios.mockResolvedValueOnce({ data: ['did:wallet:456'] });
|
|
640
|
+
mockBlocklet.mockResolvedValueOnce({
|
|
641
|
+
user: {
|
|
642
|
+
did: userDid,
|
|
643
|
+
extraConfigs: {
|
|
644
|
+
connectedAccounts: [{ provider: 'wallet', did: walletDid }],
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
// Execute
|
|
650
|
+
const result = await isUserInBlocklist(userDid, paymentMethod);
|
|
651
|
+
|
|
652
|
+
// Verify
|
|
653
|
+
expect(result).toBe(true);
|
|
654
|
+
});
|
|
655
|
+
});
|
package/blocklet.yml
CHANGED
|
@@ -14,7 +14,7 @@ repository:
|
|
|
14
14
|
type: git
|
|
15
15
|
url: git+https://github.com/blocklet/payment-kit.git
|
|
16
16
|
specVersion: 1.2.8
|
|
17
|
-
version: 1.18.
|
|
17
|
+
version: 1.18.11
|
|
18
18
|
logo: logo.png
|
|
19
19
|
files:
|
|
20
20
|
- dist
|
|
@@ -165,4 +165,4 @@ events:
|
|
|
165
165
|
- type: invoice.paid
|
|
166
166
|
description: Invoice has been fully paid and settled
|
|
167
167
|
- type: refund.succeeded
|
|
168
|
-
description: Refund has been successfully processed and completed
|
|
168
|
+
description: Refund has been successfully processed and completed
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payment-kit",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.11",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"dev": "blocklet dev --open",
|
|
6
6
|
"eject": "vite eject",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@arcblock/validator": "^1.19.10",
|
|
54
54
|
"@blocklet/js-sdk": "^1.16.39",
|
|
55
55
|
"@blocklet/logger": "^1.16.39",
|
|
56
|
-
"@blocklet/payment-react": "1.18.
|
|
56
|
+
"@blocklet/payment-react": "1.18.11",
|
|
57
57
|
"@blocklet/sdk": "^1.16.39",
|
|
58
58
|
"@blocklet/ui-react": "^2.11.48",
|
|
59
59
|
"@blocklet/uploader": "^0.1.70",
|
|
@@ -121,7 +121,7 @@
|
|
|
121
121
|
"devDependencies": {
|
|
122
122
|
"@abtnode/types": "^1.16.39",
|
|
123
123
|
"@arcblock/eslint-config-ts": "^0.3.3",
|
|
124
|
-
"@blocklet/payment-types": "1.18.
|
|
124
|
+
"@blocklet/payment-types": "1.18.11",
|
|
125
125
|
"@types/cookie-parser": "^1.4.7",
|
|
126
126
|
"@types/cors": "^2.8.17",
|
|
127
127
|
"@types/debug": "^4.1.12",
|
|
@@ -167,5 +167,5 @@
|
|
|
167
167
|
"parser": "typescript"
|
|
168
168
|
}
|
|
169
169
|
},
|
|
170
|
-
"gitHead": "
|
|
170
|
+
"gitHead": "370cb327400359ab5972ef60316ccd7fc86c174e"
|
|
171
171
|
}
|