Initial commit
This commit is contained in:
125
lib/payment/paystack/pay_stack_screen.dart
Normal file
125
lib/payment/paystack/pay_stack_screen.dart
Normal file
@@ -0,0 +1,125 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:driver/payment/paystack/paystack_url_genrater.dart';
|
||||
import 'package:driver/themes/app_them_data.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class PayStackScreen extends StatefulWidget {
|
||||
final String initialURl;
|
||||
final String reference;
|
||||
final String amount;
|
||||
final String secretKey;
|
||||
final String callBackUrl;
|
||||
|
||||
const PayStackScreen({super.key, required this.initialURl, required this.reference, required this.amount, required this.secretKey, required this.callBackUrl});
|
||||
|
||||
@override
|
||||
State<PayStackScreen> createState() => _PayStackScreenState();
|
||||
}
|
||||
|
||||
class _PayStackScreenState extends State<PayStackScreen> {
|
||||
WebViewController controller = WebViewController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
initController();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void initController() {
|
||||
controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..setBackgroundColor(const Color(0x00000000))
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onProgress: (int progress) {
|
||||
// Update loading bar.
|
||||
},
|
||||
onPageStarted: (String url) {},
|
||||
onPageFinished: (String url) {},
|
||||
onWebResourceError: (WebResourceError error) {},
|
||||
onNavigationRequest: (NavigationRequest navigation) async {
|
||||
debugPrint("--->2${navigation.url}");
|
||||
debugPrint("--->2" "${widget.callBackUrl}?trxref=${widget.reference}&reference=${widget.reference}");
|
||||
if (navigation.url == 'https://foodieweb.siswebapp.com/success?trxref=${widget.reference}&reference=${widget.reference}' ||
|
||||
navigation.url == '${widget.callBackUrl}?trxref=${widget.reference}&reference=${widget.reference}') {
|
||||
final isDone = await PayStackURLGen.verifyTransaction(secretKey: widget.secretKey, reference: widget.reference, amount: widget.amount);
|
||||
Get.back(result: isDone);
|
||||
}
|
||||
if ((navigation.url == '${widget.callBackUrl}?trxref=${widget.reference}&reference=${widget.reference}') ||
|
||||
(navigation.url == "https://hello.pstk.xyz/callback") ||
|
||||
(navigation.url == 'https://standard.paystack.co/close') ||
|
||||
(navigation.url == 'https://talazo.app/login')) {
|
||||
final isDone = await PayStackURLGen.verifyTransaction(secretKey: widget.secretKey, reference: widget.reference, amount: widget.amount);
|
||||
Get.back(result: isDone);
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse(widget.initialURl));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return WillPopScope(
|
||||
onWillPop: () async {
|
||||
_showMyDialog();
|
||||
return false;
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: AppThemeData.grey50,
|
||||
title: const Text("Payment"),
|
||||
centerTitle: false,
|
||||
leading: GestureDetector(
|
||||
onTap: () {
|
||||
_showMyDialog();
|
||||
},
|
||||
child: const Icon(
|
||||
Icons.arrow_back,
|
||||
),
|
||||
)),
|
||||
body: WebViewWidget(controller: controller),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showMyDialog() async {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: true, // user must tap button!
|
||||
builder: (BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Cancel Payment'),
|
||||
content: const SingleChildScrollView(
|
||||
child: Text("cancelPayment?"),
|
||||
),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: const Text(
|
||||
'Cancel',
|
||||
style: TextStyle(color: Colors.red),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
Navigator.of(context).pop(false);
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: const Text(
|
||||
'Continue',
|
||||
style: TextStyle(color: Colors.green),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
57
lib/payment/paystack/pay_stack_url_model.dart
Normal file
57
lib/payment/paystack/pay_stack_url_model.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
// To parse this JSON data, do
|
||||
//
|
||||
// final payStackUrlModel = payStackUrlModelFromJson(jsonString);
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
PayStackUrlModel payStackUrlModelFromJson(String str) => PayStackUrlModel.fromJson(json.decode(str));
|
||||
|
||||
String payStackUrlModelToJson(PayStackUrlModel data) => json.encode(data.toJson());
|
||||
|
||||
class PayStackUrlModel {
|
||||
PayStackUrlModel({
|
||||
required this.status,
|
||||
required this.message,
|
||||
required this.data,
|
||||
});
|
||||
|
||||
bool status;
|
||||
String message;
|
||||
Data data;
|
||||
|
||||
factory PayStackUrlModel.fromJson(Map<String, dynamic> json) => PayStackUrlModel(
|
||||
status: json["status"],
|
||||
message: json["message"],
|
||||
data: Data.fromJson(json["data"]),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"status": status,
|
||||
"message": message,
|
||||
"data": data.toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
class Data {
|
||||
Data({
|
||||
required this.authorizationUrl,
|
||||
required this.accessCode,
|
||||
required this.reference,
|
||||
});
|
||||
|
||||
String authorizationUrl;
|
||||
String accessCode;
|
||||
String reference;
|
||||
|
||||
factory Data.fromJson(Map<String, dynamic> json) => Data(
|
||||
authorizationUrl: json["authorization_url"],
|
||||
accessCode: json["access_code"],
|
||||
reference: json["reference"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"authorization_url": authorizationUrl,
|
||||
"access_code": accessCode,
|
||||
"reference": reference,
|
||||
};
|
||||
}
|
||||
75
lib/payment/paystack/paystack_url_genrater.dart
Normal file
75
lib/payment/paystack/paystack_url_genrater.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:driver/models/payment_model/pay_fast_model.dart';
|
||||
import 'package:driver/models/user_model.dart';
|
||||
import 'package:driver/payment/paystack/pay_stack_url_model.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class PayStackURLGen {
|
||||
static Future payStackURLGen({required String amount, required String secretKey, required String currency, required UserModel userModel}) async {
|
||||
const url = "https://api.paystack.co/transaction/initialize";
|
||||
final response = await http.post(Uri.parse(url), body: {
|
||||
"email": userModel.email,
|
||||
"amount": amount,
|
||||
"currency": currency,
|
||||
}, headers: {
|
||||
"Authorization": "Bearer $secretKey",
|
||||
});
|
||||
debugPrint(response.body);
|
||||
final data = jsonDecode(response.body);
|
||||
if (!data["status"]) {
|
||||
return null;
|
||||
}
|
||||
return PayStackUrlModel.fromJson(data);
|
||||
}
|
||||
|
||||
static Future<bool> verifyTransaction({
|
||||
required String reference,
|
||||
required String secretKey,
|
||||
required String amount,
|
||||
}) async {
|
||||
debugPrint("we Enter payment Settle");
|
||||
debugPrint(reference);
|
||||
|
||||
final url = "https://api.paystack.co/transaction/verify/$reference";
|
||||
|
||||
var response = await http.get(Uri.parse(url), headers: {
|
||||
"Authorization": "Bearer $secretKey",
|
||||
});
|
||||
|
||||
debugPrint(response.body);
|
||||
final data = jsonDecode(response.body);
|
||||
if (data["status"] == true) {
|
||||
if (data["message"] == "Verification successful") {}
|
||||
}
|
||||
|
||||
return data["status"];
|
||||
|
||||
//PayPalClientSettleModel.fromJson(data);
|
||||
}
|
||||
|
||||
static Future<String> getPayHTML({required String amount, required PayFastModel payFastSettingData, required UserModel userModel}) async {
|
||||
String newUrl = 'https://${payFastSettingData.isSandbox == false ? "www" : "sandbox"}.payfast.co.za/eng/process';
|
||||
Map body = {
|
||||
'merchant_id': payFastSettingData.merchantId,
|
||||
'merchant_key': payFastSettingData.merchantKey,
|
||||
'amount': amount,
|
||||
'item_name': "goRide online payment",
|
||||
'return_url': payFastSettingData.returnUrl,
|
||||
'cancel_url': payFastSettingData.cancelUrl,
|
||||
'notify_url': payFastSettingData.notifyUrl,
|
||||
'name_first': userModel.firstName,
|
||||
'name_last': userModel.lastName,
|
||||
'email_address': userModel.email,
|
||||
};
|
||||
|
||||
final response = await http.post(
|
||||
Uri.parse(newUrl),
|
||||
body: body,
|
||||
);
|
||||
|
||||
debugPrint(response.body);
|
||||
return response.body;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user