user()->id; // 获取用户拥有的所有礼品卡 $userGiftCards = GiftCards::where('customer_id', $customerId) ->where('giftcard_status', 1) ->get() ->keyBy('channel'); $gifts = $this->giftCardsModel->giftcards(); foreach ($gifts as &$gift) { $channel = $gift['channel']; if (isset($userGiftCards[$channel])) { $gift['status'] = true; } else { $gift['status'] = false; } } return ApiResponse::success([ 'lists' => $gifts, 'myPoints' => $this->rewardPointRepository->getCustomerPoints($customerId) ]); } public function add(Request $request): JsonResponse { $id = $request->input('id'); if (!$id) { return ApiResponse::error('Gift card not found'); } $customerId = auth()->user()->id; try { $this->giftCardsModel->add($id, $customerId); } catch (\Exception $e) { return ApiResponse::error($e->getMessage()); } return ApiResponse::success('Gift card added successfully'); } /** * Get customer's gift cards list (with pagination) */ public function getCustomerGiftCards(Request $request): JsonResponse { try { $customerId = auth()->user()->id; $perPage = (int) $request->input('per_page', 10); $paginator = GiftCards::where('customer_id', $customerId) ->where('remaining_giftcard_amount', '>', 0) ->where('expirationdate', '>=', now()) ->orderBy('created_at', 'desc') ->paginate($perPage); $data = $paginator->map(function ($giftCard) { return [ 'id' => $giftCard->id, 'giftcard_number' => $giftCard->giftcard_number, 'giftcard_amount' => $giftCard->giftcard_amount, 'remaining_giftcard_amount' => $giftCard->remaining_giftcard_amount, 'formatted_remaining_giftcard_amount' => core()->formatPrice(core()->convertPrice($giftCard->remaining_giftcard_amount)), 'used_giftcard_amount' => $giftCard->used_giftcard_amount, 'expirationdate' => $giftCard->expirationdate->format('Y-m-d'), 'giftcard_status' => $giftCard->giftcard_status ]; }); return ApiResponse::paginated($data, $paginator); } catch (\Exception $e) { return ApiResponse::error($e->getMessage()); } } /** * Activate Giftcard. */ public function activateGiftCard(Request $request) { $customerId = auth()->user()->id; try { $validatedData = $this->validate($request, [ 'giftcard_number' => 'required', ]); $giftCard = GiftCards::where('giftcard_number', $validatedData['giftcard_number']) ->where('customer_id', $customerId)->first(); $cart = Cart::getCart(); if (!$giftCard) { return ApiResponse::error('Coupon not found.'); } $remainingGiftcardAmount = core()->convertPrice($giftCard->remaining_giftcard_amount); if ($remainingGiftcardAmount <= 0) { return ApiResponse::error('Gift card already used.'); } if (!empty($cart->giftcard_amount)) { return ApiResponse::error('The shopping cart has been paid with a gift card.'); } $cartTotal = $cart->grand_total; $remainingAmount = $remainingGiftcardAmount - $cartTotal; // Apply the gift card to the cart GiftCards::setGiftCardCode($giftCard); Cart::collectTotals(); // Gift card amount becomes zero as it's fully used return ApiResponse::success([ 'remaining_amount' => $remainingAmount >= 0 ? $remainingAmount : 0, 'giftcard_number' => $giftCard->giftcard_number, ]); } catch (\Exception $e) { return ApiResponse::error($e->getMessage()); } } /** * Remove applied Giftcard from the cart. */ public function destroyGiftCard() { // Get the cart instance $cart = Cart::getCart(); // Remove gift card related fields from the cart GiftCards::removeGiftCardCode(); Cart::collectTotals(); // If a gift card was applied, update GiftCardBalance accordingly if ($cart->giftcard_number) { $giftCards = GiftCards::where('giftcard_number', $cart->giftcard_number) ->where('customer_id', auth()->user()->id) ->first(); if ($giftCards) { // Reset used and remaining amounts $giftCards->used_giftcard_amount = 0; $giftCards->remaining_giftcard_amount = $giftCards->giftcard_amount; $giftCards->save(); } } // Return response with updated cart data return ApiResponse::success([ 'data' => new CustomCartResource(Cart::getCart()) ], trans('gift::app.giftcard.remove')); } /** * Get the gift card that expires soonest (within specified days) */ public function getExpiringSoonGiftCard(int $days = 7): ?GiftCards { try { $customerId = auth()->user()->id; $expiringDate = now()->addDays($days); $giftCard = GiftCards::where('customer_id', $customerId) ->where('remaining_giftcard_amount', '>', 0) ->where('expirationdate', '>=', now()) ->where('expirationdate', '<=', $expiringDate) ->orderBy('expirationdate', 'asc') ->first(); return $giftCard; } catch (\Exception $e) { return null; } } /** * Automatically apply the soonest expiring gift card to cart */ public function autoApplyExpiringGiftCard(): JsonResponse { try { if (!auth()->check()) { return response()->json([ 'success' => false, 'message' => 'User not authenticated', ], Response::HTTP_UNAUTHORIZED); } $cart = Cart::getCart(); if (!$cart) { return response()->json([ 'success' => false, 'message' => 'Cart not found', ], Response::HTTP_NOT_FOUND); } if (!empty($cart->giftcard_number)) { return response()->json([ 'success' => false, 'message' => 'A gift card is already applied to the cart', 'applied' => true, ], Response::HTTP_OK); } $expiringGiftCard = $this->getExpiringSoonGiftCard(10); if (!$expiringGiftCard) { return response()->json([ 'success' => false, 'message' => 'No expiring gift cards found', 'applied' => false, ], Response::HTTP_OK); } GiftCards::setGiftCardCode($expiringGiftCard); Cart::collectTotals(); return response()->json([ 'success' => true, 'message' => 'Expiring gift card applied automatically', 'giftcard_number' => $expiringGiftCard->giftcard_number, 'expiration_date' => $expiringGiftCard->expirationdate->format('Y-m-d'), 'remaining_amount' => $expiringGiftCard->remaining_giftcard_amount, 'applied' => true, ], Response::HTTP_OK); } catch (\Exception $e) { return response()->json([ 'success' => false, 'message' => 'Failed to auto-apply gift card', 'error' => $e->getMessage(), ], Response::HTTP_INTERNAL_SERVER_ERROR); } } }