@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
@@ -13,6 +13,7 @@ var _ahooks = require("ahooks");
13
13
  var _react = require("react");
14
14
  var _api = _interopRequireDefault(require("../libs/api"));
15
15
  var _util = require("../libs/util");
16
+ var _cachedRequest = require("../libs/cached-request");
16
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18
  const PaymentContext = exports.PaymentContext = (0, _react.createContext)({
18
19
  api: _api.default
@@ -22,31 +23,15 @@ const {
22
23
  Consumer
23
24
  } = PaymentContext;
24
25
  exports.SettingsConsumer = Consumer;
25
- let settingsPromise = null;
26
26
  const getSettings = (forceRefresh = false) => {
27
27
  const livemode = localStorage.getItem("livemode") !== "false";
28
28
  const cacheKey = `payment-settings-${window.location.pathname}-${livemode}`;
29
- const cachedData = sessionStorage.getItem(cacheKey);
30
- if (cachedData && !forceRefresh) {
31
- return JSON.parse(cachedData);
32
- }
33
- if (!settingsPromise) {
34
- settingsPromise = _api.default.get("/api/settings", {
35
- params: {
36
- livemode
37
- }
38
- }).then(({
39
- data
40
- }) => {
41
- sessionStorage.setItem(cacheKey, JSON.stringify(data));
42
- return data;
43
- }).catch(error => {
44
- throw error;
45
- }).finally(() => {
46
- settingsPromise = null;
47
- });
48
- }
49
- return settingsPromise;
29
+ const cachedRequest = new _cachedRequest.CachedRequest(cacheKey, () => _api.default.get("/api/settings", {
30
+ params: {
31
+ livemode
32
+ }
33
+ }));
34
+ return cachedRequest.fetch(forceRefresh);
50
35
  };
51
36
  const getCurrency = (currencyId, methods) => {
52
37
  const currencies = methods.reduce((acc, x) => acc.concat(x.payment_currencies), []);
package/lib/index.d.ts CHANGED
@@ -34,7 +34,9 @@ export { PaymentThemeProvider } from './theme';
34
34
  export * from './libs/util';
35
35
  export * from './libs/connect';
36
36
  export * from './libs/phone-validator';
37
+ export * from './libs/cached-request';
37
38
  export * from './contexts/payment';
39
+ export * from './contexts/donate';
38
40
  export * from './hooks/subscription';
39
41
  export * from './hooks/mobile';
40
42
  export * from './hooks/table';
package/lib/index.js CHANGED
@@ -319,6 +319,18 @@ Object.keys(_phoneValidator).forEach(function (key) {
319
319
  }
320
320
  });
321
321
  });
322
+ var _cachedRequest = require("./libs/cached-request");
323
+ Object.keys(_cachedRequest).forEach(function (key) {
324
+ if (key === "default" || key === "__esModule") return;
325
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
326
+ if (key in exports && exports[key] === _cachedRequest[key]) return;
327
+ Object.defineProperty(exports, key, {
328
+ enumerable: true,
329
+ get: function () {
330
+ return _cachedRequest[key];
331
+ }
332
+ });
333
+ });
322
334
  var _payment = require("./contexts/payment");
323
335
  Object.keys(_payment).forEach(function (key) {
324
336
  if (key === "default" || key === "__esModule") return;
@@ -331,6 +343,18 @@ Object.keys(_payment).forEach(function (key) {
331
343
  }
332
344
  });
333
345
  });
346
+ var _donate2 = require("./contexts/donate");
347
+ Object.keys(_donate2).forEach(function (key) {
348
+ if (key === "default" || key === "__esModule") return;
349
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
350
+ if (key in exports && exports[key] === _donate2[key]) return;
351
+ Object.defineProperty(exports, key, {
352
+ enumerable: true,
353
+ get: function () {
354
+ return _donate2[key];
355
+ }
356
+ });
357
+ });
334
358
  var _subscription = require("./hooks/subscription");
335
359
  Object.keys(_subscription).forEach(function (key) {
336
360
  if (key === "default" || key === "__esModule") return;
@@ -0,0 +1,14 @@
1
+ type CacheItem = {
2
+ promise: Promise<any> | null;
3
+ };
4
+ declare class GlobalCacheManager {
5
+ private static instance;
6
+ private cacheMap;
7
+ private constructor();
8
+ static getInstance(): GlobalCacheManager;
9
+ get(cacheKey: string): CacheItem | undefined;
10
+ set(cacheKey: string, item: CacheItem): void;
11
+ delete(cacheKey: string): void;
12
+ }
13
+ export declare const globalCache: GlobalCacheManager;
14
+ export {};
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.globalCache = void 0;
7
+ class GlobalCacheManager {
8
+ static instance;
9
+ cacheMap;
10
+ constructor() {
11
+ this.cacheMap = /* @__PURE__ */new Map();
12
+ }
13
+ static getInstance() {
14
+ if (!GlobalCacheManager.instance) {
15
+ GlobalCacheManager.instance = new GlobalCacheManager();
16
+ }
17
+ return GlobalCacheManager.instance;
18
+ }
19
+ get(cacheKey) {
20
+ return this.cacheMap.get(cacheKey);
21
+ }
22
+ set(cacheKey, item) {
23
+ this.cacheMap.set(cacheKey, item);
24
+ }
25
+ delete(cacheKey) {
26
+ this.cacheMap.delete(cacheKey);
27
+ }
28
+ }
29
+ const globalCache = exports.globalCache = GlobalCacheManager.getInstance();
@@ -0,0 +1,17 @@
1
+ type CacheStrategy = 'session' | 'local' | 'memory';
2
+ interface CacheOptions {
3
+ strategy?: CacheStrategy;
4
+ ttl?: number;
5
+ }
6
+ export declare class CachedRequest {
7
+ private cacheKey;
8
+ private fetchData;
9
+ private options;
10
+ constructor(cacheKey: string, fetchData: () => Promise<any>, options?: CacheOptions);
11
+ private getCache;
12
+ private getCachedData;
13
+ private setCachedData;
14
+ private clearCache;
15
+ fetch(forceRefresh?: boolean): Promise<any>;
16
+ }
17
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CachedRequest = void 0;
7
+ var _cache = require("./cache");
8
+ class CachedRequest {
9
+ cacheKey;
10
+ fetchData;
11
+ options;
12
+ constructor(cacheKey, fetchData, options = {}) {
13
+ this.cacheKey = cacheKey;
14
+ this.fetchData = fetchData;
15
+ this.options = {
16
+ strategy: "session",
17
+ ttl: 0,
18
+ ...options
19
+ };
20
+ }
21
+ getCache() {
22
+ const {
23
+ strategy
24
+ } = this.options;
25
+ switch (strategy) {
26
+ case "local":
27
+ return localStorage;
28
+ case "memory":
29
+ return _cache.globalCache;
30
+ case "session":
31
+ default:
32
+ return sessionStorage;
33
+ }
34
+ }
35
+ getCachedData() {
36
+ const cache = this.getCache();
37
+ const data = this.options.strategy === "memory" ? cache.get(this.cacheKey) : cache.getItem(this.cacheKey);
38
+ if (!data) return null;
39
+ const parsed = JSON.parse(data);
40
+ if (this.options.ttl && parsed.timestamp) {
41
+ const isExpired = Date.now() - parsed.timestamp > this.options.ttl;
42
+ if (isExpired) {
43
+ this.clearCache();
44
+ return null;
45
+ }
46
+ }
47
+ return parsed.data;
48
+ }
49
+ setCachedData(data) {
50
+ const cache = this.getCache();
51
+ const value = JSON.stringify({
52
+ data,
53
+ timestamp: Date.now()
54
+ });
55
+ if (this.options.strategy === "memory") {
56
+ cache.set(this.cacheKey, value);
57
+ } else {
58
+ cache.setItem(this.cacheKey, value);
59
+ }
60
+ }
61
+ clearCache() {
62
+ const cache = this.getCache();
63
+ if (this.options.strategy === "memory") {
64
+ cache.delete(this.cacheKey);
65
+ } else {
66
+ cache.removeItem(this.cacheKey);
67
+ }
68
+ }
69
+ fetch(forceRefresh = false) {
70
+ const cachedData = !forceRefresh && this.getCachedData();
71
+ if (cachedData) return Promise.resolve(cachedData);
72
+ const globalItem = _cache.globalCache.get(this.cacheKey);
73
+ if (globalItem?.promise) {
74
+ return globalItem.promise;
75
+ }
76
+ const newPromise = this.fetchData().then(({
77
+ data
78
+ }) => {
79
+ this.setCachedData(data);
80
+ return data;
81
+ }).finally(() => {
82
+ _cache.globalCache.delete(this.cacheKey);
83
+ });
84
+ _cache.globalCache.set(this.cacheKey, {
85
+ promise: newPromise
86
+ });
87
+ return newPromise;
88
+ }
89
+ }
90
+ exports.CachedRequest = CachedRequest;
@@ -111,3 +111,5 @@ export declare function getInvoiceDescriptionAndReason(invoice: TInvoiceExpanded
111
111
  reason: string;
112
112
  type: string;
113
113
  };
114
+ export declare function getPaymentKitComponent(): any;
115
+ export declare function openDonationSettings(openInNewTab?: boolean): void;
package/lib/libs/util.js CHANGED
@@ -33,6 +33,7 @@ exports.getFreeTrialTime = getFreeTrialTime;
33
33
  exports.getInvoiceDescriptionAndReason = getInvoiceDescriptionAndReason;
34
34
  exports.getInvoiceStatusColor = getInvoiceStatusColor;
35
35
  exports.getPaymentIntentStatusColor = getPaymentIntentStatusColor;
36
+ exports.getPaymentKitComponent = getPaymentKitComponent;
36
37
  exports.getPayoutStatusColor = getPayoutStatusColor;
37
38
  exports.getPrefix = void 0;
38
39
  exports.getPriceCurrencyOptions = getPriceCurrencyOptions;
@@ -52,6 +53,7 @@ exports.isPaymentKitMounted = void 0;
52
53
  exports.isValidCountry = isValidCountry;
53
54
  exports.lazyLoad = lazyLoad;
54
55
  exports.mergeExtraParams = void 0;
56
+ exports.openDonationSettings = openDonationSettings;
55
57
  exports.sleep = sleep;
56
58
  exports.stopEvent = stopEvent;
57
59
  exports.truncateText = truncateText;
@@ -1096,4 +1098,14 @@ function getInvoiceDescriptionAndReason(invoice, locale = "en") {
1096
1098
  reason: reasonMap[reason] || reason,
1097
1099
  type: invoiceType
1098
1100
  };
1101
+ }
1102
+ function getPaymentKitComponent() {
1103
+ const paymentKit = window.blocklet?.componentMountPoints?.find(c => c.did === PAYMENT_KIT_DID);
1104
+ return paymentKit || null;
1105
+ }
1106
+ function openDonationSettings(openInNewTab = false) {
1107
+ const paymentKit = getPaymentKitComponent();
1108
+ if (paymentKit) {
1109
+ window.open(`${window.location.origin}${paymentKit.mountPoint === "/" ? "" : paymentKit.mountPoint}/integrations/donations`, openInNewTab ? "_blank" : "_self");
1110
+ }
1099
1111
  }
package/lib/locales/en.js CHANGED
@@ -141,7 +141,14 @@ module.exports = (0, _flat.default)({
141
141
  one: "{name} will receive all tips",
142
142
  multiple: "Tips will be distributed to {count} beneficiaries",
143
143
  view: "Click to view details"
144
- }
144
+ },
145
+ inactive: "Donation feature is not enabled",
146
+ enable: "Enable Donation",
147
+ enableSuccess: "Enable Success",
148
+ configPrompt: "Donation feature is enabled, you can configure donation options in Payment Kit",
149
+ configNow: "Configure Now",
150
+ later: "Configure Later",
151
+ configTip: "Configure donation settings in Payment Kit"
145
152
  },
146
153
  cardPay: "{action} with card",
147
154
  empty: "No thing to pay",
package/lib/locales/zh.js CHANGED
@@ -141,7 +141,14 @@ module.exports = (0, _flat.default)({
141
141
  one: "{name} \u5C06\u83B7\u5F97\u5168\u90E8\u6253\u8D4F",
142
142
  multiple: "\u6253\u8D4F\u5C06\u6309\u6BD4\u4F8B\u5206\u914D\u7ED9 {count} \u4F4D\u53D7\u76CA\u4EBA",
143
143
  view: "\u70B9\u51FB\u67E5\u770B\u8BE6\u60C5"
144
- }
144
+ },
145
+ inactive: "\u6253\u8D4F\u529F\u80FD\u6682\u672A\u5F00\u542F",
146
+ enable: "\u542F\u7528\u6253\u8D4F",
147
+ enableSuccess: "\u542F\u7528\u6210\u529F",
148
+ configPrompt: "\u6253\u8D4F\u529F\u80FD\u5DF2\u542F\u7528\uFF0C\u60A8\u53EF\u4EE5\u524D\u5F80 Payment Kit \u8FDB\u4E00\u6B65\u914D\u7F6E\u6253\u8D4F\u9009\u9879",
149
+ configNow: "\u7ACB\u5373\u914D\u7F6E",
150
+ later: "\u7A0D\u540E\u914D\u7F6E",
151
+ configTip: "\u524D\u5F80 Payment Kit \u914D\u7F6E\u6253\u8D4F\u9009\u9879"
145
152
  },
146
153
  cardPay: "\u4F7F\u7528\u5361\u7247{action}",
147
154
  empty: "\u6CA1\u6709\u53EF\u652F\u4ED8\u7684\u9879\u76EE",
@@ -22,7 +22,7 @@ function DonationSkeleton() {
22
22
  mt: 2
23
23
  },
24
24
  variant: "rounded",
25
- height: 80
25
+ height: 40
26
26
  }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Divider, {
27
27
  sx: {
28
28
  mt: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-react",
3
- "version": "1.18.2",
3
+ "version": "1.18.4",
4
4
  "description": "Reusable react components for payment kit v2",
5
5
  "keywords": [
6
6
  "react",
@@ -53,15 +53,15 @@
53
53
  }
54
54
  },
55
55
  "dependencies": {
56
- "@arcblock/did-connect": "^2.11.27",
57
- "@arcblock/ux": "^2.11.27",
58
- "@arcblock/ws": "^1.19.3",
59
- "@blocklet/ui-react": "^2.11.27",
56
+ "@arcblock/did-connect": "^2.11.34",
57
+ "@arcblock/ux": "^2.11.34",
58
+ "@arcblock/ws": "^1.19.9",
59
+ "@blocklet/ui-react": "^2.11.34",
60
60
  "@mui/icons-material": "^5.16.6",
61
61
  "@mui/lab": "^5.0.0-alpha.173",
62
62
  "@mui/material": "^5.16.6",
63
63
  "@mui/system": "^5.16.6",
64
- "@ocap/util": "^1.19.3",
64
+ "@ocap/util": "^1.19.9",
65
65
  "@stripe/react-stripe-js": "^2.7.3",
66
66
  "@stripe/stripe-js": "^2.4.0",
67
67
  "@vitejs/plugin-legacy": "^5.4.1",
@@ -92,7 +92,7 @@
92
92
  "@babel/core": "^7.25.2",
93
93
  "@babel/preset-env": "^7.25.2",
94
94
  "@babel/preset-react": "^7.24.7",
95
- "@blocklet/payment-types": "1.18.2",
95
+ "@blocklet/payment-types": "1.18.4",
96
96
  "@storybook/addon-essentials": "^7.6.20",
97
97
  "@storybook/addon-interactions": "^7.6.20",
98
98
  "@storybook/addon-links": "^7.6.20",
@@ -123,5 +123,5 @@
123
123
  "vite-plugin-babel": "^1.2.0",
124
124
  "vite-plugin-node-polyfills": "^0.21.0"
125
125
  },
126
- "gitHead": "577491d13f4b2e53e9a71be3b90298b5685f5e8a"
126
+ "gitHead": "003d3b762bc9e3e224e4814dea95ff02d014868d"
127
127
  }