@blocklet/payment-react 1.18.2 → 1.18.4

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 (43) hide show
  1. package/es/checkout/donate.d.ts +5 -3
  2. package/es/checkout/donate.js +109 -36
  3. package/es/contexts/donate.d.ts +41 -0
  4. package/es/contexts/donate.js +164 -0
  5. package/es/contexts/payment.js +3 -16
  6. package/es/index.d.ts +2 -0
  7. package/es/index.js +2 -0
  8. package/es/libs/cache.d.ts +14 -0
  9. package/es/libs/cache.js +23 -0
  10. package/es/libs/cached-request.d.ts +17 -0
  11. package/es/libs/cached-request.js +79 -0
  12. package/es/libs/util.d.ts +2 -0
  13. package/es/libs/util.js +13 -0
  14. package/es/locales/en.js +8 -1
  15. package/es/locales/zh.js +8 -1
  16. package/es/payment/skeleton/donation.js +1 -1
  17. package/lib/checkout/donate.d.ts +5 -3
  18. package/lib/checkout/donate.js +107 -36
  19. package/lib/contexts/donate.d.ts +41 -0
  20. package/lib/contexts/donate.js +181 -0
  21. package/lib/contexts/payment.js +7 -22
  22. package/lib/index.d.ts +2 -0
  23. package/lib/index.js +24 -0
  24. package/lib/libs/cache.d.ts +14 -0
  25. package/lib/libs/cache.js +29 -0
  26. package/lib/libs/cached-request.d.ts +17 -0
  27. package/lib/libs/cached-request.js +90 -0
  28. package/lib/libs/util.d.ts +2 -0
  29. package/lib/libs/util.js +12 -0
  30. package/lib/locales/en.js +8 -1
  31. package/lib/locales/zh.js +8 -1
  32. package/lib/payment/skeleton/donation.js +1 -1
  33. package/package.json +8 -8
  34. package/src/checkout/donate.tsx +135 -45
  35. package/src/contexts/donate.tsx +226 -0
  36. package/src/contexts/payment.tsx +5 -20
  37. package/src/index.ts +2 -0
  38. package/src/libs/cache.ts +33 -0
  39. package/src/libs/cached-request.ts +103 -0
  40. package/src/libs/util.ts +15 -0
  41. package/src/locales/en.tsx +7 -0
  42. package/src/locales/zh.tsx +7 -0
  43. package/src/payment/skeleton/donation.tsx +1 -1
@@ -0,0 +1,103 @@
1
+ import { globalCache } from './cache';
2
+
3
+ type CacheStrategy = 'session' | 'local' | 'memory';
4
+
5
+ interface CacheOptions {
6
+ strategy?: CacheStrategy;
7
+ ttl?: number; // 过期时间(毫秒)
8
+ }
9
+
10
+ export class CachedRequest {
11
+ private cacheKey: string;
12
+ private fetchData: () => Promise<any>;
13
+ private options: CacheOptions;
14
+
15
+ constructor(cacheKey: string, fetchData: () => Promise<any>, options: CacheOptions = {}) {
16
+ this.cacheKey = cacheKey;
17
+ this.fetchData = fetchData;
18
+ this.options = {
19
+ strategy: 'session',
20
+ ttl: 0,
21
+ ...options,
22
+ };
23
+ }
24
+
25
+ private getCache(): any {
26
+ const { strategy } = this.options;
27
+
28
+ switch (strategy) {
29
+ case 'local':
30
+ return localStorage;
31
+ case 'memory':
32
+ return globalCache;
33
+ case 'session':
34
+ default:
35
+ return sessionStorage;
36
+ }
37
+ }
38
+
39
+ private getCachedData() {
40
+ const cache = this.getCache();
41
+ const data = this.options.strategy === 'memory' ? cache.get(this.cacheKey) : cache.getItem(this.cacheKey);
42
+
43
+ if (!data) return null;
44
+
45
+ const parsed = JSON.parse(data);
46
+
47
+ // 检查是否过期
48
+ if (this.options.ttl && parsed.timestamp) {
49
+ const isExpired = Date.now() - parsed.timestamp > this.options.ttl;
50
+ if (isExpired) {
51
+ this.clearCache();
52
+ return null;
53
+ }
54
+ }
55
+
56
+ return parsed.data;
57
+ }
58
+
59
+ private setCachedData(data: any) {
60
+ const cache = this.getCache();
61
+ const value = JSON.stringify({
62
+ data,
63
+ timestamp: Date.now(),
64
+ });
65
+
66
+ if (this.options.strategy === 'memory') {
67
+ cache.set(this.cacheKey, value);
68
+ } else {
69
+ cache.setItem(this.cacheKey, value);
70
+ }
71
+ }
72
+
73
+ private clearCache() {
74
+ const cache = this.getCache();
75
+ if (this.options.strategy === 'memory') {
76
+ cache.delete(this.cacheKey);
77
+ } else {
78
+ cache.removeItem(this.cacheKey);
79
+ }
80
+ }
81
+
82
+ fetch(forceRefresh = false) {
83
+ const cachedData = !forceRefresh && this.getCachedData();
84
+ if (cachedData) return Promise.resolve(cachedData);
85
+
86
+ const globalItem = globalCache.get(this.cacheKey);
87
+ if (globalItem?.promise) {
88
+ return globalItem.promise;
89
+ }
90
+
91
+ const newPromise = this.fetchData()
92
+ .then(({ data }: any) => {
93
+ this.setCachedData(data);
94
+ return data;
95
+ })
96
+ .finally(() => {
97
+ globalCache.delete(this.cacheKey);
98
+ });
99
+
100
+ globalCache.set(this.cacheKey, { promise: newPromise });
101
+ return newPromise;
102
+ }
103
+ }
package/src/libs/util.ts CHANGED
@@ -1171,3 +1171,18 @@ export function getInvoiceDescriptionAndReason(invoice: TInvoiceExpanded, locale
1171
1171
  type: invoiceType,
1172
1172
  };
1173
1173
  }
1174
+
1175
+ export function getPaymentKitComponent() {
1176
+ const paymentKit = window.blocklet?.componentMountPoints?.find((c: any) => c.did === PAYMENT_KIT_DID);
1177
+ return paymentKit || null;
1178
+ }
1179
+
1180
+ export function openDonationSettings(openInNewTab: boolean = false) {
1181
+ const paymentKit = getPaymentKitComponent();
1182
+ if (paymentKit) {
1183
+ window.open(
1184
+ `${window.location.origin}${paymentKit.mountPoint === '/' ? '' : paymentKit.mountPoint}/integrations/donations`,
1185
+ openInNewTab ? '_blank' : '_self'
1186
+ );
1187
+ }
1188
+ }
@@ -138,6 +138,13 @@ export default flat({
138
138
  multiple: 'Tips will be distributed to {count} beneficiaries',
139
139
  view: 'Click to view details',
140
140
  },
141
+ inactive: 'Donation feature is not enabled',
142
+ enable: 'Enable Donation',
143
+ enableSuccess: 'Enable Success',
144
+ configPrompt: 'Donation feature is enabled, you can configure donation options in Payment Kit',
145
+ configNow: 'Configure Now',
146
+ later: 'Configure Later',
147
+ configTip: 'Configure donation settings in Payment Kit',
141
148
  },
142
149
  cardPay: '{action} with card',
143
150
  empty: 'No thing to pay',
@@ -137,6 +137,13 @@ export default flat({
137
137
  multiple: '打赏将按比例分配给 {count} 位受益人',
138
138
  view: '点击查看详情',
139
139
  },
140
+ inactive: '打赏功能暂未开启',
141
+ enable: '启用打赏',
142
+ enableSuccess: '启用成功',
143
+ configPrompt: '打赏功能已启用,您可以前往 Payment Kit 进一步配置打赏选项',
144
+ configNow: '立即配置',
145
+ later: '稍后配置',
146
+ configTip: '前往 Payment Kit 配置打赏选项',
140
147
  },
141
148
  cardPay: '使用卡片{action}',
142
149
  empty: '没有可支付的项目',
@@ -5,7 +5,7 @@ export default function DonationSkeleton() {
5
5
  <Fade in>
6
6
  <Stack direction="column">
7
7
  <Skeleton variant="text" sx={{ fontSize: '2rem', width: '40%' }} />
8
- <Skeleton sx={{ mt: 2 }} variant="rounded" height={80} />
8
+ <Skeleton sx={{ mt: 2 }} variant="rounded" height={40} />
9
9
  <Divider
10
10
  sx={{
11
11
  mt: {