diff --git a/api/src/controllers/paypalController.ts b/api/src/controllers/paypalController.ts index 426038abb..a63da2e06 100644 --- a/api/src/controllers/paypalController.ts +++ b/api/src/controllers/paypalController.ts @@ -19,9 +19,9 @@ import * as bookingController from './bookingController' */ export const createPayPalOrder = async (req: Request, res: Response) => { try { - const { bookingId, amount, currency, name }: bookcarsTypes.CreatePayPalOrderPayload = req.body + const { bookingId, amount, currency, name, description }: bookcarsTypes.CreatePayPalOrderPayload = req.body - const orderId = await paypal.createOrder(bookingId, amount, currency, name) + const orderId = await paypal.createOrder(bookingId, amount, currency, name, description) return res.json(orderId) } catch (err) { diff --git a/api/src/paypal.ts b/api/src/paypal.ts index 39c99b1dc..757827fd3 100644 --- a/api/src/paypal.ts +++ b/api/src/paypal.ts @@ -24,7 +24,7 @@ export const getToken = async () => { return res.data.access_token } -export const createOrder = async (bookingId: string, amount: number, currency: string, name: string) => { +export const createOrder = async (bookingId: string, amount: number, currency: string, name: string, description: string) => { const price = helper.formatPayPalPrice(amount) const token = await getToken() const res = await axios.post( @@ -59,7 +59,7 @@ export const createOrder = async (bookingId: string, amount: number, currency: s items: [ { name, - description: name, + description, unit_amount: { currency_code: currency, value: price, diff --git a/frontend/src/pages/Checkout.tsx b/frontend/src/pages/Checkout.tsx index 578dd10ae..a4491021f 100644 --- a/frontend/src/pages/Checkout.tsx +++ b/frontend/src/pages/Checkout.tsx @@ -141,11 +141,7 @@ const Checkout = () => { const _format = _fr ? 'eee d LLL yyyy kk:mm' : 'eee, d LLL yyyy, p' const bookingDetailHeight = env.SUPPLIER_IMAGE_HEIGHT + 10 const days = bookcarsHelper.days(from, to) - const daysLabel = from && to && ` - ${helper.getDaysShort(days)} (${bookcarsHelper.capitalize( - format(from, _format, { locale: _locale }), - )} - - ${bookcarsHelper.capitalize(format(to, _format, { locale: _locale }))})` + const daysLabel = from && to && `${helper.getDaysShort(days)} (${bookcarsHelper.capitalize(format(from, _format, { locale: _locale }))} - ${bookcarsHelper.capitalize(format(to, _format, { locale: _locale }))})` const handleFullNameChange = (e: React.ChangeEvent) => { setFullName(e.target.value) @@ -413,9 +409,7 @@ const Checkout = () => { currency: PaymentService.getCurrency(), locale: language, receiptEmail: (!authenticated ? driver?.email : user?.email) as string, - name: `${car.name} - - ${daysLabel} - - ${pickupLocation._id === dropOffLocation._id ? pickupLocation.name : `${pickupLocation.name} - ${dropOffLocation.name}`}`, + name: `${car.name} - ${daysLabel} - ${pickupLocation._id === dropOffLocation._id ? pickupLocation.name : `${pickupLocation.name} - ${dropOffLocation.name}`}`, description: `${env.WEBSITE_NAME} Web Service`, customerName: (!authenticated ? driver?.fullName : user?.fullName) as string, } @@ -944,9 +938,11 @@ const Checkout = () => {
{ - const name = `${car.name}` + const name = bookcarsHelper.truncateString(car.name, PayPalService.ORDER_NAME_MAX_LENGTH) + const _description = `${car.name} - ${daysLabel} - ${pickupLocation._id === dropOffLocation._id ? pickupLocation.name : `${pickupLocation.name} - ${dropOffLocation.name}`}` + const description = bookcarsHelper.truncateString(_description, PayPalService.ORDER_DESCRIPTION_MAX_LENGTH) const amount = payDeposit ? depositPrice : price - const orderId = await PayPalService.createOrder(bookingId!, amount, PaymentService.getCurrency(), name) + const orderId = await PayPalService.createOrder(bookingId!, amount, PaymentService.getCurrency(), name, description) return orderId }} onApprove={async (data) => { diff --git a/frontend/src/services/PayPalService.ts b/frontend/src/services/PayPalService.ts index bef033638..14b263244 100644 --- a/frontend/src/services/PayPalService.ts +++ b/frontend/src/services/PayPalService.ts @@ -1,6 +1,22 @@ import axiosInstance from './axiosInstance' import * as UserService from '@/services/UserService' +/** + * Order item name max length 200 characters + * https://developer.paypal.com/docs/api/invoicing/v2/#invoices_create!ct=application/json&path=items/name&t=request + * + * @type {200} + */ +export const ORDER_NAME_MAX_LENGTH = 200 + +/** + * Order item description max length 1000 characters + * https://developer.paypal.com/docs/api/invoicing/v2/#invoices_create!ct=application/json&path=items/description&t=request + * + * @type {1000} + */ +export const ORDER_DESCRIPTION_MAX_LENGTH = 1000 + /** * Returns PayPal locale. * @@ -27,7 +43,7 @@ export const getLocale = () => { * @param {string} sessionId * @returns {Promise} */ -export const createOrder = (bookingId: string, amount: number, currency: string, name: string): Promise => +export const createOrder = (bookingId: string, amount: number, currency: string, name: string, description: string): Promise => axiosInstance .post( '/api/create-paypal-order/', @@ -36,6 +52,7 @@ export const createOrder = (bookingId: string, amount: number, currency: string, amount, currency, name, + description, } ) .then((res) => res.data) diff --git a/packages/bookcars-helper/index.ts b/packages/bookcars-helper/index.ts index a472d47dc..3f28fedad 100644 --- a/packages/bookcars-helper/index.ts +++ b/packages/bookcars-helper/index.ts @@ -556,3 +556,22 @@ export const trim = (str: string, char: string): string => { export const delay = (milliseconds: number) => new Promise((resolve) => { setTimeout(resolve, milliseconds) }) + +/** + * Truncates a string. + * + * @param {string} str + * @param {number} maxLength + * @returns {string} + */ +export const truncateString = (str: string, maxLength: number) => { + if (str.length <= maxLength) { + return str + } + + if (maxLength >= 6) { + return `${str.slice(0, maxLength - 3)}...` + } + + return str.slice(0, maxLength) +} diff --git a/packages/bookcars-types/index.ts b/packages/bookcars-types/index.ts index e61de0a4a..023cd4b93 100644 --- a/packages/bookcars-types/index.ts +++ b/packages/bookcars-types/index.ts @@ -527,6 +527,7 @@ export interface CreatePayPalOrderPayload { amount: number currency: string name: string + description: string } export interface PaymentResult {