getCheckoutID property

Future<String> getCheckoutID

A call to the endpoint on your server to get a checkout ID.

Implementation

Future<String> get getCheckoutID async {
  try {
    final body = {
      'entityID': _checkoutSettings?.brand.entityID(config),
      'amount': _checkoutSettings?.amount.toStringAsFixed(2),
      ..._checkoutSettings?.additionalParams ?? {},
    };
    final Response response = await post(
      _config.checkoutEndpoint,
      headers: _checkoutSettings?.headers,
      body: (_checkoutSettings?.headers['Content-Type'] ?? '') ==
              'application/json'
          ? json.encode(body)
          : body,
    );

    if (response.statusCode != 200) {
      throw HttpException('${response.statusCode}: ${response.body}');
    }

    final Map _resBody = json.decode(response.body);

    if (_resBody['result'] != null && _resBody['result']['code'] != null) {
      switch (_resBody['result']['code']) {
        case '000.200.100':
          _checkoutID = _resBody['id'];
          break;
        case '200.300.404':
          throw HyperpayException(
            _resBody['result']['description'],
            _resBody['result']['code'],
            _resBody.containsKey('parameterErrors')
                ? _resBody['result']['parameterErrors']
                    .map(
                      (param) =>
                          '(param: ${param['name']}, value: ${param['value']})',
                    )
                    .join(',')
                : '',
          );
        default:
          throw HyperpayException(
            _resBody['description'],
            _resBody['code'],
          );
      }

      log(_checkoutID, name: "HyperpayPlugin/getCheckoutID");

      return _checkoutID;
    } 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) {
    log('${exception.toString()}', name: "HyperpayPlugin/getCheckoutID");
    rethrow;
  }
}