Created
June 16, 2021 08:58
-
-
Save joekenpat/e5b818927984e09a8aac6c03043d28fd to your computer and use it in GitHub Desktop.
Handle paystack payment callback with Laravel Http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Obtain Paystack payment information | |
* @return void | |
*/ | |
public function handleUserFeeViaPaystackCallback(Request $request) | |
{ | |
//Instantiate Laravel Http get request to Paystack Veryify API route | |
//passing paystack secret key as Bearer token. | |
$paystack_client = Http::withToken(config('paystack.secretKey')) | |
//----------------------------------------------> attach the transaction ref from the request URl | |
->get("https://api.paystack.co/transaction/verify/" . $request->query('trxref')); | |
$paymentDetails = $paystack_client->json(); | |
$valid_user = User::where('email', $paymentDetails['data']['customer']['email'])->firstOrFail(); | |
// check if transaction is successfull | |
if ($paymentDetails['data']['status'] === "success") { | |
// check if transaction amount received is equal to the amount requested. | |
if (($paymentDetails['data']['amount'] / 100) == 1500) { | |
//do what you want with the valid data | |
} | |
//else check if transaction is successfull | |
} elseif ($paymentDetails['data']['status'] === "failed") { | |
//do what you want with the failed transaction in your db | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment