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

Ayomaid08's avatar

How to Use Webhook Url and save data in Database with Paystack

I am using https://github.com/iamraphson/vue-paystack in my laravel+vue SPA project. I can receive payments perfectly but now need to hit a webhook url so that I can update database and give value to user. I have set up the route and added the url on my paystack dashboard. What should I do next? How do I get the charge events and other details in my controller method and send to database? Here's Paystack's documentation largely done in php: https://developers.paystack.co/v2.0/docs/events

I have tried the following in my controller:

public function payme(Request $request){
     //This receives the webhook

//$email = json_decode($input['data']['customer']['email']);
$email = $request->input('data.customer.email');
$bankname = $request->input('data.gateway_response');
  //$bankname = json_decode($input['data']['gateway_response']);
$user = User::where('email', $email);

$user->bankname = $bankname;
$user->save();

return response()->json('processed', 200);
0 likes
8 replies
Ayomaid08's avatar

I did Nakov.

protected $except = [

'payme'
];

}

Please check my controller code. Any errors? Am I decoding properly? According to Paystack, they'll send data like so:

    {  

"event":"charge.success", "data":{ "id":302961, "domain":"live", "status":"success", "reference":"qTPrJoy9Bx", "amount":10000, "message":null, "gateway_response":"Approved by Financial Institution", "paid_at":"2016-09-30T21:10:19.000Z", "created_at":"2016-09-30T21:09:56.000Z", "channel":"card", "currency":"NGN", "ip_address":"41.242.49.37", "metadata":0, "log":{ "time_spent":16, "attempts":1, "authentication":"pin", "errors":0, "success":false, "mobile":false, "input":[

     ],
     "channel":null,
     "history":[  
        {  
           "type":"input",
           "message":"Filled these fields: card number, card expiry, card cvv",
           "time":15
        },
        {  
           "type":"action",
           "message":"Attempted to pay",
           "time":15
        },
        {  
           "type":"auth",
           "message":"Authentication Required: pin",
           "time":16
        }
     ]
  },
  "fees":null,
  "customer":{  
     "id":68324,
     "first_name":"BoJack",
     "last_name":"Horseman",
     "email":"[email protected]",
     "customer_code":"CUS_qo38as2hpsgk2r0",
     "phone":null,
     "metadata":null,
     "risk_action":"default"
  },
  "authorization":{  
     "authorization_code":"AUTH_f5rnfq9p",
     "bin":"539999",
     "last4":"8877",
     "exp_month":"08",
     "exp_year":"2020",
     "card_type":"mastercard DEBIT",
     "bank":"Guaranty Trust Bank",
     "country_code":"NG",
     "brand":"mastercard"
  },
  "plan":{}

} }

Nakov's avatar

@ayomaid08 did you logged the result that you are receiving to make sure that the endpoint is hit?

You can try this:

$user = User::where('email', $email)->firstOrFail();
$user->bankname = $bankname;
$user->save();

if $email exists..

Otherwise when you leave it to where the result is a Query Builder object. So now this will either find the user with the given email, or will throw an exception.

mspace's avatar

I am facing same issue. How can I log the result?

Ayomaid08's avatar
Ayomaid08
OP
Best Answer
Level 1

This worked:

        public function payme(Request $request){
         //This receives the webhook
    //amount //email prevent multiple pending payments
    $email = $request->input('data.customer.email');
    $amountpaid = ($request->input('data.amount'))/100;

    $user_id = User::where('email', $email)->firstOrFail()->id;
    $user = User::find($user_id);

Please or to participate in this conversation.