Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ pluginConfiguration:
hmac_key: '12345'
hmac_key_2: ''
use_hmac_2: FALSE
debug: FALSE
pluginId: payment_datatrans
status: true
3 changes: 3 additions & 0 deletions config/schema/payment_datatrans.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ payment.plugin_configuration.payment_method_configuration.payment_datatrans:
use_hmac_2:
type: boolean
label: Use HMAC 2
debug:
type: boolean
label: Log Datatrans responses
81 changes: 56 additions & 25 deletions src/Controller/DatatransResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use Symfony\Component\HttpFoundation\Request;

/**
* Class DatatransResponseController
* Datatrans Response Controller
* Class DatatransResponseController.
*
* Datatrans Response Controller
*
* @package Drupal\payment_datatrans\Controller
*/
Expand All @@ -22,15 +23,24 @@ class DatatransResponseController {
/**
* Page callback for processing successful Datatrans response.
*
* @param Request $request
* Request
* @param PaymentInterface $payment
* @param \Symfony\Component\HttpFoundation\Request $request
* Request.
* @param \Drupal\payment\Entity\PaymentInterface $payment
* The Payment entity type.
*
* @return \Symfony\Component\HttpFoundation\Response
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Returns the redirect response.
*/
public function processSuccessResponse(Request $request, PaymentInterface $payment) {
try {
if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) {
if (\Drupal::moduleHandler()->moduleExists('past')) {
past_event_save('datatrans', 'response_success', 'Success response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]);
}
else {
\Drupal::logger('datatrans')->debug(t('Payment success response: @response', ['@response' => $request->request->all()]));
}
}
// This needs to be checked to match the payment method settings
// ND being valid with its keys and data.
$post_data = $request->request->all();
Expand Down Expand Up @@ -88,15 +98,24 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme
/**
* Page callback for processing error Datatrans response.
*
* @param PaymentInterface $payment
* The Payment entity type.
*
* @return \Symfony\Component\HttpFoundation\Response
* @param \Symfony\Component\HttpFoundation\Request $request
* Request.
* @param \Drupal\payment\Entity\PaymentInterface $payment
* The Payment entity type.
*
* @throws \Exception
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Returns the redirect response.
*/
public function processErrorResponse(PaymentInterface $payment) {

public function processErrorResponse(Request $request, PaymentInterface $payment) {
if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) {
if (\Drupal::moduleHandler()->moduleExists('past')) {
past_event_save('datatrans', 'response_error', 'Error response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]);
}
else {
\Drupal::logger('datatrans')->info(t('Payment error response: @response', ['@response' => $request->request->all()]));
}
drupal_set_message(t('Payment error response: @response', ['@response' => implode(', ', $request->request->all())]));
}
$message = 'Datatrans communication failure. Invalid data received from Datatrans.';
\Drupal::logger('datatrans')->error('Processing failed with exception @e.', array('@e' => $message));
drupal_set_message(t('Payment processing failed.'), 'error');
Expand All @@ -106,14 +125,23 @@ public function processErrorResponse(PaymentInterface $payment) {
/**
* Page callback for processing cancellation Datatrans response.
*
* @param PaymentInterface $payment
* The Payment entity type.
* @param \Symfony\Component\HttpFoundation\Request $request
* Request received from datatrans server.
* @param \Drupal\payment\Entity\PaymentInterface $payment
* The Payment entity type.
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Exception
* Returns the redirect response.
*/
public function processCancelResponse(PaymentInterface $payment) {
public function processCancelResponse(Request $request, PaymentInterface $payment) {
if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) {
if (\Drupal::moduleHandler()->moduleExists('past')) {
past_event_save('datatrans', 'response_cancel', 'Cancel response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]);
}
else {
\Drupal::logger('datatrans')->info(t('Payment cancel response: @response', ['@response' => $request->request->all()]));
}
}
drupal_set_message(t('Payment cancelled.'), 'error');
return $this->savePayment($payment, 'payment_cancelled');
}
Expand All @@ -128,9 +156,11 @@ public function processCancelResponse(PaymentInterface $payment) {
*
* @return string
* The generated sign.
*
* @throws \Exception
* Exception when generating the sign failed.
*/
public function generateSign2($plugin_definition, $post_data) {
public function generateSign2(array $plugin_definition, array $post_data) {
if ($plugin_definition['security']['hmac_key'] || $plugin_definition['security']['hmac_key_2']) {
if ($plugin_definition['security']['use_hmac_2']) {
$key = $plugin_definition['security']['hmac_key_2'];
Expand All @@ -148,11 +178,12 @@ public function generateSign2($plugin_definition, $post_data) {
* Saves success/cancelled/failed payment.
*
* @param \Drupal\payment\Entity\PaymentInterface $payment
* Payment entity.
* Payment entity.
* @param string $status
* Payment status to set
* Payment status to set.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @return \Symfony\Component\HttpFoundation\Response
* Saves payment and returns a response.
*/
public function savePayment(PaymentInterface $payment, $status = 'payment_failed') {
$payment->setPaymentStatus(\Drupal::service('plugin.manager.payment.status')
Expand All @@ -166,12 +197,12 @@ public function savePayment(PaymentInterface $payment, $status = 'payment_failed
*
* No validation.
*
* @param PaymentInterface $payment
* @param \Drupal\payment\Entity\PaymentInterface $payment
* Payment Interface.
* @param $post_data
* @param array $post_data
* Datatrans Post Data.
*/
public function setPaymentConfiguration(PaymentInterface $payment, $post_data) {
public function setPaymentConfiguration(PaymentInterface $payment, array $post_data) {
/** @var \Drupal\payment_datatrans\Plugin\Payment\Method\DatatransMethod $payment_method */
$payment_method = $payment->getPaymentMethod();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function defaultConfiguration() {
'hmac_key_2' => '',
'use_hmac_2' => FALSE,
),
'debug' => FALSE,
);
}

Expand Down Expand Up @@ -332,6 +333,23 @@ public function getHmacKeyTwo() {
return $this->configuration['security']['hmac_key_2'];
}

/**
* Enables logging the response from Datatrans.
*
* @param bool $state
* Whether debugging should be dis/enabled.
*/
public function setDebug($state = TRUE) {
$this->configuration['debug'] = $state;
}

/**
* Disables logging the response from Datatrans.
*/
public function getDebug() {
return $this->configuration['debug'];
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -432,6 +450,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
),
);

$form['debug'] = array(
'#type' => 'checkbox',
'#title' => 'Log response from Datatrans server',
'#default_value' => $this->getDebug(),
);

return $form;
}

Expand All @@ -449,7 +473,8 @@ public function formElementsValidate(array $element, FormStateInterface $form_st
->setMerchantControlConstant($values['security']['merchant_control_constant'])
->setHmacKey($values['security']['hmac_key'])
->setUseHmacTwo($values['security']['use_hmac_2'])
->setHmacKeyTwo($values['security']['hmac_key_2']);
->setHmacKeyTwo($values['security']['hmac_key_2'])
->setDebug($values['debug']);
}

}
Loading