|
@@ -22,47 +22,47 @@ function airwallexCardReducer(state: AirwallexCardStateType, action: Partial<Air
|
|
|
return {...newState};
|
|
return {...newState};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const defaultAirwallexCardState: AirwallexCardStateType = {
|
|
|
|
|
+ cardNumberReady: false,
|
|
|
|
|
+ cvcReady: false,
|
|
|
|
|
+ expiryReady: false,
|
|
|
|
|
|
|
|
|
|
+ cardNumberValid: false,
|
|
|
|
|
+ cvcValid: false,
|
|
|
|
|
+ expiryValid: false,
|
|
|
|
|
+
|
|
|
|
|
+ cardNumberErrorMsg: "",
|
|
|
|
|
+ cvcErrorMsg: "",
|
|
|
|
|
+ expiryErrorMsg: "",
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
export function useAirwallexCard(
|
|
export function useAirwallexCard(
|
|
|
enabled: boolean,
|
|
enabled: boolean,
|
|
|
mountId: { cardNumber: string; cvc: string; expiry: string; },
|
|
mountId: { cardNumber: string; cvc: string; expiry: string; },
|
|
|
// callback: EventCallback
|
|
// callback: EventCallback
|
|
|
) {
|
|
) {
|
|
|
-
|
|
|
|
|
- const defaultAirwallexCardState: AirwallexCardStateType = {
|
|
|
|
|
- cardNumberReady: false,
|
|
|
|
|
- cvcReady: false,
|
|
|
|
|
- expiryReady: false,
|
|
|
|
|
-
|
|
|
|
|
- cardNumberValid: false,
|
|
|
|
|
- cvcValid: false,
|
|
|
|
|
- expiryValid: false,
|
|
|
|
|
-
|
|
|
|
|
- cardNumberErrorMsg: "",
|
|
|
|
|
- cvcErrorMsg: "",
|
|
|
|
|
- expiryErrorMsg: "",
|
|
|
|
|
- };
|
|
|
|
|
- const [airwallexCardState, airwallexCardDispatch] = useReducer(airwallexCardReducer, defaultAirwallexCardState);
|
|
|
|
|
- const readyAirwallexElement = useEffectEvent((event: any): void => {
|
|
|
|
|
- console.log('----------------------------onReady---------------------');
|
|
|
|
|
- const { type } = event.detail;
|
|
|
|
|
- if (type === 'cardNumber') {
|
|
|
|
|
- airwallexCardDispatch({
|
|
|
|
|
- cardNumberReady: true
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- if (type === 'cvc') {
|
|
|
|
|
- airwallexCardDispatch({
|
|
|
|
|
- cvcReady: true
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- if (type === 'expiry') {
|
|
|
|
|
- airwallexCardDispatch({
|
|
|
|
|
- expiryReady: true
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ const [airwallexCardState, airwallexCardDispatch] = useReducer(airwallexCardReducer, defaultAirwallexCardState);
|
|
|
|
|
+ const readyAirwallexElement = useEffectEvent((event: any): void => {
|
|
|
|
|
+ console.log('----------------------------onReady---------------------');
|
|
|
|
|
+ const { type } = event.detail;
|
|
|
|
|
+ if (type === 'cardNumber') {
|
|
|
|
|
+ airwallexCardDispatch({
|
|
|
|
|
+ cardNumberReady: true
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ if (type === 'cvc') {
|
|
|
|
|
+ airwallexCardDispatch({
|
|
|
|
|
+ cvcReady: true
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ if (type === 'expiry') {
|
|
|
|
|
+ airwallexCardDispatch({
|
|
|
|
|
+ expiryReady: true
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
|
|
|
|
|
const changeAirwallexElement = useEffectEvent((event: any) => {
|
|
const changeAirwallexElement = useEffectEvent((event: any) => {
|
|
@@ -143,34 +143,37 @@ export function useAirwallexCard(
|
|
|
}>({});
|
|
}>({});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
|
|
+ console.log('enabled ------- ',enabled);
|
|
|
if (!enabled) return;
|
|
if (!enabled) return;
|
|
|
|
|
|
|
|
let destroyed = false;
|
|
let destroyed = false;
|
|
|
|
|
|
|
|
const init = async () => {
|
|
const init = async () => {
|
|
|
- await airwallexInit();
|
|
|
|
|
- // await airwallexManager.init();
|
|
|
|
|
-
|
|
|
|
|
- const els = await airwallexManager.createElements();
|
|
|
|
|
- elementRef.current = els;
|
|
|
|
|
-console.log('useAirwallexCard +++++++++++++++++++++++++++++',destroyed);
|
|
|
|
|
- if (destroyed) return;
|
|
|
|
|
-
|
|
|
|
|
- airwallexManager.mount({
|
|
|
|
|
- cardNumber: mountId.cardNumber,
|
|
|
|
|
- cvc: mountId.cvc,
|
|
|
|
|
- expiry: mountId.expiry
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- mountedRef.current = true;
|
|
|
|
|
-
|
|
|
|
|
- // register events ONLY ONCE
|
|
|
|
|
- bindEvents(els,{
|
|
|
|
|
- focusCallback: focusAirwallexElement,
|
|
|
|
|
- blurCallback: blurAirwallexElement,
|
|
|
|
|
- changeCallback: changeAirwallexElement,
|
|
|
|
|
- readyCallback: readyAirwallexElement
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ await airwallexInit();
|
|
|
|
|
+ // await airwallexManager.init();
|
|
|
|
|
+
|
|
|
|
|
+ const els = await airwallexManager.createElements();
|
|
|
|
|
+ elementRef.current = els;
|
|
|
|
|
+ console.log('useAirwallexCard +++++++++++++++++++++++++++++',destroyed);
|
|
|
|
|
+ if (destroyed) return;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ airwallexManager.mount({
|
|
|
|
|
+ cardNumber: mountId.cardNumber,
|
|
|
|
|
+ cvc: mountId.cvc,
|
|
|
|
|
+ expiry: mountId.expiry
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // register events ONLY ONCE
|
|
|
|
|
+ bindEvents(els,{
|
|
|
|
|
+ focusCallback: focusAirwallexElement,
|
|
|
|
|
+ blurCallback: blurAirwallexElement,
|
|
|
|
|
+ changeCallback: changeAirwallexElement,
|
|
|
|
|
+ readyCallback: readyAirwallexElement
|
|
|
|
|
+ });
|
|
|
|
|
+ mountedRef.current = true;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
init();
|
|
init();
|
|
@@ -179,11 +182,11 @@ console.log('useAirwallexCard +++++++++++++++++++++++++++++',destroyed);
|
|
|
console.log('useAirwallexCard ___________________________________',mountedRef.current)
|
|
console.log('useAirwallexCard ___________________________________',mountedRef.current)
|
|
|
destroyed = true;
|
|
destroyed = true;
|
|
|
if (mountedRef.current) {
|
|
if (mountedRef.current) {
|
|
|
- airwallexManager.unmount();// 解除挂载
|
|
|
|
|
|
|
+ airwallexManager.destroy();// 解除挂载
|
|
|
mountedRef.current = false;
|
|
mountedRef.current = false;
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
- }, [enabled]);
|
|
|
|
|
|
|
+ }, [enabled,mountId.cardNumber,mountId.cvc,mountId.expiry]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -212,21 +215,4 @@ function bindEvents(els: any,callback:EventCallback) {
|
|
|
airwallexManager.on(expiry,"blur", blurCallback);
|
|
airwallexManager.on(expiry,"blur", blurCallback);
|
|
|
airwallexManager.on(expiry,"change", changeCallback);
|
|
airwallexManager.on(expiry,"change", changeCallback);
|
|
|
airwallexManager.on(expiry,"ready", readyCallback);
|
|
airwallexManager.on(expiry,"ready", readyCallback);
|
|
|
-
|
|
|
|
|
- // ⚠️ 关键:绑定只发生一次(manager内部防重复)
|
|
|
|
|
-// cardNumber.on("focus", focusCallback);
|
|
|
|
|
-// cardNumber.on("blur", blurCallback);
|
|
|
|
|
-// cardNumber.on("change", changeCallback);
|
|
|
|
|
-// cardNumber.on("ready", readyCallback);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-// cvc.on("focus", focusCallback);
|
|
|
|
|
-// cvc.on("blur", blurCallback);
|
|
|
|
|
-// cvc.on("change", changeCallback);
|
|
|
|
|
-// cvc.on("ready", readyCallback);
|
|
|
|
|
-
|
|
|
|
|
-// expiry.on("focus", focusCallback);
|
|
|
|
|
-// expiry.on("blur", blurCallback);
|
|
|
|
|
-// expiry.on("change", changeCallback);
|
|
|
|
|
-// expiry.on("ready", readyCallback);
|
|
|
|
|
}
|
|
}
|