mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 14:50:32 +00:00
feat: Onboard accounts to Stripe Connect
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { NextFunction, Request, Response, Router } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { StripePaymentService } from '@/services/StripePayment/StripePaymentService';
|
||||
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
|
||||
|
||||
@Service()
|
||||
export class StripeIntegrationController {
|
||||
@Inject()
|
||||
private stripePaymentService: StripePaymentService;
|
||||
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/account', asyncMiddleware(this.createAccount.bind(this)));
|
||||
router.post(
|
||||
'/account_session',
|
||||
asyncMiddleware(this.createAccountSession.bind(this))
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
public async createAccount(req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const accountId = await this.stripePaymentService.createAccount();
|
||||
res.status(201).json({ accountId });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async createAccountSession(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const { account } = req.body;
|
||||
try {
|
||||
const clientSecret = await this.stripePaymentService.createAccountSession(
|
||||
account
|
||||
);
|
||||
res.status(200).json({ clientSecret });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,7 @@ import { Webhooks } from './controllers/Webhooks/Webhooks';
|
||||
import { ExportController } from './controllers/Export/ExportController';
|
||||
import { AttachmentsController } from './controllers/Attachments/AttachmentsController';
|
||||
import { OneClickDemoController } from './controllers/OneClickDemo/OneClickDemoController';
|
||||
import { StripeIntegrationController } from './controllers/StripeIntegration/StripeIntegrationController';
|
||||
|
||||
export default () => {
|
||||
const app = Router();
|
||||
@@ -147,6 +148,7 @@ export default () => {
|
||||
dashboard.use('/import', Container.get(ImportController).router());
|
||||
dashboard.use('/export', Container.get(ExportController).router());
|
||||
dashboard.use('/attachments', Container.get(AttachmentsController).router());
|
||||
dashboard.use('/stripe_integration', Container.get(StripeIntegrationController).router());
|
||||
|
||||
dashboard.use('/', Container.get(ProjectTasksController).router());
|
||||
dashboard.use('/', Container.get(ProjectTimesController).router());
|
||||
|
||||
@@ -259,6 +259,14 @@ module.exports = {
|
||||
*/
|
||||
posthog: {
|
||||
apiKey: process.env.POSTHOG_API_KEY,
|
||||
host: process.env.POSTHOG_HOST
|
||||
}
|
||||
host: process.env.POSTHOG_HOST,
|
||||
},
|
||||
|
||||
/**
|
||||
* Stripe Payment Integration.
|
||||
*/
|
||||
stripePayment: {
|
||||
secretKey: process.env.STRIPE_PAYMENT_SECRET_KEY || '',
|
||||
publishableKey: process.env.STRIPE_PAYMENT_PUBLISHABLE_KEY || '',
|
||||
},
|
||||
};
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Service } from 'typedi';
|
||||
import stripe from 'stripe';
|
||||
import config from '@/config';
|
||||
|
||||
@Service()
|
||||
export class StripePaymentService {
|
||||
private stripe;
|
||||
|
||||
constructor() {
|
||||
this.stripe = new stripe(config.stripePayment.secretKey, {
|
||||
apiVersion: '2023-10-16',
|
||||
});
|
||||
}
|
||||
|
||||
public async createAccountSession(accountId: string) {
|
||||
try {
|
||||
const accountSession = await this.stripe.accountSessions.create({
|
||||
account: accountId,
|
||||
components: {
|
||||
account_onboarding: { enabled: true },
|
||||
},
|
||||
});
|
||||
return accountSession.client_secret;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
'An error occurred when calling the Stripe API to create an account session'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async createAccount() {
|
||||
try {
|
||||
const account = await this.stripe.accounts.create({});
|
||||
|
||||
return account.id;
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
'An error occurred when calling the Stripe API to create an account'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user