tpv working

parent 114d280f
......@@ -26,7 +26,7 @@ module.exports = {
},
/**
* Create a new Instruction, which is associated to the given method
* Create a new License, which is associated to the given method
* @param {request} req
* {
* duration (number of months)
......
......@@ -21,77 +21,87 @@ module.exports = {
return res.badRequest();
//
// Submit data
// Generate new license and use ID for order
//
var amount = sails.config.pictogram.tpv.prices.oneYearEuro;
if (params.type == 'forever')
amount = sails.config.pictogram.tpv.prices.foreverEuro;
var tpvdata =
{
DS_MERCHANT_PAYMETHODS: 'C', // Payment managed by RedSys
DS_MERCHANT_MERCHANTCODE: sails.config.pictogram.tpv.merchantCode, // Number of commerce (Yotta)
DS_MERCHANT_TERMINAL: '001', // Terminal number
DS_MERCHANT_CURRENCY: '978', // Terminal currency
DS_MERCHANT_TRANSACTIONTYPE: "0", // Type of the transaction (0: authorized)
DS_MERCHANT_AMOUNT: amount + "00", // Amount
DS_MERCHANT_ORDER: moment().format('YYMMDD') + params.id_stu,
DS_MERCHANT_MERCHANTURL: sails.getBaseUrl() + "/tpv/notify",
DS_MERCHANT_URLOK: sails.getBaseUrl() + '/app/#/student/' + params.id_stu + "/setup/renewed/1", // Returning URL (success)
DS_MERCHANT_URLKO: sails.getBaseUrl() + '/app/#/student/' + params.id_stu + "/setup/renewed/0" // Returning URL (error)
};
//
// Sign data
//
// Base64 encoding of parameters
var merchantWordArray = cryptojs.enc.Utf8.parse(JSON.stringify(tpvdata));
var merchantBase64 = merchantWordArray.toString(cryptojs.enc.Base64);
// Decode key
var keyWordArray = cryptojs.enc.Base64.parse(sails.config.pictogram.tpv.key);
// Generate transction key
var iv = cryptojs.enc.Hex.parse("00000000");
var cipher = cryptojs.TripleDES.encrypt(tpvdata.DS_MERCHANT_ORDER, keyWordArray, {
iv:iv,
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.NoPadding
});
// Sign
var signature = cryptojs.HmacSHA256(merchantBase64, cipher.ciphertext);
var signatureBase64 = signature.toString(cryptojs.enc.Base64);
// Done, we can return response
var response = {
signatureVersion: "HMAC_SHA256_V1",
merchantParameters: merchantBase64,
signature: signatureBase64
};
sails.log.debug(JSON.stringify(response));
res.ok(response);
new Promise((resolve, reject) => {
License.genLicenseNumber((number) => {
if (number)
resolve(number);
else
reject();
});
})
.then((number) => {
return License.create({
number: number,
duration: params.type == 'forever' ? 1200 : 12,
creator: req.token.email
});
})
.then((license) => {
if (!license)
throw new Error("Unable to create license");
//
// Prepare submit data
//
var amount = sails.config.pictogram.tpv.prices.oneYearEuro;
if (params.type == 'forever')
amount = sails.config.pictogram.tpv.prices.foreverEuro;
var tpvdata =
{
DS_MERCHANT_PAYMETHODS: 'C', // Payment managed by RedSys
DS_MERCHANT_MERCHANTCODE: sails.config.pictogram.tpv.merchantCode, // Number of commerce (Yotta)
DS_MERCHANT_TERMINAL: '001', // Terminal number
DS_MERCHANT_CURRENCY: '978', // Terminal currency
DS_MERCHANT_TRANSACTIONTYPE: "0", // Type of the transaction (0: authorized)
DS_MERCHANT_AMOUNT: amount + "00", // Amount
DS_MERCHANT_ORDER: moment().format('YYMMDD') + license.id.toString().slice(-6), // hast to be from 4 to 12 characters long
DS_MERCHANT_MERCHANTURL: sails.getBaseUrl() + "/tpv/notify", // not used
DS_MERCHANT_URLOK: sails.getBaseUrl() + '/app/#/student/' + params.id_stu + "/setup/renewed/" + license.number, // Returning URL (success)
DS_MERCHANT_URLKO: sails.getBaseUrl() + '/app/#/student/' + params.id_stu + "/setup/renewed/0" // Returning URL (error)
};
console.log("Data: " + JSON.stringify(tpvdata));
//
// Sign submit data
//
// Base64 encoding of parameters
var merchantWordArray = cryptojs.enc.Utf8.parse(JSON.stringify(tpvdata));
var merchantBase64 = merchantWordArray.toString(cryptojs.enc.Base64);
// Decode key
var keyWordArray = cryptojs.enc.Base64.parse(sails.config.pictogram.tpv.key);
// Generate transaction key
var iv = cryptojs.enc.Hex.parse("0000000000000000");
var cipher = cryptojs.TripleDES.encrypt(tpvdata.DS_MERCHANT_ORDER, keyWordArray, {
iv:iv,
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.ZeroPadding
});
// Sign
var signature = cryptojs.HmacSHA256(merchantBase64, cipher.ciphertext);
var signatureBase64 = signature.toString(cryptojs.enc.Base64);
// Done, we can return response
var response = {
signatureVersion: "HMAC_SHA256_V1",
merchantParameters: merchantBase64,
signature: signatureBase64
};
sails.log.debug(JSON.stringify(response));
res.ok(response);
})
.catch((err) => {return res.serverError(err)});
},
/**
* Notification of transaction from RedSys
*/
notify: function(req, res) {
var params = req.allParams();
var received_signature = params.Ds_Signature;
var calculated_signature = nacl_util.encodeBase64(sha256.hmac(sails.config.pictogram.tpv.key, params.Ds_MerchantParameters));
if (received_signature == calculated_signature) {
console.log("OK");
// TBD: CREAR NUEVA LICENCIA
}
else
console.log("ERROR");
console.log(JSON.stringify(params));
notify: function(res, req) {
return res.ok();
}
};
......@@ -151,7 +151,7 @@ module.exports = {
return found;
},
function () {
callback(license);
return callback(license);
}
);
},
......
......@@ -207,6 +207,7 @@
"license_expired_official": "PRO license expired. To maintain access to therapeutical functionalities you can",
"license_expired_trial": "Trial license expired. To keep the account you should",
"license_expired_buy": "buy one license",
"license_trial_buy": "Buy an official one",
"license_expired_renew": "renew it",
"license_expires_official": "PRO license expires on ",
"license_expires_trial": "Trial license expires on ",
......@@ -349,6 +350,7 @@
"reloading_pictos": "Reloading pictograms",
"reload_scene": "Reloading scene",
"remember": "Remember me",
"renew": "renew license",
"reports": "Reports",
"request_change_password": "Request change password",
"resume": "Resume",
......@@ -461,6 +463,7 @@
"tpl_date_frame": "de {{ begin | date:'dd-MM-yyyy' }} a {{ end | date:'dd-MM-yyyy' }}",
"tpl_day": "{{ day | date:'yyyy-MM-dd' }}",
"tpl_hours_frame": "from {{ begin | date:'HH:mm:ss' }} to {{ end | date:'HH:mm:ss' }}",
"tpv_error": "The acquisition of a new license has not been successful",
"tpv_init_error": "Unable to initiate payment process",
"tpv_success": "The new license is now active",
"tpv_title": "Select license type",
......
......@@ -218,6 +218,7 @@
"license_warning": "Sólo disponible para licencias profesionales (Pictogram Pro).",
"license_missing_official": "La licencia PRO expiró. El acceso a las funcionalidades terapéuticas queda restringido, aunque puede seguir gestionando pictogramas y dispositivos.",
"license_missing_trial": "La licencia de prueba expiró. Adquiera una licencia oficial para mantener esta cuenta (puede hacerlo en Configuración).",
"license_trial_buy": "Adquirir licencia oficial",
"licenses_created": "Licencias creadas",
"light_up": "Iluminar",
"link": "Vincular",
......@@ -347,6 +348,7 @@
"reload_scene": "Recargando escena",
"reloading_pictos": "Recargando pictogramas",
"remember": "No cerrar sesión",
"renew": "renovar licencia",
"reports": "Informes",
"request_change_password": "Solicitar cambio de contraseña",
"resume": "Continuar",
......@@ -459,6 +461,7 @@
"tpl_date_frame": "de {{ begin | date:'dd-MM-yyyy' }} a {{ end | date:'dd-MM-yyyy' }}",
"tpl_day": "{{ day | date:'dd-MM-yyyy' }}",
"tpl_hours_frame": "de {{ begin | date:'HH:mm:ss' }} a {{ end | date:'HH:mm:ss' }}",
"tpv_error": "La adquisición de una nueva licencia no se ha completado con éxito",
"tpv_init_error": "No se pudo iniciar el proceso de compra",
"tpv_success": "La nueva licencia ha sido activada",
"tpv_title": "Seleccione el tipo de licencia",
......
......@@ -167,6 +167,12 @@ dashboardApp.config(function ($stateProvider, $urlRouterProvider) {
templateUrl: 'modules/student/views/setup.html',
controller: 'StudentSetupCtrl',
})
.state('student_renew', {
url: '/student/:idStudent/setup/renewed/:serial',
parent: 'student',
templateUrl: 'modules/student/views/setup.html',
controller: 'StudentSetupCtrl',
})
// Abstract page for admin
.state('admin', {
templateUrl: 'modules/admin/views/admin.html',
......
......@@ -21,13 +21,7 @@ dashboardControllers.controller('StudentSetupCtrl', function StudentSetupCtrl(
// For tab navigation (here too, if the user refresh the page...)
$scope.nav.tab = 'setup';
// If we get with this parameter, we're back from a TPV transaction!
if ($stateParams.renewed && $stateParams.renewed == 1)
ngToast.success($translate.instant('tpv_success'));
if ($stateParams.renewed && $stateParams.renewed == 0)
ngToast.success($translate.instant('tpv_error'));
//
//
// Student setup
//
......@@ -265,8 +259,21 @@ dashboardControllers.controller('StudentSetupCtrl', function StudentSetupCtrl(
// ---------------------------------------------------------------------------
// TPV modal window
//
// TPV interaction
// ---------------------------------------------------------------------------
// If we get with this parameter, we're back from a TPV transaction!
console.log(JSON.stringify($stateParams));
if ($stateParams.serial && $stateParams.serial == 0)
ngToast.danger($translate.instant('tpv_error'));
else {
$scope.formUser.license_number = $stateParams.serial;
$scope.updateStudent();
}
/**
* Opens the modal window to select and buy license
*/
$scope.openBuyModal = function() {
var modalInstance = $modal.open({
animation: true,
......
......@@ -69,7 +69,8 @@
<div class="row">
<div class="col-xs-2"><i class="fa fa-flask fa-lg" aria-hidden="true"></i></div>
<div class="col-xs-10">
{{ 'license_expires_trial' | translate }} <b>{{ studentData.expiration_date }}</b>
{{ 'license_expires_trial' | translate }} <b>{{ studentData.expiration_date }}</b><br>
<a ng-click="openBuyModal()">{{ 'license_trial_buy' | translate }}</a>
</div>
</div>
</div>
......@@ -79,7 +80,7 @@
<div class="row">
<div class="col-xs-2"><i class="fa fa-certificate fa-lg text-danger" aria-hidden="true"></i></div>
<div class="col-xs-10">
{{ 'license_expired_official' | translate }} <a href="http://pictogramweb.com/caracteristicas-de-pictogram/">{{ 'license_expired_renew' | translate }}</a>.
{{ 'license_expired_official' | translate }} <a ng-click="openBuyModal()">{{ 'license_expired_renew' | translate }}</a>.
</div>
</div>
</div>
......@@ -89,7 +90,8 @@
<div class="row">
<div class="col-xs-2"><i class="fa fa-flask fa-lg text-danger" aria-hidden="true"></i></div>
<div class="col-xs-10">
{{ 'license_expired_trial' | translate }} <a href="http://pictogramweb.com/caracteristicas-de-pictogram/">{{ 'license_expired_buy' | translate }}</a>.
{{ 'license_expired_trial' | translate }} <a ng-click="openBuyModal()">{{ 'license_expired_buy' | translate }}
</a>
</div>
</div>
</div>
......@@ -101,14 +103,12 @@
<div class="col-xs-10">
<h4 class="alert-heading"> {{ 'license_missing' | translate }} </h4>
<p>
<a href="http://pictogramweb.com/caracteristicas-de-pictogram/">{{ 'more_info' | translate }}</a>
<a ng-click="openBuyModal()">{{ 'license_expired_buy' | translate }}</a>
</p>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" ng-click="openBuyModal()" translate>renew</button>
</div>
</fieldset>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment