unicodeveloper / laravel-paystack

:credit_card: :package: :moneybag: Laravel 6, 7, 8, 9, 10 and 11 Package for Paystack
https://paystack.co
MIT License
597 stars 310 forks source link

How to implement webhook, some transactions do not redirect to the laravel app #87

Closed samsperd closed 4 years ago

samsperd commented 4 years ago

Hello, Some of my client's transactions do not redirect to the laravel application. I don't know why this is happening because i followed the docs. But i was also told to include a webhook on the app, so that i can get a response when a transaction is valid. I need help integrating this. Thank you

Bazzly commented 4 years ago

I thin this can help if you show a screenshot or explain more about what you really need. but before that check https://developers.paystack.co/docs/paystack-standard

samsperd commented 4 years ago

@Bazzly Actually there is nothing to screenshot that's the point. Some payment don't go through and i read that the webhook was supposed to notify me if the callback url didn't show that the payment was a "success", if it was successful. So I'm here to know how i can implement the webhook for this api. I have tried using their raw php code to implement the webhook, but it didn't work. Have you got any sample?

Bazzly commented 4 years ago

let me explain in your .env file PAYSTACK_PUBLIC_KEY=** PAYSTACK_SECRET_KEY=** PAYSTACK_PAYMENT_URL=https://api.paystack.co MERCHANT_EMAIL=@gmail.com

PAYSTACK_PAYMENT_URL should not be changed for any reason then Go to routes/web.php

and create two route Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay'); Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');

I assume you know you have to create PaymentController

so in your payment controller just create two method \\\\\\\\\\\\\\\\\\\\\\\ public function redirectToGateway() { return Paystack::getAuthorizationUrl()->redirectNow(); }

public function handleGatewayCallback(Request $request) { $paymentDetails = Paystack::getPaymentData(); dd($paymentDetails); } \\\\\\\\\\\\\\\\\\\\\\\

the problem I notice is that callback URL is case sensitive I fight with it for over 5days without me knowing I am using /payment/Callback with capital C instead of small letter c

login to your paystack account(https://dashboard.paystack.com/#/settings/developer) and just copy http://example.com/payment/callback to your Test Callback URL (here I assume you are using test callback Url not live)

an example might be your localhost:8080/payment/callback or any domain name you are using { i hope you understand } when paystack is redirected it will only be redirected to the same route registered in your web route only which is payment/callback

Bazzly commented 4 years ago

if you did not get it with my explanation let me know how I can help again

odutolaabisoye commented 4 years ago

In your payment controller

public function redirectToGateway(Request $request)
{
  $payant = new Paystack();
  $user = Auth::user();
  return Payant::getAuthorizationUrl()->redirectNow();
}

public function handleGatewayCallback(Request $request)
{
    $paymentDetails = Paystack::getPaymentData();
   // $paymentDetails = array_collapse;
 $user = User::find(Auth::id());
 $user->sub_paid_at = date("Y-m-d H:i:s",strtotime($paymentDetails['data']['paidAt']));
 $user->plan= $paymentDetails['data']['plan_object']['name'];
 $user->save();
 $url = ($request->session()->get('previousurl'));
 return redirect($url);
}

In your web.php Route::get('/subscription-plan', 'PaymentController@index')->name('subscription-plan')->middleware('auth'); Route::post('/subscription-plan', 'PaymentController@redirectToGateway')->name('subscription-plan')->middleware('auth'); Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');

Goto https://dashboard.paystack.com/#/settings/developer In CallbackURL --> enter your callback URL e.g https://example.com/payment/callback

If you're using app.yaml PAYSTACK_PUBLIC_KEY: ################ PAYSTACK_SECRET_KEY: ################ PAYSTACK_PAYMENT_URL: https://api.paystack.co MERCHANT_EMAIL: ############@gmail.com

If you're using heroku Goto https://dashboard.heroku.com/apps/libri-web/settings -> Config Vars, enter your paystack details

samsperd commented 4 years ago

@Bazzly @odutolaabisoye okay am I the only one who sees the Live webhook url there?.... And also you are telling me that all payments go through for you?

Bazzly commented 4 years ago

Use Test Callback URL first for the payment not webhook URL for my example @samsperd

samsperd commented 4 years ago

@Bazzly I am using it currently. I'm just saying it don't always get all the transactions. Some of them don't send back to the website as a completed payment through the callback URL. When I asked paystack, they said I should implement webhook it will notify me of any confirmed payment. But they have raw php there and I use this api, it doesn't fit in. So that's why I'm here

akinkunminexgen commented 4 years ago

@samsperd you can still implement it with the raw php. just create a route Route::post('/payment/webhook', 'PaymentController@handleGatewayWebhook')->name('webhook');

and ensure you put the url in your paystack dashboard webhook url. Go to your controller and create the function in this way

public function handleGatewayWebhook()
 {
 $input = @file_get_contents("php://input");
 $PAYSTACK = config('paystack.secretKey');

 if($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, $PAYSTACK))
   exit();
 http_response_code(200);
 $event = json_decode($input);
switch ($event->event) {
     case 'charge.success':
//your logic
break;
}
exit();
}
miketayo1 commented 3 years ago

I noticed it only redirected when the payment is successful and doesn't when the card is declined

kelvin-j432 commented 3 years ago

did somebody know how i can intergrate paystack in bagisto?

edwinlongjohn commented 3 months ago

you can try the solution from this video https://www.youtube.com/watch?v=x4bhbj3wy_w&t=15s