Skip to content
Permalink
Browse files

Adding eSewa Integration

  • Loading branch information...
sudiptpa committed Mar 16, 2019
1 parent c1442b5 commit 324127c54ce191dbe8f6188f2168f4af5fe8e0bc
@@ -0,0 +1,80 @@
<?php
namespace App;
use Exception;
use Omnipay\Omnipay;
/**
* Class Esewa
* @package App
*/
class Esewa
{
/**
* @return \SecureGateway
*/
public function gateway()
{
$gateway = Omnipay::create('Esewa_Secure');
$gateway->setMerchantCode(config('services.esewa.merchant'));
$gateway->setTestMode(config('services.esewa.sandbox'));
return $gateway;
}
/**
* @param array $parameters
* @return $response
*/
public function purchase(array $parameters)
{
try {
$response = $this->gateway()
->purchase($parameters)
->send();
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
return $response;
}
/**
* @param array $parameters
* @return $response
*/
public function verifyPayment(array $parameters)
{
$response = $this->gateway()
->verifyPayment($parameters)
->send();
return $response;
}
/**
* @param $amount
*/
public function formatAmount($amount)
{
return number_format($amount, 2, '.', '');
}
/**
* @param $order
*/
public function getFailedUrl($order)
{
return route('checkout.payment.esewa.failed', $order->id);
}
/**
* @param $order
*/
public function getReturnUrl($order)
{
return route('checkout.payment.esewa.completed', $order->id);
}
}
@@ -0,0 +1,104 @@
<?php
namespace App\Http\Controllers;
use App\Esewa;
use App\Order;
use Exception;
use Illuminate\Http\Request;
/**
* Class EsewaController
* @package App\Http\Controllers
*/
class EsewaController extends Controller
{
/**
* @param Request $request
*/
public function checkout(Request $request)
{
$order = Order::findOrFail(mt_rand(1, 20));
return view('esewa.checkout', compact('order'));
}
/**
* @param $order_id
* @param Request $request
*/
public function payment($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
$gateway = with(new Esewa);
try {
$response = $gateway->purchase([
'amount' => $gateway->formatAmount($order->amount),
'totalAmount' => $gateway->formatAmount($order->amount),
'productCode' => 'ABAC2098',
'failedUrl' => $gateway->getFailedUrl($order),
'returnUrl' => $gateway->getReturnUrl($order),
], $request);
} catch (Exception $e) {
$order->update(['payment_status' => Order::PAYMENT_PENDING]);
return redirect()
->route('checkout.payment.esewa.failed', [$order->id])
->with('message', sprintf("Your payment failed with error: %s", $e->getMessage()));
}
if ($response->isRedirect()) {
$response->redirect();
}
return redirect()->back()->with([
'message' => "We're unable to process your payment at the moment, please try again !",
]);
}
/**
* @param $order_id
* @param Request $request
*/
public function completed($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
$gateway = with(new Esewa);
$response = $gateway->verifyPayment([
'amount' => $gateway->formatAmount($order->amount),
'referenceNumber' => $request->get('refId'),
'productCode' => $request->get('oid'),
], $request);
if ($response->isSuccessful()) {
$order->update([
'transaction_id' => $request->get('refId'),
'payment_status' => Order::PAYMENT_COMPLETED,
]);
return redirect()->route('checkout.payment.esewa')->with([
'message' => 'Thank you for your shopping, Your recent payment was successful.',
]);
}
return redirect()->route('checkout.payment.esewa')->with([
'message' => 'Thank you for your shopping, However, the payment has been declined.',
]);
}
/**
* @param $order_id
* @param Request $request
*/
public function failed($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
return view('esewa.checkout', compact('order'));
}
}
@@ -0,0 +1,30 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
/**
* Class Order
* @package App
*/
class Order extends Model
{
const PAYMENT_COMPLETED = 1;
const PAYMENT_PENDING = 0;
/**
* @var string
*/
protected $table = 'orders';
/**
* @var array
*/
protected $dates = ['deleted_at'];
/**
* @var array
*/
protected $fillable = ['transaction_id', 'amount', 'payment_status'];
}
@@ -11,7 +11,9 @@
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0"
"laravel/tinker": "^1.0",
"league/omnipay": "^3.0",
"sudiptpa/omnipay-esewa": "^3.0@dev"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
Oops, something went wrong.

0 comments on commit 324127c

Please sign in to comment.
You can’t perform that action at this time.