@abdelrahman.rabie/payment-sdk-react-native
Version:
React Native SDK for payment processing with E_API and E_LINKS support
77 lines (76 loc) • 2.35 kB
JavaScript
import { useState, useCallback } from 'react';
export const usePayment = ({ sdk }) => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [paymentInfo, setPaymentInfo] = useState(null);
const [paymentResult, setPaymentResult] = useState(null);
const clearError = useCallback(() => {
setError(null);
}, []);
const reset = useCallback(() => {
setLoading(false);
setError(null);
setPaymentInfo(null);
setPaymentResult(null);
}, []);
const initializePayment = useCallback(async (product, paymentToken) => {
try {
setLoading(true);
setError(null);
const info = await sdk.initializePayment(product, paymentToken);
setPaymentInfo(info);
}
catch (err) {
setError(err.message || 'Failed to initialize payment');
}
finally {
setLoading(false);
}
}, [sdk]);
const executePayment = useCallback(async (product, paymentToken, payload) => {
try {
setLoading(true);
setError(null);
const result = await sdk.executePayment(product, paymentToken, payload);
setPaymentResult(result);
if (!result.success) {
setError(result.error || 'Payment failed');
}
}
catch (err) {
setError(err.message || 'Failed to execute payment');
setPaymentResult({
success: false,
error: err.message || 'Payment failed',
});
}
finally {
setLoading(false);
}
}, [sdk]);
const getPaymentStatus = useCallback(async (product, paymentToken) => {
try {
setLoading(true);
setError(null);
const info = await sdk.getPaymentStatus(product, paymentToken);
setPaymentInfo(info);
}
catch (err) {
setError(err.message || 'Failed to get payment status');
}
finally {
setLoading(false);
}
}, [sdk]);
return {
loading,
error,
paymentInfo,
paymentResult,
initializePayment,
executePayment,
getPaymentStatus,
clearError,
reset,
};
};