php - Sending data from Wordpress Contact form 7 to Laravel via Webhooks

one text

I am very new at APIs and stuff. I would appreciate it if anyone can help me to understand or guide me on how to accomplish the following.

I have a contact form in Wordpress with the Contact-form 7 plugin. It is a basic contact form to capture enquiries with just 3 fields i.e. Name, Contact Number & Type of Enquiry. The data is sent to Laravel Application, and saves in Lead Management where the followups are taken care of.

I referred to some tutorials online to accomplish the following.

  • I am using wp-webhooks wordpress plugin to send data to my Laravel API.
  • I have created the necessary API routes and AuthController in my Laravel Application.
  • Created SalesController to handle the store method.

My issue is, I am not able to get the data in my Laravel application. While testing in postman, the api works when I manually insert the type as bearer and set the token. Or when the application is logged-in in the same browser.

I dont know how to authenticate the user.

My API Routes

//API route for register new user
Route::post('/register', [App\Http\Controllers\API\AuthController::class, 'register']);
//API route for login user
Route::post('/login', [App\Http\Controllers\API\AuthController::class, 'login']);


Route::group(['middleware' => ['auth:sanctum']], function() {
    Route::get('/profile', function(Request $request) {
    return auth()->user();
    });

// API route for Cf7 to system
Route::resource('externalsales', App\Http\Controllers\API\SalesController::class);


// API route for logout user
Route::post('/logout', [App\Http\Controllers\API\AuthController::class, 'logout']);
});

My AuthController

class AuthController extends Controller
{
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:8'
        ]);

        if($validator->fails()){
            return response()->json($validator->errors());       
        }

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password)
         ]);

        $token = $user->createToken('auth_token')->plainTextToken;

        return response()
            ->json(['data' => $user,'access_token' => $token, 'token_type' => 'Bearer', ]);
    }

    public function login(Request $request)
    {
        if (!Auth::attempt($request->only('email', 'password')))
        {
            return response()
                ->json(['message' => 'Unauthorized'], 401);
        }

        $user = User::where('email', $request['email'])->firstOrFail();

        $token = $user->createToken('auth_token')->plainTextToken;

        return response()
            ->json(['message' => 'Hi '.$user->name.', welcome to home','access_token' => $token, 'token_type' => 'Bearer', ]);
    }

    // method for user logout and delete token
    public function logout()
    {
        auth()->user()->tokens()->delete();

        return [
            'message' => 'You have successfully logged out and the token was successfully deleted'
        ];
    }
}

My SalesController

public function index()
{
    $data = SalesManagement::latest()->get();
    return response()->json([SalesResource::collection($data), 'Leads fetched.']);
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $validator = Validator::make($request->all(),[
        'fullname' => 'required|string|max:255',
        'mobile' => 'required',
        'description' => 'required'

    ]);

    if($validator->fails()){
        return response()->json($validator->errors());       
    }

    $sales = SalesManagement::create([
        'fullname' => $request->fullname,
        'mobile' => $request->mobile,
        'description' => $request->description,
     ]);
    $token = $request->bearerToken();

    
    return response()->json(['Lead created successfully.','access_token' => $token, 'token_type' => 'Bearer', new SalesResource($sales)]);
}

Source