Skip to content
Snippets Groups Projects
Commit b30a80fc authored by Panagiotis Moutsopoulos's avatar Panagiotis Moutsopoulos
Browse files

Issue #3103704 by idimopoulos: Mock server for testing purposes

parent a01fee67
No related branches found
No related tags found
No related merge requests found
# Mock server AlphaBank
This module is a simplistic form that enables to test the responses from the AlphaBank endpoint.
This is build upon the implementation of
[commerce_alphabank_redirect](http://drupal.org/project/commerce_alphabank_redirect) so it might
not work outside and is meant only for tests.
## How it works
The only thing needed is to switch the service url in the AlphaBank payment method to not point to
AlphaBank but to `<base url of your website>/mock-server-alphabank`.
The module offers a form where the user can simply select if the transaction is successful, failed or cancelled.
By clicking one of the corresponding data, the user is then redirected to the result page of the commerce corresponding
module.
## Disclaimer
The module does not check for the validity of the data nor does it guarantee to return alphanumeric strings that are
according to the official (unknown) algorithms of the bank.
The module does not check the validity of the digest either as this is being calculated automatically by the
`commerce_alphabank_redirect` module.
Drupal.behaviors.mock_server_alphabank = {
attach: function(context, settings) {
document.getElementById('mock-server-alphabank-endpoint').submit();
}
};
name = AlphaBank GR - Mock server
description = Mocks the AlphaBank endpoint by returning mock responses. For test purposes only.
package = Commerce - Payment
core = 7.x
dependencies[] = commerce_alphabank_redirect
<?php
/**
* @file
* Contains functions and methods for the mock_server_alphabank module.
*/
/**
* Implements hook_menu().
*/
function mock_server_alphabank_menu() {
$items['mock-server-alphabank'] = [
'title' => 'AlphaBank endpoint',
'page callback' => 'drupal_get_form',
'page arguments' => ['mock_server_alphabank_endpoint'],
'access arguments' => ['access checkout'],
'type' => MENU_NORMAL_ITEM,
];
return $items;
}
/**
* Form callback for the 'mock-server-alphabank' menu item.
*
* @param array $form
* The form array.
* @param array $form_state
* The form state array.
*
* @return array
* The final form array.
*/
function mock_server_alphabank_endpoint($form, &$form_state) {
$data = $_POST;
foreach (mock_server_alphabank_get_request_fields() as $field) {
if (isset($data[$field])) {
$form[$field] = [
'#type' => 'hidden',
'#value' => $data[$field],
];
}
}
// If the form was submitted and the op exists and the value 'return_url' is
// set, construct a form that will redirect to the final success/error screen.
if (isset($form_state['triggering_element']) && !empty($form_state['values']['return_url'])) {
foreach (mock_server_alphabank_get_response_fields() as $field) {
// Do not override previously inserted values.
// @todo: Are we sure about it?
if (isset($form_state['values'][$field]) && !isset($form[$field])) {
$form[$field] = [
'#type' => 'hidden',
'#value' => $form_state['values'][$field],
];
}
}
$form['help'] = [
'#markup' => t('Wait a few seconds until your are redirected back.<br />If you are not redirected after a few seconds, please, press "Go back" below.'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Go back',
];
$form['#attached']['js'][] = drupal_get_path('module', 'mock_server_alphabank') . '/js/form-submit.js';
$form['#method'] = 'POST';
$form['#action'] = $form_state['values']['return_url'];
}
else {
foreach (['success', 'error', 'cancel'] as $action) {
$form[$action] = [
'#type' => 'submit',
'#value' => "Return with {$action}",
'#name' => $action,
];
}
}
$form['#attributes']['class'][] = 'mock_server_alphabank_form';
return $form;
}
/**
* Validation callback for the 'mock-server-alphabank' menu item form.
*/
function mock_server_alphabank_endpoint_validate($form, &$form_state) {
$button = $form_state['triggering_element'];
$form_state['values']['return_url'] = $button['#name'] === 'success' ? $form_state['values']['confirmUrl'] : $form_state['values']['cancelUrl'];
// Keep the length low for easier debugging.
$form_state['values']['txId'] = 'tx_' . user_password(4);
$form_state['values']['paymentRef'] = 'ref_' . user_password(4);
switch ($button['#name']) {
case 'success':
$form_state['values']['status'] = 'CAPTURED';
break;
case 'cancel':
$form_state['values']['status'] = 'CANCELLED';
break;
default:
$form_state['values']['status'] = 'ERROR';
break;
}
$form_state['values']['paymentTotal'] = $form_state['values']['orderAmount'];
$form_state['rebuild'] = TRUE;
}
/**
* Returns a list of field names that the alpha bank mock server receives.
*
* Note that this is the list of the currently supported fields.
*
* @return array
* An array of field names.
*
* @see: \commerce_alphabank_redirect_order_form().
*/
function mock_server_alphabank_get_request_fields() {
return [
'mid',
'orderid',
'orderDesc',
'orderAmount',
'currency',
'payerEmail',
'trType',
'confirmUrl',
'cancelUrl',
'digest',
];
}
/**
* Returns a list of field names that the alpha bank mock server should return.
*
* @return array
* An array of field names.
*/
function mock_server_alphabank_get_response_fields() {
return [
'mid',
'orderid',
'status',
'orderAmount',
'currency',
'paymentTotal',
'message',
'riskScore',
'payMethod',
'txId',
'paymentRef',
'digest',
'status',
];
}
<?php
/**
* @file
* Contains test data and helper methods for the mock_server_alphabank module.
*/
/**
* Returns a list of test data.
*
* @return array
* A list of test data. Each index is an array of test data that match the
* specifications from AlphaBank.
*/
function mock_server_alphabank_get_test_data() {
return [
// Returns a success response.
'mickey_mouse' => [
'cardholder' => 'Mickey Mouse',
'card_number' => '0000111122223333',
'expiry_date' => '01/44',
'ccv' => '000'
],
// Returns a failure response.
'donald_duck' => [
'cardholder' => 'Donald Duck',
'card_number' => '3333222211110000',
'expiry_date' => '01/44',
'ccv' => '000'
],
];
}
/**
* Returns the client data with the response array.
*
* @param string|null $card_number
* The card number.
* @param string|null $code_name
* The code name.
*
* @return array|null
* The client entry with the response code or null if none is found.
*/
function mock_server_alphabank_find_test_data($card_number = NULL, $code_name = NULL) {
$data = mock_server_alphabank_get_test_data();
if (!empty($card_number)) {
foreach ($data as $code_name => $entry) {
if ($entry['card_number'] === $card_number) {
return $entry;
}
}
}
elseif (!empty($code_name) && isset($data[$code_name])) {
return $data[$code_name];
}
return NULL;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment