diff --git a/payment/src/api/PaymentAPI.js b/payment/src/api/PaymentAPI.js index e455694a..67b9af82 100644 --- a/payment/src/api/PaymentAPI.js +++ b/payment/src/api/PaymentAPI.js @@ -1,4 +1,3 @@ -import Stripe from 'stripe'; import axios from 'axios'; import { @@ -6,25 +5,20 @@ import { ERROR_STATUS_CODE, CLINIC_BASE_URL, PATIENTS_BASE_URL, - BAD_REQUEST_CODE_400, - SECRET_KEY + BAD_REQUEST_CODE_400 } from '../utils/Constants.js'; import { isValidMongoId } from '../utils/Validation.js'; +import PaymentService from '../service/payment-service.js'; -const stripe = new Stripe(SECRET_KEY); export const payment = (app) => { + const service = new PaymentService(); + app.post('/payment/card', async (req, res) => { try{ const total_amount = Number(req.body.paymentAmount); - const paymentIntent = await stripe.paymentIntents.create({ - amount: parseInt(total_amount * 100), - currency: "usd", - automatic_payment_methods: { - enabled: true, - }, - }); + const paymentIntent = service.createPaymentIntent(total_amount); res.status(OK_STATUS_CODE).send({ clientSecret: paymentIntent.client_secret, }); diff --git a/payment/src/service/payment-service.js b/payment/src/service/payment-service.js new file mode 100644 index 00000000..aa9d834f --- /dev/null +++ b/payment/src/service/payment-service.js @@ -0,0 +1,21 @@ +import Stripe from 'stripe'; +import { + SECRET_KEY +} from '../utils/Constants.js'; + +const stripe = new Stripe(SECRET_KEY); + +class PaymentService{ + + async createPaymentIntent(total_amount){ + await stripe.paymentIntents.create({ + amount: parseInt(total_amount * 100), + currency: "usd", + automatic_payment_methods: { + enabled: true, + }, + }); + } +} + +export default PaymentService; \ No newline at end of file