Documentation Index
Fetch the complete documentation index at: https://docs-mstore.faisalaffan.com/llms.txt
Use this file to discover all available pages before exploring further.
Payment Gateway Integration
Integrasi dengan berbagai metode pembayaran
๐ฏ Overview
Payment adalah salah satu fitur utama dalam MStore Mobile yang menyediakan fungsionalitas untuk integrasi dengan berbagai metode pembayaran.
๐ Features
- โ
Cash payment
- โ
Card payment (EDC)
- โ
E-Wallet (GoPay, OVO, Dana)
- โ
Bank transfer
- โ
QRIS
- โ
Split payment
- โ
Payment validation
๐๏ธ Architecture
BLoC Implementation
BLoC: PaymentBloc
// Events
abstract class PaymentEvent extends Equatable {}
class LoadPayment extends PaymentEvent {}
class CreatePayment extends PaymentEvent {}
class UpdatePayment extends PaymentEvent {}
class DeletePayment extends PaymentEvent {}
// States
abstract class PaymentState extends Equatable {}
class PaymentInitial extends PaymentState {}
class PaymentLoading extends PaymentState {}
class PaymentLoaded extends PaymentState {}
class PaymentError extends PaymentState {}
// BLoC
class PaymentBloc extends Bloc<PaymentEvent, PaymentState> {
final PaymentRepository _repository;
PaymentBloc({required PaymentRepository repository})
: _repository = repository,
super(PaymentInitial()) {
on<LoadPayment>(_onLoad);
on<CreatePayment>(_onCreate);
on<UpdatePayment>(_onUpdate);
on<DeletePayment>(_onDelete);
}
Future<void> _onLoad(
LoadPayment event,
Emitter<PaymentState> emit,
) async {
emit(PaymentLoading());
final result = await _repository.getPayments();
result.fold(
(failure) => emit(PaymentError(failure.message)),
(data) => emit(PaymentLoaded(data)),
);
}
}
Repository Pattern
abstract class PaymentRepository {
Future<Either<Failure, List<Payment>>> getPayments();
Future<Either<Failure, Payment>> getPaymentById(String id);
Future<Either<Failure, Payment>> createPayment(Payment data);
Future<Either<Failure, Payment>> updatePayment(String id, Payment data);
Future<Either<Failure, void>> deletePayment(String id);
}
class PaymentRepositoryImpl implements PaymentRepository {
final PaymentApi _api;
final PaymentLocalRepository _localRepo;
@override
Future<Either<Failure, List<Payment>>> getPayments() async {
try {
// Try local first (offline-first)
final local = await _localRepo.getPayments();
// Sync with API in background
final result = await _api.getPayments();
result.fold(
(failure) => null,
(data) => _localRepo.savePayments(data),
);
return Right(local.isNotEmpty ? local : result.getOrElse(() => []));
} catch (e) {
return Left(UnexpectedFailure(e.toString()));
}
}
}
๐ก API Integration
Endpoints
Request/Response Examples
Get List
GET /api/v1/payments/*
Authorization: Bearer {access_token}
Response:
{
"success": true,
"data": [
{
"id": "123",
"name": "Example",
"created_at": "2024-10-14T10:00:00Z"
}
],
"pagination": {
"page": 1,
"limit": 20,
"total": 100
}
}
๐พ Local Database (Isar)
@collection
class PaymentLocal {
Id id = Isar.autoIncrement;
@Index()
String? paymentId;
String? name;
DateTime? createdAt;
DateTime? updatedAt;
DateTime? syncedAt;
bool? isSynced;
bool? isDeleted;
}
Queries
// Get all
final items = await isar.paymentLocals.where().findAll();
// Get by ID
final item = await isar.paymentLocals
.filter()
.paymentIdEqualTo(id)
.findFirst();
// Search
final results = await isar.paymentLocals
.filter()
.nameContains(query, caseSensitive: false)
.findAll();
// Get unsynced
final unsynced = await isar.paymentLocals
.filter()
.isSyncedEqualTo(false)
.findAll();
๐ Offline-First Strategy
Write Operations
- Save to local Isar immediately
- Show success to user
- Add to sync queue
- Background sync when online
- Update with server response
Read Operations
- Read from local Isar (fast)
- Show to user immediately
- Background fetch from API
- Update local cache if changed
- Notify UI if data updated
Conflict Resolution
- Strategy: Last-write-wins
- Timestamp: Server timestamp as source of truth
- Logging: All conflicts logged for audit
๐จ UI Components
Main Screen
class PaymentPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<PaymentBloc>()..add(LoadPayment()),
child: Scaffold(
appBar: AppBar(title: Text('Payment Gateway Integration')),
body: BlocBuilder<PaymentBloc, PaymentState>(
builder: (context, state) {
if (state is PaymentLoading) {
return Center(child: CircularProgressIndicator());
}
if (state is PaymentError) {
return ErrorWidget(message: state.message);
}
if (state is PaymentLoaded) {
return PaymentListView(items: state.items);
}
return SizedBox.shrink();
},
),
floatingActionButton: FloatingActionButton(
onPressed: () => _navigateToCreate(context),
child: Icon(Icons.add),
),
),
);
}
}
๐งช Testing
Unit Tests
void main() {
group('PaymentBloc', () {
late PaymentBloc bloc;
late MockPaymentRepository mockRepository;
setUp(() {
mockRepository = MockPaymentRepository();
bloc = PaymentBloc(repository: mockRepository);
});
tearDown(() {
bloc.close();
});
test('initial state is PaymentInitial', () {
expect(bloc.state, equals(PaymentInitial()));
});
blocTest<PaymentBloc, PaymentState>(
'emits [Loading, Loaded] when Load succeeds',
build: () {
when(() => mockRepository.getPayments()).thenAnswer(
(_) async => Right([Payment(id: '1', name: 'Test')]),
);
return bloc;
},
act: (bloc) => bloc.add(LoadPayment()),
expect: () => [
PaymentLoading(),
isA<PaymentLoaded>(),
],
);
});
}
- Lazy Loading: Load data on demand
- Pagination: Implement pagination for large datasets
- Caching: Cache frequently accessed data
- Indexing: Use Isar indexes for fast queries
- Background Sync: Sync in background to avoid blocking UI
๐ Security
- Authorization: Check user permissions before operations
- Data Encryption: Sensitive data encrypted in Isar
- Input Validation: Validate all user inputs
- Audit Trail: Log all operations for audit
- Use Cupertino widgets
- Follow iOS HIG
- Handle safe area insets
Android
- Use Material widgets
- Follow Material Design
- Handle back button
Last Updated: October 14, 2024
Status: โ
Production Ready