Skip to content

Instantly share code, notes, and snippets.

@mavinothkumar
Created September 11, 2019 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavinothkumar/79b743dcb742f690dcd12ed2baa034ef to your computer and use it in GitHub Desktop.
Save mavinothkumar/79b743dcb742f690dcd12ed2baa034ef to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mypos\IPC\Cart;
use Mypos\IPC\Config;
use Mypos\IPC\Customer;
use Mypos\IPC\Defines;
use Mypos\IPC\IPC_Exception;
use Mypos\IPC\Purchase;
use Mypos\IPC\Response;
use function auth;
use function logger;
use function public_path;
use function route;
use const CASE_LOWER;
class MyPOSController extends Controller
{
/**
* @var \Illuminate\Config\Repository
*/
private $mode;
/**
* @var \Illuminate\Config\Repository
*/
private $private_key;
/**
* @var \Illuminate\Config\Repository
*/
private $public_key;
/**
* @var \Illuminate\Config\Repository
*/
private $url;
/**
* @var \Illuminate\Config\Repository
*/
private $key_index;
/**
* @var \Illuminate\Config\Repository
*/
private $shop_id;
/**
* @var \Illuminate\Config\Repository
*/
private $wallet;
/**
* @var \Illuminate\Config\Repository
*/
private $version;
/**
* @var \Illuminate\Config\Repository
*/
private $lang;
public function __construct()
{
$this->mode = \config('custom.mode');
$this->private_key = \config('custom.sandbox.private_key');
$this->public_key = \config('custom.sandbox.public_key');
$this->shop_id = \config('custom.sandbox.shop_id');
$this->wallet = \config('custom.sandbox.wallet');
$this->url = \config('custom.sandbox.url');
$this->key_index = \config('custom.key_index');
$this->version = \config('custom.version');
$this->lang = \config('custom.lang');
}
public function start()
{
$cnf = new Config();
$cnf->setIpcURL($this->url);
$cnf->setLang($this->lang);
$cnf->setPrivateKeyPath($this->private_key);
$cnf->setAPIPublicKeyPath($this->public_key);
$cnf->setKeyIndex($this->key_index);
$cnf->setSid($this->shop_id);
$cnf->setVersion($this->version);
$cnf->setWallet($this->wallet);
$customer = new Customer();
$customer->setFirstName(auth()->user()->first_name ?? 'NA');
$customer->setLastName(auth()->user()->last_name ?? 'NA');
$customer->setEmail(auth()->user()->email ?? 'error@error.com');
$customer->setCountry('GRC');
// $customer->setPhone( '+359111111111' );
// $customer->setAddress( 'Business Park Varna' );
// $customer->setCity( 'Varna' );
// $customer->setZip( '9000' );
$cart = new Cart();
$cart->add('Annual Subscription', 1, 30); //name, quantity, price
$purchase = new Purchase($cnf);
$purchase->setUrlCancel(route('pos.cancel'));
$purchase->setUrlOk(route('pos.ok'));
$purchase->setUrlNotify(route('pos.notify'));
$purchase->setOrderID(uniqid()); //Some unique ID
$purchase->setCurrency('EUR');
$purchase->setNote('Thanks for purchasing'); //Not required
$purchase->setCustomer($customer);
$purchase->setCart($cart);
$purchase->setCardTokenRequest(Purchase::CARD_TOKEN_REQUEST_PAY_AND_STORE);
$purchase->setPaymentParametersRequired(Purchase::PURCHASE_TYPE_FULL);
$purchase->setPaymentMethod(Purchase::PAYMENT_METHOD_BOTH);
// dd($purchase);
$status = array();
try {
$status = $purchase->process();
} catch (IPC_Exception $ex) {
echo $ex->getMessage();
//Invalid params. To see details use "echo $ex->getMessage();"
}
logger('mypos', [$status]);
return $status;
}
public function cancel(Request $request)
{
logger('cancel', [$request->all(), $_REQUEST]);
return \response('OK', 200);
}
public function ok(Request $request)
{
logger('ok', [$request->all(), $_REQUEST]);
return \response('OK', 200);
}
public function notify(Request $request)
{
$cnf = new Config();
$cnf->setIpcURL($this->url);
$cnf->setLang($this->lang);
$cnf->setPrivateKeyPath($this->private_key);
$cnf->setAPIPublicKeyPath($this->public_key);
$cnf->setKeyIndex($this->key_index);
$cnf->setSid($this->shop_id);
$cnf->setVersion($this->version);
$cnf->setWallet($this->wallet);
try {
$response = Response::getInstance($cnf, $_POST, Defines::COMMUNICATION_FORMAT_POST);
// logger('notify response', [$response]);
echo 'OK';
exit();
} catch (\Mypos\IPC\IPC_Exception $e) {
//Display Some general error or redirect to merchant store home page
}
echo 'ERROR';
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment