diff --git a/controllers/payment.js b/controllers/payment.js index 728935b..c92a883 100644 --- a/controllers/payment.js +++ b/controllers/payment.js @@ -33,100 +33,125 @@ exports.getOneUserPayments = async (req, res, next) => exports.saveUserPaymentInfos = async (req, res, next) => { - // exemple d'url : WP-infos.html?dom=NOM_DE_DOMAINE_DU_BOUTON&ref=ID_DU_USER&mt=MONTANT_TTC&cmd=CODE_COMMANDE_WEBPORTAGE&cl=NOM+DU+CLIENT&hKey=le_hash_en_md5 - // dom=wikilerni.com&ref=5&mt=24&cmd=de11de&cl=monsieur+dugenoux&hKey=998dccdef52bd27dc0a674941c0e9340 + // exemple d'url Ok : WP-infos.html?dom=wikilerni.com&ref=21&mt=24&cmd=de11de&cl=monsieur+dugenoux&hKey=1b42653d28ecd07b9b3ba202770a1a45 try { - require('dotenv').config(); - const db = require("../models/index"); - const md5 = require("md5"); - const montantsAbonnement=["12","24", "60", "120"]; - // !! attention req.query enlève les caractères spéciaux comme les "+" des paramètres de l'url. Il vaut donc mieux utiliser req.url pour comparer avec le hash au reste de la chaîne. - const testUrl=req.url.slice(req.url.indexOf("?")+1,req.url.lastIndexOf("&")); - console.log(testUrl); - console.log(md5(testUrl+process.env.MD5_WP)); - if(md5(testUrl+process.env.MD5_WP)!==req.query.hKey) // le hashage est effectué après le remplacement des caractères spéciaux dans l'url. - throw { message: txt.paymentUrlFail+testUrl }; - else if(req.query.ref==="" || montantsAbonnement.indexOf(req.query.mt) === -1) - throw { message: txt.paymentDatasFail+testUrl }; - else + // l'utilisateur étant incité à venir sur l'API, on le redirige vers le site + // ex url retour : /payment/WP-infos.html?article=nomArticle&ht=montantHT&client=nomClient + if(req.query.article!=undefined) { - const client=await userCtrl.searchUserById(req.query.ref); - if(!client) - throw { message: txt.paymentUserNotFound+testUrl }; + res.writeHead(302, { "Location": config.siteUrl+"/merci.html" }); + res.end(); + } + else + { + require('dotenv').config(); + const ndDaysSubscription=365;// nombre de jour de l'abonnement + const ndDaysGodFather=30;// nombre de jour en plus si parrainage + const db = require("../models/index"); + const md5 = require("md5"); + const montantsAbonnement=["12","24","60","120"]; + // !! attention req.query enlève les caractères spéciaux comme les "+" des paramètres de l'url. Il vaut donc mieux utiliser req.url pour comparer avec le hash au reste de la chaîne. + const testUrl=req.url.slice(req.url.indexOf("?")+1,req.url.lastIndexOf("&")); + console.log(testUrl); + console.log(md5(testUrl+process.env.MD5_WP)); + if(md5(testUrl+process.env.MD5_WP)!==req.query.hKey) // le hashage est effectué après le remplacement des caractères spéciaux dans l'url. + throw { message: txt.paymentUrlFail+testUrl }; + else if(req.query.ref==="" || montantsAbonnement.indexOf(req.query.mt) === -1) + throw { message: txt.paymentDatasFail+testUrl }; else { - // Si cet utilisateur a un parrain on le remercie et lui ajoute 30 jours d'abonnement - // Cela impacte aussi la durée de l'abonnement commandé par l'utilisateur - let numberOfDays=365; - if(client.User.GodfatherId) + const client=await userCtrl.searchUserById(req.query.ref); + if(!client) + throw { message: txt.paymentUserNotFound+testUrl }; + else { - const parrain=await userCtrl.searchUserById(client.User.GodfatherId); - if(parrain) + // Si cet utilisateur a un parrain on le remercie et lui ajoute 30 jours d'abonnement + // Cela impacte aussi la durée à ajouter à l'abonnement du client + let numberOfDays=ndDaysSubscription; + if(client.User.GodfatherId) { - parrain.Subscription.numberOfDays+=30; - numberOfDays+=30; - await db["Subscription"].update({ ...parrain.Subscription }, { where: { UserId : client.User.GodfatherId }, fields: ["numberOfDays"], limit:1 }); - userCtrl.creaUserJson(client.User.GodfatherId); - const mapMail = + const parrain=await userCtrl.searchUserById(client.User.GodfatherId); + if(parrain) { - USER_NAME: parrain.User.name - }; - const mailDatas= - { - mailSubject: txt.mailPaymentThankGodfatherSubject, - mailPreheader: txt.mailPaymentThankGodfatherSubject, - mailTitle: txt.mailPaymentThankGodfatherSubject, - mailHeaderLinkUrl: config.siteUrl+"/"+configTpl.userHomePage, - mailHeaderLinkTxt: txt.mailPaymentLinkTxt, - mailMainContent: tool.replaceAll(txt.mailPaymentThankGodfatherBodyHTML, mapMail), - linksCTA: [{ url:config.siteUrl+"/"+configTpl.userHomePage, txt:txt.mailPaymentLinkTxt }], - mailRecipientAddress: parrain.User.email + addDays(parrain, ndDaysGodFather); + numberOfDays+=ndDaysGodFather; + await db["Subscription"].update({ ...parrain.Subscription }, { where: { UserId : client.User.GodfatherId }, fields: ["numberOfDays"], limit:1 }); + userCtrl.creaUserJson(client.User.GodfatherId); + const mapMail = + { + USER_NAME: parrain.User.name + }; + const mailDatas= + { + mailSubject: txt.mailPaymentThankGodfatherSubject, + mailPreheader: txt.mailPaymentThankGodfatherSubject, + mailTitle: txt.mailPaymentThankGodfatherSubject, + mailHeaderLinkUrl: config.siteUrl+"/"+configTpl.userHomePage, + mailHeaderLinkTxt: txt.mailPaymentLinkTxt, + mailMainContent: tool.replaceAll(txt.mailPaymentThankGodfatherBodyHTML, mapMail), + linksCTA: [{ url:config.siteUrl+"/"+configTpl.userHomePage, txt:txt.mailPaymentLinkTxt }], + mailRecipientAddress: parrain.User.email + } + await toolMail.sendMail(parrain.User.smtp, parrain.User.email, txt.mailPaymentThankGodfatherSubject, tool.replaceAll(txt.mailPaymentThankGodfatherBodyTxt, mapMail), "", mailDatas); } - await toolMail.sendMail(parrain.User.smtp, parrain.User.email, txt.mailPaymentThankGodfatherSubject, tool.replaceAll(txt.mailPaymentThankGodfatherBodyTxt, mapMail), "", mailDatas); + else + res.alerte=txt.paymentGodfatherNotFound+client.User.GodfatherId; } - else - res.alerte=txt.paymentGodfatherNotFound+client.User.GodfatherId; - } - const infosClient= - { - clientName: req.query.cl, - amount: req.query.mt, - codeCommande: req.query.cmd, - UserId: client.User.id, - numberOfDays: client.Subscription.numberOfDays+numberOfDays - }; - await db["Payment"].create({ ...infosClient }, { fields: ["clientName", "amount", "codeCommande", "UserId"] }); - await db["Subscription"].update({ ...infosClient }, { where: { UserId : infosClient.UserId }, fields: ["numberOfDays"], limit:1 }); - userCtrl.creaUserJson(infosClient.UserId); - // mail remerciement abonné - const mapMail2 = - { - SITE_NAME: config.siteName, - USER_NAME: client.User.name, - NBDAYS: numberOfDays - }; - const mailDatas2 = - { - mailSubject: txt.mailPaymentThankSubject, - mailPreheader: txt.mailPaymentThankSubject, - mailTitle: txt.mailPaymentThankSubject, - mailHeaderLinkUrl: config.siteUrl+"/"+configTpl.userHomePage, - mailHeaderLinkTxt: txt.mailPaymentLinkTxt, - mailMainContent: tool.replaceAll(txt.mailPaymentThankBodyHTML, mapMail2), - linksCTA: [{ url:config.siteUrl+"/"+configTpl.userHomePage, txt:txt.mailPaymentLinkTxt }], - mailRecipientAddress: client.User.email + addDays(client, numberOfDays); + const infosClient= + { + clientName: req.query.cl, + amount: req.query.mt, + codeCommande: req.query.cmd, + UserId: client.User.id, + numberOfDays: client.Subscription.numberOfDays + }; + await db["Payment"].create({ ...infosClient }, { fields: ["clientName", "amount", "codeCommande", "UserId"] }); + await db["Subscription"].update({ ...infosClient }, { where: { UserId : infosClient.UserId }, fields: ["numberOfDays"], limit:1 }); + userCtrl.creaUserJson(infosClient.UserId); + const mapMail2 = + { + SITE_NAME: config.siteName, + USER_NAME: client.User.name, + NBDAYS: numberOfDays + }; + const mailDatas2 = + { + mailSubject: txt.mailPaymentThankSubject, + mailPreheader: txt.mailPaymentThankSubject, + mailTitle: txt.mailPaymentThankSubject, + mailHeaderLinkUrl: config.siteUrl+"/"+configTpl.userHomePage, + mailHeaderLinkTxt: txt.mailPaymentLinkTxt, + mailMainContent: tool.replaceAll(txt.mailPaymentThankBodyHTML, mapMail2), + linksCTA: [{ url:config.siteUrl+"/"+configTpl.userHomePage, txt:txt.mailPaymentLinkTxt }], + mailRecipientAddress: client.User.email + } + await toolMail.sendMail(client.User.smtp, client.User.email, txt.mailPaymentThankSubject, tool.replaceAll(txt.mailPaymentThankBodyTxt, mapMail2), "", mailDatas2); + // + info admin site + await toolMail.sendMail(0, config.adminEmail, txt.mailPaymentAdminNoticeSubject, txt.mailPaymentAdminNoticeBodyTxt.replace("EMAIL", client.User.email), txt.mailPaymentAdminNoticeBodyHTML.replace("EMAIL", client.User.email)); + res.status(200).json(true); } - await toolMail.sendMail(client.User.smtp, client.User.email, txt.mailPaymentThankSubject, tool.replaceAll(txt.mailPaymentThankBodyTxt, mapMail2), "", mailDatas2); - // + info admin site - await toolMail.sendMail(0, config.adminEmail, txt.mailPaymentAdminNoticeSubject, txt.mailPaymentAdminNoticeBodyTxt.replace("EMAIL", client.User.email), txt.mailPaymentAdminNoticeBodyHTML.replace("EMAIL", client.User.email)); - res.status(200).json(true); } + next(); } - next(); } catch(e) { next(e); } +} + +// Ajoute le nombre de jours nécessaire à un abonnement en vérifiant si il est expiré ou non. +// Si l'abonnement est toujours actif, on se contente d'ajouter le nombre de jours souhaité à celui déjà connu +// Sinon, on complète pour arriver à la date actuelle, puis on ajoute le nombre de jours souhaité +const addDays = (user, nbDays) => +{ + let dateEnd=new Date(user.Subscription.createdAt).getTime()+user.Subscription.numberOfDays*24*3600*1000; + if(dateEnd < Date.now()) + { + let needDays=Math.round((Date.now()-dateEnd)/(24*3600*1000)); + nbDays+=needDays; + } + user.Subscription.numberOfDays+=nbDays; } \ No newline at end of file diff --git a/front/public/img/tirelire.jpg b/front/public/img/tirelire.jpg new file mode 100644 index 0000000..0cb17e6 Binary files /dev/null and b/front/public/img/tirelire.jpg differ diff --git a/front/public/merci.html b/front/public/merci.html new file mode 100644 index 0000000..52672fc --- /dev/null +++ b/front/public/merci.html @@ -0,0 +1,61 @@ + + + + + + + WikiLerni : remerciement + + + + + + + + + + + + + +
+ WikiLerni (logo) + +
+ +
+ Logo WikiLerni +

Cultivons notre jardin !

+
+ +
+
+

Merci !

+

Votre paiement vient d'être enregistré. Merci à vous !

+

Normalement, vous avez reçu une facture par e-mail et votre abonnement vient d'être prolongé.

+

Si jamais ce n'est pas le cas et que vous avez une quelconque question, n'hésitez pas à me prévenir.

+

+

+ Tirelire +
User Cornischong on lb.wikipedia / CC BY-SA
+
+

+
+
+ + + \ No newline at end of file diff --git a/lang/fr/payment.js b/lang/fr/payment.js index 25992ad..56fe275 100644 --- a/lang/fr/payment.js +++ b/lang/fr/payment.js @@ -10,9 +10,9 @@ module.exports = mailPaymentThankSubject: "Merci !", mailPaymentLinkTxt: "Mon compte WikiLerni.", mailPaymentThankBodyTxt: "Bonjour USER_NAME,\n\nSuite à votre paiement, votre abonnement à SITE_NAME vient d'être prolongé de NBDAYS jours.\n\nMerci beaucoup et à bientôt !", - mailPaymentThankBodyHTML: "

Bonjour USER_NAME,

Suite à votre paiement, votre abonnement à SITE_NAME vient d'être prolongé de NBDAYS jours.
Merci beaucoup et à bientôt !

", + mailPaymentThankBodyHTML: "

Bonjour USER_NAME,

Suite à votre paiement, votre abonnement à SITE_NAME vient d'être prolongé de NBDAYS jours.

Merci beaucoup et à bientôt sur SITE_NAME !

", mailPaymentAdminNoticeSubject: "Nouvel abonnement prémium !", - mailPaymentAdminNoticeBodyTxt: "Bonjour,\nUn nouvel abonnement prémium vient d'être enregistré pour l'utilisateur EMAIL.", + mailPaymentAdminNoticeBodyTxt: "Bonjour,\nUn nouvel abonnement prémium vient d'être enregistré par l'utilisateur EMAIL.", mailPaymentAdminNoticeBodyHTML: "

Bonjour,

Un nouvel abonnement payant vient d'être enregistré pour l'utilisateur EMAIL.

", mailPaymentThankGodfatherSubject: "Merci !", mailPaymentThankGodfatherBodyTxt: "Bonjour USER_NAME,\n\nUn des utilisateurs que vous avez parrainé vient de souscrire à un abonnement payant.\n\nEn récompense, votre abonnement vient donc d'être prolongé de 30 jours.\n\nMerci à vous et à bientôt !",