diff --git a/config/install/payment.payment_method_configuration.payment_postfinance_payment_form.yml b/config/install/payment.payment_method_configuration.payment_postfinance_payment_form.yml index 4f54cc3..64614b4 100644 --- a/config/install/payment.payment_method_configuration.payment_postfinance_payment_form.yml +++ b/config/install/payment.payment_method_configuration.payment_postfinance_payment_form.yml @@ -8,6 +8,6 @@ pluginConfiguration: language: 'en_US' sha_in_key: '' sha_out_key: '' + debug: FALSE pluginId: payment_postfinance_payment_form status: true -uuid: 324fad23-d1da-4059-b621-3378fa67b480 diff --git a/config/schema/payment_postfinance_payment_form.schema.yml b/config/schema/payment_postfinance_payment_form.schema.yml index 802ccdb..5d58524 100644 --- a/config/schema/payment_postfinance_payment_form.schema.yml +++ b/config/schema/payment_postfinance_payment_form.schema.yml @@ -10,6 +10,9 @@ payment.plugin_configuration.payment_method_configuration.payment_postfinance_pa sha_out_key: type: string label: SHA Out Key + debug: + type: boolean + label: Log repsonses language: type: string label: Language diff --git a/src/Controller/PostfinanceResponseController.php b/src/Controller/PostfinanceResponseController.php index 6eabdbc..09574bc 100644 --- a/src/Controller/PostfinanceResponseController.php +++ b/src/Controller/PostfinanceResponseController.php @@ -33,6 +33,15 @@ class PostfinanceResponseController { * The Response to the accepting request. */ public function processAcceptResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::config('payment.payment_method_configuration.payment_postfinance_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('postfinance', 'response_success', 'Success response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('postfinance')->debug(t('Payment success response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } + // The definition of the plugin implementation. $plugin_definition = $payment->getPaymentMethod()->getPluginDefinition(); @@ -78,6 +87,14 @@ public function processAcceptResponse(Request $request, PaymentInterface $paymen * The Response to the accepting request. */ public function processDeclineResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::config('payment.payment_method_configuration.payment_postfinance_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('postfinance', 'response_decline', 'Decline response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('postfinance')->debug(t('Payment decline response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } $request_data = $request->query->all(); if ($request_data['STATUS'] == 2) { @@ -109,6 +126,14 @@ public function processDeclineResponse(Request $request, PaymentInterface $payme * The Response to the accepting request. */ public function processExceptionResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::config('payment.payment_method_configuration.payment_postfinance_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('postfinance', 'response_exception', 'Exception response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('postfinance')->debug(t('Payment exception response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } $request_data = $request->query->all(); if ($request_data['STATUS'] == 52 || $request_data['STATUS'] == 92) { @@ -140,6 +165,14 @@ public function processExceptionResponse(Request $request, PaymentInterface $pay * The Response to the accepting request. */ public function processCancelResponse(Request $request, PaymentInterface $payment) { + if (\Drupal::config('payment.payment_method_configuration.payment_postfinance_payment_form')->get('pluginConfiguration')['debug']) { + if (\Drupal::moduleHandler()->moduleExists('past')) { + past_event_save('postfinance', 'response_cancel', 'Cancel response - Payment ' . $payment->id() . ': POST data', ['POST' => $request->request->all(), 'Payment' => $payment]); + } + else { + \Drupal::logger('postfinance')->debug(t('Payment cancel response: @response', ['@response' => implode(', ', $request->request->all())])); + } + } $request_data = $request->query->all(); if ($request_data['STATUS'] == 1) { diff --git a/src/Plugin/Payment/MethodConfiguration/PostfinancePaymentFormConfiguration.php b/src/Plugin/Payment/MethodConfiguration/PostfinancePaymentFormConfiguration.php index 93bc3d0..59af100 100644 --- a/src/Plugin/Payment/MethodConfiguration/PostfinancePaymentFormConfiguration.php +++ b/src/Plugin/Payment/MethodConfiguration/PostfinancePaymentFormConfiguration.php @@ -80,6 +80,7 @@ public function defaultConfiguration() { 'language' => '', 'sha_in_key' => '', 'sha_out_key' => '', + 'debug' => FALSE, ); } @@ -150,6 +151,22 @@ public function setLanguage($language) { public function getLanguage() { return $this->configuration['language']; } + /** + * En/disable logging the response from Postfinance. + * + * @param bool $state + * Whether debugging should be dis/enabled. + */ + public function setDebug($state = TRUE) { + $this->configuration['debug'] = $state; + } + + /** + * Returns the state of the debug setting. + */ + public function getDebug() { + return $this->configuration['debug']; + } /** * {@inheritdoc} @@ -190,6 +207,12 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#default_value' => $this->getLanguage(), ); + $form['debug'] = array( + '#type' => 'checkbox', + '#title' => 'Log response from Postfinance server', + '#default_value' => $this->getDebug(), + ); + // @TODO: Create production and testing option. return $form; @@ -212,6 +235,7 @@ public function formElementsValidate(array $element, FormStateInterface $form_st $this->setShaInKey($values['sha_in_key']); $this->setShaOutKey($values['sha_out_key']); $this->setLanguage($values['language']); + $this->setDebug($values['debug']); } } diff --git a/src/Tests/PostfinancePaymentTest.php b/src/Tests/PostfinancePaymentTest.php index 03f681a..0a54df3 100644 --- a/src/Tests/PostfinancePaymentTest.php +++ b/src/Tests/PostfinancePaymentTest.php @@ -29,6 +29,7 @@ class PostfinancePaymentTest extends WebTestBase { 'payment_postfinance_test', 'node', 'field_ui', + 'dblog', ); /** @@ -110,6 +111,7 @@ protected function setUp() { 'access administration pages', 'access user profiles', 'payment.payment.view.any', + 'access site reports', )); $this->drupalLogin($this->admin_user); @@ -139,6 +141,16 @@ function testPostfinanceAcceptPayment() { \Drupal::state()->set('postfinance.return_url_key', 'ACCEPT'); \Drupal::state()->set('postfinance.testing', TRUE); + $this->drupalGet('admin/config/services/payment/method/configuration/payment_postfinance_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_postfinance_payment_form'); + $this->assertFieldChecked('edit-plugin-form-debug'); + // Create postfinance payment. $this->drupalPostForm('node/' . $this->node->id(), array(), t('Pay')); @@ -178,6 +190,10 @@ function testPostfinanceAcceptPayment() { $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:'); } /** @@ -186,7 +202,7 @@ function testPostfinanceAcceptPayment() { function testPostfinanceDeclinePayment() { // Set payment to decline. \Drupal::state()->set('postfinance.return_url_key', 'DECLINE'); - + $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_postfinance_payment_form', ['plugin_form[debug]' => TRUE], t('Save')); // Load payment configuration. $payment_config = $this->config('payment_postfinance.settings'); @@ -206,12 +222,19 @@ function testPostfinanceDeclinePayment() { $this->drupalGet('payment/1'); $this->assertNoText('Completed'); $this->assertText('Failed'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment decline response:'); } /** * Tests exception Postfinance payment. */ function testPostfinanceExceptionPayment() { + // Enable logging of responses. + $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_postfinance_payment_form', ['plugin_form[debug]' => TRUE], t('Save')); + // Set callback status to decline payment. \Drupal::state()->set('postfinance.callback_status', 52); \Drupal::state()->set('postfinance.testing', TRUE); @@ -230,12 +253,19 @@ function testPostfinanceExceptionPayment() { $this->drupalGet('payment/1'); $this->assertNoText('Completed'); $this->assertText('Failed'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment exception response:'); } /** * Tests cancel Postfinance payment. */ function testPostfinanceCancelPayment() { + // Enable logging of responses. + $this->drupalPostForm('admin/config/services/payment/method/configuration/payment_postfinance_payment_form', ['plugin_form[debug]' => TRUE], t('Save')); + // Set callback status to decline payment. \Drupal::state()->set('postfinance.callback_status', 1); \Drupal::state()->set('postfinance.testing', TRUE); @@ -254,6 +284,10 @@ function testPostfinanceCancelPayment() { $this->drupalGet('payment/1'); $this->assertNoText('Completed'); $this->assertText('Cancelled'); + + // Check the log for the response debug. + $this->drupalGet('/admin/reports/dblog'); + $this->assertText('Payment cancel response:'); } /** * Tests wrong signature validation in Postfinance payment.