UNPKG

@abdelrahman.rabie/payment-sdk-react-native

Version:

React Native SDK for payment processing with E_API and E_LINKS support

140 lines (139 loc) 4.64 kB
import { EENDPOINTS, PAYMENT_TOKEN_REGEX } from '../constants/endpoints'; import { EPaymentProduct, } from '../types/payment.types'; export class PaymentService { constructor(httpClient) { this.httpClient = httpClient; } /** * Validates payment token format based on product type */ validatePaymentToken(product, token) { switch (product) { case EPaymentProduct.E_LINKS: return PAYMENT_TOKEN_REGEX.E_LINKS.test(token); case EPaymentProduct.E_API: return PAYMENT_TOKEN_REGEX.E_API.test(token); default: return false; } } /** * Get charge details for E_API product */ async getChargeDetails(token) { if (!this.validatePaymentToken(EPaymentProduct.E_API, token)) { throw new Error('Invalid E_API payment token format'); } try { const response = await this.httpClient.post(`${EENDPOINTS.EAPI}${EENDPOINTS.REQUEST_CHARGE_INFO}/${token}`); return response; } catch (error) { throw new Error(`Failed to get charge details: ${error}`); } } /** * Get invoice details for E_LINKS product */ async getInvoiceDetails(token) { if (!this.validatePaymentToken(EPaymentProduct.E_LINKS, token)) { throw new Error('Invalid E_LINKS payment token format'); } try { const response = await this.httpClient.post(`${EENDPOINTS.ELINKS}${EENDPOINTS.REQUEST_CHARGE_INFO}/${token}`); return response; } catch (error) { throw new Error(`Failed to get invoice details: ${error}`); } } /** * Get payment info based on product type */ async getPaymentInfo(product, token) { if (!token) { throw new Error('Payment token is required'); } if (!Object.values(EPaymentProduct).includes(product)) { throw new Error('Invalid payment product'); } switch (product) { case EPaymentProduct.E_API: return this.getChargeDetails(token); case EPaymentProduct.E_LINKS: return this.getInvoiceDetails(token); default: throw new Error('Unsupported payment product'); } } /** * Process payment for E_API product */ async requestChargePay(token, payload) { if (!this.validatePaymentToken(EPaymentProduct.E_API, token)) { throw new Error('Invalid E_API payment token format'); } try { const response = await this.httpClient.post(`${EENDPOINTS.EAPI}${EENDPOINTS.REQUEST_CHARGE_PAY}/${token}`, payload); return { success: true, data: response, }; } catch (error) { return { success: false, error: error.message || 'Payment failed', }; } } /** * Process payment for E_LINKS product */ async requestInvoicePay(token, payload) { if (!this.validatePaymentToken(EPaymentProduct.E_LINKS, token)) { throw new Error('Invalid E_LINKS payment token format'); } try { const response = await this.httpClient.post(`${EENDPOINTS.ELINKS}${EENDPOINTS.REQUEST_CHARGE_PAY}/${token}`, payload); return { success: true, data: response, }; } catch (error) { return { success: false, error: error.message || 'Payment failed', }; } } /** * Process payment based on product type */ async processPayment(product, token, payload) { switch (product) { case EPaymentProduct.E_API: return this.requestChargePay(token, payload); case EPaymentProduct.E_LINKS: return this.requestInvoicePay(token, payload); default: return { success: false, error: 'Unsupported payment product', }; } } /** * Validate Apple Pay merchant */ async validateApplePayMerchant(validationURL) { try { const response = await this.httpClient.paymentCorePost(EENDPOINTS.VALIDATE_APPLE_PAY_MERCHANT, { validationURL }); return response; } catch (error) { throw new Error(`Apple Pay validation failed: ${error.message}`); } } }