Process service payment
POST/truckers/payment/pay
Process the payment for a service using the default payment method in Stripe.
Technical Flow:
- Verify user authentication
- Retrieve the company associated with the user
- Validate that Stripe is enabled for the company
- Verify that a default payment method exists
- Find the delivery associated with the serviceCode
- Calculate the amount and commission
- Create a PaymentIntent in Stripe
- Save the reference in the delivery
- Send a confirmation email
Validations:
- User must be authenticated (valid JWT)
- User must belong to a valid company
- Stripe must be enabled for the company
- A default payment method must exist
- serviceCode must correspond to an existing delivery
Required Parameters:
- serviceCode: Unique code of the service to be paid
Example Request:
{
serviceCode: DEL123456
}
Example Successful Response:
{
success: true
}
Error Cases:
- 400 Bad Request:
- serviceCode not provided
- serviceCode not valid
- 401 Unauthorized:
- Invalid JWT token
- User has no associated company
- Stripe not enabled for the company
- No default payment method
- 500 Internal Server Error: Error processing payment in Stripe
Frontend Usage Example:
// Frontend: Process service payment
const processPayment = async (serviceCode) => {
try {
const response = await axios.post(
'/truckers/payment/pay',
{ serviceCode },
{ headers: { Authorization: `Bearer ${token}` } }
);
if (response.data.success) {
showPaymentSuccess(); // Show confirmation
}
} catch (error) {
handleError(error);
}
};
Important Notes:
- Uses the configured default payment method
- The amount is automatically calculated based on the auction
- A 6% commission is applied by default
- Generates a PaymentIntent record in Stripe
- Sends confirmation email to both parties
Request
Responses
- 200
- 400
- 401
Payment successfully processed.
Incorrect request. Possible causes:
- Missing serviceCode parameter
- Invalid or not found serviceCode
Unauthorized or payment failure. Possible causes:
- Invalid JWT token
- User has no associated company
- Stripe not enabled for the company
- No default payment method
- Failed to process payment in Stripe