bayarcash 1.0.0
bayarcash: ^1.0.0 copied to clipboard
Dart SDK for the Bayarcash payment gateway. A feature-parity, idiomatic port of the official PHP SDK, usable from Flutter apps and server-side Dart.
example/bayarcash_example.dart
import 'package:bayarcash/bayarcash.dart';
/// A minimal end-to-end FPX payment example.
///
/// Replace the token, secret key, and portal key with your own credentials.
Future<void> main() async {
final bayarcash = Bayarcash(
token: 'YOUR_API_TOKEN',
secretKey: 'YOUR_API_SECRET_KEY',
sandbox: true, // remove in production
);
// 1. Build the payment request.
final data = <String, dynamic>{
'portal_key': 'your_portal_key',
'payment_channel': Bayarcash.fpx,
'order_number': 'INV-1001',
'amount': '10.00',
'payer_name': 'Ahmad bin Abdullah',
'payer_email': 'ahmad@example.com',
'payer_telephone_number': '0123456789',
'return_url': 'https://your-site.com/payment/return',
'callback_url': 'https://your-site.com/payment/callback',
};
// 2. Sign it with your secret key.
data['checksum'] = bayarcash.createPaymentIntentChecksumValue(data);
try {
// 3. Create the payment intent and redirect the payer to `url`.
final paymentIntent = await bayarcash.createPaymentIntent(data);
print('Redirect the payer to: ${paymentIntent.url}');
// Later, when Bayarcash calls your callback_url, verify it before trusting:
final callbackData = <String, dynamic>{/* parsed request body */};
if (bayarcash.verifyTransactionCallbackData(callbackData)) {
final status = int.tryParse('${callbackData['status']}') ?? -1;
print('Callback status: ${Fpx.getStatusText(status)}');
}
} on ValidationException catch (e) {
print('Validation failed: ${e.errors}');
} on BayarcashException catch (e) {
print('Payment failed: ${e.message}');
} finally {
bayarcash.close();
}
}