1.0.3 • Published 7 months ago
daraza v1.0.3
Daraza Pay
A lightweight, flexible payment processing library for mobile money transactions.
Installation
npm install daraza
Usage
import { PaymentService } from 'daraza';
// Initialize the payment service
const paymentService = new PaymentService({
apiKey: 'YOUR_API_KEY'
});
// Process a payment
async function processPayment() {
try {
// Validate phone number and amount
const validatedPhone = paymentService.validatePhoneNumber('772123456');
const validatedAmount = paymentService.validateAmount(1000);
// Prepare payment data
const paymentData = {
method: 1,
amount: validatedAmount,
phone: validatedPhone,
note: 'Payment'
};
// Request to pay
const response = await paymentService.requestToPay(paymentData);
if (response.code === 'Success') {
console.log('Payment successful:', response);
} else {
console.error('Payment failed:', response.details);
}
} catch (error) {
console.error('Payment error:', error);
}
}
API Reference
PaymentService Constructor
new PaymentService(config: PaymentConfig)
PaymentConfig
apiKey
: (Required) Your API key for payment processing
Methods
validatePhoneNumber(phoneNumber: string): string
- Validates and formats phone number
- Throws an error for invalid phone numbers
- Returns formatted phone number (e.g., '+256772123456')
validateAmount(amount: number): number
- Validates payment amount
- Throws an error for invalid amounts
- Returns validated amount
requestToPay(data: PaymentData): Promise<PaymentResponse>
- Sends payment request to the API
- Returns payment response
PaymentData Interface
interface PaymentData {
method: number;
amount: number;
phone: string;
note: string;
}
PaymentResponse Interface
interface PaymentResponse {
code: string;
details?: string;
}
Error Handling
The library throws descriptive errors for:
- Invalid phone numbers
- Invalid payment amounts
- API request failures
Features
- Simple, lightweight payment processing
- Phone number validation
- Amount validation
- Flexible API configuration
- TypeScript support
Requirements
- Node.js 14+
- Daraza API Key
License
MIT
The key differences from the previous README:
1. Removed React-specific references
2. Updated installation instructions
3. Provided comprehensive usage example
4. Added API reference with detailed explanations
5. Included information about error handling
6. Specified requirements
This README now accurately reflects the non-UI, service-based nature of the package. Would you like me to modify anything further?