fix: add payment features #52

Merged
TakahashiNguyen merged 13 commits from add-payment into main 2026-05-12 05:22:10 +00:00
4 changed files with 53 additions and 15 deletions
Showing only changes of commit a0ae43f1ff - Show all commits
@@ -1,7 +1,6 @@
package com.drinkool.controllers;
import com.drinkool.Role;
import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
@@ -15,7 +14,7 @@ import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
public interface ManagerController {
@GET
@Path(Role.Manager + "/{id}/payment-qr")
public Uni<Response> getManagerPaymentQr(
public Response getManagerPaymentQr(
@PathParam("id") UUID id,
@QueryParam("amount") Long amount,
@QueryParam("cartId") String cartId
@@ -15,4 +15,6 @@ public class Cart {
public UUID userId;
public UUID eateryId;
public List<CartItem> items;
public long totalAmount;
public String paymentQrUrl;
}
@@ -11,4 +11,7 @@ public class CartItem {
public String productId;
public Integer quantity;
public Double priceAtTimeOfAdding;
public Double subTotal;
}
@@ -2,6 +2,7 @@ package com.drinkool.services;
import com.drinkool.InternalValue;
import com.drinkool.controllers.EateryController;
import com.drinkool.controllers.ManagerController;
import com.drinkool.dtos.Cart;
import com.drinkool.dtos.CartItem;
import io.quarkus.redis.datasource.RedisDataSource;
@@ -35,6 +36,10 @@ public class CartService {
@RestClient
EateryController eateryController;
@Inject
@RestClient
ManagerController managerController;
private final HashCommands<String, String, String> hashCommands;
private final ValueCommands<String, String> valueCommands;
@@ -67,7 +72,7 @@ public class CartService {
userId != null ? userId : "GUEST",
"_metadata_eatery",
eateryId.toString(),
"_metadata_ownerId",
"_metadata_eateryManagerId",
ownerId
)
);
@@ -90,27 +95,56 @@ public class CartService {
@Query("getCart")
public Cart getCart(@Name("cartId") UUID cartId) throws GraphQLException {
String key = "cart:" + cartId;
String priceKey = "cart_prices:" + cartId;
Map<String, String> data = hashCommands.hgetall(key);
Map<String, String> itemPrices = hashCommands.hgetall(priceKey);
String managerId = hashCommands.hget(key, "_metadata_eateryManagerId");
if (data.isEmpty()) {
throw new GraphQLException("NotFoundCart");
}
List<CartItem> items = new ArrayList<>();
Cart cart = new Cart();
cart.Id = cartId;
cart.eateryId = UUID.fromString(data.get("_metadata_eatery"));
cart.userId = UUID.fromString(data.get("_metadata_owner"));
cart.items = new ArrayList<>();
data.forEach((k, v) -> {
if (!k.startsWith("_")) {
items.add(new CartItem(k, Integer.parseInt(v)));
long total = 0;
for (Map.Entry<String, String> entry : data.entrySet()) {
String field = entry.getKey();
if (field.startsWith("_metadata_")) continue;
Integer qty = Integer.parseInt(entry.getValue());
Double price = Double.parseDouble(itemPrices.getOrDefault(field, "0"));
Double subTotal = qty * price;
CartItem item = new CartItem();
item.productId = field;
item.quantity = qty;
item.priceAtTimeOfAdding = price;
item.subTotal = subTotal;
cart.items.add(item);
total += subTotal;
}
});
cart.totalAmount = total;
return new Cart(
cartId,
UUID.fromString(data.get("_metadata_owner")),
UUID.fromString(data.get("_metadata_eatery")),
items
try {
Response response = managerController.getManagerPaymentQr(
UUID.fromString(managerId),
total,
cartId.toString()
);
cart.paymentQrUrl = response.readEntity(String.class);
} catch (Exception e) {
cart.paymentQrUrl = "";
}
return cart;
}
@Mutation("addItem")