BMKeros / mercadopago_sdk_dart

MercadoPago Dart SDK
MIT License
31 stars 11 forks source link

preference info #11

Closed luisNezi closed 4 years ago

luisNezi commented 5 years ago

hello! I'm having some problems trying to implement the funcionality "getpreference" It lead's me to an error message.

the code is:

import 'dart:async'; import 'package:flutter/material.dart'; import 'package:mood/classes.dart'; import 'package:mood/style/components/textFieldFormularios.dart'; import 'package:mood/style/fonts.dart'; import 'package:flutter_google_pay/flutter_google_pay.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class CartaoPage extends StatefulWidget { CartaoPage(this.mediaHeight, this.mediaWidth, {Key key}) : super(key: key); final double mediaHeight; final double mediaWidth;

@override CartaoPageState createState() => new CartaoPageState(); }

class CartaoPageState extends State { @override void initState() { super.initState(); }

@override Widget build(BuildContext context) { MercadoPago mercadoInstance = new MercadoPago(); _makeCustomPayment() async { var environment = 'rest'; // or 'production'

  if (!(await FlutterGooglePay.isAvailable(environment))) {
    print('Google pay not available');
  } else {
    ///docs https://developers.google.com/pay/api/android/guides/tutorial
    PaymentBuilder pb = PaymentBuilder()
      ..addGateway( "PAYMENT_GATEWAY","3359798608018742195")
      ..addTransactionInfo("1.0", "USD")
      ..addAllowedCardAuthMethods(["PAN_ONLY", "CRYPTOGRAM_3DS"])
      ..addAllowedCardNetworks(
          ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"]
      )
      ..addBillingAddressRequired(true)
      ..addPhoneNumberRequired(true)
      ..addShippingAddressRequired(true)
      ..addShippingSupportedCountries(["US", "GB","BR"])
      ..addMerchantInfo("Example");

    Result result = await FlutterGooglePay.makeCustomPayment(pb.build()).catchError((error) {
      print(error);
    });
    if (result.status == ResultStatus.SUCCESS) {
      print('Success');
    } else if (result.error != null) {
      print(result.error);
    }
  }
}

Future<Map<String, dynamic>> index() async {
  var payer = {
    'email': 'abdul@live.com'
  };
  var preference = {
    "items": [
      {
        "title": "Test",
        "quantity": 1,
        "currency_id": "BRL",
        "unit_price": 10.4,
        "payer":payer
      }
    ],
  };

  var result = await mercadoInstance.mp.createPreference(preference);

  return result;
}

_launchURL(String url) async {
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

return Scaffold(
  backgroundColor: Color(0xff5c31b8),
  appBar: AppBar(
    backgroundColor: Color(0xff5c31b8),
    elevation: 0,
    actions: <Widget>[],
  ),
  body: SingleChildScrollView(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ConstrainedBox(
              constraints: BoxConstraints(
                maxWidth: widget.mediaWidth * 0.9,
              ),
              child: TextFieldFormulario(
                context,
                textInputAction: TextInputAction.next,
                style: AntipastoTextStyle(
                    color: Colors.white, fontSize: 20.0),
                labelText: "Numero do Cartão ",
                labelStyle: AntipastoTextStyle(
                    color: Colors.white, fontSize: 20.0),
              ),
            ),
          ],
        ),
        FlatButton(
          child: Text('Google Pay'),
          onPressed: ()async{
            _makeCustomPayment();
          },
        ),
        FlatButton(
          child: Text('Mercado Pago'),
          onPressed: ()async{
            var payment = await index();
            print(payment);
            var url = payment['response']["sandbox_init_point"];
            String iDe = payment['response']["id"];
            var result = await mercadoInstance.mp.getPreference(iDe);
            print(result);
          },
        )
      ],
    ),
  )
);

} }

it gives me the error message:

Exception has occurred. NoSuchMethodError (NoSuchMethodError: The getter 'keys' was called on null. Receiver: null Tried calling: keys)

Thanks

hostelix commented 4 years ago

Fix #14