startPayment method

  1. @override
Future<Result> startPayment(
  1. SingleTransaction transaction, {
  2. void onPaymentCreated(
    1. String? transactionId
    )?,
})
override

Method used to start standard payment with Tpay UI Module

transaction - The transaction details required to initiate the payment. onPaymentCreated - Optional callback that is invoked when a payment is successfully created. It receives the transaction ID as a parameter.

Implementation

@override
Future<Result> startPayment(
  SingleTransaction transaction, {
  void Function(String? transactionId)? onPaymentCreated,
}) async {
  if (isPaymentOngoing) {
    throw Exception('Payment is already in progress');
  }
  isPaymentOngoing = true;
  final intermediateResultStreamListener = eventChannel.receiveBroadcastStream().listen(
    (result) {
      final mappedResult = mapResult(result);
      if (mappedResult is PaymentCreated) {
        onPaymentCreated?.call(mappedResult.transactionId);
      }
    },
  );
  final result = await methodChannel.invokeMethod(startPaymentMethod, jsonEncode(transaction)).whenComplete(() {
    isPaymentOngoing = false;
    intermediateResultStreamListener.cancel();
  });
  return mapResult(result);
}