getToken method

Future<Response?> getToken(
  1. RequestModel requestModel,
  2. BuildContext context
)

Implementation

Future<Response?> getToken(
    RequestModel requestModel, BuildContext context) async {
  try {
    String authToken =
        "Basic ${base64.encode(utf8.encode("${requestModel.user.toString()}:${requestModel.password.toString()}"))}";
    String url = ApiConstants.getSelectedBaseUrl() + ApiConstants.GET_TOKEN;

    Map<String, String> data = new Map();
    data["MerchantCode"] = requestModel.merchantCode.toString();
    data["Amount"] = requestModel.amount.toString();
    data["RefId"] = requestModel.refId.toString();
    data["DeliveryUrl"] = requestModel.deliveryUrl.toString();

    Map<String, String> headers = new Map();
    headers["Authorization"] = authToken;
    headers["Module"] = requestModel.module.toString();
    headers["Content-Type"] = "application/json;charset=UTF-8";

    var response = await http.post(Uri.parse(url),
        body: jsonEncode(data), headers: headers);
    if (response.statusCode == 401) {
      Utils.showSnackbar(context, "Unauthorized");
      return response;
    } else if (response.body == null) {
      Utils.showSnackbar(context, "Something Went Wrong");
      return null;
    } else {
      return response;
    }
  } catch (ex) {
    return null;
  }
}