diff --git a/config/install/payment.payment_method_configuration.payment_saferpay_payment_form.yml b/config/install/payment.payment_method_configuration.payment_saferpay_payment_form.yml index 4d40791..ed09b34 100644 --- a/config/install/payment.payment_method_configuration.payment_saferpay_payment_form.yml +++ b/config/install/payment.payment_method_configuration.payment_saferpay_payment_form.yml @@ -8,5 +8,6 @@ pluginConfiguration: account_id: 99867-94913159 spPassword: XAjc3Kna settle_option: 1 + debug: FALSE pluginId: payment_saferpay_payment_form status: true diff --git a/config/schema/payment_saferpay.schema.yml b/config/schema/payment_saferpay.schema.yml index 1ad3659..d2ed7b9 100644 --- a/config/schema/payment_saferpay.schema.yml +++ b/config/schema/payment_saferpay.schema.yml @@ -13,6 +13,9 @@ payment.plugin_configuration.payment_method_configuration.payment_saferpay_payme settle_option: type: integer label: Settle option + debug: + type: boolean + label: Log responses payment_saferpay.settings: type: mapping diff --git a/src/Controller/SaferpayResponseController.php b/src/Controller/SaferpayResponseController.php index 3d8c135..bf2af63 100644 --- a/src/Controller/SaferpayResponseController.php +++ b/src/Controller/SaferpayResponseController.php @@ -27,11 +27,13 @@ class SaferpayResponseController { * * @param \Drupal\payment\Entity\Payment $payment * The payment entity type. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request from the server. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * Redirect Response. */ - public function processMPIResponse(Payment $payment) { + public function processMPIResponse(Payment $payment, Request $request) { $data = simplexml_load_string($_GET['DATA']); if ($data['RESULT'] != 0) { drupal_set_message(t('Payment failed: @message', array('@message' => $data['MESSAGE']))); @@ -75,15 +77,24 @@ public function processMPIResponse(Payment $payment) { * * @param \Drupal\payment\Entity\Payment $payment * The payment entity type. + * @param \Symfony\Component\HttpFoundation\Request $request + * The request from the server. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * Redirect Response. */ - public function processSCDResponse(Payment $payment) { + public function processSCDResponse(Payment $payment, Request $request) { $scd_response = simplexml_load_string($_GET['DATA']); if ($scd_response['RESULT'] != 0) { drupal_set_message(t('Credit card verification failed: @error.', array('@error' => $scd_response['DESCRIPTION'])), 'error'); - \Drupal::logger(t('Credit card verification failed: @error'), array('@error' => $scd_response['DESCRIPTION']))->warning('SaferpayResponseController.php'); + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_fail', 'Fail response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment fail response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } return new RedirectResponse('payment/' . $payment->id()); } @@ -184,7 +195,14 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme // If the verification failed, return with an error. if (!(substr($verify_pay_confirm_callback, 0, 2) == 'OK')) { - \Drupal::logger(t('Payment verification failed: @error'), array('@error' => $verify_pay_confirm_callback))->warning('SaferpayResponseController.php'); + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_fail', 'Fail response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment fail response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } drupal_set_message(t('Payment verification failed: @error.', array('@error' => $verify_pay_confirm_callback)), 'error'); return $this->savePayment($payment, 'payment_failed'); } @@ -200,11 +218,26 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme // If the response to our request is anything but OK the payment fails. if (!($settle_payment_callback == 'OK')) { - \Drupal::logger(t('Payment settlement failed: @error'), array('@error' => $settle_payment_callback))->warning('SaferpayResponseController.php'); + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_fail', 'Fail response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment fail response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } drupal_set_message(t('Payment settlement failed: @error.', array('@error' => $settle_payment_callback)), 'error'); return $this->savePayment($payment, 'payment_failed'); } } + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_success', 'Success response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment success response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } return $this->savePayment($payment, 'payment_success'); } @@ -222,7 +255,14 @@ public function processSuccessResponse(Request $request, PaymentInterface $payme */ public function processFailResponse(Request $request, PaymentInterface $payment) { drupal_set_message('Payment failed'); - \Drupal::logger('Payment settlement failed')->warning('SaferpayResponseController.php'); + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_fail', 'Fail response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment fail response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } return $this->savePayment($payment, 'payment_failed'); } @@ -237,7 +277,14 @@ public function processFailResponse(Request $request, PaymentInterface $payment) public function processBackResponse(Request $request, PaymentInterface $payment) { $this->savePayment($payment, 'payment_cancelled'); drupal_set_message('Payment cancelled'); - \Drupal::logger('Payment cancelled')->alert('SaferpayResponseController.php'); + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_cancel', 'Cancel response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment cancel response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } } /** @@ -260,7 +307,14 @@ public function processBackResponse(Request $request, PaymentInterface $payment) */ public function processNotifyResponse(Request $request, PaymentInterface $payment) { $this->savePayment($payment, 'payment_config'); - + if (\Drupal::config('payment.payment_method_configuration.payment_saferpay_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('saferpay', 'response_notify', 'Notify response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('saferpay')->debug(t('Payment notify response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } // @todo: Logger & drupal_set_message payment config. } diff --git a/src/Plugin/Payment/MethodConfiguration/SaferpayPaymentFormConfiguration.php b/src/Plugin/Payment/MethodConfiguration/SaferpayPaymentFormConfiguration.php index 40e2d4f..0051995 100644 --- a/src/Plugin/Payment/MethodConfiguration/SaferpayPaymentFormConfiguration.php +++ b/src/Plugin/Payment/MethodConfiguration/SaferpayPaymentFormConfiguration.php @@ -80,6 +80,7 @@ public function defaultConfiguration() { 'account_id' => '99867-94913159', 'spPassword' => 'XAjc3Kna', 'settle_option' => TRUE, + 'debug' => FALSE, ); } @@ -158,6 +159,23 @@ public function getSettleOption() { return $this->configuration['settle_option']; } + /** + * En/disables logging the response from Saferpay. + * + * @param bool $state + * Whether debugging should be dis/enabled. + */ + public function setDebug($state = TRUE) { + $this->configuration['debug'] = $state; + } + + /** + * Returns the logging setting for Saferpay. + */ + public function getDebug() { + return $this->configuration['debug']; + } + /** * {@inheritdoc} */ @@ -186,6 +204,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#default_value' => $this->getSettleOption(), ); + $form['debug'] = array( + '#type' => 'checkbox', + '#title' => 'Log response from Postfinance server', + '#default_value' => $this->getDebug(), + ); + return $form; } @@ -202,7 +226,8 @@ public function formElementsValidate(array $element, FormStateInterface $form_st $this->setAccountId($values['account_id']) ->setSpPassword($values['spPassword']) - ->setSettleOption($values['settle_option']); + ->setSettleOption($values['settle_option']) + ->setDebug($values['debug']); } } diff --git a/src/Tests/SaferpayPaymentFormTest.php b/src/Tests/SaferpayPaymentFormTest.php index b382f33..ce63db3 100644 --- a/src/Tests/SaferpayPaymentFormTest.php +++ b/src/Tests/SaferpayPaymentFormTest.php @@ -29,7 +29,8 @@ class SaferpayPaymentFormTest extends WebTestBase { // @todo: Check if you need these 'node', 'field_ui', - 'config' + 'config', + 'dblog', ); /** @@ -98,7 +99,8 @@ protected function setUp() { 'access content', 'access administration pages', 'access user profiles', - 'payment.payment.view.any' + 'payment.payment.view.any', + 'access site reports', )); $this->drupalLogin($this->admin_user); } @@ -121,6 +123,16 @@ function testSaferpaySuccessPayment() { ); $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_saferpay_payment_form', $saferpay_configuration, t('Save')); + $this->drupalGet('admin/config/services/payment/method/configuration/payment_saferpay_payment_form'); + // 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_saferpay_payment_form'); + $this->assertFieldChecked('edit-plugin-form-debug'); + // Create saferpay payment. $this->drupalPostForm('node/' . $this->node->id(), NULL, t('Pay')); @@ -166,6 +178,10 @@ function testSaferpaySuccessPayment() { $this->assertText('CHF 123.00'); $this->assertText('CHF 246.00'); $this->assertText('Completed'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment success response:'); } /** * Tests failed Saferpay payment. @@ -174,23 +190,28 @@ function testSaferpayFailedPayment() { // Modifies the saferpay configuration for testing purposes. $payment_config = \Drupal::configFactory()->getEditable('payment_saferpay.settings')->set('payment_link', $GLOBALS['base_root']); $payment_config->save(); + $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_saferpay_payment_form', ['plugin_form[debug]' => TRUE], t('Save')); - // Create saferpay payment + // Create saferpay payment. \Drupal::state()->set('saferpay.return_url_key', 'fail'); $this->drupalPostForm('node/' . $this->node->id(), array(), t('Pay')); - // 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'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment fail response:'); } /** * Tests succesfull Saferpay payment with wrong signature. @@ -200,6 +221,7 @@ function testSaferpayWrongSignature() { $payment_config = \Drupal::configFactory()->getEditable('payment_saferpay.settings')->set('payment_link', $GLOBALS['base_root']); \Drupal::state()->set('saferpay.signature', 'AAA'); $payment_config->save(); + $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_saferpay_payment_form', ['plugin_form[debug]' => TRUE], t('Save')); // Create saferpay payment $this->drupalPostForm('node/' . $this->node->id(), array(), t('Pay')); @@ -216,6 +238,10 @@ function testSaferpayWrongSignature() { $this->drupalGet('payment/1'); $this->assertText('Failed'); $this->assertNoText('Success'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment fail response:'); } /**