payment redirect

This commit is contained in:
2025-12-01 10:34:40 +03:30
parent 016e2f24a9
commit d82c850fab
6 changed files with 206 additions and 8 deletions
+24 -7
View File
@@ -8,22 +8,22 @@ import { Food } from '../foods/entities/food.entity';
import { UserAddress } from '../users/entities/user-address.entity';
import { RestaurantPaymentMethod } from '../payments/entities/restaurant-payment-method.entity';
import { CartService } from '../cart/providers/cart.service';
import { CacheService } from '../utils/cache.service';
import { OrderStatus } from './interface/order-status';
import { PaymentStatus } from '../payments/interface/payment-status';
import { Cart } from '../cart/interfaces/cart.interface';
import { RestaurantPaymentMethodRepository } from '../payments/repositories/restaurant-payment-method.repository';
import { PaymentGatewayService } from '../payments/services/payment-gateway.service';
@Injectable()
export class OrdersService {
private readonly CART_KEY_PREFIX = 'cart';
constructor(
private readonly em: EntityManager,
private readonly cartService: CartService,
private readonly cacheService: CacheService,
private readonly RestaurantPaymentMethodRepository: RestaurantPaymentMethodRepository,
private readonly paymentGatewayService: PaymentGatewayService,
) {}
async create(userId: string, restaurantId: string): Promise<Order> {
async create(userId: string, restaurantId: string): Promise<Order | { order: Order; redirectUrl: string }> {
// Get cart
const cart = await this.cartService.findOne(userId, restaurantId);
@@ -77,8 +77,25 @@ export class OrdersService {
await em.flush();
// Clear cart from cache after successful order creation
const cacheKey = `${this.CART_KEY_PREFIX}:${userId}:${restaurantId}`;
await this.cacheService.del(cacheKey);
await this.cartService.clearCart(userId, restaurantId);
// Check if payment method is online and redirect to payment gateway
if (validationResult.paymentMethod.paymentMethod.isOnline) {
const paymentRedirect = await this.paymentGatewayService.generateRedirectUrl(
order,
validationResult.paymentMethod,
);
// Return order with redirect URL for online payment
return {
order,
redirectUrl: paymentRedirect.redirectUrl,
};
} else {
// For offline payment methods (cash on delivery, etc.), order is created and pending
// Payment status will be updated when payment is confirmed
// TODO: Implement offline payment confirmation flow if needed
}
return order;
});