Last active
November 28, 2023 11:07
-
-
Save iZaL/61908c821f31be351c8b5b45dedbf08d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/** | |
* Checkout method | |
*/ | |
public function knetCheckout($orderID) | |
{ | |
$order = $this->orderModel->find($orderID); | |
$invoiceID = $this->generateInvoiceCode(); | |
$order->invoice_id = $invoiceID; | |
$order->save(); | |
$successURL = route('payment.knet.response.success'); | |
$errorURL = route('payment.knet.error'); | |
$knetAlias = env('KNET_ALIAS'); | |
try { | |
$knetGateway = new KnetBilling([ | |
'alias' => $knetAlias, | |
'resourcePath' => base_path() . '/' | |
]); | |
$knetGateway->setResponseURL($successURL); | |
$knetGateway->setErrorURL($errorURL); | |
$knetGateway->setAmt($order->amount); | |
$knetGateway->setTrackId($orderID); | |
$knetGateway->requestPayment(); | |
$paymentURL = $knetGateway->getPaymentURL(); | |
$order->payment_id = $knetGateway->getPaymentID(); | |
$order->status = 'checkout'; | |
$order->save(); | |
return redirect()->away($paymentURL); | |
} catch (\Exception $e) { | |
$order->status = 'error'; | |
$order->save(); | |
return redirect()->route('home')->with('error', 'حدث خلل اثناء التحويل الي موقع الدفع'); | |
} | |
} | |
/** | |
* Knet Response Page | |
*/ | |
public function onKnetPaymentResponseSuccess(Request $request) | |
{ | |
$paymentID = $request->paymentid; | |
$result = $request->result; | |
$transactionID = $request->tranid; | |
$trackID = $request->trackid; | |
$urlParams = "/?paymentID=" . $paymentID . "&transactionID=" . $transactionID . "&trackID=" . $trackID; | |
$order = $this->orderModel->find($trackID); | |
$order->transaction_id = $transactionID; | |
// $order->payment_id = $paymentID; | |
if ($result == "CAPTURED") { | |
$order->status = 'success'; | |
$order->save(); | |
$redirectURL = route('payment.knet.success'); | |
} else { | |
$order->status = 'failed'; | |
$order->save(); | |
$redirectURL = route('payment.knet.error'); | |
} | |
return "REDIRECT=" . $redirectURL . $urlParams; | |
} | |
/** | |
* Knet Success | |
*/ | |
public function onKnetPaymentSuccess(Request $request) | |
{ | |
$order = $this->orderModel->find($request->trackID); | |
try { | |
$this->onPaymentSuccess($order); | |
} catch (\Exception $e) { | |
} | |
return redirect()->route('order.success', [$order->id, 'ref' => $order->transaction_id]); | |
} | |
public function onKnetPaymentError(Request $request) | |
{ | |
$order = $this->orderModel->find($request->trackID); | |
if($order) { | |
$order->status = 'failed'; | |
$order->transaction_id = $request->transactionID; | |
$order->save(); | |
} | |
return view('payment.failure'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment