Skip to content

Commit

Permalink
Update PayPal price calculation logic ; refactor PayPal tests and log…
Browse files Browse the repository at this point in the history
…ging
  • Loading branch information
aelassas committed Feb 1, 2025
1 parent 243fb39 commit 06eae04
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions api/__tests__/paypal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ afterAll(async () => {

describe('POST /api/create-paypal-order', () => {
it('should create paypal order', async () => {
// test success (create checkout session whith non existant user)
// test success (create paypal order whith non existant user)
const payload: bookcarsTypes.CreatePayPalOrderPayload = {
amount: 234,
currency: 'USD',
Expand All @@ -39,7 +39,7 @@ describe('POST /api/create-paypal-order', () => {
expect(res.statusCode).toBe(200)
expect(res.body.length).toBeGreaterThan(0)

// test failure (create checkout sessions failure)
// test failure (create paypal order failure)
payload.currency = 'xxxxxxxxxxxxxxx'
res = await request(app)
.post('/api/create-paypal-order')
Expand Down
5 changes: 3 additions & 2 deletions api/src/common/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ export const validateAccessToken = async (socialSignInType: bookcarsTypes.Social
* 1 1.00
* 1.2 1.20
* 1.341 1.34
* 1.345 1.35
* 1.345 1.34
* 1.378 1.37
*
* @param {number} price
* @returns {string}
*/
export const formatPayPalPrice = (price: number) => (Math.round(price * 100) / 100).toFixed(2)
export const formatPayPalPrice = (price: number) => (Math.floor(price * 100) / 100).toFixed(2)
4 changes: 2 additions & 2 deletions api/src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ export const LANGUAGES = [
]

/**
* Website Name
* Website Name.
*
* @type {string}
*/
export const WEBSITE_NAME = __env__('BC_WEBSITE_NAME', false, 'bookcars')
export const WEBSITE_NAME = __env__('BC_WEBSITE_NAME', false, 'BookCars')

/**
* Server Port. Default is 4002.
Expand Down
8 changes: 4 additions & 4 deletions api/src/controllers/paypalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ export const checkPayPalOrder = async (req: Request, res: Response) => {
const booking = await Booking.findOne({ _id: bookingId, expireAt: { $ne: null } })
if (!booking) {
const msg = `Booking with id ${bookingId} not found`
logger.info(`[paypal.checkPaypalOrder] ${msg}`)
logger.info(`[paypal.checkPayPalOrder] ${msg}`)
return res.status(204).send(msg)
}

let order
try {
order = await paypal.getOrder(orderId)
} catch (err) {
logger.error(`[paypal.checkPaypalOrder] retrieve paypal order error: ${orderId}`, err)
logger.error(`[paypal.checkPayPalOrder] retrieve paypal order error: ${orderId}`, err)
}

if (!order) {
const msg = `Order ${order} not found`
logger.info(`[paypal.checkPaypalOrder] ${msg}`)
logger.info(`[paypal.checkPayPalOrder] ${msg}`)
return res.status(204).send(msg)
}

Expand Down Expand Up @@ -116,7 +116,7 @@ export const checkPayPalOrder = async (req: Request, res: Response) => {
await booking.deleteOne()
return res.status(400).send(order.status)
} catch (err) {
logger.error(`[paypal.checkPaypalOrder] ${i18n.t('ERROR')}`, err)
logger.error(`[paypal.checkPayPalOrder] ${i18n.t('ERROR')}`, err)
return res.status(400).send(i18n.t('ERROR') + err)
}
}
1 change: 1 addition & 0 deletions api/src/paypal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const getOrder = async (orderId: string) => {
`https://api-m.sandbox.paypal.com/v2/checkout/orders/${orderId}`,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
Expand Down

0 comments on commit 06eae04

Please sign in to comment.