prototype complete

This commit is contained in:
2025-07-14 01:04:53 +02:00
parent 03d0f77ac2
commit 44d21b3f14
69 changed files with 5033 additions and 765 deletions

View File

@@ -0,0 +1,136 @@
import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qpay/screens/transaction_controller.dart';
import '../../http/http.dart';
part 'confirm_controller.freezed.dart';
part 'confirm_controller.g.dart';
class ConfirmModel {
bool isLoading = false;
String status = '';
String? errorMessage;
bool isCancelled = false;
}
@freezed
abstract class ConfirmData with _$ConfirmData {
const factory ConfirmData({
required String status,
required String errorMessage,
}) = _ConfirmData;
factory ConfirmData.fromJson(Map<String, dynamic> json) =>
_$ConfirmDataFromJson(json);
}
class ConfirmController extends ChangeNotifier {
final ConfirmModel model = ConfirmModel();
late TransactionController transactionController;
final Http http = Http();
BuildContext context;
ConfirmController(this.context) {
transactionController = Provider.of<TransactionController>(
context,
listen: false,
);
}
void _showErrorSnackBar(String? message) {
WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message.toString()),
behavior: SnackBarBehavior.floating,
backgroundColor: Colors.deepOrange,
),
);
});
}
Future<void> doTransaction() async {
try {
model.isLoading = true;
notifyListeners();
transactionController
.model
.formData = transactionController.model.formData.copyWith(
type: "REQUEST",
trace: transactionController.model.confirmationData['trace'],
authType: transactionController.model.selectedPaymentProcessor.authType,
);
dynamic response = await http.post(
'/transaction',
transactionController.model.formData,
);
logger.i(response.toString());
if (response['status'] != 'FAILED') {
model.status = response['status'];
transactionController.updateReceiptData(response);
if (transactionController.model.selectedPaymentProcessor.authType ==
"WEB") {
if (context.mounted) {
context.push(
'/gateway',
extra: transactionController.model.receiptData?['targetUrl'],
);
}
} else {
await pollTransaction(transactionController.model.receiptData?['id']);
context.push('/receipt');
}
} else {
model.status = response['status'];
model.errorMessage = response['errorMessage'];
_showErrorSnackBar(response['errorMessage']);
}
} catch (e) {
logger.e(e);
model.errorMessage = e.toString();
_showErrorSnackBar(
"Problem completing the transaction, please try again.",
);
}
model.isLoading = false;
notifyListeners();
}
Future<void> pollTransaction(String uid) async {
while (!model.isCancelled) {
// Check again after delay in case it was cancelled during the delay
if (model.isCancelled) break;
try {
dynamic response = await http.get('/transaction/poll/$uid');
logger.i(response.toString());
if (response['status'] == 'SUCCESS') {
model.status = response['status'];
transactionController.updateReceiptData(response);
notifyListeners();
break;
}
} catch (e) {
logger.e(e);
_showErrorSnackBar(
"Network error. Please try again or contact support",
);
}
// Check cancellation flag instead of true
// Wait 5 seconds before the next poll
await Future.delayed(const Duration(seconds: 5));
}
}
}

View File

@@ -0,0 +1,151 @@
// dart format width=80
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'confirm_controller.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
// dart format off
T _$identity<T>(T value) => value;
/// @nodoc
mixin _$ConfirmData {
String get status; String get errorMessage;
/// Create a copy of ConfirmData
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
$ConfirmDataCopyWith<ConfirmData> get copyWith => _$ConfirmDataCopyWithImpl<ConfirmData>(this as ConfirmData, _$identity);
/// Serializes this ConfirmData to a JSON map.
Map<String, dynamic> toJson();
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is ConfirmData&&(identical(other.status, status) || other.status == status)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,status,errorMessage);
@override
String toString() {
return 'ConfirmData(status: $status, errorMessage: $errorMessage)';
}
}
/// @nodoc
abstract mixin class $ConfirmDataCopyWith<$Res> {
factory $ConfirmDataCopyWith(ConfirmData value, $Res Function(ConfirmData) _then) = _$ConfirmDataCopyWithImpl;
@useResult
$Res call({
String status, String errorMessage
});
}
/// @nodoc
class _$ConfirmDataCopyWithImpl<$Res>
implements $ConfirmDataCopyWith<$Res> {
_$ConfirmDataCopyWithImpl(this._self, this._then);
final ConfirmData _self;
final $Res Function(ConfirmData) _then;
/// Create a copy of ConfirmData
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline') @override $Res call({Object? status = null,Object? errorMessage = null,}) {
return _then(_self.copyWith(
status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
/// @nodoc
@JsonSerializable()
class _ConfirmData implements ConfirmData {
const _ConfirmData({required this.status, required this.errorMessage});
factory _ConfirmData.fromJson(Map<String, dynamic> json) => _$ConfirmDataFromJson(json);
@override final String status;
@override final String errorMessage;
/// Create a copy of ConfirmData
/// with the given fields replaced by the non-null parameter values.
@override @JsonKey(includeFromJson: false, includeToJson: false)
@pragma('vm:prefer-inline')
_$ConfirmDataCopyWith<_ConfirmData> get copyWith => __$ConfirmDataCopyWithImpl<_ConfirmData>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$ConfirmDataToJson(this, );
}
@override
bool operator ==(Object other) {
return identical(this, other) || (other.runtimeType == runtimeType&&other is _ConfirmData&&(identical(other.status, status) || other.status == status)&&(identical(other.errorMessage, errorMessage) || other.errorMessage == errorMessage));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType,status,errorMessage);
@override
String toString() {
return 'ConfirmData(status: $status, errorMessage: $errorMessage)';
}
}
/// @nodoc
abstract mixin class _$ConfirmDataCopyWith<$Res> implements $ConfirmDataCopyWith<$Res> {
factory _$ConfirmDataCopyWith(_ConfirmData value, $Res Function(_ConfirmData) _then) = __$ConfirmDataCopyWithImpl;
@override @useResult
$Res call({
String status, String errorMessage
});
}
/// @nodoc
class __$ConfirmDataCopyWithImpl<$Res>
implements _$ConfirmDataCopyWith<$Res> {
__$ConfirmDataCopyWithImpl(this._self, this._then);
final _ConfirmData _self;
final $Res Function(_ConfirmData) _then;
/// Create a copy of ConfirmData
/// with the given fields replaced by the non-null parameter values.
@override @pragma('vm:prefer-inline') $Res call({Object? status = null,Object? errorMessage = null,}) {
return _then(_ConfirmData(
status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
as String,errorMessage: null == errorMessage ? _self.errorMessage : errorMessage // ignore: cast_nullable_to_non_nullable
as String,
));
}
}
// dart format on

View File

@@ -0,0 +1,18 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'confirm_controller.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_ConfirmData _$ConfirmDataFromJson(Map<String, dynamic> json) => _ConfirmData(
status: json['status'] as String,
errorMessage: json['errorMessage'] as String,
);
Map<String, dynamic> _$ConfirmDataToJson(_ConfirmData instance) =>
<String, dynamic>{
'status': instance.status,
'errorMessage': instance.errorMessage,
};

View File

@@ -0,0 +1,354 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:qpay/screens/confirm/confirm_controller.dart';
import 'package:skeletonizer/skeletonizer.dart';
class ConfirmScreen extends StatefulWidget {
const ConfirmScreen({super.key});
@override
State<ConfirmScreen> createState() => _ConfirmScreenState();
}
class _ConfirmScreenState extends State<ConfirmScreen>
with TickerProviderStateMixin {
late ConfirmController controller;
late Animation<double> _fadeAnimation;
late Animation<Offset> _slideAnimation;
late AnimationController _controller;
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
controller = ConfirmController(context);
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 800),
);
_fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
),
);
_slideAnimation = Tween<Offset>(
begin: const Offset(0, 0.1),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5, curve: Curves.easeOut),
),
);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading:
context.canPop()
? IconButton(
onPressed: () {
context.pop();
},
icon: const Icon(Icons.arrow_back),
)
: SizedBox.shrink(),
title: const Text(
'Confirm Payment',
style: TextStyle(color: Colors.black87),
),
),
body: ListenableBuilder(
listenable: controller,
builder: (context, child) {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: FadeTransition(
opacity: _fadeAnimation,
child: SlideTransition(
position: _slideAnimation,
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 1.0,
color: Theme.of(context).colorScheme.primary,
),
),
),
padding: EdgeInsets.only(
bottom: 4.0,
), // Adjust spacing
child: Text(
"PROVIDER DETAILS",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
...controller
.transactionController
.model
.confirmationData["additionalData"]
.map<Widget>((data) {
return Column(
children: [
const SizedBox(height: 10),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Text(
data["name"] ?? "",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
data["value"] ?? "",
style: TextStyle(fontSize: 16),
),
],
),
],
);
})
.toList(),
const SizedBox(height: 30),
Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 1.0,
color: Theme.of(context).colorScheme.primary,
),
),
),
padding: EdgeInsets.only(
bottom: 4.0,
), // Adjust spacing
child: Text(
"TRANSACTIONS DETAILS",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Amount",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
controller
.transactionController
.model
.confirmationData["amount"]
.toString(),
style: TextStyle(fontSize: 16),
),
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Our Charge",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
controller
.transactionController
.model
.confirmationData["charge"]
.toString(),
style: TextStyle(fontSize: 16),
),
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Gateway Charge",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
controller
.transactionController
.model
.confirmationData["gatewayCharge"]
.toString(),
style: TextStyle(fontSize: 16),
),
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Tax",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
controller
.transactionController
.model
.confirmationData["tax"]
.toString(),
style: TextStyle(fontSize: 16),
),
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Total Amount",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
Text(
controller
.transactionController
.model
.confirmationData["totalAmount"]
.toString(),
style: TextStyle(fontSize: 16),
),
],
),
const SizedBox(height: 20),
_buildPaymentProcessorButton(),
],
),
),
),
),
),
);
},
),
);
}
Widget _buildPaymentProcessorButton() {
return Skeletonizer(
enabled: controller.model.isLoading,
child: SlideTransition(
position: _slideAnimation,
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black87.withAlpha(20)),
borderRadius: BorderRadius.circular(12),
gradient: LinearGradient(
colors: [
Theme.of(context).colorScheme.primary.withValues(alpha: 0.1),
Theme.of(context).colorScheme.tertiary.withValues(alpha: 0.05),
],
stops: const [0.3, 0.7],
begin: Alignment.topRight,
end: Alignment.bottomLeft,
),
),
child: InkWell(
customBorder: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
onTap: () async {
if (controller.model.isLoading) return;
await controller.doTransaction();
},
child: ListTile(
leading: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(
context,
).colorScheme.primary.withValues(alpha: 0.1),
shape: BoxShape.circle,
),
child: Image(
image: AssetImage(
"assets/${controller.transactionController.model.selectedPaymentProcessor.image}",
),
),
),
title: Text(
controller
.transactionController
.model
.selectedPaymentProcessor
.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
controller
.transactionController
.model
.selectedPaymentProcessor
.description,
style: TextStyle(fontWeight: FontWeight.normal, fontSize: 10),
),
trailing: Icon(
Icons.chevron_right,
color: Theme.of(
context,
).colorScheme.secondary.withValues(alpha: 0.7),
),
),
),
),
),
);
}
}