createTransaction method

Future<PaywayCreateTransactionResponse> createTransaction({
  1. required PaywayCreateTransaction transaction,
  2. bool enabledLogger = false,
})

createTransaction

create a new trasaction

Implementation

Future<PaywayCreateTransactionResponse> createTransaction(
    {required PaywayCreateTransaction transaction,
    bool enabledLogger = false}) async {
  var res = PaywayCreateTransactionResponse(status: 11);
  var _transaction = transaction;
  if (![ABAPaymentOption.abapay_deeplink].contains(transaction.option)) {
    _transaction =
        _transaction.copyWith(option: ABAPaymentOption.abapay_deeplink);
  }
  assert([ABAPaymentOption.abapay_deeplink].contains(_transaction.option));
  Map<String, dynamic> map = _transaction.toFormDataMap();

  var formData = FormData.fromMap(map);
  try {
    if (helper == null) return PaywayCreateTransactionResponse();
    final client = helper!.client;

    if (enabledLogger) {
      debugPrint(json.encode(map));
      client.interceptors.add(dioLoggerInterceptor);
    }

    Response<String> response =
        await client.post("/purchase", data: formData);

    var _map = json.decode(response.data!) as Map<String, dynamic>;
    res = PaywayCreateTransactionResponse.fromMap(_map);
    return res;
  } catch (error, stacktrace) {
    print("Exception occured: $error stackTrace: $stacktrace");
    res = res.copyWith(
        description: ABAClientService.handleResponseError(error));
  }
  return res;
}