mnemo_cards/mnemo_cards_backend/lib/cron/check_payment.dart
Dmitry 306b2fdca7
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
fixes
2025-12-13 17:48:00 +03:00

61 lines
1.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:injectable/injectable.dart';
import 'package:mnemo_cards_backend/api/purchase/payment_manager.dart';
import 'package:mnemo_cards_backend/database/database.dart';
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
import 'task.dart' as task;
@LazySingleton()
class CheckPaymentTask with task.Task {
final PaymentManager _paymentManager;
final AppDatabase _db;
CheckPaymentTask(
this._paymentManager,
this._db,
);
Future<void> task() => check();
Future<void> check() async {
print('Checking payments...');
// Получаем платежи, которые не обработаны и не unknown
final allPayments = await _db.paymentDao.getAllPayments();
final unprocessedPayments = allPayments.where((payment) {
final status = PaymentStatus.values.firstWhere(
(s) => s.name == payment.status,
orElse: () => PaymentStatus.unknown,
);
if (status == PaymentStatus.processed || status == PaymentStatus.unknown) {
return false;
}
final system = PaymentSystem.values.firstWhere(
(s) => s.name == payment.paymentSystem,
orElse: () => PaymentSystem.yookassa,
);
return system == PaymentSystem.yookassa || system == PaymentSystem.rustore;
}).toList();
print('Found ${unprocessedPayments.length} unprocessed payments');
for (final payment in unprocessedPayments) {
try {
// PaymentManager должен работать с Payment (Drift)
// TODO: Возможно нужна конвертация в PaymentModel если PaymentManager еще не мигрирован
await _paymentManager.checkAndProcessPayment(payment);
} catch (e, s) {
print(e);
print(s);
}
}
}
@override
String get name => 'check_payment';
@override
int get intervalSeconds => 300;
}