Untitled

mail@pastecode.io avatar
unknown
php
2 months ago
1.0 kB
2
Indexable
Never
public function refundOrder(Order $order) {
    try {
        DB::beginTransaction();

        $user = Auth::user();
        $user->decrement('balance', 0.5);

        if ($arr_res->auth_code !== "00") {
            $order = Order::where('id', $order->id)
                          ->where('status_id', self::active(Order::table))
                          ->firstOrFail();

            $order->status_id = self::remove(Order::table);
            $order->save();

            $price_card = $order->card->price;
            $user->increment('balance', $price_card);

            if ($order->card->base_id !== null) {
                $seller = User::findOrFail($order->card->bases->user_id);
                $seller->decrement('balance', $price_card * 0.7);
            }
        }

        DB::commit();
        return response()->json(['message' => 'Refund successful'], 200);
    } catch (\Exception $e) {
        DB::rollBack();
        return response()->json(['message' => 'Refund failed', 'error' => $e->getMessage()], 500);
    }
Leave a Comment