forked from eatvio/eatvio_app
102 lines
3.6 KiB
Dart
102 lines
3.6 KiB
Dart
import 'package:dio/dio.dart';
|
||
import 'package:eatvio_app/core/error_reporting/api_response_error_reporter.dart';
|
||
import 'package:eatvio_app/features/chat/models/payment_model.dart';
|
||
import 'package:eatvio_app/features/chat/services/chat_service.dart';
|
||
import 'package:eatvio_app/features/payment/models/promo_applied_list_response.dart';
|
||
import 'package:eatvio_app/features/payment/models/promo_apply_response.dart';
|
||
import 'package:eatvio_app/features/payment/models/promo_code_model.dart';
|
||
import 'package:eatvio_app/features/payment/models/promo_remove_response.dart';
|
||
import 'package:eatvio_app/features/payment/models/promo_validate_response.dart';
|
||
|
||
/// Репозиторий оплаты консультаций (chosen_plan) через Alfa Bank / СБП.
|
||
class PaymentRepository {
|
||
final ChatService chatService;
|
||
final ApiResponseErrorReporter? responseErrors;
|
||
|
||
PaymentRepository({required this.chatService, this.responseErrors});
|
||
|
||
/// Запускает процесс оплаты: POST `/api/chosen_plans/{id}/start_payment_process`
|
||
/// с `payment_provider: alfa-bank`.
|
||
Future<PaymentModel> startPaymentProcess({required int planId}) async {
|
||
return chatService.getPaymentLink(
|
||
id: planId,
|
||
body: const {'payment_provider': 'alfa-bank'},
|
||
);
|
||
}
|
||
|
||
/// Список applied-промокодов текущего пользователя.
|
||
Future<PromoAppliedListResponse> listAppliedPromoCodes() {
|
||
return chatService.listAppliedPromoCodes();
|
||
}
|
||
|
||
/// Applied-промокод для конкретного chosen_plan, либо null (нет / ошибка сети).
|
||
Future<PromoCodeModel?> findAppliedPromoForPlan(int planId) async {
|
||
try {
|
||
final response = await listAppliedPromoCodes();
|
||
for (final promo in response.promoCodes) {
|
||
if (promo.chosenPlanId == planId && promo.state == 'applied') {
|
||
return promo;
|
||
}
|
||
}
|
||
return null;
|
||
} catch (_) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// Preview-валидация промокода без применения.
|
||
Future<PromoValidateResponse> validatePromoCode({
|
||
required int planId,
|
||
required String promoCode,
|
||
}) {
|
||
return chatService.validatePromoCode(
|
||
id: planId,
|
||
body: {'promo_code': promoCode},
|
||
);
|
||
}
|
||
|
||
/// Применяет промокод к chosen_plan. При HTTP 422 возвращает тело ответа.
|
||
Future<PromoApplyResponse> applyPromoCode({
|
||
required int planId,
|
||
required String promoCode,
|
||
}) async {
|
||
try {
|
||
return await chatService.applyPromoCode(
|
||
id: planId,
|
||
body: {'promo_code': promoCode},
|
||
);
|
||
} on DioException catch (e) {
|
||
final response = e.response;
|
||
final data = response?.data;
|
||
if (response != null && data is Map<String, dynamic>) {
|
||
return ApiResponseErrorReporter.decode(
|
||
responseErrors,
|
||
response,
|
||
'/api/chosen_plans/{id}/apply_promo',
|
||
() => PromoApplyResponse.fromJson(data),
|
||
);
|
||
}
|
||
rethrow;
|
||
}
|
||
}
|
||
|
||
/// Удаляет применённый промокод (idempotent).
|
||
Future<PromoRemoveResponse> removePromoCode({required int planId}) async {
|
||
try {
|
||
return await chatService.removePromoCode(id: planId);
|
||
} on DioException catch (e) {
|
||
final response = e.response;
|
||
final data = response?.data;
|
||
if (response != null && data is Map<String, dynamic>) {
|
||
return ApiResponseErrorReporter.decode(
|
||
responseErrors,
|
||
response,
|
||
'/api/chosen_plans/{id}/remove_promo_code',
|
||
() => PromoRemoveResponse.fromJson(data),
|
||
);
|
||
}
|
||
rethrow;
|
||
}
|
||
}
|
||
}
|