Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kazzuya's avatar

Update field

Hello, I'm trying to update a field ('opened') after a success payment

public function return(Request $request): string
{
    $validSignature = $this->validateSignature($request->all());

    if ($validSignature) {
        if ($request->respStatus == 'A') {
            $transaction = Transaction::where('paytabs_transaction_reference', $request->tranRef)->first();
            $transaction->paid = true;
            $transaction->save();
            
            DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->update(['opened' => 1]);

            return 'Paid Message ' . $request->respMessage;
        }
        return 'Unpaid Message ' . $request->respMessage;
    } else {
        return 'Invalid  Transaction Signature';
    }
}

But it doesn't update , Any help please ?

0 likes
30 replies
Sinnbeck's avatar

@kazzuya Ok then check if this finds a record

$test = DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->first();
dd($test);
Sinnbeck's avatar

@kazzuya Ok just below setting $validSignature try this and post the result

dd($validSignature, $request->respStatus);
kazzuya's avatar

@Sinnbeck Still nothing happend

    $validSignature = $this->validateSignature($request->all());
    dd($validSignature, $request->respStatus);

It's correct right ?

Sinnbeck's avatar

@kazzuya If nothing happens, you are never hitting that method. Can you show the route and form?

kazzuya's avatar

@Sinnbeck That's the route

Route::post('return', [PaymentController::class, 'return'])->name('return');

The form is the paytabs payment form not in blade

Sinnbeck's avatar

@kazzuya You are calling the index method but the method is named return (which is a bad name as return is a reserved keyword). I am also confused why the url is /index?

A suggestion for better naming

Route::post('payment', [PaymentController::class, 'store'])->name('payment.store');

And controller

public function store(Request $request): string
{
kazzuya's avatar

@Sinnbeck a question please, I need to make a (VerifyCsrfToken ) in Providers,

and here

protected $except = [
    'stripe/*',
    'http://example.com/foo/bar',
    'http://example.com/foo/*',
];

It should be the url of my routes ? That belongs to paytabs

Sinnbeck's avatar

@kazzuya Yeah. So just return/*

Maybe pick another name for the route that is easier to remember :) paytabs/* perhaps?

Sinnbeck's avatar

@kazzuya And you followed all of my suggestions or just the csrf one? What happens when you submit that form? Does it get this return message 'Paid Message ' . $request->respMessage;?

kazzuya's avatar

@Sinnbeck yes i created the VerifyCsrfToken in providers and a hidden input for csrf_token

kazzuya's avatar

@Sinnbeck I don't know if this makes a problem

The return redirect($response['redirect_url']); It takes me directly to LogIn Page after the success payment While it should takes me to the home page..

public function index(Request $request): RedirectResponse
{
    $response = $this->request(
        url: 'https://secure-global.paytabs.com/payment/request',
        payload: $this->transactionPayload(
            amount: 0
        )
    );

    Transaction::create([
        'paytabs_transaction_reference' => $response->json()['tran_ref'] ?? null
    ]);

    Cart::where('opened', '=', 0)->where('user_id', Auth::id())->update(['paytabs_transaction_reference' => $response->json()['tran_ref']]);

    return redirect($response['redirect_url']);
}

public function return(Request $request): string
{
    $validSignature = $this->validateSignature($request->all());

    if ($validSignature) {
        if ($request->respStatus == 'A') {
            $transaction = Transaction::where('paytabs_transaction_reference', $request->tranRef)->first();
            $transaction->paid = true;
            $transaction->save();
            
            DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->update(['opened' => 1]);

            return 'Paid Message ' . $request->respMessage;
        }
        return 'Unpaid Message ' . $request->respMessage;
    } else {
        return 'Invalid  Transaction Signature';
    }
}
Sinnbeck's avatar

@kazzuya What is $response['redirect_url']? dd($response['redirect_url']); Is it an external url?

kazzuya's avatar

@Sinnbeck done change it, But still not updating the (opened) field and after the success payment it logged out the user account , sorry..

Sinnbeck's avatar

@kazzuya What happens after you submit their form? Any chance that they expect it to be a GET request (a redirect) and not a post request?

kazzuya's avatar

@Sinnbeck after the submit it updates the null value and loged out the user account

Sinnbeck's avatar

@kazzuya Instead of dd() you can try using Log::info() to write debuggin to the log file.

kazzuya's avatar

@Sinnbeck hey sir.. I change the payment route from inside middleware(['verified']); to outside it And now i got the message and i tried this

$test = DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->first();

dd($test);

It gives null, what i can do now ?

kazzuya's avatar

@Sinnbeck It's also null , I think because after clicking on pay it directly loged me out but i don't know why

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@kazzuya Sounds like it. Maybe you are losing the session. You can try adding something to the session in the index method and see if it is gone in the return method

kazzuya's avatar

@Sinnbeck it works Thank you so much

i have change this 'same_site' => 'lax', to this 'same_site' => null,

Please or to participate in this conversation.