import * as React from 'react'; import sabpaisaService from '../../../services/sabpaisa-service'; import { Button, Form, FormGroup, Label, Input } from 'reactstrap'; import { MappingBeanInterface, UpiPaymentPayload } from '../../../services/Models/sabpaisa-payment-types'; import { getPaymentModeByName } from '../../../helpers/common-helper'; export interface Props { updateState: (arg: number) => void; [key: string]: any, makeApiReqCardFeeCalculation: (arg: MappingBeanInterface) => void; paymentData: { [key: string]: any }, updateStateWithError: (resError: any, screenId: number) => void, } export interface State { isVerified: boolean, processing: boolean, isPaymentProceed: boolean, [key: string]: any, } export const isValidUPISyntax = (vpa: string) => { const upiRegExp = /^[\w.-]+@[\w.-]+$/; return upiRegExp.test(vpa) } export default class UpiMethods extends React.Component { constructor(props: Props) { super(props); this.state = { submitted: false, currentState: 0, isVerifying: false, isVerified: false, processing: false, isPaymentProceed: false, vpa: '' }; } onChangeUPI = (e: any) => { const { value } = e.target; this.setState({ vpa: value, isVerified: false, isPaymentProceed: false }) } // UPI Verification makeReqForVallidation = async () => { const { vpa } = this.state; const payload: { username: any, password: any } = { username: process.env.VPA_USER_NAME, password: process.env.VPA_PASSWORD } if (isValidUPISyntax(vpa)) { try { this.setState({ isVerifying: true, isVerified: false, error: "", isPaymentProceed: false }); const response = await sabpaisaService.vpaAuthenticate(payload); const { token } = response.data; const response2 = await sabpaisaService.vpaValidate({ vpa, bankName: 'CashFree', token }) this.setState({ isVerifying: false, isVerified: response2.data.valid }); } catch (error: any) { this.setState({ isVerifying: false, isPaymentProceed: true }); } } } makeApiReqForUpiPayment = async (paymentData: any, vpa: string) => { const { mappingBean, amountBean, amountType, clientCode, clientId, clientName, clientTxnId, payerEmail, payerMobNumber, requestAmount, spTxnId, } = paymentData; const { endpoint, paymode } = mappingBean; this.setState({ processing: true }) const payload: UpiPaymentPayload = { clientId, paidAmount: amountBean["paidAmount"], txnId: spTxnId, clientTxnId, clientName, clientCode, requestAmount, payeeEmail: payerEmail, payeeMobile: payerMobNumber, amountType, payMode: paymode, endPoint: endpoint, udf1: vpa, } sabpaisaService.upiPayment(payload) .then(response => { this.setState({ processing: false }); if (response.data.bankUrl) { window.open(response.data.bankUrl, "_blank") } }) .catch(err => { this.props.updateStateWithError(err, 400); this.setState({ processing: false }); }) } // LifeCycle methods componentDidMount() { const { paymentData } = this.props; const upiDetails = (paymentData && paymentData.activeMapping.length) ? (getPaymentModeByName("upi", paymentData.activeMapping))[0] : {}; let data = { ...upiDetails } this.props.makeApiReqCardFeeCalculation(data); } render() { const { makeReqForVallidation, onChangeUPI, makeApiReqForUpiPayment, state, props } = this; const { isVerified, isVerifying, vpa, processing, isPaymentProceed } = state; const { paymentData } = props; const { amountBean } = paymentData const isRenderUpi = Object.hasOwn(amountBean, 'paidAmount'); return (
{/*

UPI

*/} {/*
gpay Gpay gpay Phonepe gpay Paytm gpay Bhim Upi
*/} {isRenderUpi &&
{!isVerifying && } {isVerifying && }

A payment request notification will be sent to this UPI ID

{((isPaymentProceed || isVerified) && !processing) && } {((isPaymentProceed || isVerified) && processing) && }
}
); } }