log into TPVController

parent c53cf1dd
Showing with 35 additions and 16 deletions
......@@ -116,30 +116,41 @@ module.exports = {
* Notification of payment from RedSys
*/
notify: function(req, res) {
console.log("starting notify method");
var params = req.allParams();
if (!params.Ds_Signature || !params.Ds_MerchantParameters)
if (!params.Ds_Signature || !params.Ds_MerchantParameters){
console.log("bad request");
return res.badRequest();
}
var postSignature = params.Ds_Signature;
var postParams = params.Ds_MerchantParameters;
var postParams = params.Ds_MerchantParameters;
var decodedParams = decodeMerchantParams(postParams);
var ownParams = decodeMerchantParams(decodedParams.Ds_MerchantData);
var decodedParams = decodeMerchantParams(postParams);
var ownParams = decodeMerchantParams(decodedParams.Ds_MerchantData);
var key = cipherMerchantKey(decodedParams.Ds_Order, sails.config.pictogram.tpv.key);
var signatureBase64 = signMerchantParams(postParams, key);
if (postSignature != signatureBase64) {
console.log("postSignature != signatureBase64 (1)");
// Try again if some URLSafe replacement has been done
signatureBase64 = signatureBase64.replace(/\+/gi, "-"); // Change any plus (+) characters to dashes (-)
signatureBase64 = signatureBase64.replace(/\//gi, "_"); // Change any slashes (/) characters to underscores (_)
if (postSignature != signatureBase64) {
// Try again if some URLSafe replacement has been done
signatureBase64 = signatureBase64.replace(/\+/gi, "-"); // Change any plus (+) characters to dashes (-)
signatureBase64 = signatureBase64.replace(/\//gi, "_"); // Change any slashes (/) characters to underscores (_)
if (postSignature != signatureBase64) {
console.log("postSignature != signatureBase64 (2)");
sails.log.debug("tpv/notify: Signature verification on TPV operation failed");
return res.badRequest("Invalid signature");
}
}
}
}
// Everything is correct: generate serial number and activate it
......@@ -148,6 +159,7 @@ module.exports = {
// Generate new license and use ID for order
//
new Promise((resolve, reject) => {
console.log("new promise");
License.genLicenseNumber((number) => {
if (number)
resolve(number);
......@@ -156,6 +168,7 @@ module.exports = {
});
})
.then((number) => {
console.log("then number");
return License.create({
number: number,
duration: ownParams.type == 'forever' ? 1200 : 12,
......@@ -163,14 +176,20 @@ module.exports = {
});
})
.then((license) => {
console.log("then license");
if (!license)
if (!license){
console.log("not license");
throw new Error("Unable to create license");
}
License.activate(license.number, parseInt(ownParams.id_stu), function(err, license) {
if (err)
return res.badRequest(err);
sails.log.debug("New license sold! " + license.number);
License.activate(license.number, parseInt(ownParams.id_stu), function(err, license) {
if (err){
console.log("activate license err");
return res.badRequest(err);
}
console.log("res ok license");
sails.log.debug("New license sold! " + license.number);
return res.ok(license);
});
})
......
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