xpay-redirect 0.3.0 → 1.0.0

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.
@@ -1,145 +0,0 @@
1
- import React from 'react';
2
-
3
- // Material UI (ui lib)
4
- import Button from '@mui/material/Button';
5
- // axios (api call lib)
6
- import axios from 'axios';
7
-
8
- function strcmpXPAY(a, b) {
9
- return (a < b ? -1 : (a > b ? 1 : 0));
10
- };
11
-
12
- export default function PayNowBtn({ config }) {
13
-
14
- // for calling only once due to react18 useEffect calls twice
15
- const callingOnce = React.useRef(true);
16
- const { baseurl } = config;
17
- const onResponse = config?.onResponse
18
- ? config.onResponse
19
- : (data) => {
20
- console.log("Btn Response ::> ", data);
21
- };
22
-
23
- const onError = config?.onError
24
- ? config.onError
25
- : (err) => {
26
- throw err;
27
- };
28
-
29
- // default btn disabled, enables when gets payment token ID
30
- const [webPayNowBtnDisable, setWebPayNowBtnDisable] = React.useState(true);
31
-
32
- // webpages sends data-
33
- const [sessionId, setSessionId] = React.useState(config?.sessionid);
34
- // error message at checking payment token
35
- const [errMsg, setErrMsg] = React.useState('');
36
-
37
- // openSessionPurchase response ptk
38
- const [paymentTokenId, setPaymentTokenId] = React.useState(null);
39
-
40
- // API calls while pay-now btn shows
41
- const OpenSessionPurchase = async() => {
42
- // open session api call
43
- let jsonData = {
44
- "channelId":config?.channelId || '',
45
- "loginId": config?.loginId || '',
46
- "password": config?.password || '',
47
- "merchantID": config?.merchantID || '',
48
- "signature": config?.signature || '',
49
- "integrityCheckString": config?.integrityCheckString || '',
50
- "transactionDetails": config?.transactionDetails || ''
51
- }
52
-
53
- try {
54
- const response = await axios({
55
- method: 'post',
56
- url :`${baseurl}/openSessionv2`,
57
- data: jsonData
58
- });
59
- onResponse({"at":"/openSessionv2", "response":response.data});
60
- setSessionId(response.data.result.sessionid);
61
- setPaymentTokenId(response.data.result.xTran.paymentTokenid);
62
-
63
- // set Payouts api call
64
- let data = {
65
- "ptokenId": response.data.result.xTran.paymentTokenid || "",
66
- "sessionid": response.data.result.sessionid || "",
67
- // "payoutrequest": config.transactionDetails.amount
68
- "xpayPayOutRequest": [{
69
- "poAccount": "0",
70
- "poAmount": `${config.transactionDetails.amount}`
71
- }]
72
- }
73
- try {
74
- const resNext = await axios({
75
- method: 'post',
76
- url: `${baseurl}/SetPayouts`,
77
- data: data
78
- });
79
- onResponse({"at":"/SetPayouts", "response":resNext.data});
80
- if (strcmpXPAY(resNext.data.errorMessage, "SUCCESS") == 0) {
81
- setWebPayNowBtnDisable(false);
82
- setErrMsg('');
83
- } else {
84
- setWebPayNowBtnDisable(true);
85
- setErrMsg(resNext.data.errorMessage);
86
- }
87
- } catch (error) {
88
- onError({"at":"/SetPayouts", "error":error?.response?.data || error?.message || error});
89
- }
90
-
91
-
92
- } catch (error) {
93
- onError({"at":"/openSessionv2", "error":error?.response?.data || error?.message || error});
94
- }
95
-
96
- };
97
- // init transaction
98
- const handlePayNowTransaction = async() => {
99
- let text = {
100
- "ptokenId": paymentTokenId || "",
101
- "sessionid": sessionId || ""
102
- };
103
- try {
104
- const res = await axios({
105
- method: 'post',
106
- url: `${baseurl}/initialiseTransactionV2`,
107
- data: text
108
- });
109
- onResponse({"at":"/initialiseTransactionV2", "response":res.data});
110
-
111
- sessionStorage.setItem("sessionid", sessionId);
112
- sessionStorage.setItem("paymenttokenid", paymentTokenId);
113
- window.location.href = '/XPayConnector';
114
- } catch (error) {
115
- onError({"at":"/initialiseTransactionV2", "error":error?.response?.data || error?.message || error});
116
- }
117
- };
118
-
119
- React.useEffect(() => {
120
- if(callingOnce.current) {
121
- callingOnce.current=false;
122
- if (!config) onError(new Error("Please pass all required config in Props"));
123
- OpenSessionPurchase();
124
- }
125
-
126
- return () => { }
127
- }, []);
128
-
129
- return (
130
- <Button
131
- variant='contained'
132
- style={{
133
- textTransform: 'none',
134
- ...config?.style
135
- }}
136
- className='web-paynow-btn'
137
- disabled={webPayNowBtnDisable}
138
- onClick={() => {
139
- handlePayNowTransaction();
140
- }}
141
- >
142
- {errMsg || config?.btnName}
143
- </Button>
144
- );
145
- };