create static method

Future<Payment> create({
  1. required MethodChannel channel,
  2. required IStoneHandler paymentHandler,
  3. IStoneTechHandler? iStoneTechHandler,
  4. required String licenceKey,
})

Método factory assíncrono para criação controlada e validação da licença

Implementation

static Future<Payment> create({
  required MethodChannel channel,
  required IStoneHandler paymentHandler,
  IStoneTechHandler? iStoneTechHandler,
  required String licenceKey,
}) async {
  // Se já existe instância e licença validada, retorna ela diretamente
  if (_instance != null && _isLicenceValidated) {
    return _instance!;
  }

  // Cria nova instância
  final payment = Payment._(
    channel: channel,
    paymentHandler: paymentHandler,
    licenceKey: licenceKey,
  );

  _isLicenceValidated = await payment._checkLicence();

  if (!_isLicenceValidated) {
    // Limpa cache pois licença inválida
    _instance = null;
    _isLicenceValidated = false;
    throw Exception('Invalid licence key. Please check your licence key.');
  }

  // Salva instância e estado da licença validada
  _instance = payment;
  _isLicenceValidated = true;

  payment.channel.setMethodCallHandler((e) => IStoneHelper.callHandler(
        call: e,
        iStoneHandler: paymentHandler,
        stoneTechHandler: iStoneTechHandler,
        onTransaction: (model) => _checkPinpadActivated(model),
      ));

  return payment;
}