@blocklet/payment-react 1.13.253 → 1.13.254

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.
@@ -18,13 +18,15 @@ export type PaymentContextType = {
18
18
  setLivemode: (livemode: boolean) => void;
19
19
  api: Axios;
20
20
  };
21
+ export type PaymentContextProps = {
22
+ session: import('@arcblock/did-connect/lib/types').SessionContext['session'];
23
+ connect: import('@arcblock/did-connect/lib/types').SessionContext['connectApi'];
24
+ children: any;
25
+ baseUrl?: string;
26
+ };
21
27
  declare const PaymentContext: import("react").Context<PaymentContextType>;
22
28
  declare const Consumer: import("react").Consumer<PaymentContextType>;
23
- declare function PaymentProvider({ children, session, connect }: {
24
- children: any;
25
- session: any;
26
- connect: any;
27
- }): import("react").JSX.Element | null;
29
+ declare function PaymentProvider({ session, connect, children, baseUrl }: PaymentContextProps): import("react").JSX.Element | null;
28
30
  declare namespace PaymentProvider {
29
31
  var defaultProps: {};
30
32
  }
@@ -4,7 +4,6 @@ import { useLocalStorageState, useRequest } from "ahooks";
4
4
  import { createContext, useContext } from "react";
5
5
  import api from "../libs/api.js";
6
6
  import { getPrefix } from "../libs/util.js";
7
- const prefix = getPrefix();
8
7
  const PaymentContext = createContext({ api });
9
8
  const { Provider, Consumer } = PaymentContext;
10
9
  const getSettings = async () => {
@@ -15,9 +14,13 @@ const getCurrency = (currencyId, methods) => {
15
14
  const currencies = methods.reduce((acc, x) => acc.concat(x.payment_currencies), []);
16
15
  return currencies.find((x) => x.id === currencyId);
17
16
  };
18
- function PaymentProvider({ children, session, connect }) {
17
+ function PaymentProvider({ session, connect, children, baseUrl }) {
18
+ if (baseUrl) {
19
+ window.__PAYMENT_KIT_BASE_URL = baseUrl;
20
+ }
19
21
  const { data, error, run, loading } = useRequest(getSettings);
20
22
  const [livemode, setLivemode] = useLocalStorageState("livemode", { defaultValue: true });
23
+ const prefix = getPrefix();
21
24
  if (error) {
22
25
  return /* @__PURE__ */ jsx(Alert, { severity: "error", children: error.message });
23
26
  }
@@ -46,7 +46,6 @@ const getInvoiceLink = (invoice, action) => {
46
46
  external: false,
47
47
  connect: invoice.status === "uncollectible",
48
48
  url: joinURL(
49
- window.location.origin,
50
49
  getPrefix(),
51
50
  `/customer/invoice/${invoice.id}?action=${invoice.status === "uncollectible" ? action : ""}`
52
51
  )
@@ -63,7 +63,7 @@ export default function MiniInvoiceList() {
63
63
  if (!subscription) {
64
64
  return "#";
65
65
  }
66
- const pageUrl = joinURL(window.location.origin, getPrefix(), `/customer/subscription/${subscription.id}`);
66
+ const pageUrl = joinURL(getPrefix(), `/customer/subscription/${subscription.id}`);
67
67
  return withQuery(pageUrl, {
68
68
  source: "embed",
69
69
  ...getDidConnectQueryParams({ forceConnected: subscription.customer.did })
package/es/libs/util.js CHANGED
@@ -11,15 +11,29 @@ export const isPaymentKitMounted = () => {
11
11
  return (window.blocklet?.componentMountPoints || []).some((x) => x.did === PAYMENT_KIT_DID);
12
12
  };
13
13
  export const getPrefix = () => {
14
- const componentId = (window?.blocklet?.componentId || "").split("/").pop();
14
+ const prefix = window.blocklet?.prefix || "/";
15
+ let baseUrl = window.location?.origin;
16
+ if (window.__PAYMENT_KIT_BASE_URL) {
17
+ try {
18
+ const tmp = new URL(window.__PAYMENT_KIT_BASE_URL);
19
+ if (tmp.origin !== window.location.origin) {
20
+ baseUrl = window.__PAYMENT_KIT_BASE_URL;
21
+ } else {
22
+ baseUrl = tmp.origin;
23
+ }
24
+ } catch (err) {
25
+ console.warn("Invalid baseUrl for PaymentProvider", window.__PAYMENT_KIT_BASE_URL);
26
+ }
27
+ }
28
+ const componentId = (window.blocklet?.componentId || "").split("/").pop();
15
29
  if (componentId === PAYMENT_KIT_DID) {
16
- return window.blocklet?.prefix;
30
+ return joinURL(baseUrl, prefix);
17
31
  }
18
32
  const component = (window.blocklet?.componentMountPoints || []).find((x) => x.did === PAYMENT_KIT_DID);
19
33
  if (component) {
20
- return component.mountPoint;
34
+ return joinURL(baseUrl, component.mountPoint);
21
35
  }
22
- return window.blocklet?.prefix;
36
+ return joinURL(baseUrl, prefix);
23
37
  };
24
38
  export function formatToDate(date, locale = "en", format = "YYYY-MM-DD HH:mm:ss") {
25
39
  if (!date) {
@@ -206,7 +206,7 @@ export default function PaymentForm({
206
206
  connect.open({
207
207
  containerEl: void 0,
208
208
  action: checkoutSession.mode,
209
- prefix: joinURL(window.location.origin, getPrefix(), "/api/did"),
209
+ prefix: joinURL(getPrefix(), "/api/did"),
210
210
  saveConnect: false,
211
211
  extraParams: { checkoutSessionId: checkoutSession.id },
212
212
  onSuccess: async () => {
@@ -398,7 +398,7 @@ export default function PaymentForm({
398
398
  state.customerLimited && /* @__PURE__ */ jsx(
399
399
  ConfirmDialog,
400
400
  {
401
- onConfirm: () => window.open(joinURL(window.location.origin, getPrefix(), "/customer/invoice/past-due"), "_blank"),
401
+ onConfirm: () => window.open(joinURL(getPrefix(), "/customer/invoice/past-due"), "_blank"),
402
402
  onCancel: () => setState({ customerLimited: false }),
403
403
  confirm: t("payment.customer.pastDue.alert.confirm"),
404
404
  title: t("payment.customer.pastDue.alert.title"),
@@ -14,3 +14,5 @@ declare module '@blocklet/*';
14
14
  declare module 'pretty-ms-i18n';
15
15
 
16
16
  declare var blocklet: import('@blocklet/sdk').WindowBlocklet;
17
+
18
+ declare var __PAYMENT_KIT_BASE_URL: string;
@@ -18,13 +18,15 @@ export type PaymentContextType = {
18
18
  setLivemode: (livemode: boolean) => void;
19
19
  api: Axios;
20
20
  };
21
+ export type PaymentContextProps = {
22
+ session: import('@arcblock/did-connect/lib/types').SessionContext['session'];
23
+ connect: import('@arcblock/did-connect/lib/types').SessionContext['connectApi'];
24
+ children: any;
25
+ baseUrl?: string;
26
+ };
21
27
  declare const PaymentContext: import("react").Context<PaymentContextType>;
22
28
  declare const Consumer: import("react").Consumer<PaymentContextType>;
23
- declare function PaymentProvider({ children, session, connect }: {
24
- children: any;
25
- session: any;
26
- connect: any;
27
- }): import("react").JSX.Element | null;
29
+ declare function PaymentProvider({ session, connect, children, baseUrl }: PaymentContextProps): import("react").JSX.Element | null;
28
30
  declare namespace PaymentProvider {
29
31
  var defaultProps: {};
30
32
  }
@@ -14,7 +14,6 @@ var _react = require("react");
14
14
  var _api = _interopRequireDefault(require("../libs/api"));
15
15
  var _util = require("../libs/util");
16
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
- const prefix = (0, _util.getPrefix)();
18
17
  const PaymentContext = exports.PaymentContext = (0, _react.createContext)({
19
18
  api: _api.default
20
19
  });
@@ -34,10 +33,14 @@ const getCurrency = (currencyId, methods) => {
34
33
  return currencies.find(x => x.id === currencyId);
35
34
  };
36
35
  function PaymentProvider({
37
- children,
38
36
  session,
39
- connect
37
+ connect,
38
+ children,
39
+ baseUrl
40
40
  }) {
41
+ if (baseUrl) {
42
+ window.__PAYMENT_KIT_BASE_URL = baseUrl;
43
+ }
41
44
  const {
42
45
  data,
43
46
  error,
@@ -47,6 +50,7 @@ function PaymentProvider({
47
50
  const [livemode, setLivemode] = (0, _ahooks.useLocalStorageState)("livemode", {
48
51
  defaultValue: true
49
52
  });
53
+ const prefix = (0, _util.getPrefix)();
50
54
  if (error) {
51
55
  return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Alert, {
52
56
  severity: "error",
@@ -44,7 +44,7 @@ const getInvoiceLink = (invoice, action) => {
44
44
  return {
45
45
  external: false,
46
46
  connect: invoice.status === "uncollectible",
47
- url: (0, _ufo.joinURL)(window.location.origin, (0, _util.getPrefix)(), `/customer/invoice/${invoice.id}?action=${invoice.status === "uncollectible" ? action : ""}`)
47
+ url: (0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/invoice/${invoice.id}?action=${invoice.status === "uncollectible" ? action : ""}`)
48
48
  };
49
49
  }
50
50
  return {
@@ -56,7 +56,7 @@ function MiniInvoiceList() {
56
56
  if (!subscription) {
57
57
  return "#";
58
58
  }
59
- const pageUrl = (0, _ufo.joinURL)(window.location.origin, (0, _util.getPrefix)(), `/customer/subscription/${subscription.id}`);
59
+ const pageUrl = (0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/subscription/${subscription.id}`);
60
60
  return (0, _ufo.withQuery)(pageUrl, {
61
61
  source: "embed",
62
62
  ...(0, _didConnect.getDidConnectQueryParams)({
package/lib/libs/util.js CHANGED
@@ -55,15 +55,29 @@ const isPaymentKitMounted = () => {
55
55
  };
56
56
  exports.isPaymentKitMounted = isPaymentKitMounted;
57
57
  const getPrefix = () => {
58
- const componentId = (window?.blocklet?.componentId || "").split("/").pop();
58
+ const prefix = window.blocklet?.prefix || "/";
59
+ let baseUrl = window.location?.origin;
60
+ if (window.__PAYMENT_KIT_BASE_URL) {
61
+ try {
62
+ const tmp = new URL(window.__PAYMENT_KIT_BASE_URL);
63
+ if (tmp.origin !== window.location.origin) {
64
+ baseUrl = window.__PAYMENT_KIT_BASE_URL;
65
+ } else {
66
+ baseUrl = tmp.origin;
67
+ }
68
+ } catch (err) {
69
+ console.warn("Invalid baseUrl for PaymentProvider", window.__PAYMENT_KIT_BASE_URL);
70
+ }
71
+ }
72
+ const componentId = (window.blocklet?.componentId || "").split("/").pop();
59
73
  if (componentId === PAYMENT_KIT_DID) {
60
- return window.blocklet?.prefix;
74
+ return (0, _ufo.joinURL)(baseUrl, prefix);
61
75
  }
62
76
  const component = (window.blocklet?.componentMountPoints || []).find(x => x.did === PAYMENT_KIT_DID);
63
77
  if (component) {
64
- return component.mountPoint;
78
+ return (0, _ufo.joinURL)(baseUrl, component.mountPoint);
65
79
  }
66
- return window.blocklet?.prefix;
80
+ return (0, _ufo.joinURL)(baseUrl, prefix);
67
81
  };
68
82
  exports.getPrefix = getPrefix;
69
83
  function formatToDate(date, locale = "en", format = "YYYY-MM-DD HH:mm:ss") {
@@ -245,7 +245,7 @@ function PaymentForm({
245
245
  connect.open({
246
246
  containerEl: void 0,
247
247
  action: checkoutSession.mode,
248
- prefix: (0, _ufo.joinURL)(window.location.origin, (0, _util.getPrefix)(), "/api/did"),
248
+ prefix: (0, _ufo.joinURL)((0, _util.getPrefix)(), "/api/did"),
249
249
  saveConnect: false,
250
250
  extraParams: {
251
251
  checkoutSessionId: checkoutSession.id
@@ -485,7 +485,7 @@ function PaymentForm({
485
485
  })]
486
486
  })
487
487
  }), state.customerLimited && /* @__PURE__ */(0, _jsxRuntime.jsx)(_confirm.default, {
488
- onConfirm: () => window.open((0, _ufo.joinURL)(window.location.origin, (0, _util.getPrefix)(), "/customer/invoice/past-due"), "_blank"),
488
+ onConfirm: () => window.open((0, _ufo.joinURL)((0, _util.getPrefix)(), "/customer/invoice/past-due"), "_blank"),
489
489
  onCancel: () => setState({
490
490
  customerLimited: false
491
491
  }),
@@ -14,3 +14,5 @@ declare module '@blocklet/*';
14
14
  declare module 'pretty-ms-i18n';
15
15
 
16
16
  declare var blocklet: import('@blocklet/sdk').WindowBlocklet;
17
+
18
+ declare var __PAYMENT_KIT_BASE_URL: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-react",
3
- "version": "1.13.253",
3
+ "version": "1.13.254",
4
4
  "description": "Reusable react components for payment kit v2",
5
5
  "keywords": [
6
6
  "react",
@@ -91,7 +91,7 @@
91
91
  "@babel/core": "^7.24.5",
92
92
  "@babel/preset-env": "^7.24.5",
93
93
  "@babel/preset-react": "^7.24.1",
94
- "@blocklet/payment-types": "1.13.253",
94
+ "@blocklet/payment-types": "1.13.254",
95
95
  "@storybook/addon-essentials": "^7.6.19",
96
96
  "@storybook/addon-interactions": "^7.6.19",
97
97
  "@storybook/addon-links": "^7.6.19",
@@ -120,5 +120,5 @@
120
120
  "vite-plugin-babel": "^1.2.0",
121
121
  "vite-plugin-node-polyfills": "^0.21.0"
122
122
  },
123
- "gitHead": "4ad023bae87cc6b0c7480e701e5ba51b7b66ca19"
123
+ "gitHead": "9a6635035f3979e3f5dce34867cc748f22a8436e"
124
124
  }
@@ -7,8 +7,6 @@ import { createContext, useContext } from 'react';
7
7
  import api from '../libs/api';
8
8
  import { getPrefix } from '../libs/util';
9
9
 
10
- const prefix = getPrefix();
11
-
12
10
  export interface Settings {
13
11
  paymentMethods: TPaymentMethodExpanded[];
14
12
  baseCurrency: TPaymentCurrency;
@@ -26,6 +24,14 @@ export type PaymentContextType = {
26
24
  api: Axios;
27
25
  };
28
26
 
27
+ export type PaymentContextProps = {
28
+ session: import('@arcblock/did-connect/lib/types').SessionContext['session'];
29
+ connect: import('@arcblock/did-connect/lib/types').SessionContext['connectApi'];
30
+ children: any;
31
+ // eslint-disable-next-line react/require-default-props
32
+ baseUrl?: string;
33
+ };
34
+
29
35
  // @ts-ignore
30
36
  const PaymentContext = createContext<PaymentContextType>({ api });
31
37
  const { Provider, Consumer } = PaymentContext;
@@ -40,10 +46,15 @@ const getCurrency = (currencyId: string, methods: TPaymentMethodExpanded[]) => {
40
46
  return currencies.find((x) => x.id === currencyId);
41
47
  };
42
48
 
43
- // eslint-disable-next-line react/prop-types
44
- function PaymentProvider({ children, session, connect }: { children: any; session: any; connect: any }) {
49
+ function PaymentProvider({ session, connect, children, baseUrl }: PaymentContextProps) {
50
+ if (baseUrl) {
51
+ // This is hack but efficient to share
52
+ window.__PAYMENT_KIT_BASE_URL = baseUrl;
53
+ }
54
+
45
55
  const { data, error, run, loading } = useRequest(getSettings);
46
56
  const [livemode, setLivemode] = useLocalStorageState('livemode', { defaultValue: true });
57
+ const prefix = getPrefix();
47
58
 
48
59
  if (error) {
49
60
  return <Alert severity="error">{error.message}</Alert>;
@@ -56,7 +67,7 @@ function PaymentProvider({ children, session, connect }: { children: any; sessio
56
67
  return (
57
68
  <Provider
58
69
  value={{
59
- session,
70
+ session: session as any,
60
71
  connect,
61
72
  prefix,
62
73
  livemode: !!livemode,
@@ -65,7 +65,6 @@ const getInvoiceLink = (invoice: TInvoiceExpanded, action?: string) => {
65
65
  external: false,
66
66
  connect: invoice.status === 'uncollectible',
67
67
  url: joinURL(
68
- window.location.origin,
69
68
  getPrefix(),
70
69
  `/customer/invoice/${invoice.id}?action=${invoice.status === 'uncollectible' ? action : ''}`
71
70
  ),
@@ -73,7 +73,7 @@ export default function MiniInvoiceList() {
73
73
  return '#';
74
74
  }
75
75
 
76
- const pageUrl: string = joinURL(window.location.origin, getPrefix(), `/customer/subscription/${subscription.id}`);
76
+ const pageUrl: string = joinURL(getPrefix(), `/customer/subscription/${subscription.id}`);
77
77
 
78
78
  return withQuery(pageUrl, {
79
79
  source: 'embed',
package/src/libs/api.ts CHANGED
@@ -9,7 +9,6 @@ const api = axios.create();
9
9
  api.interceptors.request.use(
10
10
  (config) => {
11
11
  const prefix = getPrefix();
12
- // 'https://storage.staging.abtnet.io/app/payment-kit/.well-known/service'
13
12
  config.baseURL = prefix || '';
14
13
 
15
14
  const locale = getLocale(window.blocklet?.languages);
package/src/libs/util.ts CHANGED
@@ -31,16 +31,30 @@ export const isPaymentKitMounted = () => {
31
31
  };
32
32
 
33
33
  export const getPrefix = (): string => {
34
- const componentId = (window?.blocklet?.componentId || '').split('/').pop();
34
+ const prefix = window.blocklet?.prefix || '/';
35
+ let baseUrl = window.location?.origin; // required when use payment feature cross origin
36
+ if (window.__PAYMENT_KIT_BASE_URL) {
37
+ try {
38
+ const tmp = new URL(window.__PAYMENT_KIT_BASE_URL);
39
+ if (tmp.origin !== window.location.origin) {
40
+ baseUrl = window.__PAYMENT_KIT_BASE_URL;
41
+ } else {
42
+ baseUrl = tmp.origin;
43
+ }
44
+ } catch (err) {
45
+ console.warn('Invalid baseUrl for PaymentProvider', window.__PAYMENT_KIT_BASE_URL);
46
+ }
47
+ }
48
+ const componentId = (window.blocklet?.componentId || '').split('/').pop();
35
49
  if (componentId === PAYMENT_KIT_DID) {
36
- return window.blocklet?.prefix;
50
+ return joinURL(baseUrl, prefix);
37
51
  }
38
52
  const component = (window.blocklet?.componentMountPoints || []).find((x: TComponent) => x.did === PAYMENT_KIT_DID);
39
53
  if (component) {
40
- return component.mountPoint;
54
+ return joinURL(baseUrl, component.mountPoint);
41
55
  }
42
56
 
43
- return window.blocklet?.prefix;
57
+ return joinURL(baseUrl, prefix);
44
58
  };
45
59
 
46
60
  export function formatToDate(date: Date | string | number, locale = 'en', format = 'YYYY-MM-DD HH:mm:ss') {
@@ -270,7 +270,7 @@ export default function PaymentForm({
270
270
  connect.open({
271
271
  containerEl: undefined as unknown as Element,
272
272
  action: checkoutSession.mode,
273
- prefix: joinURL(window.location.origin, getPrefix(), '/api/did'),
273
+ prefix: joinURL(getPrefix(), '/api/did'),
274
274
  saveConnect: false,
275
275
  extraParams: { checkoutSessionId: checkoutSession.id },
276
276
  onSuccess: async () => {
@@ -464,9 +464,7 @@ export default function PaymentForm({
464
464
  </Fade>
465
465
  {state.customerLimited && (
466
466
  <ConfirmDialog
467
- onConfirm={() =>
468
- window.open(joinURL(window.location.origin, getPrefix(), '/customer/invoice/past-due'), '_blank')
469
- }
467
+ onConfirm={() => window.open(joinURL(getPrefix(), '/customer/invoice/past-due'), '_blank')}
470
468
  onCancel={() => setState({ customerLimited: false })}
471
469
  confirm={t('payment.customer.pastDue.alert.confirm')}
472
470
  title={t('payment.customer.pastDue.alert.title')}
@@ -14,3 +14,5 @@ declare module '@blocklet/*';
14
14
  declare module 'pretty-ms-i18n';
15
15
 
16
16
  declare var blocklet: import('@blocklet/sdk').WindowBlocklet;
17
+
18
+ declare var __PAYMENT_KIT_BASE_URL: string;