Skip to content
Snippets Groups Projects
Commit 6d6d8425 authored by Primoz Hmeljak's avatar Primoz Hmeljak
Browse files

by Primsi: create the payment in onNotify.

parent 98793447
No related branches found
No related tags found
No related merge requests found
......@@ -42,14 +42,14 @@ class DatatransHelper {
* Transaction amount in cents or smallest available unit of the currency.
* @param string $currency
* Transaction currency – ISO Character Code (CHF, USD, EUR etc.).
* @param string $payment_id
* @param string $order_id
* Our reference number.
*
* @return string
* The computed hash.
*/
public static function generateSign($hmac_key, $merchant_id, $amount, $currency, $payment_id) {
$hmac_data = $merchant_id . $amount . $currency . $payment_id;
public static function generateSign($hmac_key, $merchant_id, $amount, $currency, $order_id) {
$hmac_data = $merchant_id . $amount . $currency . $order_id;
return hash_hmac('md5', $hmac_data, pack('H*', $hmac_key));
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\commerce_datatrans\Plugin\Commerce\PaymentGateway;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_payment\Exception\PaymentGatewayException;
use Drupal\commerce_payment\PaymentMethodTypeManager;
use Drupal\commerce_payment\PaymentTypeManager;
use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase;
......@@ -207,45 +208,19 @@ class Datatrans extends OffsitePaymentGatewayBase {
// @todo Add examples of request validation.
$post_data = $request->request->all();
if ($payment = \Drupal::entityTypeManager()->getStorage('commerce_payment')->load($post_data['refno'])) {
$currency_code = $payment->getAmount()->getCurrencyCode();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
$currency = Currency::load($currency_code);
$a = $post_data['amount'] / pow(10, $currency->getFractionDigits());
$price = new Price((string) $a, $currency_code);
// @todo Missing 'state' here.
$payment->setAmount($price);
$payment->setRemoteId($post_data['acqAuthorizationCode']);
$payment->setRemoteState($post_data['status']);
$payment->setAuthorizedTime(REQUEST_TIME);
// $payment->save();
if (isset($post_data['status'])) {
switch ($post_data['status']) {
case 'success':
drupal_set_message($this->t('Payment was processed successfully'));
break;
default:
// @todo Should we give more info.
drupal_set_message($this->t('There was a problem while processing your payment.'), 'warning');
throw new PaymentGatewayException();
}
}
// @todo Missing 'state' here.
drupal_set_message('Payment was processed');
}
/**
* {@inheritdoc}
*/
public function onCancel(OrderInterface $order, Request $request) {
drupal_set_message($this->t('You have canceled checkout at @gateway but may resume the checkout process here when you are ready.', [
'@gateway' => $this->getDisplayLabel(),
]));
}
/**
* {@inheritdoc}
*/
public function getNotifyUrl() {
return Url::fromRoute('commerce_payment.notify', [
'commerce_payment_gateway' => $this->entityId,
], ['absolute' => TRUE]);
}
/**
......@@ -258,43 +233,40 @@ class Datatrans extends OffsitePaymentGatewayBase {
return new Response('', 400);
}
/** @var \Drupal\commerce_payment\entity\PaymentInterface $payment */
$payment = $this->entityTypeManager->getStorage('commerce_payment')->load($post_data['refno']);
if (!$payment) {
/** @var \Drupal\commerce_order\entity\OrderInterface $order */
$order = $this->entityTypeManager->getStorage('commerce_order')->load($post_data['refno']);
if (!$order) {
return new Response('', 400);
}
switch ($post_data['status']) {
case 'success':
$currency_code = $payment->getAmount()->getCurrencyCode();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
$currency = Currency::load($currency_code);
$datatrans_amount = $post_data['amount'] / pow(10, $currency->getFractionDigits());
$price = new Price((string) $datatrans_amount, $currency_code);
// @todo Missing 'state' here.
$payment->setAmount($price);
// Documentation states we should use acqAuthorizationCode but that
// number is not visible in their ui.
$payment->setRemoteId($post_data['authorizationCode']);
$payment->setRemoteState($post_data['status']);
$payment->setAuthorizedTime(REQUEST_TIME);
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
$payment = $payment_storage->create([
'state' => 'authorization',
// @todo Should we rather set price we get from datatrans?
'amount' => $order->getTotalPrice(),
'payment_gateway' => $this->entityId,
'order_id' => $order->id(),
'test' => $this->getMode() == 'test',
'remote_id' => $post_data['authorizationCode'],
'remote_state' => $post_data['responseMessage'],
'authorized' => REQUEST_TIME,
]);
$payment->save();
break;
case 'error':
$this->logger->error('The payment gateway returned the error code %code (%details) for payment %payment_id', [
$this->logger->error('The payment gateway returned the error code %code (%details) for payment %order_id', [
'%code' => $post_data['errorCode'],
'%details' => $post_data['errorDetail'],
'%payment_id' => $payment->id(),
'%order_id' => $order->id(),
]);
break;
case 'cancel':
$this->logger->info('The user canceled the authorisation process for payment %payment_id', [
'%payment_id' => $payment->id(),
$this->logger->info('The user canceled the authorisation process for payment %order_id', [
'%order_id' => $order->id(),
]);
break;
}
......
......@@ -20,14 +20,9 @@ class DatatransForm extends PaymentOffsiteForm {
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment = $this->entity;
if ($payment->isNew()) {
$payment->save();
$this->setEntity($payment);
}
$gateway = $payment->getPaymentGateway()->getPlugin();
$gateway_config = $gateway->getConfiguration();
$order = $payment->getOrder();
$currency_code = $payment->getAmount()->getCurrencyCode();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
......@@ -35,8 +30,8 @@ class DatatransForm extends PaymentOffsiteForm {
$data = [
'merchantId' => $gateway_config['merchant_id'],
'amount' => intval($payment->getAmount()->getNumber() * pow(10, $currency->getFractionDigits())),
'refno' => $payment->id(),
'amount' => intval($order->getTotalPrice()->getNumber() * pow(10, $currency->getFractionDigits())),
'refno' => $order->id(),
'sign' => NULL,
'currency' => $currency_code,
'successUrl' => $form['#return_url'],
......@@ -47,7 +42,7 @@ class DatatransForm extends PaymentOffsiteForm {
if ($gateway_config['security_level'] == 2) {
// Generates the sign.
$payment['sign'] = DatatransHelper::generateSign($gateway_config['hmac_key'], $gateway_config['merchant_id'], $payment->getAmount()->getNumber(), $currency_code, $payment->id());
$payment['sign'] = DatatransHelper::generateSign($gateway_config['hmac_key'], $gateway_config['merchant_id'], $order->getTotalPrice()->getNumber(), $currency_code, $order->id());
}
return $this->buildRedirectForm($form, $form_state, $gateway_config['service_url'], $data, static::REDIRECT_POST);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment