STC Pay Integration Guide
This guide will walk you through integrating STC Pay payments into your Flutter application using the Moyasar Flutter SDK.
Prerequisites
Before you begin, ensure you have:
- A Moyasar account with STC Pay enabled
- Your Moyasar publishable API key
- Flutter SDK installed (v2.0.17 or later)
Installation
Add the latest version of the moyasar package to your pubspec.yaml:
dependencies:
moyasar: ^2.1.2 # or the latest version
Then run:
flutter pub get
Implementation
1. Add STC Pay Component
// Initialize payment config
final paymentConfig = PaymentConfig(
publishableApiKey: 'YOUR_PUBLISHABLE_API_KEY',
amount: 10000, // 100.00 SAR
description: 'Order #12345',
metadata: {'order_id': '12345'},
);
// In your widget
STCPaymentComponent(
config: paymentConfig,
onPaymentResult: onPaymentResult,
)
// Payment result handler
void onPaymentResult(PaymentResult result) {
if (result is PaymentResponse) {
switch (result.status) {
case PaymentStatus.paid:
print('Payment successful: ${result.id}');
break;
case PaymentStatus.failed:
print('Payment failed: ${result.message}');
break;
case PaymentStatus.initiated:
print('Payment initiated: ${result.id}');
break;
}
}
}
Testing
Test Environment
- Use the sandbox environment for testing
- Enter a valid Saudi mobile number starting with 05
- For testing OTP:
- Use
123456for successful payment - Use
000000for failed payment
- Use
Error Handling
Handle different payment statuses in your onPaymentResult callback:
void onPaymentResult(PaymentResult result) {
if (result is PaymentResponse) {
switch (result.status) {
case PaymentStatus.paid:
// Handle successful payment
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Payment successful!')),
);
break;
case PaymentStatus.failed:
// Handle failed payment
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Payment failed: ${result.message}')),
);
break;
case PaymentStatus.initiated:
// Handle initiated state (waiting for OTP)
break;
}
} else if (result is PaymentError) {
// Handle errors
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: ${result.message}')),
);
}
}
Production Deployment
Before going live:
- Replace the test API key with your live publishable API key
- Test the complete payment flow with real STC Pay accounts
- Ensure you have proper error handling in place
Support
For any issues or questions, please contact Moyasar Support.