Skip to content

Instantly share code, notes, and snippets.

@codeInBit
Created October 24, 2018 15:13
Show Gist options
  • Save codeInBit/d2be082f133de068e4bc33ca13731a1d to your computer and use it in GitHub Desktop.
Save codeInBit/d2be082f133de068e4bc33ca13731a1d to your computer and use it in GitHub Desktop.
/**
* Redirect the User to Paystack / Paypal Payment Page
* @param Request $request
* @return URL
*/
public function redirectToGateway(Request $request)
{
//Check if user is logged in
if(Auth::check()){
//Check if the subscription for Professional Plan
if($request->get('plan') == 'pro'){
if($request->get('payMethod') == 'paystack'){
$email =$request->get('email') ;
$amount =$request->get('amount1') ;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paystack.co/transaction/initialize",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSLVERSION => 6,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
CURLOPT_POSTFIELDS => json_encode([
'amount'=>$amount,
'email'=>$email,
'callback_url' =>'http://localhost:8000/payment/callback'
]),
CURLOPT_HTTPHEADER => [
"authorization: Bearer sk_test_899d59b2451eecfa9392c64a7c8f32b661f66659",
"content-type: application/json",
"cache-control: no-cache"
],
));
$response = curl_exec($curl);
$err = curl_error($curl);
if($err){
// there was an error contacting the Paystack API
return redirect('billing')->with('status', 'There was an error connecting to Paystack API. Please try again');
//die('Curl returned error: ' . $err);
}
$tranx = json_decode($response);
if(!$tranx->status){
// there was an error from the API
return redirect('billing')->with('status', 'There was an error from Paystack API. Please try again');
}
// store transaction reference so we can query in case user never comes back perhaps due to network issue
$lastTransref = $tranx->data->reference;
// redirect to page so User can pay
return Redirect()->away($tranx->data->authorization_url);
}
}
}else{
return redirect('login')->with('status', 'Please login to your PushIt account');
}
}
//Callback URL for paystack
public function handlePayStackCallback()
{
$curl = curl_init();
$reference = isset($_GET['reference']) ? $_GET['reference'] : '';
if(!$reference){
die('No reference supplied');
}
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($reference),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSLVERSION => 6,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
CURLOPT_HTTPHEADER => [
"accept: application/json",
"authorization: Bearer sk_test_899d59b2451eecfa9392c64a7c8f32b661f66659",
"cache-control: no-cache"
],
));
$response = curl_exec($curl);
$err = curl_error($curl);
if($err){
// there was an error contacting the Paystack API
return redirect('billing')->with('status', 'There was an error connecting to Paystack API. Please try again');
}
$tranx = json_decode($response);
// echo "<pre>";
// print_r ($tranx);
// echo "</pre>"; exit;
if(!$tranx->status){
// there was an error from the API
return redirect('billing')->with('status', 'There was an error from Paystack API. Please try again');
}
if('success' == $tranx->data->status){
// transaction was successful...
$userId = Auth::user()->id;
$userdetails = User::find($userId);
// please check other things like whether you already gave value for this ref
// if the email matches the customer who owns the product etc
if($userdetails->email == $tranx->data->customer->email){
// Give value
$transaction = new Pay_transaction;
$web_payment = new Web_payment;
$amountPaid = $tranx->data->amount / 100;
if($amountPaid == 7500){
$userdetails->userType = "professional";
$userdetails->status = 1;
$web_payment->trxnid = "PRO".$userId.str_random(5).str_random(5).str_random(3);
$web_payment->details = "Payment for Professional Plan (Paystack)";
$transaction->transdesc = "Payment for Professional Plan (Paystack)";
$transaction->payid = $web_payment->trxnid;
}elseif($amountPaid == 30000){
$userdetails->userType = "advance";
$userdetails->status = 1;
$web_payment->trxnid = "ADV".$userId.str_random(5).str_random(5).str_random(3);
$web_payment->details = "Payment for Advance Plan (Paystack)";
$transaction->transdesc = "Payment for Advance Plan (Paystack)";
$transaction->payid = $web_payment->trxnid;
}
$transaction->ownerid = $userId;
$transaction->amount = $amountPaid;
$web_payment->userid = $userId;
$web_payment->amount = $amountPaid;
$userdetails->paid_date = date("Y-m-d h:i:s");
$userdetails->save();
$transaction->save();
$web_payment->save();
return redirect('billing')->with('status', 'Your payment was successful, your subscription has a validity period of 30days ');
}else{
return redirect('billing')->with('status', 'Your payment was not completed, please contact the admin.');
}
}
}
//Routes
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay'); // Laravel 5.1.17 and above
Route::get('/payment/callback', 'PaymentController@handlePayStackCallback'); //Paystack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment