Commit 8bcf2900 authored by GiorgosK's avatar GiorgosK

Add offsite redirect WinbankPaymentRedirectForm

parent 0a3e26ef
<?php
namespace Drupal\commerce_winbank\PluginForm\OffsiteRedirect;
use Drupal\commerce_payment\Exception\InvalidResponseException;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Core\Form\FormStateInterface;
class WinbankPaymentRedirectForm extends BasePaymentOffsiteForm {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$payment = $this->entity;
$payment_gateway_plugin = $payment->getPaymentGateway()->getPlugin();
$config = $payment_gateway_plugin->getConfiguration();
$order = $payment->getOrder();
// Format amount
$amount = sprintf('%0.2f', $order->getTotalPrice()->getNumber());
if ($config['mode'] == 'test') {
$config["tickets_url"] = $config["tickets_url_test"];
$config["paycenter_url"] = $config["paycenter_url_test"];
}
// Validate merchant_id, other validations ?
if (empty($config['merchant_id'])) {
throw new PaymentGatewayException('Merchant ID not provided.');
}
// Get new ticket
try {
$result = $this->getNewTicket($amount, $order->id(), $config);
if ($result->IssueNewTicketResult->ResultCode == 0) {
// save response data for validating response later
$order->setData("IssueNewTicketResult",[
"ResultCode" => $result->IssueNewTicketResult->ResultCode,
"TranTicket" => $result->IssueNewTicketResult->TranTicket,
"Timestamp" => $result->IssueNewTicketResult->Timestamp,
"MinutesToExpiration" => $result->IssueNewTicketResult->MinutesToExpiration,
]);
} else {
$message = 'There was a problem connecting ot the ticket issuer: '. $result->IssueNewTicketResult->ResultDescription;
throw new InvalidResponseException($message);
}
} catch (Exception $e) {
throw new InvalidResponseException('Could not connect to bank ticket issuer');
}
// Prepare redirect form
$data = [
'AcquirerId' => $config['acquirer_id'],
'MerchantId' => $config['merchant_id'],
'PosId' => $config['pos_id'],
'User' => $config['user'],
'MerchantReference' => $order->id(),
'LanguageCode' => $config['language_code'],
'ParamBackLink' => '',
];
$form = $this->buildRedirectForm($form, $form_state, $config['paycenter_url'], $data);
//temporary
unset($form['#attached']['library']);
return $form;
}
/**
* Soap request to get NewTicket for transaction
*
* @param int $amount
* The order amount.
* @param int $orderid
* The order id.
* @param int $config
* The configuration object.
*
* @return array
* Object containing the soap service response.
*
*/
public function getNewTicket($amount, $orderid, $config){
//libxml_disable_entity_loader(false);
$options= array(
'location' => $config['tickets_url'],
'uri' => $config['tickets_url']
);
$soap = new \SoapClient(NULL, $options);
$ticketRequest = [
'Username' => $config['username'],
'Password' => hash('md5', $config['password']),
'MerchantId' => $config['merchant_id'],
'PosId' => $config['pos_id'],
'AcquirerId' => $config['acquirer_id'],
'MerchantReference' => $orderid,
'RequestType' => $config['request_type'],
'ExpirePreauth' => 0,
'Amount' => $amount,
'CurrencyCode' => $config['currency_code'],
'Installments' => 0,
'Bnpl' => '0',
'Parameters' => ''
];
$xml = array(
'Request' => $ticketRequest
);
return $soap->IssueNewTicket($xml);
}
}
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