Created
March 23, 2013 18:21
-
-
Save Amatewasu/5228834 to your computer and use it in GitHub Desktop.
Un petit script pour utiliser l'API d'allopass avec node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var express = require('express'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.get("/", function (req, res){ | |
var html = ''; | |
html = '<form action ="http://payment.allopass.com/acte/access.apu" method="post">'; | |
html += '<input type="hidden" name="ids" value="123456" />'; | |
html += '<input type="hidden" name="idd" value="1234567" />'; | |
html += '<input type="hidden" name="recall" value="1" />'; | |
html += '<input type="text" name="code[]" size="8" />'; | |
html += '<input type="submit" value=" Entrer " />' | |
html += '</form>'; | |
res.send(html); | |
}); | |
app.get("/test", function (req, res){ | |
var recall = req.query["RECALL"]; | |
if (!recall.trim()){ | |
res.send("Error"); | |
return; | |
} | |
// recall contient le code d'accès | |
recall = encodeURIComponent(recall); | |
// auth doit contenir l'identifiant de VOTRE document | |
var auth = encodeURIComponent("123/456/789"); | |
// Envoi de la requête vers le serveur allopass | |
http.get("http://payment.allopass.com/api/checkcode.apu?code="+ recall +"&auth="+ auth, function (response){ | |
response.setEncoding("utf-8"); | |
response.on("data", function (chunk){ | |
var r = chunk.split("\n"); | |
// Réponse du serveur ; OK | NOK | ERR (cf doc) | |
var statusCodeServer = r[0].trim(); | |
// Code du pays de l'appel de l'internaute (FR, BE, UK, DE, etc.) | |
var countryCode = r[1].trim(); | |
// Vérification de la réponse du serveur | |
if (statusCodeServer != "OK"){ | |
// Le serveur a répondu ERR ou NOK, l'accès est donc refusé. | |
res.send("ERROR"); | |
return; | |
} | |
/* Le code est valide, vous pouvez maintenant faire ce que vous voulez. | |
* Comme par exemple placer un cookie pour autoriser l'accès à certaines page comme la documentation officielle. | |
* (méthode complètement insécurisée au passage, préférez enregistrer ça dans une variable de session) | |
*/ | |
// Dans cet exemple on va se contenter d'afficher "OK" :-) | |
res.send("OK"); | |
}); | |
}).on("error", function (error){ | |
console.error(error.message); | |
res.send("Error"); | |
}); | |
}); | |
app.listen(3001); | |
console.log("http://127.0.0.1:3001"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment