simpay 0.0.4 simpay: ^0.0.4 copied to clipboard
SDK para la integración con la API de Simpay (Solución de pagos en chile).
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:simpay/simpay.dart';
import './const.dart';
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 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 sdk = new Simpay(
commerceCode: commerceCode,
apiKey: apikey,
secretKey: secretkey,
production: false);
DataPayments data = new DataPayments(amount: 0, order: "", subject: "");
String? dv = "";
void getVersionDevice() async {
String? d = await Simpay.platformVersion;
setState(() => dv = d);
}
bool active = false;
ResponsePayment response = new ResponsePayment(
success: false, order: '', url: '', session: '', amount: 0);
ListPayments _methods = ListPayments(list: []);
int num = 0;
void initState() {
super.initState();
Timer.run(() {
simpayInit();
getVersionDevice();
});
}
void simpayInit() async {
sdk.getMethodsPayments().then((ListPayments payments) {
setState(() => _methods = payments);
});
sdk.success.stream.listen((ResPayment res) {
print("RES -> ${res}");
switch (res.status) {
case StatusPayment.AUTHORIZED:
print("Transacción exitosa");
break;
case StatusPayment.FAILED:
print("Transacción Fallida");
break;
case StatusPayment.CANCEL:
print("CANCEL BY USER");
break;
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Integración Simpay'),
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
Container(
child: Column(children: [
TextField(
onChanged: (v) {
if (v.isEmpty)
setState(() => num = 0);
else
setState(() => num = int.parse(v));
setState(() => active = false);
},
decoration:
InputDecoration(labelText: "Ingresa un monto"),
keyboardType:
TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp('[0-9]'))
],
),
Container(height: 20,),
Container(
margin: EdgeInsets.symmetric(vertical: 15),
child: Text("Formas de pago"),
),
Container(
child: Column(
children: _methods.list.map((PaymentMethods e) {
return GestureDetector(
onTap: () {
data.order = "O-002322";
data.subject = "Flutter";
data.method = e.code;
data.amount = num;
sdk.configViewPay(title: e.name);
sdk.createPay(data).then((ResponsePayment value) {
setState(() {
response = value;
active = true;
});
});
},
child: Container(
child: Card(
child: Container(
padding: EdgeInsets.all(15),
child: Row(
children: [
Container(
width: 100,
child: Text(e.name),
),
Expanded(
child: Container(
child: Container(
alignment: Alignment.bottomRight,
child: Icon(Icons.chevron_right),
),
),
)
],
),
),
),
),
);
}).toList()),
),
Container(
height: 60,
),
ElevatedButton(
child: Text("Pagar"),
onPressed: () {
if (active) sdk.getPay(context, response);
},
style: ElevatedButton.styleFrom(
primary: active ? Colors.black : Colors.grey,
minimumSize: Size(250, 50),
),
),
Container(height: 10),
ElevatedButton(
child: Text("Conectar"),
onPressed: () => sdk.openConnect(),
style: ElevatedButton.styleFrom(
primary: Colors.lightBlue,
minimumSize: Size(250, 50),
),
),
Container(height: 10),
ElevatedButton(
child: Text("Desconectar"),
onPressed: () => sdk.closeConnection(),
style: ElevatedButton.styleFrom(
primary: Colors.red,
minimumSize: Size(250, 50),
),
),
]),
),
],
),
),
],
)),
),
);
}
}