mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-23 12:32:06 +00:00
ed51d8b13f
major merge of features from the hosted branch that was created temporarily during the initial launch of jambonz.org
49 lines
1.8 KiB
JavaScript
49 lines
1.8 KiB
JavaScript
const router = require('express').Router();
|
|
const Account = require('../../models/account');
|
|
const sysError = require('../error');
|
|
|
|
/* list */
|
|
router.get('/', async(req, res) => {
|
|
const {createCustomer} = require('../../utils/stripe-utils');
|
|
const logger = req.app.locals.logger;
|
|
try {
|
|
const {account_sid, email, name} = req.user;
|
|
logger.debug({account_sid, email, name}, 'GET /StripeCustomerId');
|
|
const results = await Account.retrieve(account_sid);
|
|
if (results.length === 0) return res.sendStatus(404);
|
|
const account = results[0];
|
|
|
|
/* is account already provisioned in Stripe ? */
|
|
if (account.stripe_customer_id) return res.status(200).json({stripe_customer_id: account.stripe_customer_id});
|
|
|
|
/* no - provision it now */
|
|
const customer = await createCustomer(logger, account_sid, email, name);
|
|
account.stripe_customer_id = customer.id;
|
|
await Account.updateStripeCustomerId(account_sid, customer.id);
|
|
res.status(200).json({stripe_customer_id: customer.id});
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
|
|
/* delete */
|
|
router.delete('/', async(req, res) => {
|
|
const {deleteCustomer} = require('../../utils/stripe-utils');
|
|
const logger = req.app.locals.logger;
|
|
const {account_sid} = req.user;
|
|
try {
|
|
const acc = await Account.retrieve(account_sid);
|
|
logger.debug({acc}, 'retrieved account');
|
|
if (!acc || 0 === acc.length || !acc[0].stripe_customer_id) return res.sendStatus(404);
|
|
const {stripe_customer_id} = acc[0];
|
|
logger.info(`deleting stripe customer id ${stripe_customer_id}`);
|
|
await deleteCustomer(logger, stripe_customer_id);
|
|
await Account.updateStripeCustomerId(account_sid, null);
|
|
res.sendStatus(204);
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|