pay method

Future<void> pay(
  1. String orderId, {
  2. required PaymentCompletionCallback onFinish,
  3. required PaymentErrorCallback onError,
})

Initiates the payment flow for the specified orderId.

This method starts the payment process by:

  1. Fetching payment details from the CoFee payment API
  2. Opening the payment page in an in-app browser
  3. Monitoring the payment process
  4. Calling appropriate callbacks based on the outcome

Parameters:

  • orderId: A unique identifier for the payment order
  • onFinish: Called when the payment flow completes (success or cancellation)
  • onError: Called when an error occurs during the payment process

The onFinish callback does NOT guarantee successful payment - you must verify the payment status with your server after receiving this callback.

The onError callback provides a PaymentException with details about what went wrong. Check PaymentErrorType to handle different types of errors appropriately.

Example:

payment.pay(
  'order_123',
  onFinish: () => print('Payment flow completed'),
  onError: (error) => print('Error: ${error.message}'),
);

Throws:

Implementation

Future<void> pay(
  String orderId, {
  required PaymentCompletionCallback onFinish,
  required PaymentErrorCallback onError,
}) async {
  // Create a payment service with an API payment details provider
  final paymentService = PaymentService(
    paymentEnvironment: paymentEnvironment,
    paymentDetailsProvider: ApiPaymentDetailsProvider(
      clientId: clientId,
    ),
  );

  // Initiate the payment flow
  await paymentService.pay(orderId, onFinish: onFinish, onError: onError);
}