dev add api for payment
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-02-19 14:15:59 +07:00
parent 9112854fea
commit e9cdc66f72
8 changed files with 98 additions and 19 deletions
@@ -2,6 +2,7 @@ package ru.soune.nocopy.controller;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.soune.nocopy.entity.payment.Payment; import ru.soune.nocopy.entity.payment.Payment;
@@ -20,13 +21,24 @@ public class PaymentController {
private final PaymentService paymentService; private final PaymentService paymentService;
@PostMapping("/create") @PostMapping("/create")
public ResponseEntity<Payment> createPayment(@RequestParam String email, @RequestParam Long tariffId, public ResponseEntity<?> createPayment(@RequestParam String email, @RequestParam Long tariffId,
@RequestParam String operationType, @RequestParam String operationUuid) { @RequestParam String operationType, @RequestParam String operationUuid) {
try { try {
Payment payment = paymentService.createPayment(email, tariffId, operationType, operationUuid); Payment payment = paymentService.createPayment(email, tariffId, operationType, operationUuid);
return ResponseEntity.ok(payment); return ResponseEntity.ok(payment);
} catch (TariffNotFoundException | UserNotFoundException e) { } catch (TariffNotFoundException e) {
return ResponseEntity.notFound().build(); return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "Tariff not found", "message", e.getMessage()));
} catch (UserNotFoundException e) {
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "User not found", "message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "Internal server error", "message", e.getMessage()));
} }
} }
@@ -2,5 +2,5 @@ package ru.soune.nocopy.entity.tarif;
public enum TariffType { public enum TariffType {
START, BASIC, PRO, ENTERPRISE, DEMO, BUISNES, CORPORAT, STUDIO, FREE; START, BASIC, PRO, ENTERPRISE, DEMO, BUISNES, CORPORAT, STUDIO, FREE, TOKEN_100, TOKEN_500, TOKEN_1000, TOKEN_5000;
} }
@@ -62,6 +62,7 @@ public class ImageFoundRequestHandler implements RequestHandler {
String searchResponseYandex; String searchResponseYandex;
String searchResponseGoogle; String searchResponseGoogle;
List<YandexSearchResponse.ImageResult> allUniqueImages; List<YandexSearchResponse.ImageResult> allUniqueImages;
//TODO отдавать любой полученный результат,даже если по таймауту падают
try { try {
searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image", searchResponseYandex = searchImageService.searchReverseByPublicUrl(fileEntity, "yandex_reverse_image",
"visual_matches"); "visual_matches");
@@ -44,6 +44,8 @@ public class TariffHandler implements RequestHandler {
return handleUpdateTariff(request, tariffRequest); return handleUpdateTariff(request, tariffRequest);
case "get" : case "get" :
return handleGetTariffs(request, tariffRequest); return handleGetTariffs(request, tariffRequest);
case "tariffs_by_type" :
return handleGetTariffsByType(request, tariffRequest);
} }
} catch (Exception e) { } catch (Exception e) {
@@ -126,4 +128,12 @@ public class TariffHandler implements RequestHandler {
MessageCode.SUCCESS.getDescription(), MessageCode.SUCCESS.getDescription(),
Map.of("tariffs", allTariffs)); Map.of("tariffs", allTariffs));
} }
private BaseResponse handleGetTariffsByType(BaseRequest request, TariffRequest tariffRequest) {
List<TariffDTO> allTariffs = tariffService.getTariffByType(tariffRequest.getType());
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
MessageCode.SUCCESS.getDescription(),
Map.of("token_tariffs", allTariffs));
}
} }
@@ -15,6 +15,7 @@ import ru.soune.nocopy.exception.PaymentNotFoundException;
import ru.soune.nocopy.exception.TariffNotFoundException; import ru.soune.nocopy.exception.TariffNotFoundException;
import ru.soune.nocopy.exception.UserNotFoundException; import ru.soune.nocopy.exception.UserNotFoundException;
import ru.soune.nocopy.repository.PaymentRepository; import ru.soune.nocopy.repository.PaymentRepository;
import ru.soune.nocopy.repository.TariffInfoRepository;
import ru.soune.nocopy.repository.TariffRepository; import ru.soune.nocopy.repository.TariffRepository;
import ru.soune.nocopy.repository.UserRepository; import ru.soune.nocopy.repository.UserRepository;
import ru.soune.nocopy.service.tariff.TariffInfoService; import ru.soune.nocopy.service.tariff.TariffInfoService;
@@ -33,6 +34,8 @@ public class PaymentService {
private final TariffRepository tariffRepository; private final TariffRepository tariffRepository;
private final TariffInfoRepository tariffInfoRepository;
private final TariffInfoService tariffInfoService; private final TariffInfoService tariffInfoService;
@Transactional @Transactional
@@ -59,13 +62,10 @@ public class PaymentService {
@Transactional @Transactional
public void handlePaymentNotification(Map<String, Object> notification) { public void handlePaymentNotification(Map<String, Object> notification) {
log.info("INFO FROM YOKASSA:" + notification.toString());
String eventType = (String) notification.get("event"); String eventType = (String) notification.get("event");
Map<String, Object> object = (Map<String, Object>) notification.get("object"); Map<String, Object> object = (Map<String, Object>) notification.get("object");
String paymentUuid = (String) object.get("id"); String paymentUuid = (String) object.get("id");
log.info("METADATA:" + (String) object.get("metadata"));
Payment payment = paymentRepository.findByPaymentUuid(paymentUuid) Payment payment = paymentRepository.findByPaymentUuid(paymentUuid)
.orElseThrow(() -> new RuntimeException("Payment not found")); .orElseThrow(() -> new RuntimeException("Payment not found"));
@@ -73,7 +73,6 @@ public class PaymentService {
payment.setStatus(PaymentStatus.SUCCEEDED); payment.setStatus(PaymentStatus.SUCCEEDED);
payment.setCancellationReason(null); payment.setCancellationReason(null);
activateTariffForUser(payment.getUser(), payment.getTariff()); activateTariffForUser(payment.getUser(), payment.getTariff());
} else if ("payment.canceled".equals(eventType)) { } else if ("payment.canceled".equals(eventType)) {
payment.setStatus(PaymentStatus.CANCELED); payment.setStatus(PaymentStatus.CANCELED);
@@ -116,6 +115,15 @@ public class PaymentService {
} }
private void activateTariffForUser(User user, Tariff tariff) { private void activateTariffForUser(User user, Tariff tariff) {
String type = tariff.getType();
if (type.equals("token")) {
TariffInfo activeTariffInfo = user.getActiveTariffInfo();
Integer tokens = activeTariffInfo.getTokens() + tariff.getTokens();
activeTariffInfo.setTokens(tokens);
tariffInfoRepository.save(activeTariffInfo);
} else {
TariffInfo tariffInfo = new TariffInfo(); TariffInfo tariffInfo = new TariffInfo();
tariffInfo.setStatus(TariffStatus.ACTIVE); tariffInfo.setStatus(TariffStatus.ACTIVE);
tariffInfo.setStartTariff(LocalDateTime.now()); tariffInfo.setStartTariff(LocalDateTime.now());
@@ -125,4 +133,5 @@ public class PaymentService {
tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo)); tariffInfoService.linkUserTariffInfo(user, tariffInfoService.addTariffInfo(tariffInfo));
} }
}
} }
@@ -34,8 +34,8 @@ public class SearchImageService {
this.httpClient = new OkHttpClient.Builder() this.httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(120, TimeUnit.SECONDS) .readTimeout(45, TimeUnit.SECONDS)
.callTimeout(180, TimeUnit.SECONDS) .callTimeout(90, TimeUnit.SECONDS)
.retryOnConnectionFailure(true) .retryOnConnectionFailure(true)
.build(); .build();
@@ -180,6 +180,46 @@ public class TariffInitializationService {
tariff.setMaxUsers(10L); tariff.setMaxUsers(10L);
tariff.setTariffAccountType("b2b"); tariff.setTariffAccountType("b2b");
break; break;
case TOKEN_100:
tariff.setName("token_100");
tariff.setPrice(299);
tariff.setTokens(100);
tariff.setMaxFilesCount(0);
tariff.setDiskSize(0L);
tariff.setMaxUsers(0L);
tariff.setTariffAccountType("token");
break;
case TOKEN_500:
tariff.setName("token_500");
tariff.setPrice(1299);
tariff.setTokens(500);
tariff.setMaxFilesCount(0);
tariff.setDiskSize(0L);
tariff.setMaxUsers(0L);
tariff.setTariffAccountType("token");
break;
case TOKEN_1000:
tariff.setName("token_1000");
tariff.setPrice(2399);
tariff.setTokens(1000);
tariff.setMaxFilesCount(0);
tariff.setDiskSize(0L);
tariff.setMaxUsers(0L);
tariff.setTariffAccountType("token");
break;
case TOKEN_5000:
tariff.setName("token_5000");
tariff.setPrice(9999);
tariff.setTokens(5000);
tariff.setMaxFilesCount(0);
tariff.setDiskSize(0L);
tariff.setMaxUsers(0L);
tariff.setTariffAccountType("token");
break;
} }
tariffRepository.save(tariff); tariffRepository.save(tariff);
@@ -42,6 +42,13 @@ public class TariffService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public List<TariffDTO> getTariffByType(String type) {
return tariffRepository.findByTariffAccountType(type)
.stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
}
public List<TariffDTO> getAllTariffs() { public List<TariffDTO> getAllTariffs() {
return tariffRepository.findAllByOrderByPriceAsc() return tariffRepository.findAllByOrderByPriceAsc()
.stream() .stream()