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 @@ -8,5 +8,6 @@ pluginConfiguration:
account_id: 99867-94913159
spPassword: XAjc3Kna
settle_option: 1
debug: FALSE
pluginId: payment_saferpay_payment_form
status: true
3 changes: 3 additions & 0 deletions config/schema/payment_saferpay.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 62 additions & 8 deletions src/Controller/SaferpayResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])));
Expand Down Expand Up @@ -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());
}

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

Expand All @@ -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())]));
}
}
}

/**
Expand 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.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function defaultConfiguration() {
'account_id' => '99867-94913159',
'spPassword' => 'XAjc3Kna',
'settle_option' => TRUE,
'debug' => FALSE,
);
}

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

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

}
38 changes: 32 additions & 6 deletions src/Tests/SaferpayPaymentFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class SaferpayPaymentFormTest extends WebTestBase {
// @todo: Check if you need these
'node',
'field_ui',
'config'
'config',
'dblog',
);

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

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

/**
Expand Down