Commit 23070576 authored by GiorgosK's avatar GiorgosK

Add callback controller

parent 0e546ba3
commerce_winbank.payment_callback:
path: '/commerce_winbank/callback'
defaults:
_controller: '\Drupal\commerce_winbank\Controller\CallbackController::callback'
_title: 'Winbank Payment Controller'
requirements:
_permission: 'access content'
<?php
namespace Drupal\commerce_winbank\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Drupal\commerce_order\Entity\Order;
/**
* Endpoints for the routes defined.
*/
class CallbackController extends ControllerBase {
/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* Callback action.
*
* Listen for callbacks from QuickPay and creates any payment specified.
*
* @param Request $request
*
* @return Response
*/
public function callback(Request $request) {
$this->processCallback($request);
return new Response();
}
/**
* Process the callback from winbank
*/
public function processCallback(Request $request){
$hashkey = $request->get('HashKey');
$order_id = $request->get('MerchantReference');
$order = Order::load($order_id);
$ticket_result = $order->getData("IssueNewTicketResult");
$hash = $this->calculateHash($request, $order_id, $ticket_result);
if( $hash !== $hashkey) {
$this->createPayment($order, $request, $ticket_result, "Unvalidated");
return;
}
$this->createPayment($order, $request, $ticket_result);
}
/**
* create Payment
*/
public function createPayment(Order $order, $request, $ticket_result, $state = "Completed"){
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
$payment = $payment_storage->create([
'state' => $state,
'amount' => $order->getBalance(),
'payment_gateway' => $ticket_result["payment_gateway"],
'order_id' => $order->id(),
'remote_id' => $request->get('TransactionId'),
'remote_state' => $request->get('StatusFlag'),
]);
if($state == "Completed"){
$payment->setAuthorizedTime(REQUEST_TIME);
$payment->setCompletedTime(REQUEST_TIME);
}
$payment->save();
}
/**
* Calculates hash key by concatenation of values, then uses sha256 algorithm.
*
* @param $request
*
* @param $IssueNewTicketResult
*
* @return string
*/
public function calculateHash($request, $order_id, $ticket_result){
$concatValues = sprintf(
'%s;%s;%s;%s;%s;%s;%s;%s;%s;%s;%s',
$ticket_result['TranTicket'],
$ticket_result['PosId'],
$ticket_result['AcquirerId'],
$order_id,
$request->get('ApprovalCode'),
$ticket_result['Parameters'],
$request->get('ResponseCode'),
$request->get('SupportReferenceID'),
$request->get('AuthStatus'),
$request->get('PackageNo'),
$request->get('StatusFlag')
);
return hash_hmac('sha256', $concatValues, $ticket_result['TranTicket'], false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment