setFetchAccessTokenCallback static method

void setFetchAccessTokenCallback(
  1. Future<PEAccessTokenJson> callback()
)

Sets the callback function that provides an access token when required by PayEngine.

When the PayEngine SDK needs an access token, it will call this callback function to request the host application to fetch a new token from its backend.

Important:

  • PayEngine will invoke this callback whenever an access token is needed or needs to be refreshed.
  • The integrator must ensure that the token retrieval logic is secure and efficient.
  • For more details on access token management, refer to the official documentation: PayEngine Merchant Session

Example Usage

The following example demonstrates how an integrator can set the access token callback:

class MyPayEngineManager {
  void configurePayEngine() {
    PayEngine.setFetchAccessTokenCallback(fetchAccessToken);
  }

  Future<PEAccessTokenJson> fetchAccessToken() async {
    try {
      // Simulate a network request to fetch the token
      final token = "sample_access_token_123";
      final expiresIn = 900; // Token expires in 900 seconds (15 minutes)

      return PEAccessTokenJson(accessToken: token, expiresIn: expiresIn);
    } catch (e) {
      throw Exception("Failed to fetch access token: $e");
    }
  }
}
  • callback: A function that returns a Future<PEAccessTokenJson> representing the access token. The function must handle errors properly in case token retrieval fails.

Implementation

static void setFetchAccessTokenCallback(
    Future<PEAccessTokenJson> Function() callback) {
  _accessTokenProvider = callback;

  // Notify the native side that the callback has been set
  _channel.invokeMethod('setFetchAccessTokenCallback', {});

  // Set up the method call handler to respond to native requests for an access token
  _channel.setMethodCallHandler(_fetchAccessTokenHandler);
}