From 5f61cdb3fb8aef90f0418c66bd8bdb26f12b11d2 Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 17 Jun 2015 10:46:38 +0200 Subject: [PATCH 1/8] added logging, ammended method parameters --- .../DatatransResponseController.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 0e6f33c..895bbca 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -31,6 +31,15 @@ class DatatransResponseController { */ public function processSuccessResponse(Request $request, PaymentInterface $payment) { try { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $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(); @@ -88,6 +97,8 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme /** * Page callback for processing error Datatrans response. * + * @param Request $request + * Request * @param PaymentInterface $payment * The Payment entity type. * @@ -95,8 +106,16 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme * * @throws \Exception */ - public function processErrorResponse(PaymentInterface $payment) { - + public function processErrorResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment 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'); @@ -106,6 +125,8 @@ public function processErrorResponse(PaymentInterface $payment) { /** * Page callback for processing cancellation Datatrans response. * + * @param Request $request + * Request * @param PaymentInterface $payment * The Payment entity type. * @@ -113,7 +134,16 @@ public function processErrorResponse(PaymentInterface $payment) { * * @throws \Exception */ - public function processCancelResponse(PaymentInterface $payment) { + public function processCancelResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $request->request->all())])); + } drupal_set_message(t('Payment cancelled.'), 'error'); return $this->savePayment($payment, 'payment_cancelled'); } From fd1ebb87f9ae815d99dd7c24d697548dfa46f64a Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 17 Jun 2015 10:46:38 +0200 Subject: [PATCH 2/8] added logging, ammended method parameters --- .../DatatransResponseController.php | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 0e6f33c..895bbca 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -31,6 +31,15 @@ class DatatransResponseController { */ public function processSuccessResponse(Request $request, PaymentInterface $payment) { try { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $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(); @@ -88,6 +97,8 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme /** * Page callback for processing error Datatrans response. * + * @param Request $request + * Request * @param PaymentInterface $payment * The Payment entity type. * @@ -95,8 +106,16 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme * * @throws \Exception */ - public function processErrorResponse(PaymentInterface $payment) { - + public function processErrorResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment 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'); @@ -106,6 +125,8 @@ public function processErrorResponse(PaymentInterface $payment) { /** * Page callback for processing cancellation Datatrans response. * + * @param Request $request + * Request * @param PaymentInterface $payment * The Payment entity type. * @@ -113,7 +134,16 @@ public function processErrorResponse(PaymentInterface $payment) { * * @throws \Exception */ - public function processCancelResponse(PaymentInterface $payment) { + public function processCancelResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if(\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', $payment->id(), $request); + } + else { + \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + } + drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $request->request->all())])); + } drupal_set_message(t('Payment cancelled.'), 'error'); return $this->savePayment($payment, 'payment_cancelled'); } From 01f47b547bb1c6874abb101eb31f83009572b2d2 Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 24 Jun 2015 14:14:33 +0200 Subject: [PATCH 3/8] Added full namespaces, fixed code sniffer complaints --- ...method_configuration.payment_datatrans.yml | 1 + .../DatatransResponseController.php | 81 ++++++++++--------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/config/install/payment.payment_method_configuration.payment_datatrans.yml b/config/install/payment.payment_method_configuration.payment_datatrans.yml index 3c474c0..43c6d6e 100644 --- a/config/install/payment.payment_method_configuration.payment_datatrans.yml +++ b/config/install/payment.payment_method_configuration.payment_datatrans.yml @@ -14,5 +14,6 @@ pluginConfiguration: hmac_key: '12345' hmac_key_2: '' use_hmac_2: FALSE + debug: FALSE pluginId: payment_datatrans status: true diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 895bbca..4a0ace1 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -12,8 +12,9 @@ use Symfony\Component\HttpFoundation\Request; /** - * Class DatatransResponseController - * Datatrans Response Controller + * Class DatatransResponseController. + * + * Datatrans Response Controller * * @package Drupal\payment_datatrans\Controller */ @@ -22,23 +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::state()->get('datatrans.debug', FALSE)) { - if(\Drupal::moduleHandler()->moduleExists('past')) { - past_event_save('datatrans', $payment->id(), $request); + if (\Drupal::config('datatrans')->get('debug')) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', 'Success response - POST data', $request->request->all()); } else { - \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + \Drupal::logger('datatrans')->info(t('Payment success response: @response', ['@response' => $request])); } - drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $request->request->all())])); + drupal_set_message(t('Payment success response: @response', ['@response' => implode(', ', $request->request->all())])); } // This needs to be checked to match the payment method settings // ND being valid with its keys and data. @@ -97,24 +99,23 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme /** * Page callback for processing error Datatrans response. * - * @param Request $request - * Request - * @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(Request $request, PaymentInterface $payment) { if (\Drupal::state()->get('datatrans.debug', FALSE)) { - if(\Drupal::moduleHandler()->moduleExists('past')) { - past_event_save('datatrans', $payment->id(), $request); + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', 'Error response - POST data', $request); } else { - \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + \Drupal::logger('datatrans')->info(t('Payment error response: @response', ['@response' => $request])); } - drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $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)); @@ -125,24 +126,23 @@ public function processErrorResponse(Request $request, PaymentInterface $payment /** * Page callback for processing cancellation Datatrans response. * - * @param Request $request - * Request - * @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(Request $request, PaymentInterface $payment) { if (\Drupal::state()->get('datatrans.debug', FALSE)) { - if(\Drupal::moduleHandler()->moduleExists('past')) { - past_event_save('datatrans', $payment->id(), $request); + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('datatrans', 'Cancel response - POST data', $request); } else { - \Drupal::logger('datatrans')->info(t('Payment response: @response', ['@response' => $request])); + \Drupal::logger('datatrans')->info(t('Payment cancel response: @response', ['@response' => $request])); } - drupal_set_message(t('Payment response: @response', ['@response' => implode(', ', $request->request->all())])); + drupal_set_message(t('Payment cancel response: @response', ['@response' => implode(', ', $request->request->all())])); } drupal_set_message(t('Payment cancelled.'), 'error'); return $this->savePayment($payment, 'payment_cancelled'); @@ -158,9 +158,11 @@ public function processCancelResponse(Request $request, PaymentInterface $paymen * * @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']; @@ -178,11 +180,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') @@ -196,12 +199,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(); From 1d90914da81c9504a362988d563c59d1e7ee4a26 Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 24 Jun 2015 14:15:39 +0200 Subject: [PATCH 4/8] Corrected back to state based debugging --- src/Controller/DatatransResponseController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 4a0ace1..89a7a13 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -33,7 +33,7 @@ class DatatransResponseController { */ public function processSuccessResponse(Request $request, PaymentInterface $payment) { try { - if (\Drupal::config('datatrans')->get('debug')) { + if (\Drupal::state()->get('datatrans.debug', FALSE)) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Success response - POST data', $request->request->all()); } From 6e235c1c4a7479517797615d370eab2f72904a5f Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 24 Jun 2015 14:17:08 +0200 Subject: [PATCH 5/8] Added debugging as method confiugration --- config/schema/payment_datatrans.schema.yml | 3 +++ .../DatatransResponseController.php | 6 ++--- .../DatatransConfiguration.php | 27 ++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/config/schema/payment_datatrans.schema.yml b/config/schema/payment_datatrans.schema.yml index dbf0563..8674ed0 100644 --- a/config/schema/payment_datatrans.schema.yml +++ b/config/schema/payment_datatrans.schema.yml @@ -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 diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 89a7a13..8dbf601 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -33,7 +33,7 @@ class DatatransResponseController { */ public function processSuccessResponse(Request $request, PaymentInterface $payment) { try { - if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if (\Drupal::config('datatrans')->get('debug')) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Success response - POST data', $request->request->all()); } @@ -108,7 +108,7 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme * Returns the redirect response. */ public function processErrorResponse(Request $request, PaymentInterface $payment) { - if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if (\Drupal::config('datatrans')->get('debug')) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Error response - POST data', $request); } @@ -135,7 +135,7 @@ public function processErrorResponse(Request $request, PaymentInterface $payment * Returns the redirect response. */ public function processCancelResponse(Request $request, PaymentInterface $payment) { - if (\Drupal::state()->get('datatrans.debug', FALSE)) { + if (\Drupal::config('datatrans')->get('debug')) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Cancel response - POST data', $request); } diff --git a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php index 06911fd..8755cea 100644 --- a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php +++ b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php @@ -87,6 +87,7 @@ public function defaultConfiguration() { 'hmac_key_2' => '', 'use_hmac_2' => FALSE, ), + 'debug' => FALSE, ); } @@ -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} */ @@ -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; } @@ -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']); } } From cc6e1dba30add1fe566bb21a7fbec6383d9f727e Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Wed, 24 Jun 2015 16:43:26 +0200 Subject: [PATCH 6/8] added test for logging/debugging --- .../DatatransResponseController.php | 6 +- .../DatatransConfiguration.php | 2 +- src/Tests/DatatransPaymentTest.php | 136 +++++++++++------- 3 files changed, 88 insertions(+), 56 deletions(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index 8dbf601..b478adf 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -33,7 +33,7 @@ class DatatransResponseController { */ public function processSuccessResponse(Request $request, PaymentInterface $payment) { try { - if (\Drupal::config('datatrans')->get('debug')) { + if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Success response - POST data', $request->request->all()); } @@ -108,7 +108,7 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme * Returns the redirect response. */ public function processErrorResponse(Request $request, PaymentInterface $payment) { - if (\Drupal::config('datatrans')->get('debug')) { + if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Error response - POST data', $request); } @@ -135,7 +135,7 @@ public function processErrorResponse(Request $request, PaymentInterface $payment * Returns the redirect response. */ public function processCancelResponse(Request $request, PaymentInterface $payment) { - if (\Drupal::config('datatrans')->get('debug')) { + if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) { if (\Drupal::moduleHandler()->moduleExists('past')) { past_event_save('datatrans', 'Cancel response - POST data', $request); } diff --git a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php index 8755cea..f1b7493 100644 --- a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php +++ b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php @@ -455,7 +455,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#title' => 'Log response from Datatrans server', '#default_value' => $this->getDebug(), ); - + \Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']; return $form; } diff --git a/src/Tests/DatatransPaymentTest.php b/src/Tests/DatatransPaymentTest.php index 07ba26e..fa8e86f 100644 --- a/src/Tests/DatatransPaymentTest.php +++ b/src/Tests/DatatransPaymentTest.php @@ -27,7 +27,8 @@ class DatatransPaymentTest extends WebTestBase { 'payment_datatrans_test', 'node', 'field_ui', - 'config' + 'config', + 'dblog', ); /** @@ -95,7 +96,8 @@ protected function setUp() { 'access content', 'access administration pages', 'access user profiles', - 'payment.payment.view.any' + 'access site reports', + 'payment.payment.view.any', )); $this->drupalLogin($this->adminUser); } @@ -103,7 +105,7 @@ protected function setUp() { /** * Tests succesfull Datatrans payment. */ - function testDatatransSuccessPayment() { + public function testDatatransSuccessPayment() { // Modifies the datatrans configuration for testing purposes. $generator = \Drupal::urlGenerator(); $datatrans_configuration = array( @@ -118,23 +120,33 @@ function testDatatransSuccessPayment() { ); $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_datatrans', $datatrans_configuration, t('Save')); - // Create datatrans payment + $this->drupalGet('admin/config/services/payment/method/configuration/payment_datatrans'); + // Test the debug checkbox. + $this->assertNoFieldChecked('edit-plugin-form-debug'); + $edit = [ + 'plugin_form[debug]' => TRUE, + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $this->drupalGet('admin/config/services/payment/method/configuration/payment_datatrans'); + $this->assertFieldChecked('edit-plugin-form-debug'); + + // Create datatrans payment. $this->drupalPostForm('node/' . $this->node->id(), array(), t('Pay')); - // Retrieve plugin configuration of created node + // Retrieve plugin configuration of created node. $plugin_configuration = $this->node->{$this->fieldName}->plugin_configuration; // Array of Datatrans payment method configuration. $datatrans_payment_method_configuration = entity_load('payment_method_configuration', 'payment_datatrans')->getPluginConfiguration(); - // Check for correct Merchant ID + // Check for correct Merchant ID. $this->assertText('merchantId' . $datatrans_payment_method_configuration['merchant_id']); - // Check for correct amount + // Check for correct amount. $calculated_amount = $this->calculateAmount($plugin_configuration['amount'], $plugin_configuration['quantity'], $plugin_configuration['currency_code']); $this->assertText('amount' . $calculated_amount); - // Check for correct success, error and cancel url + // Check for correct success, error and cancel url. $this->assertText('successUrl' . $generator->generateFromRoute('payment_datatrans.response_success', array('payment' => 1), array('absolute' => TRUE))); $this->assertText('errorUrl' . $generator->generateFromRoute('payment_datatrans.response_error', array('payment' => 1), array('absolute' => TRUE))); $this->assertText('cancelUrl' . $generator->generateFromRoute('payment_datatrans.response_cancel', array('payment' => 1), array('absolute' => TRUE))); @@ -142,19 +154,19 @@ function testDatatransSuccessPayment() { // Check for correct sign with using hmac_key $this->assertText('sign' . '309dd30ad0cb07770d3a1ffda64585a9'); - // Finish and save payment + // Finish and save payment. $this->drupalPostForm(NULL, array(), t('Submit')); - // Check out the payment overview page + // Check out the payment overview page. $this->drupalGet('admin/content/payment'); - // Check for correct currency code and payment amount + // Check for correct currency code and payment amount. $this->assertText('CHF 246.00'); - // Check for correct Payment Method + // Check for correct Payment Method. $this->assertText('Datatrans'); - // Check payment configuration (city, street & zipcode) + // Check payment configuration (city, street & zipcode). /** @var \Drupal\payment\Entity\PaymentInterface $payment */ $payment = entity_load('payment', 1); $payment_method = $payment->getPaymentMethod(); @@ -167,20 +179,24 @@ function testDatatransSuccessPayment() { $this->assertTrue($payment_configuration['uppCustomerStreet'], 'street'); $this->assertTrue($payment_configuration['uppCustomerZipCode'], 'CHE'); - // Check for detailed payment information + // Check for detailed payment information. $this->drupalGet('payment/1'); $this->assertNoText('Failed'); $this->assertText('Payment description'); $this->assertText('CHF 123.00'); $this->assertText('CHF 246.00'); $this->assertText('Completed'); + + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment success response: POST'); } /** * Tests failing Datatrans payment. + * * The test fails by providing an incorrect hmac key. */ - function testDatatransFailedPayment() { + public function testDatatransFailedPayment() { // Modifies the datatrans configuration for testing purposes. $generator = \Drupal::urlGenerator(); $datatrans_configuration = array( @@ -193,35 +209,40 @@ function testDatatransFailedPayment() { 'plugin_form[security][hmac_key]' => '1234', // For failed test we give a wrong hmac_key 'plugin_form[security][hmac_key_2]' => '', + 'plugin_form[debug]' => TRUE, ); $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_datatrans', $datatrans_configuration, t('Save')); - // Create datatrans payment + // Create datatrans payment. \Drupal::state()->set('datatrans.return_url_key', 'error'); $this->drupalPostForm('node/' . $this->node->id(), array(), t('Pay')); // Check for incorrect sign. $this->assertNoText('sign309dd30ad0cb07770d3a1ffda64585a9'); - // Finish and save payment + // Finish and save payment. $this->drupalPostForm(NULL, array(), t('Submit')); - // Check out the payment overview page + // Check out the payment overview page. $this->drupalGet('admin/content/payment'); $this->assertText('Failed'); $this->assertNoText('Success'); - // Check for detailed payment information + // Check for detailed payment information. $this->drupalGet('payment/1'); $this->assertText('Failed'); $this->assertNoText('Success'); + + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment error response: POST'); } /** * Tests failing Datatrans payment. + * * The test fails by providing an incorrect hmac key. */ - function testDatatransTestSignPayment() { + public function testDatatransTestSignPayment() { // Modifies the datatrans configuration for testing purposes. $generator = \Drupal::urlGenerator(); $datatrans_configuration = array( @@ -234,10 +255,11 @@ function testDatatransTestSignPayment() { 'plugin_form[security][hmac_key]' => '1234', // For failed test we give a wrong hmac_key 'plugin_form[security][hmac_key_2]' => '', + 'plugin_form[debug]' => TRUE, ); $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_datatrans', $datatrans_configuration, t('Save')); - // Create datatrans payment + // Create datatrans payment. \Drupal::state()->set('datatrans.return_url_key', 'error'); \Drupal::state()->set('datatrans.identifier', rand(10, 100)); @@ -248,24 +270,27 @@ function testDatatransTestSignPayment() { \Drupal::state()->get('datatrans.identifier'), '24600', 'CHF'); $this->assertText($generated_sign); - // Finish and save payment + // Finish and save payment. $this->drupalPostForm(NULL, array(), t('Submit')); - // Check out the payment overview page + // Check out the payment overview page. $this->drupalGet('admin/content/payment'); $this->assertText('Failed'); $this->assertNoText('Success'); - // Check for detailed payment information + // Check for detailed payment information. $this->drupalGet('payment/1'); $this->assertText('Failed'); $this->assertNoText('Success'); + + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment error response: POST'); } /** * Tests cancelled Datatrans payment. */ - function testDatatransCancelPayment() { + public function testDatatransCancelPayment() { // Modifies the datatrans configuration for testing purposes. $generator = \Drupal::urlGenerator(); $datatrans_configuration = array( @@ -278,6 +303,7 @@ function testDatatransCancelPayment() { 'plugin_form[security][hmac_key]' => '1234', // For failed test we give a wrong hmac_key 'plugin_form[security][hmac_key_2]' => '', + 'plugin_form[debug]' => TRUE, ); $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_datatrans', $datatrans_configuration, t('Save')); @@ -291,67 +317,73 @@ function testDatatransCancelPayment() { // Check for incorrect sign. $this->assertNoText('sign309dd30ad0cb07770d3a1ffda64585a9'); - // Check out the payment overview page + // Check out the payment overview page. $this->drupalGet('admin/content/payment'); $this->assertText('Cancelled'); $this->assertNoText('Failed'); $this->assertNoText('Success'); - // Check for detailed payment information + // Check for detailed payment information. $this->drupalGet('payment/1'); $this->assertText('Cancelled'); $this->assertNoText('Failed'); $this->assertNoText('Success'); + + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment cancel response: POST'); } /** - * Calculates the total amount + * Calculates the total amount. + * + * @param int $amount + * Base amount. + * @param int $quantity + * Quantity. + * @param string $currency_code + * Currency code. * - * @param $amount - * Base amount - * @param $quantity - * Quantity - * @param $currency_code - * Currency code * @return int - * Returns the total amount + * Returns the total amount. */ - function calculateAmount($amount, $quantity, $currency_code) { + public function calculateAmount($amount, $quantity, $currency_code) { $base_amount = $amount * $quantity; $currency = Currency::load($currency_code); return intval($base_amount * $currency->getSubunits()); } /** - * Generates the sign + * Generates the sign. + * + * @param string $hmac_key + * Hmac key. + * @param string $merchant_id + * Merchant ID. + * @param string $identifier + * The identifier. + * @param int $amount + * The order amount. + * @param string $currency + * Currency Code. * - * @param $hmac_key - * hmac key - * @param $merchant_id - * Merchant ID - * @param $identifier - * @param $amount - * The order amount - * @param $currency - * Currency Code * @return string - * Returns the sign + * Returns the sign. */ - function generateSign($hmac_key, $merchant_id, $identifier, $amount, $currency) { + public function generateSign($hmac_key, $merchant_id, $identifier, $amount, $currency) { $hmac_data = $merchant_id . $amount . $currency . $identifier; return hash_hmac('md5', $hmac_data, pack('H*', $hmac_key)); } /** - * Adds the payment field to the node + * Adds the payment field to the node. * * @param NodeTypeInterface $type - * Node type interface type - * + * Node type interface type. * @param string $label - * Field label + * Field label. * * @return \Drupal\Core\Entity\EntityInterface|static + * Returns the payment form field. */ function addPaymentFormField(NodeTypeInterface $type, $label = 'Payment Label') { $field_storage = FieldStorageConfig::create(array( From 86edc30036182337093f3017c2ab7420406bfd6d Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Tue, 30 Jun 2015 15:31:15 +0200 Subject: [PATCH 7/8] corrected past logging, removed leftover line which caused errors --- src/Controller/DatatransResponseController.php | 14 ++++++-------- .../MethodConfiguration/DatatransConfiguration.php | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index b478adf..e670281 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -35,12 +35,11 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme try { if (\Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']) { if (\Drupal::moduleHandler()->moduleExists('past')) { - past_event_save('datatrans', 'Success response - POST data', $request->request->all()); + past_event_save('datatrans', 'response_success', 'Success response - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); } else { - \Drupal::logger('datatrans')->info(t('Payment success response: @response', ['@response' => $request])); + \Drupal::logger('datatrans')->debug(t('Payment success response: @response', ['@response' => $request->request->all()])); } - drupal_set_message(t('Payment success response: @response', ['@response' => implode(', ', $request->request->all())])); } // This needs to be checked to match the payment method settings // ND being valid with its keys and data. @@ -110,10 +109,10 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme 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', 'Error response - POST data', $request); + past_event_save('datatrans', 'response_error', 'Error response - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); } else { - \Drupal::logger('datatrans')->info(t('Payment error response: @response', ['@response' => $request])); + \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())])); } @@ -137,12 +136,11 @@ public function processErrorResponse(Request $request, 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', 'Cancel response - POST data', $request); + past_event_save('datatrans', 'response_cancel', 'Cancel response - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); } else { - \Drupal::logger('datatrans')->info(t('Payment cancel response: @response', ['@response' => $request])); + \Drupal::logger('datatrans')->info(t('Payment cancel response: @response', ['@response' => $request->request->all()])); } - drupal_set_message(t('Payment cancel response: @response', ['@response' => implode(', ', $request->request->all())])); } drupal_set_message(t('Payment cancelled.'), 'error'); return $this->savePayment($payment, 'payment_cancelled'); diff --git a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php index f1b7493..8755cea 100644 --- a/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php +++ b/src/Plugin/Payment/MethodConfiguration/DatatransConfiguration.php @@ -455,7 +455,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#title' => 'Log response from Datatrans server', '#default_value' => $this->getDebug(), ); - \Drupal::config('payment.payment_method_configuration.payment_datatrans')->get('pluginConfiguration')['debug']; + return $form; } From 58be8e71fd27b51a6f5e6d722d6c66788001957b Mon Sep 17 00:00:00 2001 From: Lukas Schneider Date: Tue, 30 Jun 2015 15:55:08 +0200 Subject: [PATCH 8/8] added payment id to error message --- src/Controller/DatatransResponseController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Controller/DatatransResponseController.php b/src/Controller/DatatransResponseController.php index e670281..1c06a64 100644 --- a/src/Controller/DatatransResponseController.php +++ b/src/Controller/DatatransResponseController.php @@ -35,7 +35,7 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme 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 - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + 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()])); @@ -109,7 +109,7 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme 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 - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + 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()])); @@ -136,7 +136,7 @@ public function processErrorResponse(Request $request, 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 - POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + 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()]));