createCharge method

Future<Charge> createCharge({
  1. required String requestId,
  2. required Charge charge,
  3. String? realmId,
  4. String? authToken,
})

To charge a credit card, you create a new charge object. If your API key is in development mode, the supplied card won't actually be charged, though everything else will occur as if in production mode.

Implementation

Future<Charge> createCharge({
  required String requestId,
  required Charge charge,
  String? realmId,
  String? authToken,
}) async {
  authToken ??= authenticationService.getCachedToken()?.access_token;
  realmId ??= authenticationService.getCachedRealmId();

  Map<String, String> headers = {
    "Request-ID": requestId,
    "Authorization": "Bearer ${authToken ?? ""}",
    'Content-Type': 'application/json',
    //'Accept': 'application/json',
  };

  Uri endpoint = Uri.https(baseUrl, "/quickbooks/v4/payments/charges");

  //print(endpoint.toString());

  var response = await http.post(endpoint,
      headers: headers, body: jsonEncode(charge.toJson()));

  if (response.statusCode == 200 || response.statusCode == 201) {
    //print(response.body);
    return Charge.fromJson(jsonDecode(response.body));
  } else {
    throw ChargeException(
        statusCode: response.statusCode, message: response.body);
  }
}