pay method
Future<void>
pay(
- String orderId, {
- required PaymentCompletionCallback onFinish,
- required PaymentErrorCallback onError,
Initiates the payment flow for the specified orderId
.
This method starts the payment process by:
- Fetching payment details from the CoFee payment API
- Opening the payment page in an in-app browser
- Monitoring the payment process
- Calling appropriate callbacks based on the outcome
Parameters:
orderId
: A unique identifier for the payment orderonFinish
: 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:
- PaymentNetworkException for network connectivity issues
- PaymentApiException for API-related errors
- PaymentInvalidResponseException for invalid API responses
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);
}