paymob-pixel-alpha 1.1.4 → 1.1.5

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 (2) hide show
  1. package/main.js +45 -32
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -19987,7 +19987,6 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
19987
19987
  const [errorMessage, setErrorMessage] = (0,react.useState)('');
19988
19988
  const iframeRef = (0,react.useRef)(null);
19989
19989
  const [errors, setErrors] = (0,react.useState)({});
19990
- const iframe = document.getElementById('iFrame');
19991
19990
  const [isIframeLoaded, setIsIframeLoaded] = (0,react.useState)(false);
19992
19991
  const [showDeleteCardModal, setShowDeleteCardModal] = (0,react.useState)(false);
19993
19992
  const [selectedDeleteCard, setSelectedDeleteCard] = (0,react.useState)();
@@ -20009,11 +20008,13 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20009
20008
  const shouldDisablePayBtn = showCvvInput ? loading || (isAmexCardSelected ? selectedCardCvvLength !== constants_AMEX_CVV_LENGTH : selectedCardCvvLength !== constants_CVV_LENGTH) : !selectedCardId;
20010
20009
  (0,react.useEffect)(() => {
20011
20010
  const handleDataFromIframe = event => {
20012
- var _event$data, _event$data2, _event$data3;
20011
+ var _iframeRef$current, _event$data, _event$data2, _event$data3;
20013
20012
  if (!event.data) return;
20014
20013
  if (!event.data.type) return;
20014
+ if (event.source !== ((_iframeRef$current = iframeRef.current) == null ? void 0 : _iframeRef$current.contentWindow)) return;
20015
20015
  if (event.data.shouldFocusOnCardName) {
20016
- const cardNameField = document.querySelector(`input[name=name]`);
20016
+ var _iframeRef$current2;
20017
+ const cardNameField = (_iframeRef$current2 = iframeRef.current) == null || (_iframeRef$current2 = _iframeRef$current2.nextElementSibling) == null ? void 0 : _iframeRef$current2.querySelector(`input[name=name]`);
20017
20018
  if (cardNameField !== null) {
20018
20019
  cardNameField.focus();
20019
20020
  }
@@ -20052,10 +20053,10 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20052
20053
  requiresOTP,
20053
20054
  isOTPResponse
20054
20055
  }) => {
20055
- var _res$data, _res$data2, _res$data3;
20056
+ var _iframeRef$current3, _res$data, _res$data2, _res$data3;
20056
20057
  setShouldSubmitData(false);
20057
20058
  setSendOTPLoading(false);
20058
- const cardHolderInput = document.getElementById('name');
20059
+ const cardHolderInput = (_iframeRef$current3 = iframeRef.current) == null || (_iframeRef$current3 = _iframeRef$current3.nextElementSibling) == null ? void 0 : _iframeRef$current3.querySelector(`input[name=name]`);
20059
20060
  if (cardHolderInput) {
20060
20061
  setTimeout(() => cardHolderInput.disabled = false, 1000);
20061
20062
  }
@@ -20127,10 +20128,11 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20127
20128
  };
20128
20129
  }, [isCardValid, shouldDisablePayBtn, showCardModal, savedCards == null ? void 0 : savedCards.length]);
20129
20130
  (0,react.useEffect)(() => {
20130
- if (iframe && shouldSubmitData) {
20131
+ if (iframeRef.current && shouldSubmitData) {
20131
20132
  const handlePay = async () => {
20133
+ var _iframeRef$current4;
20132
20134
  if (loading) return;
20133
- const cardHolderInput = document.getElementById('name');
20135
+ const cardHolderInput = (_iframeRef$current4 = iframeRef.current) == null || (_iframeRef$current4 = _iframeRef$current4.nextElementSibling) == null ? void 0 : _iframeRef$current4.querySelector(`input[name=name]`);
20134
20136
  if (cardHolderInput) {
20135
20137
  cardHolderInput.disabled = true;
20136
20138
  }
@@ -20148,7 +20150,7 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20148
20150
  };
20149
20151
  handlePay();
20150
20152
  }
20151
- }, [cardHolderName, saveCard, shouldSubmitData, iframe]);
20153
+ }, [cardHolderName, saveCard, shouldSubmitData, iframeRef.current]);
20152
20154
  (0,react.useEffect)(() => {
20153
20155
  if (isIframeLoaded) {
20154
20156
  sendCardData();
@@ -20157,8 +20159,8 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20157
20159
  (0,react.useEffect)(() => {
20158
20160
  let resizeObserver = null;
20159
20161
  if (isIframeLoaded) {
20160
- var _iframe$contentWindow;
20161
- (_iframe$contentWindow = iframe.contentWindow) == null || _iframe$contentWindow.postMessage({
20162
+ var _iframeRef$current5, _iframeRef$current6;
20163
+ (_iframeRef$current5 = iframeRef.current) == null || (_iframeRef$current5 = _iframeRef$current5.contentWindow) == null || _iframeRef$current5.postMessage({
20162
20164
  type: 'customStyles',
20163
20165
  payload: {
20164
20166
  styling: customStyle,
@@ -20167,7 +20169,7 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20167
20169
  }
20168
20170
  }
20169
20171
  }, '*');
20170
- const nextElement = iframe.nextElementSibling;
20172
+ const nextElement = (_iframeRef$current6 = iframeRef.current) == null ? void 0 : _iframeRef$current6.nextElementSibling;
20171
20173
  resizeObserver = new ResizeObserver(entries => {
20172
20174
  for (let entry of entries) {
20173
20175
  const width = Math.floor(entry.contentRect.width - 0.5);
@@ -20176,7 +20178,7 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20176
20178
  }
20177
20179
  }
20178
20180
  });
20179
- resizeObserver.observe(iframe);
20181
+ iframeRef.current && resizeObserver.observe(iframeRef.current);
20180
20182
  }
20181
20183
  return () => {
20182
20184
  var _resizeObserver;
@@ -20184,8 +20186,8 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20184
20186
  };
20185
20187
  }, [isIframeLoaded, iframeRef.current]);
20186
20188
  (0,react.useEffect)(() => {
20187
- var _iframe$contentWindow2;
20188
- iframe == null || (_iframe$contentWindow2 = iframe.contentWindow) == null || _iframe$contentWindow2.postMessage({
20189
+ var _iframeRef$current7;
20190
+ (_iframeRef$current7 = iframeRef.current) == null || (_iframeRef$current7 = _iframeRef$current7.contentWindow) == null || _iframeRef$current7.postMessage({
20189
20191
  type: 'cardHolderError',
20190
20192
  payload: {
20191
20193
  error: errors['name']
@@ -20208,12 +20210,12 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20208
20210
  }));
20209
20211
  async function handleGenerateToken() {
20210
20212
  return new Promise((resolve, reject) => {
20211
- var _iframe$contentWindow3;
20213
+ var _iframeRef$current8;
20212
20214
  if (loading || !isCardValid || isLoading) {
20213
20215
  return reject();
20214
20216
  }
20215
20217
  tokenizationResolve.current = resolve;
20216
- (_iframe$contentWindow3 = iframe.contentWindow) == null || _iframe$contentWindow3.postMessage({
20218
+ (_iframeRef$current8 = iframeRef.current) == null || (_iframeRef$current8 = _iframeRef$current8.contentWindow) == null || _iframeRef$current8.postMessage({
20217
20219
  type: 'tokenizeCard',
20218
20220
  payload: {
20219
20221
  publicKey,
@@ -20225,8 +20227,8 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20225
20227
  }
20226
20228
  ;
20227
20229
  const sendCardData = shouldSubmitData => {
20228
- var _iframe$contentWindow4, _payment$subType, _payment$subType2;
20229
- (_iframe$contentWindow4 = iframe.contentWindow) == null || _iframe$contentWindow4.postMessage({
20230
+ var _iframeRef$current9, _payment$subType, _payment$subType2;
20231
+ (_iframeRef$current9 = iframeRef.current) == null || (_iframeRef$current9 = _iframeRef$current9.contentWindow) == null || _iframeRef$current9.postMessage({
20230
20232
  type: 'cardData',
20231
20233
  payload: {
20232
20234
  paymentToken: payment == null ? void 0 : payment.token,
@@ -20247,8 +20249,8 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20247
20249
  setShouldSubmitData(true);
20248
20250
  };
20249
20251
  const handleFocusToPrevInput = () => {
20250
- var _iframe$contentWindow5;
20251
- (_iframe$contentWindow5 = iframe.contentWindow) == null || _iframe$contentWindow5.postMessage({
20252
+ var _iframeRef$current10;
20253
+ (_iframeRef$current10 = iframeRef.current) == null || (_iframeRef$current10 = _iframeRef$current10.contentWindow) == null || _iframeRef$current10.postMessage({
20252
20254
  type: 'setPrevFocus',
20253
20255
  payload: {
20254
20256
  shouldFocusOnCVV: true
@@ -20333,9 +20335,9 @@ const CardElement = /*#__PURE__*/(0,react.forwardRef)(({
20333
20335
  }
20334
20336
  };
20335
20337
  const handleOTPSubmit = async otp => {
20336
- var _iframe$contentWindow6, _payment$subType3;
20338
+ var _iframeRef$current11, _payment$subType3;
20337
20339
  setSendOTPLoading(true);
20338
- (_iframe$contentWindow6 = iframe.contentWindow) == null || _iframe$contentWindow6.postMessage({
20340
+ (_iframeRef$current11 = iframeRef.current) == null || (_iframeRef$current11 = _iframeRef$current11.contentWindow) == null || _iframeRef$current11.postMessage({
20339
20341
  type: 'otpData',
20340
20342
  payload: {
20341
20343
  paymentToken: payment.token,
@@ -31679,7 +31681,8 @@ class Pixel {
31679
31681
  });
31680
31682
  this._baseURL = this.updateAPIBaseURL();
31681
31683
  this._paymentsRef = /*#__PURE__*/react.createRef();
31682
- Pixel._instance = this;
31684
+ Pixel._instances[options.elementId] = this;
31685
+ Pixel._lastInstanceId = options.elementId;
31683
31686
  if (this._options['clientSecret'] && this._options['paymentMethods']) {
31684
31687
  this._integrationType = 'directPayment';
31685
31688
  this.getData();
@@ -31758,19 +31761,28 @@ class Pixel {
31758
31761
  this._options.cardValidationChanged(isValid);
31759
31762
  }
31760
31763
  }
31761
- static async updateIntentionData() {
31762
- var _Pixel$_instance;
31763
- if (!((_Pixel$_instance = Pixel._instance) != null && (_Pixel$_instance = _Pixel$_instance._paymentsRef) != null && (_Pixel$_instance = _Pixel$_instance.current) != null && _Pixel$_instance.getData)) {
31764
+ static getInstance(instanceId) {
31765
+ const targetId = instanceId || this._lastInstanceId;
31766
+ if (!targetId || !this._instances[targetId]) {
31767
+ throw new Error('Invalid ID or Pixel not initialized');
31768
+ }
31769
+ return this._instances[targetId];
31770
+ }
31771
+ static async updateIntentionData(instanceId) {
31772
+ var _instance$_paymentsRe;
31773
+ const instance = this.getInstance(instanceId);
31774
+ if (!((_instance$_paymentsRe = instance._paymentsRef) != null && (_instance$_paymentsRe = _instance$_paymentsRe.current) != null && _instance$_paymentsRe.getData)) {
31764
31775
  throw new Error('Pixel not initialized');
31765
31776
  }
31766
- return await Pixel._instance._paymentsRef.current.getData();
31777
+ return await instance._paymentsRef.current.getData();
31767
31778
  }
31768
- static async submit() {
31769
- var _Pixel$_instance2;
31770
- if (!((_Pixel$_instance2 = Pixel._instance) != null && (_Pixel$_instance2 = _Pixel$_instance2._paymentsRef) != null && (_Pixel$_instance2 = _Pixel$_instance2.current) != null && _Pixel$_instance2.handleGenerateToken)) {
31779
+ static async submit(instanceId) {
31780
+ var _instance$_paymentsRe2;
31781
+ const instance = this.getInstance(instanceId);
31782
+ if (!((_instance$_paymentsRe2 = instance._paymentsRef) != null && (_instance$_paymentsRe2 = _instance$_paymentsRe2.current) != null && _instance$_paymentsRe2.handleGenerateToken)) {
31771
31783
  throw new Error('Pixel not initialized');
31772
31784
  }
31773
- return await Pixel._instance._paymentsRef.current.handleGenerateToken();
31785
+ return await instance._paymentsRef.current.handleGenerateToken();
31774
31786
  }
31775
31787
  render() {
31776
31788
  const container = document.getElementById(this._options['elementId']);
@@ -31818,7 +31830,8 @@ class Pixel {
31818
31830
  }
31819
31831
  }
31820
31832
  _Pixel = Pixel;
31821
- Pixel._instance = null;
31833
+ Pixel._instances = {};
31834
+ Pixel._lastInstanceId = null;
31822
31835
  window.Pixel = Pixel;
31823
31836
  /* harmony default export */ const pixel = ((/* unused pure expression or super */ null && (Pixel)));
31824
31837
  ;// CONCATENATED MODULE: ./src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "paymob-pixel-alpha",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "keywords": ["paymob","pixel","paymob-pixel"],
5
5
  "main": "./main.js",
6
6
  "license": "MIT",