simpay 0.0.2 copy "simpay: ^0.0.2" to clipboard
simpay: ^0.0.2 copied to clipboard

outdated

pasarela de pagos chilena.

example/lib/main.dart

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(),
      // initialRoute: '/',
      // routes: <String, WidgetBuilder>{
      //   '/': (BuildContext context) => Pay(),
      //   '/success': (BuildContext context) => Success(),
      //   '/failed': (BuildContext context) => Failed(),
      //   '/cancel': (BuildContext context) => Cancel(),
      // },
    );
  }
}

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.configViewPay(title: "Webpay");

    sdk.success.stream.listen((ResPayment res) {
      print("RES -> ${res}");
      switch (res.status) {
        case StatusPayment.AUTHORIZED:
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => ResponseRoute(
                    msg: "Transacción exitosa", nabvarColor: Colors.green)),
          );
          break;
        case StatusPayment.FAILED:
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => ResponseRoute(
                    msg: "Transacción Fallida", nabvarColor: Colors.red)),
          );
          break;
        case StatusPayment.CANCEL:
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => ResponseRoute(
                      msg: "Transacción cancelada por el usuario",
                      nabvarColor: Colors.lightBlue)));
          break;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF4FC08D),
          title: const Text('Prueba de pago Simpay'),
        ),
        body: SingleChildScrollView(
            child: Column(
          children: [
            Container(
              color: Color(0xFFF6F8FA),
              padding: EdgeInsets.all(20),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                children: [
                  Container(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 15),
                            child: Text(
                              "Ingreso",
                              style: TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.bold),
                            ),
                          ),
                          Card(
                            child: Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.all(15),
                                  child: TextField(
                                    onChanged: (v) {
                                      if (v.isEmpty) {
                                        setState(() => num = 0);
                                      } else {
                                        // double c = double.parse(v);
                                        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(
                            margin: EdgeInsets.symmetric(vertical: 15),
                            child: Text(
                              "Detalle",
                              style: TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.bold),
                            ),
                          ),
                          Card(
                            child: Column(
                              children: [
                                Container(
                                  padding: EdgeInsets.all(15),
                                  child: Row(
                                    children: [
                                      Container(
                                        width: 100,
                                        child: Text("Subtotal:"),
                                      ),
                                      Expanded(
                                        child: Container(
                                          child: Container(
                                            alignment: Alignment.bottomRight,
                                            child: Text(r"$" + num.toString()),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 15),
                            child: Text(
                              "Formas de pago",
                              style: TextStyle(
                                  fontSize: 18, fontWeight: FontWeight.bold),
                            ),
                          ),
                          Container(
                            child: Column(
                                children: _methods.list.map((PaymentMethods e) {
                              return GestureDetector(
                                onTap: () {
                                  print("CLICK");
                                  data.order = "O-002322";
                                  data.subject = "Flutter";
                                  data.method = e.code;
                                  data.amount = num;

                                  sdk
                                      .createPay(data)
                                      .then((ResponsePayment value) {
                                    print(
                                        "RESPONSECREATE PAY -> ${value.JSON()}");
                                    setState(() {
                                      response = value;
                                      active = true;
                                    });
                                  }).catchError((onError) {
                                    print(onError);
                                  });
                                },
                                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(
                    padding: EdgeInsets.all(15),
                    child: Container(
                      margin: EdgeInsets.all(5),
                      padding: EdgeInsets.all(15),
                      alignment: Alignment.center,
                      color: active ? Colors.black : Colors.grey,
                      child: GestureDetector(
                        child: Text(r"Pagar",
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold)),
                        onTap: () {
                          if (active) {
                            sdk.getPay(context, response);
                          }
                        },
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        )),
      ),
    );
  }
}

class ResponseRoute extends StatefulWidget {
  String msg;
  Color nabvarColor;
  ResponseRoute({Key? key, required this.msg, required this.nabvarColor})
      : super(key: key);

  @override
  State<ResponseRoute> createState() => _ResponseRouteState();
}

class _ResponseRouteState extends State<ResponseRoute> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: widget.nabvarColor,
          title: const Text('Respuesta de pago'),
        ),
        body: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.symmetric(vertical: 100),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                child: Text(widget.msg),
              ),
              Container(
                margin: EdgeInsets.only(top: 20),
                child: ElevatedButton(
                  child: Text("Volver"),
                  onPressed: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => Pay()));
                  },
                ),
              )
            ],
          ),
        ));
  }
}