Permalink
Browse files

Adding Omnipay SecurePay Setup

  • Loading branch information...
sudiptpa committed May 25, 2018
1 parent b24fa97 commit f2989f94d2610cd78f74e33d52a019fcdc36b84a
@@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers;
use App\Order;
use App\SecurePay;
use Exception;
use Illuminate\Http\Request;
/**
* Class PaymentController
* @package App\Http\Controllers
*/
class PaymentController extends Controller
{
/**
* @param Request $request
*/
public function checkout(Request $request)
{
$order = Order::findOrFail(mt_rand(1, 140));
// you application logic goes here
// the above order is just for example.
return view('checkout.payment', compact('order'));
}
/**
* @param $order_id
* @param Request $request
*/
public function payment($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
$gateway = new SecurePay;
try {
$response = $gateway->purchase([
'amount' => $gateway->formatAmount($order->amount),
'transactionId' => $order->id,
'currency' => 'USD',
'cancelUrl' => $gateway->getCancelUrl($order),
'returnUrl' => $gateway->getReturnUrl($order),
], $request);
} catch (Exception $e) {
$order->update(['payment_status' => Order::PAYMENT_PENDING]);
return redirect()
->route('checkout.payment.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
* @return mixed
*/
public function completed($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
$gateway = new SecurePay;
$response = $gateway->complete([
'amount' => $gateway->formatAmount($order->amount),
'transactionId' => $order->id,
'currency' => 'USD',
'cancelUrl' => $gateway->getCancelUrl($order),
'returnUrl' => $gateway->getReturnUrl($order),
], $request);
if ($response->isSuccessful()) {
$order->update([
'transaction_id' => $response->getTransactionReference(),
'payment_status' => Order::PAYMENT_COMPLETED,
]);
return redirect()->route('checkout.payment')->with([
'message' => 'Payment Successful, Thank you for your order !',
]);
}
return redirect()->back()->with([
'message' => $response->getMessage(),
]);
}
/**
* @param $order_id
* @param Request $request
* @return mixed
*/
public function failed($order_id, Request $request)
{
$order = Order::findOrFail($order_id);
return view('checkout.payment', compact('order'));
}
}
@@ -0,0 +1,109 @@
<?php
namespace App;
use Exception;
use Illuminate\Http\Request;
use Omnipay\Common\CreditCard;
use Omnipay\Common\Exception\InvalidCreditCardException;
use Omnipay\Omnipay;
/**
* Class SecurePay
* @package App
*/
class SecurePay
{
/**
* @return mixed
*/
public function gateway()
{
$gateway = Omnipay::create('SecurePay_DirectPost');
$gateway->setMerchantId(config('services.securepay.merchant_id'));
$gateway->setTransactionPassword(config('services.securepay.password'));
$gateway->setTestMode(config('services.securepay.sandbox'));
return $gateway;
}
/**
* @param array $parameters
* @param Request $request
*/
public function card(array $parameters, Request $request)
{
return [
'card' => new CreditCard([
'firstName' => $request->get('first_name'),
'lastName' => $request->get('last_name'),
'number' => $request->get('card_number'),
'expiryMonth' => $request->get('expiry_month'),
'expiryYear' => $request->get('expiry_year'),
'cvv' => $request->get('cvc'),
]),
];
}
/**
* @param array $parameters
* @param Request $request
* @return mixed
*/
public function purchase(array $parameters, Request $request)
{
$parameters = array_merge($parameters, $this->card($parameters, $request));
try {
$response = $this->gateway()
->purchase($parameters)
->send();
} catch (InvalidCreditCardException $e) {
throw new Exception($e->getMessage());
}
return $response;
}
/**
* @param array $parameters
* @param Request $request
* @return mixed
*/
public function complete(array $parameters, Request $request)
{
$parameters = array_merge($parameters, $this->card($parameters, $request));
$response = $this->gateway()
->completePurchase($parameters)
->send();
return $response;
}
/**
* @param $amount
*/
public function formatAmount($amount)
{
return number_format($amount, 2, '.', '');
}
/**
* @param $order
*/
public function getCancelUrl($order)
{
return route('checkout.payment.failed', $order->id);
}
/**
* @param $order
*/
public function getReturnUrl($order)
{
return route('checkout.payment.completed', $order->id);
}
}
@@ -15,7 +15,8 @@
"php-http/message": "^1.6",
"spatie/laravel-analytics": "^3.4",
"sudiptpa/guid": "^1.0",
"vinkla/instagram": "^8.0"
"vinkla/instagram": "^8.0",
"omnipay/securepay" : "^3.0"
},
"require-dev": {
"filp/whoops": "~2.0",
Oops, something went wrong.

0 comments on commit f2989f9

Please sign in to comment.