postData method

Future postData()

This posts the payment config to the server and return a response

Implementation

Future postData() async {
  try {
    final ServiceController _serviceController = Get.find();
    dynamic response;
    Map filteredConfig = {
      "txRef": GlobalizerController.paymentConfig!.txRef,
      "amount": GlobalizerController.paymentConfig!.amount,
      "baseCurrency": GlobalizerController.paymentConfig!.baseCurrency,
      "customer": GlobalizerController.paymentConfig!.customer,
      "customerFullName":
          GlobalizerController.paymentConfig!.customerFullName,
      "meta": {"from": "Flutter SDK"}
    };

    response = await http.post(
      Uri.parse('${Util.END_POINT}/payments/checkout'),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization':
            'Bearer ${GlobalizerController.paymentConfig!.publicKey}',
      },
      body: jsonEncode(filteredConfig),
    );
    //set the statusCode inside of the controller.
    _serviceController.postDataStatusCode = response.statusCode;

    //Handling Status codes Error Via Snackbar
    if (response.statusCode == 201 || response.statusCode == 200) {
      return jsonDecode(response.body);
    } else if (response.statusCode == 401) {
      //Bad Public key
      debugPrint("Your public key is not authorized");
      return jsonDecode(response.body);
    } else if (response.statusCode == 404) {
      debugPrint("postData() status: 404");

      return jsonDecode(response.body);
    } else if (response.body == "null") {
      debugPrint("postData() returning null from API");

      return jsonDecode(response.body);
    } else {
      return jsonDecode(response.body);
    }
  } on Exception catch (e) {
    rethrow;
  }
}