@vulog/aima-payment
Version:
Payment management module for the AIMA platform. This module provides functionality to handle payments, setup intents, payment methods, and trip payments.
49 lines (43 loc) • 1.5 kB
text/typescript
import { Client } from '@vulog/aima-client';
import { z } from 'zod';
import { TripPayment } from './types';
/**
* Pay for a trip by its ID.
*
* @param client - The Aima client instance.
* @param tripId - The ID of the trip to pay for.
* @returns A promise that resolves when the payment is successful.
* @throws TypeError if the parameters are invalid.
*/
const schema = z.object({
tripId: z.string().nonempty(),
body: z.object({
online: z.boolean().optional(),
scope: z.enum(['RENTAL', 'DEPOSIT']).optional(),
amountType: z.enum(['FIXED', 'PERCENTAGE']).optional(),
amountValue: z.number().nonnegative(),
useSystemCredit: z.boolean().optional(),
}),
});
export const payATrip = async (
client: Client,
tripId: string,
body: z.infer<typeof schema>['body']
): Promise<TripPayment> => {
const validated = schema.safeParse({ tripId, body });
if (!validated.success) {
throw new TypeError('Invalid parameters', {
cause: validated.error.issues,
});
}
return client
.post<TripPayment>(`/boapi/proxy/trip/fleets/${client.clientOptions.fleetId}/trips/${tripId}/pay`, {
online: body.online ?? false,
scope: body.scope ?? 'RENTAL',
amountType: body.amountType ?? 'FIXED',
amountValue: body.amountValue,
useSystemCredit: body.useSystemCredit ?? true,
})
.then(({ data }) => data);
};
export default payATrip;