Home > Blockchain >  How could I generate an order confirmation link?
How could I generate an order confirmation link?

Time:08-06

This is my OrderController.php which is responsible for sending an e-mail with their order details to whoever placed an order.

<?php

namespace App\Http\Controllers;

use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class OrderController extends Controller
{
    public function submitOrder(Request $request)
    {
        $credentials = $request->validate([
            'email' => ['required', 'string', 'email'],
            'name' => ['required', 'string', 'max:255'],
            'order' => ['required'],
            'total' => ['required', 'numeric'],
            'status_id' => ['required'],
        ]);

        $order = Order::create($credentials);

        Mail::to($credentials['email'])->send(new \App\Mail\sendOrderConfirmation($order));

        return response([
            'msg' => 'Order submitted successfully',
            'order' => $order,
        ]);
    }
}

I don't have any user accounts. I'd like to generate a URL in the e-mail so once the user clicks it, it sets the status_id of their order to confirmed. I tried to look at how Laravel does user registration e-mail confirmations but no luck.

Any help appreciated.

CodePudding user response:

Firstly I'll advice that you create a uuid and add to your order details since user doesn't have an account This is how I do it.

function create_uuid()
{
    $nodeProvider = new RandomNodeProvider();
    $clockSequence = 16383;
    return Uuid::uuid1($nodeProvider->getNode(), $clockSequence);
}

Remember to include these as well

use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Uuid;

Then use this to create a unique url which contains the user email, the uuid token and probably an expiration time (may not be necessary)

public function createVerificationUrl($email, $order_id)
{
    $dataToken = ['email' => $email, 'id' => $order_id, 'token' => $this->create_uuid()];
    $crypted = Crypt::encrypt($dataToken);
    $url = env('APP_URL') . '/verify-user/' . $type . '?verify=' . $crypted;
    return $url;
}

Next thing is to verify the token sent to the email when you receive it

public function verifyToken($verifyToken)
{
    $decrypt = Crypt::decrypt($verifyToken);
    $verify = Order::where($decrypt);
    if ($verify->exists()) {
        $verify->update(['status_id' => 'whatever_id'])
    }
}

With some tweaking this should do the magic!

  • Related