paymob-pixel-alpha 1.0.9 → 1.1.1

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 (4) hide show
  1. package/README.md +52 -14
  2. package/main.js +70 -84
  3. package/package.json +2 -1
  4. package/styles.css +4 -0
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # pixel
1
+ # `pixel`
2
2
  Paymob Web SDK, help our merchant to have the native payment experience.
3
3
 
4
4
  ## Installing
@@ -8,13 +8,13 @@ Paymob Web SDK, help our merchant to have the native payment experience.
8
8
  Using npm:
9
9
 
10
10
  ```bash
11
- $ npm install paymob-pixel-alpha --save
11
+ $ npm install paymob-pixel --save
12
12
  ```
13
13
 
14
14
  Once the package is installed, you can import the library using `import`:
15
15
 
16
16
  ```js
17
- import { Pixel } from 'paymob-pixel-alpha';
17
+ import { Pixel } from 'paymob-pixel';
18
18
  ``` -->
19
19
 
20
20
  ### CDN
@@ -27,14 +27,13 @@ Using jsDelivr:
27
27
  <script src="https://cdn.jsdelivr.net/npm/paymob-pixel-alpha@latest/main.js" type="module"></script>
28
28
  ```
29
29
 
30
- Using unpkg:
30
+ <!-- Using unpkg:
31
31
 
32
32
  ```html
33
33
  <link rel="stylesheet" href="https://unpkg.com/paymob-pixel-alpha/styles.css">
34
34
  <link rel="stylesheet" href="https://unpkg.com/paymob-pixel-alpha/main.css">
35
35
  <script src="https://unpkg.com/paymob-pixel-alpha/main.js" type="module"></script>
36
- ```
37
-
36
+ ``` -->
38
37
 
39
38
  ## Usage
40
39
 
@@ -52,7 +51,7 @@ new Pixel({
52
51
  > <script src="https://pay.google.com/gp/p/js/pay.js"></script>
53
52
  > ```
54
53
 
55
- ## properties
54
+ ## Properties
56
55
 
57
56
  The full list of properties is as follows:
58
57
  | Property | Type | Definition |
@@ -64,12 +63,52 @@ The full list of properties is as follows:
64
63
  | disablePay | Boolean | pass true If you don’t want to use Paymob’s Pay Button for Card Payment, in this case you will dispatchEvent with name (payFromOutside) to fire the pay. |
65
64
  | showSaveCard | Boolean | If this option is set to TRUE, users will have the option to save their card details for future payment. |
66
65
  | forceSaveCard | Boolean | If this option is set to true, the user's card details will be saved automatically without requiring their consent |
66
+ | cardValidationChanged | Function | This Functionality will be processed whenever card validation status changed. |
67
67
  | beforePaymentComplete | Function | Merchants can implement their own custom logic or functions before the payment is processed by Paymob. check the full example below. |
68
- | afterPaymentComplete | Function | This Functionality will be processed fter payment is processed by Paymob. check the full example below. |
68
+ | afterPaymentComplete | Function | This Functionality will be processed after payment is processed by Paymob. check the full example below. |
69
69
  | onPaymentCancel | Function | This function applies exclusively to Apple Pay. Merchants can implement their own custom logic to handle scenarios where a user cancels the Apple Pay payment by closing the Apple Pay SDK. |
70
70
  | customStyle | Object | You can pass custom styles, for more details check the full example below.
71
71
 
72
72
 
73
+ ## Events
74
+ The full list of events is as follows:
75
+ | Event | Definition |
76
+ | ------- | --------- |
77
+ | payFromOutside | In case you need to use you pay button instead of SDK pay button.
78
+
79
+
80
+ ## Functions
81
+ The full list of functions is as follows:
82
+ | Function | Props | Definition |
83
+ | --------------------- | -------------- | -------------- |
84
+ | updateIntentionData | - | Update intention data inside the SDK, in case any updates happend on the intention.
85
+
86
+
87
+ ## Custom Pay Button Example
88
+ > Add **disablePay** to hide SDK pay button
89
+ > ```js
90
+ > new Pixel({
91
+ > publicKey: 'are_pk_live_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
92
+ > clientSecret: 'are_csk_live_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
93
+ > paymentMethods: ['card', 'google-pay', 'apple-pay'],
94
+ > elementId: 'example',
95
+ > disablePay: true,
96
+ > });
97
+ > ```
98
+ > Then you can fire **payFromOutside** event when ever you want to start the payment, this will not work with Google-pay or Apple-pay
99
+ > ```js
100
+ > window.dispatchEvent(new Event('payFromOutside'));
101
+ > ```
102
+
103
+
104
+
105
+ ## Update Intention Example
106
+ > Whenever you made any update to the intention via API, you will need to update the intention data inside the SDK.
107
+ > ```js
108
+ > const response = await Pixel.updateIntentionData();
109
+ > ```
110
+ > **updateIntentionData** will return the response of the latest retrieve intention request.
111
+
73
112
  ## Full Example
74
113
 
75
114
  ```html
@@ -80,20 +119,19 @@ The full list of properties is as follows:
80
119
  <title>Pixels</title>
81
120
  <base href="/">
82
121
  <meta name="viewport" content="width=device-width, initial-scale=1" />
83
- <link rel="stylesheet" href="https://unpkg.com/paymob-pixel-alpha/styles.css">
84
- <link rel="stylesheet" href="https://unpkg.com/paymob-pixel-alpha/main.css">
122
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/paymob-pixel-alpha@latest/styles.css">
123
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/paymob-pixel-alpha@latest/main.css">
85
124
  </head>
86
125
  <body>
87
126
  <div style="position: absolute; width: 80%; margin-top: 10%;" id="example"></div>
88
127
  <button id="payFromOutsideButton">Pay From Outside Button</button>
89
128
 
90
- <script src="https://unpkg.com/paymob-pixel-alpha/main.js" type="module"></script>
129
+ <script src="https://cdn.jsdelivr.net/npm/paymob-pixel-alpha@latest/main.js" type="module"></script>
91
130
  <script>
92
131
  const button = document.getElementById('payFromOutsideButton');
93
132
  button.addEventListener('click', function () {
94
133
  // Calling pay request
95
- const event = new Event('payFromOutside');
96
- window.dispatchEvent(event);
134
+ window.dispatchEvent(new Event('payFromOutside'));
97
135
  });
98
136
  onload = (event) => {
99
137
  new Pixel({
@@ -107,7 +145,7 @@ The full list of properties is as follows:
107
145
  cardValidationChanged: (isValid) => {
108
146
  console.log("Is valid ? ", isValid)
109
147
  },
110
- beforePaymentComplete: async () => {
148
+ beforePaymentComplete: async (paymentMethod) => {
111
149
  console.log('Before payment start');
112
150
  console.log('We are waiting for 5 seconds');
113
151
  await new Promise(res => setTimeout(() => res(''),5000))
package/main.js CHANGED
@@ -19888,9 +19888,7 @@ function CardElement({
19888
19888
  onCardValidationChanged,
19889
19889
  showPaymobLogo,
19890
19890
  hideCardHolderName,
19891
- isLoading,
19892
- onProcessingPayment,
19893
- isProcessingPayment
19891
+ isLoading
19894
19892
  }) {
19895
19893
  var _customStyle$containe;
19896
19894
  const {
@@ -19924,7 +19922,6 @@ function CardElement({
19924
19922
  const [showOTP, setShowOTP] = (0,react.useState)(false);
19925
19923
  const [sendOTPLoading, setSendOTPLoading] = (0,react.useState)(false);
19926
19924
  const previousValidationRef = (0,react.useRef)(false);
19927
- const [startCardPay, setStartCardPay] = (0,react.useState)(false);
19928
19925
  const isCardValid = hideCardHolderName ? !isActionBtnDisabled : !(isActionBtnDisabled || !cardHolderName);
19929
19926
  const isAmexCardSelected = (selectedCard == null ? void 0 : selectedCard.type) === constants_CARD_TYPES.AMEX;
19930
19927
  const selectedCardCvvLength = selectedCardCVV.length;
@@ -20053,39 +20050,21 @@ function CardElement({
20053
20050
  return;
20054
20051
  }
20055
20052
  }
20056
- setStartCardPay(true);
20057
- onProcessingPayment(true);
20053
+ sendCardData(true);
20058
20054
  setShouldSubmitData(false);
20059
20055
  };
20060
20056
  handlePay();
20061
20057
  }
20062
20058
  }, [cardHolderName, saveCard, shouldSubmitData, iframe]);
20063
20059
  (0,react.useEffect)(() => {
20064
- if (isIframeLoaded || startCardPay && isProcessingPayment) {
20065
- var _iframe$contentWindow, _payment$subType, _payment$subType2;
20066
- (_iframe$contentWindow = iframe.contentWindow) == null || _iframe$contentWindow.postMessage({
20067
- type: 'cardData',
20068
- payload: {
20069
- paymentToken: payment.token,
20070
- subType: {
20071
- paymentToken: (_payment$subType = payment.subType) == null ? void 0 : _payment$subType.token,
20072
- type: (_payment$subType2 = payment.subType) == null ? void 0 : _payment$subType2.type
20073
- },
20074
- currency,
20075
- cardHolderName,
20076
- saveCard: forceSaveCard || saveCard,
20077
- tenure,
20078
- shouldSubmitData: startCardPay && isProcessingPayment
20079
- }
20080
- }, '*');
20081
- setStartCardPay(false);
20082
- onProcessingPayment(false);
20060
+ if (isIframeLoaded) {
20061
+ sendCardData();
20083
20062
  }
20084
- }, [isIframeLoaded, payment, isProcessingPayment]);
20063
+ }, [isIframeLoaded, payment]);
20085
20064
  (0,react.useEffect)(() => {
20086
20065
  if (isIframeLoaded) {
20087
- var _iframe$contentWindow2;
20088
- (_iframe$contentWindow2 = iframe.contentWindow) == null || _iframe$contentWindow2.postMessage({
20066
+ var _iframe$contentWindow;
20067
+ (_iframe$contentWindow = iframe.contentWindow) == null || _iframe$contentWindow.postMessage({
20089
20068
  type: 'customStyles',
20090
20069
  payload: {
20091
20070
  styling: customStyle,
@@ -20097,8 +20076,8 @@ function CardElement({
20097
20076
  }
20098
20077
  }, [isIframeLoaded, iframeRef.current]);
20099
20078
  (0,react.useEffect)(() => {
20100
- var _iframe$contentWindow3;
20101
- iframe == null || (_iframe$contentWindow3 = iframe.contentWindow) == null || _iframe$contentWindow3.postMessage({
20079
+ var _iframe$contentWindow2;
20080
+ iframe == null || (_iframe$contentWindow2 = iframe.contentWindow) == null || _iframe$contentWindow2.postMessage({
20102
20081
  type: 'cardHolderError',
20103
20082
  payload: {
20104
20083
  error: errors['name']
@@ -20116,6 +20095,24 @@ function CardElement({
20116
20095
  previousValidationRef.current = isCardValid;
20117
20096
  }
20118
20097
  }, [isCardValid, shouldDisablePayBtn]);
20098
+ const sendCardData = shouldSubmitData => {
20099
+ var _iframe$contentWindow3, _payment$subType, _payment$subType2;
20100
+ (_iframe$contentWindow3 = iframe.contentWindow) == null || _iframe$contentWindow3.postMessage({
20101
+ type: 'cardData',
20102
+ payload: {
20103
+ paymentToken: payment.token,
20104
+ subType: {
20105
+ paymentToken: (_payment$subType = payment.subType) == null ? void 0 : _payment$subType.token,
20106
+ type: (_payment$subType2 = payment.subType) == null ? void 0 : _payment$subType2.type
20107
+ },
20108
+ currency,
20109
+ cardHolderName,
20110
+ saveCard: forceSaveCard || saveCard,
20111
+ tenure,
20112
+ shouldSubmitData: shouldSubmitData
20113
+ }
20114
+ }, '*');
20115
+ };
20119
20116
  const handlePayWithoutButton = () => {
20120
20117
  setShouldSubmitData(true);
20121
20118
  };
@@ -27242,9 +27239,7 @@ function GooglePayElement({
27242
27239
  onBeforePaymentComplete,
27243
27240
  onAfterPaymentComplete,
27244
27241
  onErrorMessage,
27245
- isLoading,
27246
- onProcessingPayment,
27247
- isProcessingPayment
27242
+ isLoading
27248
27243
  }) {
27249
27244
  var _String;
27250
27245
  const {
@@ -27253,22 +27248,14 @@ function GooglePayElement({
27253
27248
  keyPrefix: 'googlePay'
27254
27249
  });
27255
27250
  const [loading, setLoading] = (0,react.useState)(false);
27256
- const [googlePayload, setGooglePayload] = (0,react.useState)(null);
27257
- const [startGooglePay, setStartGooglePay] = (0,react.useState)(false);
27258
27251
  const {
27259
27252
  amount,
27260
27253
  currency
27261
27254
  } = payment;
27262
27255
  const googlePay = payment;
27263
27256
  const integrationId = googlePay == null ? void 0 : googlePay.integrationId;
27264
- (0,react.useEffect)(() => {
27265
- if (startGooglePay && isProcessingPayment) {
27266
- handlePay(googlePay == null ? void 0 : googlePay.token, googlePayload);
27267
- setStartGooglePay(false);
27268
- onProcessingPayment(false);
27269
- }
27270
- }, [startGooglePay, isProcessingPayment]);
27271
27257
  const handleSubmit = async (paymentToken, googlePayload) => {
27258
+ var _res$data;
27272
27259
  setLoading(true);
27273
27260
  if (onBeforePaymentComplete) {
27274
27261
  const result = await onBeforePaymentComplete(payment.name);
@@ -27277,12 +27264,6 @@ function GooglePayElement({
27277
27264
  return;
27278
27265
  }
27279
27266
  }
27280
- setStartGooglePay(true);
27281
- onProcessingPayment(true);
27282
- setGooglePayload(googlePayload);
27283
- };
27284
- const handlePay = async (paymentToken, googlePayload) => {
27285
- var _res$data;
27286
27267
  const res = await payWithGooglePay(paymentToken, integrationId, googlePayload);
27287
27268
  setLoading(false);
27288
27269
  const redirectionUrl = (_res$data = res.data) == null ? void 0 : _res$data.redirection_url;
@@ -27491,7 +27472,7 @@ async function fetchPayments(publicKey, clientSecret, paymentMethods) {
27491
27472
 
27492
27473
 
27493
27474
 
27494
- function PaymentsList(props) {
27475
+ const PaymentsList = /*#__PURE__*/(0,react.forwardRef)((props, ref) => {
27495
27476
  var _customStyle$containe;
27496
27477
  const {
27497
27478
  payments,
@@ -27520,28 +27501,24 @@ function PaymentsList(props) {
27520
27501
  const [savedCardsList, setSavedCardsList] = (0,react.useState)(savedCards);
27521
27502
  const [errorMessage, setErrorMessage] = (0,react.useState)('');
27522
27503
  const [isLoading, setIsLoading] = (0,react.useState)(false);
27523
- const [isProcessingPayment, setIsProcessingPayment] = (0,react.useState)(false);
27524
- (0,react.useEffect)(() => {
27525
- window.addEventListener('updateIntentionData', getData);
27526
- return () => {
27527
- window.removeEventListener('updateIntentionData', getData);
27528
- };
27529
- }, []);
27530
- const getData = async () => {
27531
- setIsLoading(true);
27532
- const {
27533
- res,
27534
- paymentList,
27535
- savedCards
27536
- } = await fetchPayments(publicKey, clientSecret, selectedPaymentMethods);
27537
- setIsLoading(false);
27538
- if (res.status === 200) {
27539
- setPaymentList(paymentList);
27540
- setSavedCardsList(savedCards);
27504
+ (0,react.useImperativeHandle)(ref, () => ({
27505
+ getData: async () => {
27506
+ setIsLoading(true);
27507
+ const {
27508
+ res,
27509
+ paymentList,
27510
+ savedCards
27511
+ } = await fetchPayments(publicKey, clientSecret, selectedPaymentMethods);
27512
+ setIsLoading(false);
27513
+ if (res.status === 200) {
27514
+ setPaymentList(paymentList);
27515
+ setSavedCardsList(savedCards);
27516
+ }
27517
+ return res;
27541
27518
  }
27542
- };
27519
+ }));
27543
27520
  const handleOnBeforePaymentComplete = async paymentMethod => {
27544
- return await onBeforePaymentComplete(paymentMethod, getData);
27521
+ return await onBeforePaymentComplete(paymentMethod);
27545
27522
  };
27546
27523
  const hasOnlyXPays = () => {
27547
27524
  const xPays = ['apple-pay', 'google-pay'];
@@ -27591,18 +27568,14 @@ function PaymentsList(props) {
27591
27568
  onAfterPaymentComplete: onAfterPaymentComplete,
27592
27569
  onPaymentCancel: onPaymentCancel,
27593
27570
  customStyle: customStyle == null ? void 0 : customStyle.button,
27594
- isLoading: isLoading,
27595
- onProcessingPayment: setIsProcessingPayment,
27596
- isProcessingPayment: isProcessingPayment
27571
+ isLoading: isLoading
27597
27572
  }), isGooglePay() && /*#__PURE__*/(0,jsx_runtime.jsx)(GooglePayElement, {
27598
27573
  payment: googlePaymentMethod,
27599
27574
  customStyle: customStyle == null ? void 0 : customStyle.button,
27600
27575
  onBeforePaymentComplete: onBeforePaymentComplete && handleOnBeforePaymentComplete,
27601
27576
  onAfterPaymentComplete: onAfterPaymentComplete,
27602
27577
  onErrorMessage: setErrorMessage,
27603
- isLoading: isLoading,
27604
- onProcessingPayment: setIsProcessingPayment,
27605
- isProcessingPayment: isProcessingPayment
27578
+ isLoading: isLoading
27606
27579
  })]
27607
27580
  })
27608
27581
  }), paymentList.length === 0 ? /*#__PURE__*/(0,jsx_runtime.jsx)("div", {
@@ -27641,13 +27614,11 @@ function PaymentsList(props) {
27641
27614
  showPaymobLogo,
27642
27615
  hideCardHolderName,
27643
27616
  isLoading,
27644
- onProcessingPayment: setIsProcessingPayment,
27645
- isProcessingPayment,
27646
27617
  isList: true
27647
27618
  })
27648
27619
  })]
27649
27620
  });
27650
- }
27621
+ });
27651
27622
  /* harmony default export */ const List = (PaymentsList);
27652
27623
  ;// CONCATENATED MODULE: ./src/utils/getStyles.ts
27653
27624
  function getStyles(customStyle) {
@@ -27782,7 +27753,7 @@ function getStyles(customStyle) {
27782
27753
 
27783
27754
 
27784
27755
 
27785
- function Payments_Payments(props) {
27756
+ const Payments_Payments = /*#__PURE__*/(0,react.forwardRef)((props, ref) => {
27786
27757
  const {
27787
27758
  payment,
27788
27759
  customStyle,
@@ -27918,13 +27889,14 @@ function Payments_Payments(props) {
27918
27889
  hideCardHolderName: hideCardHolderName,
27919
27890
  publicKey: publicKey,
27920
27891
  clientSecret: clientSecret,
27921
- selectedPaymentMethods: selectedPaymentMethods
27892
+ selectedPaymentMethods: selectedPaymentMethods,
27893
+ ref: ref
27922
27894
  });
27923
27895
  };
27924
27896
  return /*#__PURE__*/(0,jsx_runtime.jsx)(jsx_runtime.Fragment, {
27925
27897
  children: render()
27926
27898
  });
27927
- }
27899
+ });
27928
27900
  /* harmony default export */ const components_Payments = (Payments_Payments);
27929
27901
  ;// CONCATENATED MODULE: ./src/utils/apiUrl.ts
27930
27902
 
@@ -31471,6 +31443,7 @@ i18next.use(initReactI18next).init({
31471
31443
  });
31472
31444
  /* harmony default export */ const localization_i18n = ((/* unused pure expression or super */ null && (i18n)));
31473
31445
  ;// CONCATENATED MODULE: ./src/lib/pixel.tsx
31446
+ var _Pixel;
31474
31447
 
31475
31448
 
31476
31449
 
@@ -31491,10 +31464,13 @@ class Pixel {
31491
31464
  this._showSaveCard = void 0;
31492
31465
  this._root = null;
31493
31466
  this._error = null;
31467
+ this._paymentsRef = null;
31494
31468
  this._options = Object.assign({}, options, {
31495
31469
  showPaymobLogo: (_options$showPaymobLo = options.showPaymobLogo) != null ? _options$showPaymobLo : true
31496
31470
  });
31497
31471
  this._baseURL = this.updateAPIBaseURL();
31472
+ this._paymentsRef = /*#__PURE__*/react.createRef();
31473
+ Pixel._instance = this;
31498
31474
  this.getData();
31499
31475
  }
31500
31476
  updateAPIBaseURL() {
@@ -31536,9 +31512,9 @@ class Pixel {
31536
31512
  this.render();
31537
31513
  }
31538
31514
  }
31539
- async handleBeforePaymentComplete(paymentMethod, updateIntentionData) {
31515
+ async handleBeforePaymentComplete(paymentMethod) {
31540
31516
  if (this._options.beforePaymentComplete) {
31541
- const result = await this._options.beforePaymentComplete(paymentMethod, updateIntentionData);
31517
+ const result = await this._options.beforePaymentComplete(paymentMethod);
31542
31518
  if (result === false) return false;
31543
31519
  return true;
31544
31520
  }
@@ -31558,6 +31534,13 @@ class Pixel {
31558
31534
  this._options.cardValidationChanged(isValid);
31559
31535
  }
31560
31536
  }
31537
+ static async updateIntentionData() {
31538
+ var _Pixel$_instance;
31539
+ if (!((_Pixel$_instance = Pixel._instance) != null && (_Pixel$_instance = _Pixel$_instance._paymentsRef) != null && (_Pixel$_instance = _Pixel$_instance.current) != null && _Pixel$_instance.getData)) {
31540
+ throw new Error('Pixel not initialized');
31541
+ }
31542
+ return await Pixel._instance._paymentsRef.current.getData();
31543
+ }
31561
31544
  render() {
31562
31545
  const container = document.getElementById(this._options['elementId']);
31563
31546
  if (!container) {
@@ -31587,7 +31570,8 @@ class Pixel {
31587
31570
  hideCardHolderName: this._options['hideCardHolderName'],
31588
31571
  publicKey: this._options['publicKey'],
31589
31572
  clientSecret: this._options['clientSecret'],
31590
- selectedPaymentMethods: this._options['paymentMethods']
31573
+ selectedPaymentMethods: this._options['paymentMethods'],
31574
+ ref: this._paymentsRef
31591
31575
  }, this._options.beforePaymentComplete && {
31592
31576
  onBeforePaymentComplete: this.handleBeforePaymentComplete.bind(this)
31593
31577
  }, this._options.afterPaymentComplete && {
@@ -31600,6 +31584,8 @@ class Pixel {
31600
31584
  }));
31601
31585
  }
31602
31586
  }
31587
+ _Pixel = Pixel;
31588
+ Pixel._instance = null;
31603
31589
  window.Pixel = Pixel;
31604
31590
  /* harmony default export */ const pixel = ((/* unused pure expression or super */ null && (Pixel)));
31605
31591
  ;// CONCATENATED MODULE: ./src/index.ts
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "paymob-pixel-alpha",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "keywords": ["paymob","pixel","paymob-pixel"],
5
5
  "main": "./main.js",
6
+ "license": "MIT",
6
7
  "homepage": "https://developers.paymob.com",
7
8
  "author": "Paymob <support@paymob.com> (https://developers.paymob.com)",
8
9
  "exports": {
package/styles.css CHANGED
@@ -612,6 +612,10 @@ video {
612
612
  visibility: hidden;
613
613
  }
614
614
 
615
+ .static {
616
+ position: static;
617
+ }
618
+
615
619
  .fixed {
616
620
  position: fixed;
617
621
  }