Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging of responses from postfinance server #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 @@ -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
3 changes: 3 additions & 0 deletions config/schema/payment_postfinance_payment_form.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/Controller/PostfinanceResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function defaultConfiguration() {
'language' => '',
'sha_in_key' => '',
'sha_out_key' => '',
'debug' => FALSE,
);
}

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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;
Expand All @@ -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']);
}

}
36 changes: 35 additions & 1 deletion src/Tests/PostfinancePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PostfinancePaymentTest extends WebTestBase {
'payment_postfinance_test',
'node',
'field_ui',
'dblog',
);

/**
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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'));

Expand Down Expand Up @@ -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:');
}

/**
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand Down