Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joekenpat/feaadec416ba897004659485fed8f6df to your computer and use it in GitHub Desktop.
Save joekenpat/feaadec416ba897004659485fed8f6df to your computer and use it in GitHub Desktop.
Create a new Paystack charge using Laravel Http Request
public function userPayFeeViaPaystack()
{
//instantiate Laravel Http Client and pass paystack secret key as a bearer token
$paystack = Http::withToken(config('paystack.secretKey'))
->post('https://api.paystack.co/transaction/initialize', [
//email for the user making the payments
'email' => 'john@doe.com,
//amount must be in kobo hence: the multiplication by 100
'amount' => 1500 * 100,
'quantity' => 1,
//currency to be accepted
'currency' => 'NGN',
'channels' => ['card'],
//reference code for this charge must be unique
//it's optional if you dont put one paystack will create one for you.
'reference' => 'USERFEE9J39N21IW',
//internal route for paystack to call on successful payment
//mostly where you kept a function to validate the transaction.
'callback_url' => route('YOUR_OWN_CALLBACK_ROUTE'),
'metadata' => [
//internal route for paystack to call on failed or cancelled payment
//mostly where you have a function to update the status of the trasaction.
//in your DB to failed
'cancel_action' => route('YOUR_OWN_CANCELLED_ROUTE'),
]
])->json();
//$paystack['data'] contains the data for the new created charge
//$paystack['data']['authorization_url'] contains the url the user should visit to make the payment.
// //redirect the user finally to make the payment, if it is a SSR Application
//return redirect()->away($paystack['data']['authorization_url']);
// // else if it's an API route you return the link as a response instead
// return response()->json($paystack['data']['authorization_url'], Response::HTTP_OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment