payWithMpesa method

Future<ChargeResponse> payWithMpesa(
  1. MpesaRequest payload,
  2. Client client
)

Initiates payments via Mpesa Available for only payments with KES currency returns an instance of ChargeResponse or throws an error

Implementation

Future<ChargeResponse> payWithMpesa(
    MpesaRequest payload, http.Client client) async {
  final url = FlutterwaveURLS.getBaseUrl(this.isDebugMode) +
      FlutterwaveURLS.PAY_WITH_MPESA;
  final uri = Uri.parse(url);
  try {

    final http.Response response = await client.post(uri,
        headers: {
          HttpHeaders.authorizationHeader: this.publicKey,
          HttpHeaders.contentTypeHeader:'application/json'
        },
        body: json.encode(payload.toJson()));

    ChargeResponse chargeResponse =
        ChargeResponse.fromJson(json.decode(response.body));

    return chargeResponse;
  } catch (error) {
    throw (FlutterError(error.toString()));
  } finally {
    client.close();
  }
}