Skip to content

Commit feba15c

Browse files
committed
feat: generate paymentId on server
1 parent ee809d0 commit feba15c

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

react/lib/components/PayButton/PayButton.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import {
1313
isValidCashAddress,
1414
isValidXecAddress,
1515
CurrencyObject,
16-
generatePaymentId,
1716
getCurrencyObject,
1817
isPropsTrue,
1918
setupAltpaymentSocket,
2019
setupChronikWebSocket,
2120
CryptoCurrency,
2221
ButtonSize
2322
} from '../../util';
23+
import { createPayment } from '../../util/api-client';
2424
import { PaymentDialog } from '../PaymentDialog';
2525
import { AltpaymentCoin, AltpaymentError, AltpaymentPair, AltpaymentShift } from '../../altpayment';
2626
export interface PayButtonProps extends ButtonProps {
@@ -109,11 +109,26 @@ export const PayButton = (props: PayButtonProps): React.ReactElement => {
109109
sizeScaleAlreadyApplied
110110
} = Object.assign({}, PayButton.defaultProps, props);
111111

112-
const [paymentId] = useState(!disablePaymentId ? generatePaymentId(8) : undefined);
112+
const [paymentId, setPaymentId] = useState<string | undefined>(undefined);
113113
const [addressType, setAddressType] = useState<CryptoCurrency>(
114114
getCurrencyTypeFromAddress(to),
115115
);
116116

117+
useEffect(() => {
118+
const initializePaymentId = async () => {
119+
if (!disablePaymentId && to) {
120+
try {
121+
const responsePaymentId = await createPayment(amount, to, apiBaseUrl);
122+
setPaymentId(responsePaymentId);
123+
} catch (error) {
124+
console.error('Error creating payment ID:', error);
125+
}
126+
}
127+
};
128+
129+
initializePaymentId();
130+
}, [disablePaymentId, amount, to, apiBaseUrl]);
131+
117132
useEffect(() => {
118133
priceRef.current = price;
119134
}, [price]);

react/lib/components/Widget/WidgetContainer.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
Currency,
1010
CurrencyObject,
1111
Transaction,
12-
generatePaymentId,
1312
getCurrencyTypeFromAddress,
1413
isCrypto,
1514
isGreaterThanZero,
@@ -18,6 +17,7 @@ import {
1817
shouldTriggerOnSuccess,
1918
isPropsTrue,
2019
} from '../../util';
20+
import { createPayment } from '../../util/api-client';
2121

2222
import Widget, { WidgetProps } from './Widget';
2323

@@ -139,13 +139,23 @@ export const WidgetContainer: React.FunctionComponent<WidgetContainerProps> =
139139
const [thisPrice, setThisPrice] = useState(0);
140140
const [usdPrice, setUsdPrice] = useState(0);
141141
useEffect(() => {
142-
if ((paymentId === undefined || paymentId === '') && !disablePaymentId) {
143-
const newPaymentId = generatePaymentId(8);
144-
setThisPaymentId(newPaymentId)
145-
} else {
146-
setThisPaymentId(paymentId)
147-
}
148-
}, [paymentId, disablePaymentId]);
142+
const initializePaymentId = async () => {
143+
if ((paymentId === undefined || paymentId === '') && !disablePaymentId) {
144+
if (to) {
145+
try {
146+
const responsePaymentId = await createPayment(amount, to, apiBaseUrl);
147+
setThisPaymentId(responsePaymentId);
148+
} catch (error) {
149+
console.error('Error creating payment ID:', error);
150+
}
151+
}
152+
} else {
153+
setThisPaymentId(paymentId);
154+
}
155+
};
156+
157+
initializePaymentId();
158+
}, [paymentId, disablePaymentId, amount, to, apiBaseUrl]);
149159
const [success, setSuccess] = useState(false);
150160
const { enqueueSnackbar } = useSnackbar();
151161

react/lib/util/api-client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ export const getTransactionDetails = async (
8989
return res.json();
9090
};
9191

92+
export const createPayment = async (
93+
amount: string | number | undefined,
94+
address: string,
95+
rootUrl = config.apiBaseUrl,
96+
): Promise<string> => {
97+
const { data } = await axios.post(
98+
`${rootUrl}/api/payments/paymentId`,
99+
{ amount, address }
100+
);
101+
return data.paymentId;
102+
103+
};
104+
105+
92106
export default {
93107
getAddressDetails,
94108
getTransactionDetails,

0 commit comments

Comments
 (0)