Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support multiple servers

You can now configure a list of servers.
  • Loading branch information...
commit 69de9523d3147a37c2d61d16bbe82146f127ad42 1 parent df31861
@kumar303 authored
View
55 config-dist.js
@@ -7,31 +7,40 @@ module.exports = function(config) {
var isStackato = !!process.env.VCAP_APPLICATION;
// Get payment keys from the Firefox Marketplace DevHub.
+ config.servers = [
+ {name: 'marketplace.firefox.com (prod)',
+ mozPayAudience: 'marketplace.firefox.com',
+ mozPayType: 'mozilla/payments/pay/v1',
+ mozPayKey: '...',
+ mozPaySecret: '...'},
+ {name: 'marketplace.firefox.com (prod, simulate only)',
+ simulate: {result: 'postback'},
+ mozPayAudience: 'marketplace.firefox.com',
+ mozPayType: 'mozilla/payments/pay/v1',
+ mozPayKey: '...',
+ mozPaySecret: '...'},
+ {name: 'marketplace.allizom.org (stage)',
+ mozPayAudience: 'marketplace.allizom.org',
+ mozPayType: 'mozilla-stage/payments/pay/v1',
+ mozPayKey: '...',
+ mozPaySecret: '...'},
+ {name: 'marketplace-dev.allizom.org (dev)',
+ mozPayAudience: 'marketplace-dev.allizom.org',
+ mozPayType: 'mozilla-dev/payments/pay/v1',
+ mozPayKey: '...',
+ mozPaySecret: '...'},
+ ];
+
if (isStackato) {
- // Production
- config.mozPayAudience = 'marketplace.firefox.com';
- config.mozPayType = 'mozilla/payments/pay/v1';
- config.mozPayKey = '...';
- config.mozPaySecret = '...';
- // Stage
- /*
- config.mozPayAudience = 'marketplace.allizom.org';
- config.mozPayType = 'mozilla-stage/payments/pay/v1';
- config.mozPayKey = '...';
- config.mozPaySecret = '...';
- */
- // Dev
- /*
- config.mozPayAudience = 'marketplace-dev.allizom.org';
- config.mozPayType = 'mozilla-dev/payments/pay/v1';
- config.mozPayKey = '...';
- config.mozPaySecret = '...';
- */
+ config.isSecure = true;
} else {
- config.mozPayAudience = 'localhost';
- config.mozPayType = 'mozilla-local/payments/pay/v1';
- config.mozPayKey = '...';
- config.mozPaySecret = '...';
+ config.servers.push({
+ name: 'fireplace.local',
+ mozPayAudience: 'localhost',
+ mozPayType: 'mozilla-local/payments/pay/v1',
+ mozPayKey: '...',
+ mozPaySecret: '...'
+ });
}
var app = isStackato
View
32 routes/index.js
@@ -12,10 +12,20 @@ module.exports = function(app, config) {
});
app.get('/jwt-source', function(req, res) {
+ var server = config.servers[req.param('server')];
+ if (!server) {
+ console.log('/jwt-source: server does not exist for', req.param('server'));
+ res.send(400);
+ return;
+ }
+ // WARNING! This will not work with concurrent users on the site.
+ // mozpay needs a fix.
+ pay.configure(server);
+
var defaultJWT = {
- iss: config.mozPayKey,
- aud: config.mozPayAudience,
- typ: config.mozPayType,
+ iss: server.mozPayKey,
+ aud: server.mozPayAudience,
+ typ: server.mozPayType,
iat: pay.now(),
exp: pay.now() + 3600, // in 1hr
request: {
@@ -41,8 +51,8 @@ module.exports = function(app, config) {
}
}
};
- if (config.simulate) {
- defaultJWT.request.simulate = config.simulate;
+ if (server.simulate) {
+ defaultJWT.request.simulate = server.simulate;
}
res.send(JSON.stringify(defaultJWT, null, 2));
});
@@ -62,6 +72,16 @@ module.exports = function(app, config) {
config.db[transID] = {state: 'pending', result: null};
var jwtReq = JSON.parse(req.param('jwt'));
console.log('pay with:', jwtReq);
+ var server = config.servers[req.param('server')];
+ if (!server) {
+ console.log('/pay: server does not exist for', req.param('server'));
+ res.send(400);
+ return;
+ }
+ // WARNING! This will not work with concurrent users on the site.
+ // mozpay needs a fix.
+ pay.configure(server);
+
// fill in non-editable fields.
jwtReq.request.productData = qs.stringify({localTransID: transID}),
jwtReq.request.postbackURL = config.postbackURL('postback');
@@ -71,7 +91,7 @@ module.exports = function(app, config) {
// In a real app you would *never* sign a JSON Web Token
// that came entirely from user input (such as this textarea form).
res.send({transID: transID,
- jwt: jwt.encode(jwtReq, config.mozPaySecret, 'HS256')});
+ jwt: jwt.encode(jwtReq, server.mozPaySecret, 'HS256')});
});
};
View
5 routes/payments.js
@@ -3,7 +3,10 @@ var qs = require('qs');
module.exports = function(app, config) {
- pay.routes(app, config);
+ // Whoops! This needs to happen first but we really
+ // want to configure this dynamically. See routes/index.js
+ pay.configure(config.servers[0]);
+ pay.routes(app);
function processData(data, done) {
var transID = qs.parse(data.request.productData).localTransID;
View
6 server.js
@@ -23,13 +23,11 @@ var config = {
sessionSecret: 'set-this-in-local-settings',
// If you use HTTPS with a valid cert, set this to true.
isSecure: false,
- mozPayKey: null,
- mozPaySecret: null,
- mozPayAudience: 'marketplace.firefox.com',
- mozPayType: 'mozilla/payments/pay/v1',
mozPayRoutePrefix: '/mozpay',
// When not null, simulate this result instead of making a real purchase.
simulate: null,
+ // A list of servers to use and their corresponding values. See config-dist.js for an example.
+ servers: [],
addPort: function() {
if (this.extPort) {
View
34 views/index.jade
@@ -10,22 +10,26 @@ html
body
#app
- div #{config.mozPayAudience}
- #error
- #jwt
- //- temporarily disabling sign-in
- //
- div#signin
- button Loading...
- div#signout.hidden
- button Loading...
- div
- button#refresh Refresh JWT
- #log.hidden
- ul
- form#jwt-input
+ form#jwt-input
+ select(name='server', id='server')
+ each serv, index in config.servers
+ option(value=index) #{serv.name}
+
+ #error
+ #jwt
+ //- temporarily disabling sign-in
+ //
+ div#signin
+ button Loading...
+ div#signout.hidden
+ button Loading...
+ div
+ button#refresh Refresh JWT
+ #log.hidden
+ ul
#jwt-panel
- textarea Loading...
+ // Warning: this textarea tag gets replaced in www/js/index.js
+ textarea(name='jwt') Loading...
div
button#pay(type='submit') Pay
button#toggle(type='button') Show log
View
7 www/css/index.css
@@ -57,6 +57,13 @@ button {
cursor: pointer;
}
+select {
+ width: 100%;
+ font-size: 1.2em;
+ padding: 0.5em;
+ margin: 5px 0;
+}
+
#app {
margin: 0;
padding: 4px;
View
40 www/js/index.js
@@ -1,7 +1,35 @@
(function() {
"use strict";
- function pay(editedJWT, done) {
+ $.fn.serializeObject = function() {
+ /* returns a JS object */
+ var o = {};
+ var a = this.serializeArray();
+ $.each(a, function() {
+ if (o[this.name] !== undefined) {
+ if (!o[this.name].push) {
+ o[this.name] = [o[this.name]];
+ }
+ o[this.name].push(this.value || '');
+ } else {
+ o[this.name] = this.value || '';
+ }
+ });
+ return o;
+ };
+
+ function pay(form, done) {
+ var editedJWT;
+ // This is stupid. The textarea name attribute has a cache buster thing.
+ for (var key in form) {
+ if (key.slice(0, 3) == 'jwt') {
+ editedJWT = form[key];
+ }
+ }
+ if (!editedJWT) {
+ console.error('could not get editedJWT');
+ }
+ console.log('jwt', editedJWT, 'server', form.server);
clearLog();
showLog();
writeLog('signing JWT...');
@@ -33,7 +61,7 @@
console.log('edited JWT:', editedJWT);
$.ajax({url: '/pay', type: 'post', cache: false,
- data: {jwt: editedJWT}})
+ data: {jwt: editedJWT, server: form.server}})
.done(function(data, textStatus, jqXHR) {
writeLog('calling navigator.mozPay()...');
var req = navigator.mozPay([data.jwt]);
@@ -52,10 +80,11 @@
function refresh() {
showJWT();
- $.ajax({url: '/jwt-source', cache: false})
+ $.ajax({url: '/jwt-source', cache: false,
+ data: {server: $('select#server').val()}})
.done(function(data, textStatus, jqXHR) {
console.log('refreshed JWT');
- // Replace textare with brute force to prevent the browser from
+ // Replace textarea with brute force to prevent the browser from
// trying to preserve any edits.
$('#jwt-panel textarea').remove();
var tx = $('<textarea>Loading...</textarea>');
@@ -129,10 +158,11 @@
function onReady() {
refresh();
$('#refresh').click(refresh);
+ $('select#server').on('change', refresh);
$('form#jwt-input').on('submit', function(evt) {
evt.preventDefault();
- pay($(this).serializeArray()[0].value /* textarea value */, function(error, result) {
+ pay($(this).serializeObject(), function(error, result) {
if (error) {
writeLog('error: ' + error);
console.log('error with pay():', error);
Please sign in to comment.
Something went wrong with that request. Please try again.