simpay 0.2.3
simpay: ^0.2.3 copied to clipboard
SDK para la integración con la API de Simpay
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:simpay/simpay.dart';
import 'package:simpay/colors.dart';
import './const.dart';
import 'dart:math';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyApp createState() => _MyApp();
}
class _MyApp extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Simpay',
home: Pay(),
);
}
}
class Pay extends StatefulWidget {
const Pay({Key? key}) : super(key: key);
@override
State<Pay> createState() => _PayState();
}
class _PayState extends State<Pay> {
final GlobalKey _scaffold = GlobalKey();
final simpay = Simpay(
commerceCode: commerceCode,
apiKey: apikey,
secretKey: secretkey,
production: false);
DataPayments data = DataPayments(
amount: 0,
order: "",
subject: "",
);
bool active = false;
ResponsePayment response = ResponsePayment(
success: false, order: '', url: '', session: '', amount: 0);
ListPayments _methods = ListPayments(list: []);
int num = 0;
String message = "Cargando medios de pago";
TextStyle sTitle = TextStyle(color: AppColor.primary, fontSize: 16);
TextStyle sMethodTtitle =
TextStyle(fontWeight: FontWeight.bold, color: AppColor.primary);
Image logo =
const Image(image: AssetImage('assets/isotipo.png', package: 'simpay'));
String currentMethod = "";
TextStyle sMethods =
TextStyle(fontWeight: FontWeight.bold, color: AppColor.primary, fontSize: 16);
TextStyle sMessage =
TextStyle(fontSize: 13, color: AppColor.secondary);
@override
void initState() {
super.initState();
Timer.run(() {
simpayInit();
});
}
@override
void didChangeDependencies() {
context.dependOnInheritedWidgetOfExactType();
super.didChangeDependencies();
}
@override
void dispose() {
context.dependOnInheritedWidgetOfExactType();
super.dispose();
}
void setMessage(msg) {
setState(() => message = msg);
}
void setLoad(b) {
setState(() => active = b);
}
void showNotify(String text, Color? color) {
var snackBar = SnackBar(
content: Text(text),
backgroundColor: color,
duration: const Duration(milliseconds: 500),
);
ScaffoldMessenger.of(_scaffold.currentContext!).showSnackBar(snackBar);
}
void simpayInit() async {
simpay.getMethodsPayments().then((ListPayments payments) {
setState(() => _methods = payments);
});
simpay.success.stream.listen((ResPayment res) {
switch (res.status) {
case StatusPayment.AUTHORIZED:
showNotify("Transacción exitosa", AppColor.success);
break;
case StatusPayment.FAILED:
showNotify("Transacción Fallida", AppColor.error);
break;
case StatusPayment.CANCEL:
showNotify("Transaccion cancelada por el cliente", AppColor.secondary);
break;
case StatusPayment.TIMEOUT:
showNotify("Timeout transacción", AppColor.secondary);
break;
}
});
simpay.onProcess.stream.listen((EventStatusPayment ev) {
switch (ev.event) {
case "create_pay":
switch (ev.status) {
case "inProcess":
setLoad(false);
return showNotify("Creando transacción", AppColor.secondary);
case "ok":
return showNotify("Transacción creada", AppColor.secondary);
case "err":
return showNotify("Ah ocurrido un error al creal la transacción",
AppColor.secondary);
}
break;
case "get_payments":
switch (ev.status) {
case "inProcess":
return showNotify("Cargando métodos de pago", AppColor.secondary);
case "ok":
setMessage("");
return showNotify("Medios de pago cargados", AppColor.secondary);
case "err":
return showNotify(
"Ah ocurrido un error al cargar los métodos de pago",
AppColor.secondary);
}
break;
case "get_pay":
switch (ev.status) {
case "inProcess":
case "ok":
}
break;
}
});
}
void createPayment(PaymentMethods payment, context) {
if (num < 100) {
showNotify(
r"Debes agregar un monto superior a $99 pesos", AppColor.secondary);
} else {
data.order = "O-00" + Random().nextInt(100000).toString();
data.subject = "Prueba de integracion SDK simpay for Flutter";
data.method = payment.code;
data.amount = num;
data.contact = "luis@bukitech.cl";
simpay.createPay(data).then((ResponsePayment value) {
setState(() {
response = value;
active = true;
currentMethod = "con " + toCapitalize(payment.name);
});
});
}
}
void setAmountValue(v) {
setState(() => num = v.isEmpty ? 0 : int.parse(v));
}
String toCapitalize(String text) {
return "${text.characters.first.toUpperCase()}${text.substring(1)}";
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffold,
appBar: AppBar(
backgroundColor: AppColor.light,
elevation: 1,
title: Text('SDK Simpay for Flutter', style: sTitle),
leading: Container(padding: const EdgeInsets.all(10), child: logo),
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Column(children: [
TextField(
onChanged: (v) {
setAmountValue(v);
setState(() => active = false);
},
decoration:
const InputDecoration(labelText: "Ingresa un monto"),
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9]'))
],
),
Container(
height: 20,
),
Container(
margin: const EdgeInsets.symmetric(vertical: 15),
child: Text("Métodos de pago disponibles", style: sMethods,),
),
Column(
children: _methods.list.map((PaymentMethods payment) {
return GestureDetector(
onTap: () => createPayment(payment, context),
child: Card(
child: Container(
padding: const EdgeInsets.all(15),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
Container(
width: 100,
child: Text(
toCapitalize(payment.name),
style: sMethodTtitle,
),
),
Expanded(
child: Container(
alignment: Alignment.centerRight,
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Text(
payment.desc.toString())),
Container(
child: const Icon(
Icons.chevron_right))
]),
),
)
],
),
),
),
);
}).toList()),
Container(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(message, style: sMessage,),
),
Container(
height: 60,
),
ElevatedButton(
child: Text("Pagar " + currentMethod),
onPressed: () {
if (active) simpay.getPay(context, response);
},
style: ElevatedButton.styleFrom(
primary: active ? AppColor.primary : Colors.grey,
minimumSize: const Size(250, 50),
),
)
])
],
),
),
],
)),
),
);
}
}