@abdelrahman.rabie/payment-sdk-react-native
Version:
React Native SDK for payment processing with E_API and E_LINKS support
199 lines (149 loc) ⢠5.05 kB
Markdown
# Payment SDK React Native
A comprehensive React Native SDK for payment processing with E_API and E_LINKS support, including Apple Pay and Google Pay integration.
## Features
- š Easy integration with React Native apps
- š³ Support for multiple payment methods (Visa, Mastercard, Apple Pay, Google Pay, KNET)
- š E_API and E_LINKS payment products
- š Apple Pay merchant validation
- š£ React hooks for seamless state management
- š± TypeScript support with full type definitions
- š Multi-language support (Arabic/English)
## Installation
```bash
npm install @abdelrahman.rabie/payment-sdk-react-native
# or
yarn add @abdelrahman.rabie/payment-sdk-react-native
For iOS projects using CocoaPods:
```bash
cd ios && pod install
```
## Quick Start
### 1. Initialize the SDK
```typescript
import { PaymentSDK } from "payment-sdk-react-native";
const sdk = new PaymentSDK({
baseURL: "https://your-payment-api.com",
paymentPageApiToken: "your-api-token",
paymentCoreURL: "https://your-payment-core-api.com", // optional
});
```
### 2. Using React Hooks
```typescript
import React from "react";
import { View, Button, Text } from "react-native";
import {
usePayment,
EPaymentProduct,
EPaymentMethods,
ELanguages,
} from "payment-sdk-react-native";
const PaymentScreen = () => {
const { loading, error, paymentInfo, executePayment, initializePayment } =
usePayment({ sdk });
const handleInitialize = async () => {
await initializePayment(EPaymentProduct.E_API, "your-payment-token");
};
const handlePayment = async () => {
await executePayment(EPaymentProduct.E_API, "your-payment-token", {
paymentMethod: EPaymentMethods.VISA,
language: ELanguages.EN,
});
};
return (
<View>
<Button title="Initialize Payment" onPress={handleInitialize} />
<Button
title="Pay with Visa"
onPress={handlePayment}
disabled={loading}
/>
{error && <Text style={{ color: "red" }}>{error}</Text>}
{paymentInfo && <Text>Amount: {paymentInfo.data.amount.value}</Text>}
</View>
);
};
```
### 3. Apple Pay Integration
```typescript
import { useApplePay } from "payment-sdk-react-native";
const ApplePayComponent = () => {
const { loading, error, validateMerchant } = useApplePay({ sdk });
const handleApplePayValidation = async (validationURL: string) => {
await validateMerchant(validationURL);
};
// Use with Apple Pay button implementation
};
```
## API Reference
### PaymentSDK
Main SDK class for payment operations.
#### Methods
- `initializePayment(product, paymentToken)` - Get payment information
- `executePayment(product, paymentToken, payload)` - Process payment
- `getPaymentStatus(product, paymentToken)` - Get payment status
- `validateApplePayMerchant(validationURL)` - Validate Apple Pay merchant
### Hooks
#### usePayment
React hook for payment operations.
**Returns:**
- `loading` - Loading state
- `error` - Error message
- `paymentInfo` - Payment information
- `paymentResult` - Payment result
- `initializePayment()` - Initialize payment function
- `executePayment()` - Execute payment function
- `getPaymentStatus()` - Get status function
#### useApplePay
React hook for Apple Pay operations.
**Returns:**
- `loading` - Loading state
- `error` - Error message
- `applePaySession` - Apple Pay session data
- `validateMerchant()` - Validate merchant function
## Payment Products
- `EPaymentProduct.E_API` - API-based payments
- `EPaymentProduct.E_LINKS` - Link-based payments
## Payment Methods
- `EPaymentMethods.VISA`
- `EPaymentMethods.MASTERCARD`
- `EPaymentMethods.APPLE_PAY`
- `EPaymentMethods.GOOGLE_PAY`
- `EPaymentMethods.KNET`
- And more...
## Error Handling
The SDK provides comprehensive error handling:
```typescript
try {
const result = await sdk.executePayment(product, token, payload);
if (result.success) {
// Payment successful
console.log("Payment completed:", result.data);
} else {
// Payment failed
console.error("Payment failed:", result.error);
}
} catch (error) {
console.error("SDK error:", error.message);
}
```
## TypeScript Support
The SDK is fully typed with TypeScript. All interfaces and types are exported for use in your application.
## License
MIT
# New SDK Package Structure
payment-sdk-react-native/
āāā src/
ā āāā core/PaymentSDK.ts # Main SDK class
ā āāā services/
ā ā āāā httpClient.ts # HTTP client for API calls
ā ā āāā paymentService.ts # Payment logic extracted from your app
ā āāā hooks/
ā ā āāā usePayment.ts # React hook for payments
ā ā āāā useApplePay.ts # React hook for Apple Pay
ā āāā types/payment.types.ts # All TypeScript interfaces
ā āāā constants/endpoints.ts # API endpoints and regex
ā āāā index.ts # Main exports
āāā example/App.tsx # Complete usage example
āāā package.json # NPM package config
āāā tsconfig.json # TypeScript config
āāā README.md # Documentation