Add appStore.setupBilling() using stripe

This commit is contained in:
Johannes Zellner 2015-06-22 09:11:43 +02:00
parent 59cdb33915
commit 65002ac27a

View File

@ -1,8 +1,10 @@
'use strict'; 'use strict';
var debug = require('debug')('e2e:appstore'), var debug = require('debug')('e2e:appstore'),
path = require('path'),
request = require('superagent-sync'), request = require('superagent-sync'),
sleep = require('sleep').sleep, sleep = require('sleep').sleep,
stripe = require('stripe');
exports = module.exports = AppStore; exports = module.exports = AppStore;
@ -14,6 +16,8 @@ function AppStore(origin) {
password: null, password: null,
accessToken: null accessToken: null
}; };
this._ecosystem = require(path.join(__dirname, '..', 'keys', 'appstore', 'ecosystem-staging.json'));
} }
function verifyResponse(res, errorMessage) { function verifyResponse(res, errorMessage) {
@ -82,3 +86,31 @@ AppStore.prototype.restore = function (boxId, backupId) {
verifyResponse(res, 'Could not restore cloudron'); verifyResponse(res, 'Could not restore cloudron');
}; };
AppStore.prototype.setupBilling = function (callback) {
var stripeApi = stripe(this._ecosystem.env['STRIPE_SECRET']);
var that = this;
stripeApi.tokens.create({
card: {
number: '4242424242424242',
exp_month: 12,
exp_year: 2016,
cvc: '123'
}
}, function (error, token) {
if (error) return callback(error);
debug('Got stripe token', token.id);
var data = {
password: that._credentials.password,
billingToken: token.id
};
var res = request.put(that._origin + '/api/v1/users').send(data).query({ accessToken: that._credentials.accessToken }).end();
verifyResponse(res, 'Could not setup billing');
callback(null);
});
};