paymentStatus method

Future<Map<String, dynamic>> paymentStatus(
  1. String checkoutID, {
  2. Map<String, String>? headers,
})

Check for payment status using a checkoutID.

Implementation

Future<Map<String, dynamic>> paymentStatus(String checkoutID,
    {Map<String, String>? headers}) async {
  try {
    final body = {
      'entityID': _checkoutSettings?.brand.entityID(config),
      'checkoutID': checkoutID,
    };

    final Response response = await post(
      _config.statusEndpoint,
      headers: headers,
      body: _checkoutSettings?.headers['Content-Type'] == 'application/json'
          ? json.encode(body)
          : body,
    );

    Map<String, dynamic> _resBody = {};

    try {
      _resBody = json.decode(response.body);
    } catch (e) {
      _resBody['body'] = response.body;
    }

    if (_resBody['result'] != null && _resBody['result']['code'] != null) {
      log(
        '${_resBody['result']['code']}: ${_resBody['result']['description']}',
        name: "HyperpayPlugin/checkPaymentStatus",
      );

      return _resBody['result'];
    } else {
      throw HyperpayException(
        'The returned result does not contain the key "result" as the first key.',
        'RESPONSE BODY NOT IDENTIFIED',
        'please structure the returned body as {result: {code: CODE, description: DESCRIPTION}, id: CHECKOUT_ID, ...}.',
      );
    }
  } catch (exception) {
    rethrow;
  }
}