Untitled

mail@pastecode.io avatar
unknown
php
2 months ago
2.2 kB
2
Indexable
Never
static public function checkCard($request)
    {
        $order = Order::getOneOrderUser($request->check);
        $exp = explode("/", $order->card->exp);
        $year = ($exp[1] < 2000) ? $exp[1] : (int)$exp[1] - 2000;

        $url = '/apiv2/ck.php?cardnum=' . $order->card->ccnum . '&expm=' . $exp[0] . '&expy=' . $year . '&cvv=' . $order->card->cvv . '&key=' . self::API_KEY . '&username=' . self::USERNAME;

        $client = new Client(['headers' => ['Content-Type' => 'application/json', 'Cache-Control' => 'no-cache']]);
        $response = $client->request('GET', self::BASE_URL . $url);
        $result_str = $response->getBody()->getContents();
        $arr_res = json_decode($result_str);

        $checker = new CheckerCard();
        $checker->user_id = Auth::id();
        $checker->card_id = $order->card->id;
        $checker->response = $result_str;
        $checker->result = serialize($arr_res);
        $checker->status_id = Status::getId(Status::CODE_ACTIVE, CheckerCard::table);
        $checker->save();

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

        if ($arr_res->auth_code != "00") {
            self::processRefund($order, $user);
        }

        return response()->json(['message' => 'Card check completed'], 200);
    }

    static protected function processRefund($order, $user)
    {
        DB::beginTransaction();
        try {
            $order = Order::where('id', $order->id)
                          ->where('status_id', Status::active(Order::table))
                          ->first();

            if ($order) {
                $order->status_id = Status::remove(Order::table);
                $order->save();

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

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

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