https://github.com/programmingwormhole/piprapay-flutter
Secure Flutter SDK for integrating PipraPay payment gateway
https://github.com/programmingwormhole/piprapay-flutter
Last synced: about 2 months ago
JSON representation
Secure Flutter SDK for integrating PipraPay payment gateway
- Host: GitHub
- URL: https://github.com/programmingwormhole/piprapay-flutter
- Owner: programmingwormhole
- License: mit
- Created: 2026-02-27T22:13:47.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2026-02-27T23:09:42.000Z (5 months ago)
- Last Synced: 2026-02-28T04:52:55.057Z (5 months ago)
- Language: Dart
- Size: 62.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ Piprapay Flutter Package
[](https://pub.dev/packages/piprapay)
[](LICENSE)
[](https://flutter.dev)
[](https://dart.dev)
[](https://github.com/programmingwormhole/piprapay-flutter)
A **production-ready, type-safe** Flutter package for seamless integration with [Piprapay](https://piprapay.com) payment gateway. Build powerful payment solutions with complete API support (V2 & V3+), comprehensive error handling, and professional payment UI capabilities.
---
## โจ Features
### ๐ Core Features
- โ
**Complete Piprapay API Integration** - Full support for V2 and V3+ APIs with automatic version detection
- โ
**Payment Creation & Management** - Create charges, extract checkout URLs, handle payment references
- โ
**Real-time Verification** - Verify payment status with detailed transaction information
- โ
**Refund Processing** - Process full and partial refunds securely
- โ
**Webhook Validation** - Built-in webhook payload validation and signature verification
- โ
**Activity Logging** - Optional in-app payment activity tracking and monitoring
### ๐ Security & Reliability
- โ
**Type-Safe Models** - Null-safe, fully-typed data models with JSON serialization
- โ
**Professional Error Handling** - Specific exceptions for different error scenarios
- โ
**Flexible Type Conversion** - Handles inconsistent API responses (numeric/string fields)
- โ
**API Key Authentication** - Secure API key management and validation
- โ
**HTTPS Communication** - All traffic encrypted and secure
- โ
**Webhook Signature Verification** - Cryptographic validation of webhook payloads
### ๐ฏ Developer Experience
- โ
**Simple, Intuitive API** - Clean, easy-to-use service interface
- โ
**Sandbox & Production Modes** - Built-in environment switching
- โ
**Comprehensive Documentation** - Detailed API references and integration guides
- โ
**Example App Included** - Full-featured example showing best practices
- โ
**Input Validation Utilities** - Email, mobile number, and payment status validators
- โ
**Zero Dependencies** - Minimal external dependencies (only `http` and `crypto`)
### ๐จ UI Enhancements (Built-in)
- โ
**WebView Payment Handler** - Built-in WebView for seamless in-app payment execution
- โ
**Payment Status Detection** - Automatic detection of success, cancel, and failure states
- โ
**Customizable UI** - Configurable app bar title and display duration
- โ
**Error Handling** - Comprehensive error detection and reporting
---
## ๐ฆ Installation
### Add to your `pubspec.yaml`:
```yaml
dependencies:
piprapay: ^1.0.0
```
### Run:
```bash
flutter pub get
```
> **Note:** The package includes `webview_flutter` for in-app payment execution. No additional dependencies needed!
---
## ๐ Quick Start
### 1๏ธโฃ Initialize the Piprapay Service
#### Basic Initialization
```dart
import 'package:piprapay/piprapay.dart';
// Sandbox Mode (Testing) - Minimal configuration
final piprapay = PiprapayService.sandbox(
apiKey: 'your_sandbox_api_key', // โ
Required
);
// Production Mode - Minimal configuration
final piprapay = PiprapayService.production(
apiKey: 'your_production_api_key', // โ
Required
baseUrl: 'https://api.piprapay.com/api', // โ
Required for production
);
```
#### Advanced Initialization (With All Options)
```dart
// Sandbox with all optional parameters
final piprapay = PiprapayService.sandbox(
apiKey: 'your_sandbox_api_key', // โ
Required
panelVersion: PanelVersion.v3plus, // โ๏ธ Optional (default: V3+)
enableLogging: true, // โ๏ธ Optional (default: false) - Enables request/response logging
timeout: Duration(seconds: 30), // โ๏ธ Optional (default: system timeout)
);
// Production with all optional parameters
final piprapay = PiprapayService.production(
apiKey: 'your_production_api_key', // โ
Required
baseUrl: 'https://api.piprapay.com/api', // โ
Required
panelVersion: PanelVersion.v3plus, // โ๏ธ Optional (default: V3+)
enableLogging: false, // โ๏ธ Optional (default: false) - Set true for debugging
timeout: Duration(seconds: 60), // โ๏ธ Optional (default: system timeout)
);
// Manual Configuration (Advanced)
final piprapay = PiprapayService(
apiKey: 'your_api_key', // โ
Required
isSandbox: true, // โ
Required - true for testing, false for production
baseUrl: 'https://custom.piprapay.com/api', // โ๏ธ Optional (auto-set if isSandbox: true)
panelVersion: PanelVersion.v2, // โ๏ธ Optional - Use V2 for legacy API
enableLogging: true, // โ๏ธ Optional - Logs API calls for debugging
timeout: Duration(seconds: 45), // โ๏ธ Optional - Custom timeout duration
);
```
#### Initialization Parameters
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `apiKey` | String | โ
Yes | - | Your Piprapay API key from dashboard |
| `baseUrl` | String | โ
Yes (Production) | Sandbox URL | API endpoint URL (required when `isSandbox: false`) |
| `isSandbox` | bool | โ
Yes (Manual) | - | `true` for testing, `false` for production |
| `panelVersion` | PanelVersion | โ๏ธ Optional | `v3plus` | API version: `PanelVersion.v2` or `PanelVersion.v3plus` |
| `enableLogging` | bool | โ๏ธ Optional | `false` | Enable detailed request/response logging (useful for debugging) |
| `timeout` | Duration | โ๏ธ Optional | System default | Maximum time to wait for API responses |
#### Panel Version Options
```dart
// For V3+ API (Latest - Recommended)
final piprapay = PiprapayService.sandbox(
apiKey: 'your_key',
panelVersion: PanelVersion.v3plus, // Uses pp_id, pp_url, latest features
);
// For V2 API (Legacy Support)
final piprapay = PiprapayService.sandbox(
apiKey: 'your_key',
panelVersion: PanelVersion.v2, // Uses transaction_id, older endpoints
);
```
### 2๏ธโฃ Create a Payment Charge
#### For V3+ API (Recommended)
```dart
try {
final charge = await piprapay.createCharge(
// โ
Required Parameters (V3+)
fullName: 'Customer Name', // โ
Required
emailAddress: 'customer@example.com', // โ
Required (must be valid email)
mobileNumber: '+8801700000000', // โ
Required (with country code)
amount: '100.00', // โ
Required (as String)
returnUrl: 'https://yourapp.com/payment/return', // โ
Required (success redirect)
webhookUrl: 'https://yourapp.com/api/webhook', // โ
Required (backend notification)
// โ๏ธ Optional Parameters
currency: 'BDT', // โ๏ธ Optional (default: BDT)
metadata: {'order_id': '12345', 'user_id': '789'}, // โ๏ธ Optional (custom data)
);
// Extract payment information
String checkoutUrl = piprapay.extractCheckoutUrl(charge)!;
String paymentRef = piprapay.extractPaymentReference(charge)!;
print('โ
Invoice: ${charge.invoiceId}');
print('โ
Payment URL: $checkoutUrl');
print('โ
Payment Reference (pp_id): $paymentRef');
} on PiprapayRequestException catch (e) {
print('โ Validation Error: ${e.message}');
} on PiprapayException catch (e) {
print('โ Error: ${e.message}');
}
```
#### For V2 API (Legacy Support)
```dart
try {
final charge = await piprapay.createCharge(
// โ
Required Parameters (V2)
fullName: 'Customer Name', // โ
Required
emailOrMobile: 'customer@example.com', // โ
Required (email OR mobile)
amount: '100.00', // โ
Required
redirectUrl: 'https://yourapp.com/payment/success', // โ
Required
webhookUrl: 'https://yourapp.com/api/webhook', // โ
Required
// โ๏ธ Optional Parameters
cancelUrl: 'https://yourapp.com/payment/cancel', // โ๏ธ Optional (cancel redirect)
currency: 'BDT', // โ๏ธ Optional (default: BDT)
returnType: 'POST', // โ๏ธ Optional (default: POST)
orderId: 'order_12345', // โ๏ธ Optional (custom order ID)
metadata: {'custom_field': 'value'}, // โ๏ธ Optional
);
String paymentUrl = charge.paymentUrl;
print('โ
Payment URL: $paymentUrl');
} on PiprapayException catch (e) {
print('โ Error: ${e.message}');
}
```
#### createCharge() Parameters
**V3+ API Parameters:**
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `fullName` | String | โ
Yes | - | Customer's full name |
| `emailAddress` | String | โ
Yes | - | Valid email address (validated) |
| `mobileNumber` | String | โ
Yes | - | Mobile with country code (e.g., +8801700000000) |
| `amount` | String | โ
Yes | - | Payment amount (e.g., "100.00") |
| `returnUrl` | String | โ
Yes | - | URL to redirect after payment completion |
| `webhookUrl` | String | โ
Yes | - | Backend endpoint for payment notifications |
| `currency` | String | โ๏ธ Optional | "BDT" | Currency code (BDT, USD, etc.) |
| `metadata` | Map | โ๏ธ Optional | `{}` | Custom data to attach to payment |
**V2 API Parameters:**
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `fullName` | String | โ
Yes | - | Customer's full name |
| `emailOrMobile` | String | โ
Yes | - | Email OR mobile number (validated) |
| `amount` | String | โ
Yes | - | Payment amount |
| `redirectUrl` | String | โ
Yes | - | Success redirect URL |
| `webhookUrl` | String | โ
Yes | - | Webhook endpoint URL |
| `cancelUrl` | String | โ๏ธ Optional | - | Cancel page redirect URL |
| `currency` | String | โ๏ธ Optional | "BDT" | Currency code |
| `returnType` | String | โ๏ธ Optional | "POST" | Return method (POST/GET) |
| `orderId` | String | โ๏ธ Optional | - | Custom order identifier |
| `metadata` | Map | โ๏ธ Optional | `{}` | Custom data object |
### 3๏ธโฃ Verify Payment Status
#### For V3+ API
```dart
try {
final verification = await piprapay.verifyPayment(
ppId: 'pp_id_from_redirect', // โ
Required (V3+) - Received from payment redirect/webhook
);
if (piprapay.isSuccessfulStatus(verification.status)) {
print('โ
Payment Successful!');
print('โ
Amount: ${verification.amount} ${verification.currency}');
print('โ
Total: ${verification.total}');
print('โ
Method: ${verification.paymentMethod}');
print('โ
Transaction: ${verification.transactionId}');
} else {
print('โ Payment Status: ${verification.status}');
}
} on PiprapayPaymentException catch (e) {
print('โ Verification Error: ${e.message}');
}
```
#### For V2 API
```dart
try {
final verification = await piprapay.verifyPayment(
transactionId: 'transaction_id_from_redirect', // โ
Required (V2)
);
if (verification.status == 'completed') {
print('โ
Payment verified!');
}
} on PiprapayPaymentException catch (e) {
print('โ Error: ${e.message}');
}
```
#### verifyPayment() Parameters
| Parameter | Type | Required | API Version | Description |
|-----------|------|----------|-------------|-------------|
| `ppId` | String | โ
Yes | V3+ | Payment reference from redirect (pp_id parameter) |
| `transactionId` | String | โ
Yes | V2 | Transaction ID from redirect (V2 legacy) |
> **Note:** Use `ppId` for V3+ API or `transactionId` for V2 API based on your `panelVersion` setting.
### 4๏ธโฃ Execute Payment in WebView (Built-in)
The package includes a built-in WebView handler - no need to implement it yourself!
```dart
import 'package:piprapay/piprapay.dart';
try {
final result = await PiprapayWebView.executePayment(
context,
paymentUrl: checkoutUrl, // โ
Required - From createCharge()
successPageDisplayDuration: Duration(seconds: 2), // โ๏ธ Optional (default: 2 seconds)
appBarTitle: 'Complete Payment', // โ๏ธ Optional (default: "Complete Payment")
);
if (result != null && result.isSuccess) {
// Verify payment after successful completion
final verification = await piprapay.verifyPayment(
ppId: result.transactionRef!, // V3+
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('โ
Payment verified: ${verification.amount}')),
);
} else if (result?.isCancelled == true) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('โ ๏ธ Payment cancelled')),
);
} else if (result?.isFailed == true) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('โ Payment failed: ${result!.message}')),
);
}
} catch (e) {
print('Error executing payment: $e');
}
```
#### PiprapayWebView.executePayment() Parameters
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `context` | BuildContext | โ
Yes | - | BuildContext for navigation |
| `paymentUrl` | String | โ
Yes | - | Payment gateway URL from `createCharge()` |
| `successPageDisplayDuration` | Duration | โ๏ธ Optional | 2 seconds | How long to display success page before closing |
| `appBarTitle` | String | โ๏ธ Optional | "Complete Payment" | Custom title for the payment page |
**Returns:** `PaymentResult?` - Contains payment outcome (success/cancelled/failed)
### 5๏ธโฃ Process Refunds
#### For V3+ API
```dart
try {
final refund = await piprapay.refundPayment(
ppId: 'pp_id_value', // โ
Required (V3+) - Payment reference to refund
);
if (refund.status == 'refunded') {
print('โ
Refund processed successfully');
print('โ
Refund Amount: ${refund.refundAmount}');
print('โ
Transaction: ${refund.transactionId}');
}
} on PiprapayPaymentException catch (e) {
print('โ Refund failed: ${e.message}');
}
```
#### For V2 API
```dart
try {
final refund = await piprapay.refundPayment(
transactionId: 'transaction_id_value', // โ
Required (V2)
);
print('โ
Refund initiated');
print('Response: $refund');
} on PiprapayPaymentException catch (e) {
print('โ Refund failed: ${e.message}');
}
```
#### refundPayment() Parameters
| Parameter | Type | Required | API Version | Description |
|-----------|------|----------|-------------|-------------|
| `ppId` | String | โ
Yes | V3+ | Payment reference (pp_id) to refund |
| `transactionId` | String | โ
Yes | V2 | Transaction ID to refund (V2 legacy) |
> **Note:** Full refund is processed. Partial refunds depend on Piprapay dashboard configuration.
### 6๏ธโฃ Handle Webhooks
#### Using validateWebhook() Method
```dart
// In your Flutter backend webhook handler
try {
final webhook = await piprapay.validateWebhook(
payload: requestBody, // โ
Required - Raw JSON string from request body
receivedApiKey: apiKeyHeader, // โ
Required - API key from request header
);
// Webhook validated successfully
if (webhook.status == 'completed') {
print('โ
Payment completed: ${webhook.transactionId}');
updateDatabase(webhook.transactionId, 'completed');
}
} on PiprapayWebhookException catch (e) {
print('โ Webhook validation failed: ${e.message}');
// Return 401 Unauthorized
}
```
#### Backend Example (Node.js/Express)
```dart
app.post('/api/webhook', async (req, res) => {
const apiKey = req.headers['mh-piprapay-api-key']; // โ
Required header
const payload = JSON.stringify(req.body); // โ
Required body
// Validate using Piprapay Flutter service
const webhook = await piprapay.validateWebhook(
payload: payload,
receivedApiKey: apiKey,
);
if (webhook.status === 'completed') {
updatePaymentStatus(webhook.transactionId, 'completed');
}
res.json({ status: true, message: 'Webhook received' });
});
```
#### validateWebhook() Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `payload` | String | โ
Yes | Raw JSON string from webhook request body |
| `receivedApiKey` | String | โ
Yes | API key from `mh-piprapay-api-key` request header |
> **Security Note:** Always validate the API key matches your configured key to prevent unauthorized webhook calls.
---
## ๐ก๏ธ Error Handling
The package provides specific exception types for precise error handling:
```dart
try {
// Payment operation
} on PiprapayAuthException catch (e) {
// Handle authentication errors (invalid API key)
print('๐ Auth Error: ${e.message}');
} on PiprapayRequestException catch (e) {
// Handle validation/request errors
print('๐ Request Error: ${e.message}');
print('Status Code: ${e.statusCode}');
} on PiprapayNetworkException catch (e) {
// Handle network errors
print('๐ Network Error: ${e.message}');
} on PiprapayPaymentException catch (e) {
// Handle payment-specific errors
print('๐ณ Payment Error: ${e.message}');
print('Transaction: ${e.transactionId}');
} on PiprapayWebhookException catch (e) {
// Handle webhook validation errors
print('๐ Webhook Error: ${e.message}');
} on PiprapayFailure catch (e) {
// Handle simplified payment failures (WebView execution)
if (e.isPaymentCancelled) {
print('โ ๏ธ Payment cancelled by user');
} else if (e.isPaymentFailed) {
print('โ Payment failed: ${e.message}');
}
} on PiprapayException catch (e) {
// Handle all other Piprapay errors
print('โ Error: ${e.message}');
}
```
---
## ๐ Advanced Usage
### Custom Configuration
#### Full Manual Configuration
```dart
final piprapay = PiprapayService(
apiKey: 'your_api_key', // โ
Required - Your Piprapay API key
isSandbox: true, // โ
Required - Environment mode
baseUrl: 'https://custom.piprapay.com/api', // โ๏ธ Optional (auto-set if sandbox)
panelVersion: PanelVersion.v3plus, // โ๏ธ Optional (default: V3+)
enableLogging: true, // โ๏ธ Optional (default: false)
timeout: Duration(seconds: 60), // โ๏ธ Optional
);
```
#### Configuration Options
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `apiKey` | String | โ
Yes | - | Piprapay API key |
| `isSandbox` | bool | โ
Yes | - | `true` for sandbox, `false` for production |
| `baseUrl` | String | โ๏ธ Optional | Sandbox URL | Custom API endpoint (required if `isSandbox: false`) |
| `panelVersion` | PanelVersion | โ๏ธ Optional | `v3plus` | API version (`PanelVersion.v2` or `PanelVersion.v3plus`) |
| `enableLogging` | bool | โ๏ธ Optional | `false` | Enable request/response logging for debugging |
| `timeout` | Duration | โ๏ธ Optional | System default | Request timeout duration |
| `httpClient` | http.Client | โ๏ธ Optional | - | Custom HTTP client (for testing/mocking) |
> **Tip:** Use `enableLogging: true` during development to see full API request/response details in console.
### Input Validation
```dart
import 'package:piprapay/piprapay.dart';
// Validate email
if (!PiprapayUtils.isValidEmail('test@example.com')) {
print('Invalid email format');
}
// Validate mobile number
if (!PiprapayUtils.isValidMobileNumber('+8801700000000')) {
print('Invalid mobile number');
}
// Validate email or mobile
if (PiprapayUtils.isValidEmailOrMobile(userInput)) {
print('Valid contact information');
}
// Check payment status
if (PiprapayUtils.isPaymentCompleted(status)) {
print('Payment completed successfully');
}
// Verify status helpers
bool isSuccess = piprapay.isSuccessfulStatus(status);
bool isFailed = piprapay.isFailedStatus(status);
```
### Environment Variables
```dart
// Use environment configuration in main.dart
const String PIPRAPAY_API_KEY = String.fromEnvironment(
'PIPRAPAY_API_KEY',
defaultValue: 'sandbox_key',
);
const String PIPRAPAY_ENV = String.fromEnvironment(
'PIPRAPAY_ENV',
defaultValue: 'sandbox',
);
final piprapay = PIPRAPAY_ENV == 'production'
? PiprapayService.production(apiKey: PIPRAPAY_API_KEY)
: PiprapayService.sandbox(apiKey: PIPRAPAY_API_KEY);
```
### Resource Management
```dart
@override
void dispose() {
piprapay.dispose();
super.dispose();
}
```
---
## ๐ Data Models
### CreateChargeResponse
```dart
class CreateChargeResponse {
final String invoiceId; // Unique invoice ID
final String transactionId; // Transaction reference (pp_id)
final String checkoutUrl; // Payment gateway URL (V3+)
final String paymentUrl; // Alternative payment URL (V2/V3)
final Map? metadata; // Custom data
}
```
### VerifyPaymentResponse (V2 & V3+)
```dart
class VerifyPaymentResponse {
final String transactionId; // pp_id
final String customerName;
final String amount;
final double total; // Total amount
final String currency; // BDT, USD, etc.
final String status; // completed, failed, pending, etc.
final String paymentMethod; // bKash, Nagad, Rocket, etc.
final String date;
final String? metadata; // Custom data
}
```
### WebhookPayload
```dart
class WebhookPayload {
final String transactionId; // pp_id
final String status; // completed, failed, etc.
final String amount;
final double total;
final String currency;
final String paymentMethod;
final String customerName;
final String customerEmailOrMobile;
final Map? metadata;
}
```
### PaymentResult (WebView)
```dart
class PaymentResult {
final bool isSuccess;
final bool isCancelled;
final bool isFailed;
final String? transactionRef;
final String? message;
// Factory constructors
factory PaymentResult.success(String transactionRef);
factory PaymentResult.cancelled();
factory PaymentResult.failed(String? message, String? transactionRef);
}
```
---
## ๐งช Testing
### Run Tests
```bash
flutter test
```
### Example Test
```dart
test('Create charge request validation', () {
final piprapay = PiprapayService.sandbox(apiKey: 'test_key');
expect(
PiprapayUtils.isValidEmail('valid@example.com'),
true,
);
expect(
PiprapayUtils.isValidMobileNumber('+8801700000000'),
true,
);
});
```
### Sandbox Testing
```dart
// Use sandbox credentials for testing
final piprapay = PiprapayService.sandbox(
apiKey: 'pk_test_your_sandbox_key',
);
// Test credentials
const testEmail = 'test@example.com';
const testAmount = '100';
const testMobile = '+8801700000000';
// All transactions will be simulated
```
---
## โ
Best Practices
1. **๐ API Key Security**
- Never hardcode API keys
- Use environment variables: `String.fromEnvironment('PIPRAPAY_API_KEY')`
- Store sensitive data in secure storage (Flutter Secure Storage)
2. **โ๏ธ Input Validation**
- Always validate user input before payment
- Use `PiprapayUtils` validators
- Show validation errors to users
3. **๐ก๏ธ Error Handling**
- Implement try-catch for all payment operations
- Provide meaningful error messages
- Log errors for debugging
4. **๐ Webhook Verification**
- Always validate webhook API keys
- Verify webhook signatures
- Process payments idempotently (handle duplicate webhooks)
5. **๐พ Database Integration**
- Store transaction IDs for record-keeping
- Update payment status on verification
- Log all payment events with timestamps
6. **๐งช Testing Strategy**
- Use sandbox environment for testing
- Test all payment flows (success, cancel, fail)
- Verify webhook handling
7. **๐ Monitoring & Logging**
- Log important payment events
- Monitor API response times
- Track error rates
- Use activity logs for debugging
8. **๐ Production Deployment**
- Switch to production API key
- Update base URL for production
- Enable webhook endpoint
- Monitor payment success rates
- Have fallback payment methods
---
## ๐ง API Reference
### PiprapayService Methods
#### `createCharge()`
Creates a new payment charge.
**V3+ API Parameters:**
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `fullName` | String | โ
Yes | - | Customer's full name |
| `emailAddress` | String | โ
Yes | - | Valid email address (validated) |
| `mobileNumber` | String | โ
Yes | - | Mobile with country code |
| `amount` | String | โ
Yes | - | Payment amount (e.g., "100.00") |
| `returnUrl` | String | โ
Yes | - | Success redirect URL |
| `webhookUrl` | String | โ
Yes | - | Backend webhook endpoint |
| `currency` | String | โ๏ธ Optional | "BDT" | Currency code |
| `metadata` | Map | โ๏ธ Optional | `{}` | Custom data |
**V2 API Parameters:**
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `fullName` | String | โ
Yes | - | Customer's full name |
| `emailOrMobile` | String | โ
Yes | - | Email OR mobile number |
| `amount` | String | โ
Yes | - | Payment amount |
| `redirectUrl` | String | โ
Yes | - | Success redirect URL |
| `webhookUrl` | String | โ
Yes | - | Backend webhook endpoint |
| `cancelUrl` | String | โ๏ธ Optional | - | Cancel redirect URL |
| `currency` | String | โ๏ธ Optional | "BDT" | Currency code |
| `returnType` | String | โ๏ธ Optional | "POST" | Return method (POST/GET) |
| `orderId` | String | โ๏ธ Optional | - | Custom order identifier |
| `metadata` | Map | โ๏ธ Optional | `{}` | Custom data |
**Returns:** `CreateChargeResponseV3` (V3+) or `CreateChargeResponseV2` (V2)
#### `verifyPayment()`
Verifies payment status using transaction ID.
**Parameters:**
| Parameter | Type | Required | API Version | Description |
|-----------|------|----------|-------------|-------------|
| `ppId` | String | โ
Yes | V3+ | Payment reference (pp_id) |
| `transactionId` | String | โ
Yes | V2 | Transaction ID (V2 legacy) |
**Returns:** `VerifyPaymentResponseV3` (V3+) or `VerifyPaymentResponseV2` (V2)
#### `refundPayment()`
Processes refund for completed payment.
**Parameters:**
| Parameter | Type | Required | API Version | Description |
|-----------|------|----------|-------------|-------------|
| `ppId` | String | โ
Yes | V3+ | Payment reference (pp_id) to refund |
| `transactionId` | String | โ
Yes | V2 | Transaction ID to refund (V2) |
**Returns:** `RefundPaymentResponseV3` (V3+) or dynamic (V2)
#### `validateWebhook()`
Validates webhook payload and signature.
**Parameters:**
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `payload` | String | โ
Yes | Raw JSON string from webhook request body |
| `receivedApiKey` | String | โ
Yes | API key from `mh-piprapay-api-key` header |
**Returns:** `WebhookPayloadV3` (V3+) or `WebhookPayload` (V2)
#### Helper Methods
**`extractCheckoutUrl()`** - Extract payment URL from createCharge response
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `createResponse` | dynamic | โ
Yes | Response from `createCharge()` |
**Returns:** `String?` - Payment/checkout URL or null
```dart
String? url = piprapay.extractCheckoutUrl(charge);
```
---
**`extractPaymentReference()`** - Extract payment reference (pp_id or transaction_id)
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `createResponse` | dynamic | โ
Yes | Response from `createCharge()` |
**Returns:** `String?` - Payment reference or null
```dart
String? ppId = piprapay.extractPaymentReference(charge);
```
---
**`isSuccessfulStatus()`** - Check if payment status indicates success
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `status` | String | โ
Yes | Payment status from verification |
**Returns:** `bool` - true if status is 'completed'
```dart
bool isSuccess = piprapay.isSuccessfulStatus(status);
```
---
**`isFailedStatus()`** - Check if payment status indicates failure
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `status` | String | โ
Yes | Payment status from verification |
**Returns:** `bool` - true if status is 'failed', 'cancelled', 'expired', or 'rejected'
```dart
bool isFailed = piprapay.isFailedStatus(status);
```
---
## ๐ Troubleshooting
### "Invalid API Key" Error
```
โ Verify API key is correct
โ Check if API key has required permissions
โ Ensure API key matches environment (sandbox/production)
โ Regenerate API key in Piprapay dashboard
```
### "Request Timeout" Error
```
โ Check network connectivity
โ Verify backend service is running
โ Increase timeout: timeout: Duration(seconds: 90)
โ Check for network proxy/firewall issues
```
### "Invalid Email or Mobile" Error
```
โ Validate input format before sending
โ Use PiprapayUtils validators
โ Check phone number includes country code
โ Ensure email format is valid
```
### Webhook Not Received
```
โ Verify webhook URL is publicly accessible
โ Ensure webhook URL uses HTTPS
โ Check API key in webhook headers
โ Verify server logs for errors
โ Test webhook endpoint manually
```
### Payment Verification Fails
```
โ Verify transaction ID (pp_id) is correct
โ Wait a few seconds after payment completion
โ Check payment status in Piprapay dashboard
โ Verify API key has verification permissions
โ Check network connectivity
```
---
## ๐ Documentation
- **[Piprapay Official Docs](https://piprapay.readme.io/reference)**
- **[API Documentation](https://piprapay.com/api-docs)**
- **[GitHub Repository](https://github.com/programmingwormhole/piprapay-flutter)**
- **[Issue Tracker](https://github.com/programmingwormhole/piprapay-flutter/issues)**
---
## ๐ Changelog
See [CHANGELOG.md](CHANGELOG.md) for detailed version history and updates.
---
## ๐ค Contributing
Contributions are welcome! Please follow these steps:
1. **Fork** the repository
2. **Create** a feature branch (`git checkout -b feature/amazing-feature`)
3. **Commit** your changes (`git commit -m 'Add amazing feature'`)
4. **Push** to the branch (`git push origin feature/amazing-feature`)
5. **Open** a Pull Request
### Development Setup
```bash
# Clone repository
git clone https://github.com/programmingwormhole/piprapay-flutter.git
cd piprapay-flutter
# Get dependencies
flutter pub get
# Run tests
flutter test
# Run example app
cd example
flutter run
```
---
## ๐ License
This package is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.
---
## ๐จโ๐ป About the Developer
**Md Shirajul Islam**
A passionate Flutter developer dedicated to building professional, production-ready payment solutions for the Bangladeshi fintech ecosystem.
### Connect with Me
- **GitHub:** [github.com/programmingwormhole](https://github.com/programmingwormhole)
- **YouTube:** [youtube.com/@programmingwormhole](https://youtube.com/@programmingwormhole)
- **Facebook:** [facebook.com/no.name.virus](https://facebook.com/no.name.virus)
- **Email:** programmingwormhole@gmail.com
---
## ๐ Support
If you found this package helpful:
- โญ Star the repository on GitHub
- ๐ค Share with your developer friends
- ๐ Report issues and suggest improvements
- ๐ฌ Contribute code and documentation
---
## ๏ฟฝ Quick Reference
### All Methods & Parameters Summary
#### Initialization Methods
| Method | Required Parameters | Optional Parameters | Returns |
|--------|---------------------|---------------------|---------|
| `PiprapayService.sandbox()` | `apiKey` | `panelVersion`, `enableLogging`, `timeout` | PiprapayService |
| `PiprapayService.production()` | `apiKey`, `baseUrl` | `panelVersion`, `enableLogging`, `timeout` | PiprapayService |
#### Payment Methods
| Method | Required (V3+) | Required (V2) | Optional | Returns |
|--------|----------------|---------------|----------|---------|
| `createCharge()` | `fullName`, `emailAddress`, `mobileNumber`, `amount`, `returnUrl`, `webhookUrl` | `fullName`, `emailOrMobile`, `amount`, `redirectUrl`, `webhookUrl` | `currency`, `metadata`, `cancelUrl` (V2), `returnType` (V2), `orderId` (V2) | CreateChargeResponse |
| `verifyPayment()` | `ppId` | `transactionId` | - | VerifyPaymentResponse |
| `refundPayment()` | `ppId` | `transactionId` | - | RefundPaymentResponse |
| `validateWebhook()` | `payload`, `receivedApiKey` | `payload`, `receivedApiKey` | - | WebhookPayload |
#### Helper Methods
| Method | Required Parameters | Returns | Description |
|--------|---------------------|---------|-------------|
| `extractCheckoutUrl()` | `createResponse` | String? | Extract payment URL |
| `extractPaymentReference()` | `createResponse` | String? | Extract pp_id/transaction_id |
| `isSuccessfulStatus()` | `status` | bool | Check if status is "completed" |
| `isFailedStatus()` | `status` | bool | Check if status is failed/cancelled/expired |
#### UI Methods (WebView)
| Method | Required Parameters | Optional Parameters | Returns | Description |
|--------|---------------------|---------------------|---------|-------------|
| `PiprapayWebView.executePayment()` | `context`, `paymentUrl` | `successPageDisplayDuration`, `appBarTitle` | PaymentResult? | Execute payment in built-in WebView |
#### Utility Methods (PiprapayUtils)
| Method | Required Parameters | Returns | Description |
|--------|---------------------|---------|-------------|
| `isValidEmail()` | `email` | bool | Validate email format |
| `isValidMobileNumber()` | `mobile` | bool | Validate mobile number |
| `isValidEmailOrMobile()` | `input` | bool | Validate email OR mobile |
| `isPaymentCompleted()` | `status` | bool | Check if payment completed |
| `validateWebhookApiKey()` | `receivedApiKey`, `expectedApiKey` | bool | Validate webhook API key |
### Initialization Quick Reference
```dart
// V3+ Sandbox with logging (Development)
final piprapay = PiprapayService.sandbox(
apiKey: 'pk_sandbox_key',
panelVersion: PanelVersion.v3plus, // โ๏ธ Optional
enableLogging: true, // โ๏ธ Optional - Helps debugging
);
// V3+ Production (Live)
final piprapay = PiprapayService.production(
apiKey: 'pk_live_key',
baseUrl: 'https://api.piprapay.com/api',
panelVersion: PanelVersion.v3plus, // โ๏ธ Optional
enableLogging: false, // โ๏ธ Optional - Disable in production
);
// V2 Legacy Support
final piprapay = PiprapayService.sandbox(
apiKey: 'pk_sandbox_key',
panelVersion: PanelVersion.v2, // For V2 API
);
```
### Payment Flow Quick Reference
```dart
// 1. Create Charge (V3+)
final charge = await piprapay.createCharge(
fullName: 'Name', // โ
Required
emailAddress: 'email@x.com', // โ
Required
mobileNumber: '+880170...', // โ
Required
amount: '100', // โ
Required
returnUrl: 'https://...', // โ
Required
webhookUrl: 'https://...', // โ
Required
currency: 'BDT', // โ๏ธ Optional
metadata: {}, // โ๏ธ Optional
);
// 2. Extract Payment URL
String url = piprapay.extractCheckoutUrl(charge)!;
// 3. Execute payment in built-in WebView
final result = await PiprapayWebView.executePayment(
context,
paymentUrl: url, // โ
Required
successPageDisplayDuration: Duration(seconds: 2), // โ๏ธ Optional
);
// 4. Verify Payment (if successful)
if (result?.isSuccess == true) {
final verification = await piprapay.verifyPayment(
ppId: result!.transactionRef!, // โ
Required (V3+)
);
// 5. Check Status
if (piprapay.isSuccessfulStatus(verification.status)) {
// Payment successful - update your database
}
}
// 6. Refund (if needed)
final refund = await piprapay.refundPayment(
ppId: 'pp_id_value', // โ
Required (V3+)
);
```
---
## ๏ฟฝ๐ Quick Links
| Link | Purpose |
|------|---------|
| [GitHub Repository](https://github.com/programmingwormhole/piprapay-flutter) | Source code and issue tracking |
| [pub.dev Package](https://pub.dev/packages/piprapay) | Package page and version history |
| [Piprapay Official](https://piprapay.com) | Piprapay payment gateway website |
| [API Documentation](https://piprapay.readme.io) | Official Piprapay API docs |
---
**Made with โค๏ธ by [Md Shirajul Islam](https://github.com/programmingwormhole)**
*Professional โข Secure โข Easy to Use โข Production Ready*
[โฌ Back to Top](#-piprapay-flutter-package)