paymentsApi.createPayment working in development, failing in production

I have the following in a serverside function:

`async function processPayment(paymentRequest: any, environment: ‘sandbox’ | ‘production’) {
const client = new Client({
environment: environment === ‘sandbox’ ? Environment.Sandbox : Environment.Production,
accessToken:
environment === ‘sandbox’
? process.env.VITE_SQUARE_ACCESS_TOKEN_DEV
: process.env.VITE_SQUARE_ACCESS_TOKEN_PROD
});

try {
	const { nonce, amount, currency, userId, paymentType, eventId, eventDescription, userEmail } =
		paymentRequest;

	console.log('Processing payment:', paymentRequest);

	if (!nonce) {
		throw new Error('Payment nonce is missing');
	}

	// Create a payment request using the Square API
	const paymentsApi = client.paymentsApi;
	const response = await paymentsApi.createPayment({
		sourceId: nonce,
		idempotencyKey: uuidv4(), // Generate a unique idempotency key
		amountMoney: {
			amount: amount, // Example: 100 for $1.00
			currency: currency || 'USD'
		}
	});

	if (
		!response.result ||
		!response.result.payment ||
		response.result.payment.status !== 'COMPLETED'
	) {
		throw new Error(`Payment failed: ${response.result.payment?.status || 'Unknown error'}`);
	}`

This works with no issues when running it in development mode locally. However, when running in production, i get this error

"code": "INVALID_CARD_DATA","detail": "Card nonce not found in this application environment. Please ensure an application ID belonging to the same environment is used when generating the nonce.","category": "INV… [message truncated due to size]

Im not sure what to do, my client and server are configured correctly. Is there a mistake I am making here?

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Troubleshoot In-App Payments SDK Problems
Postman
Payment API: Process Online and In-Person Payments

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

The error message suggests that the card nonce was generated in a different environment than the one you’re trying to use it in. If you generate a nonce in a sandbox environment, you can’t use it in a production environment, and vice versa.

Ensure that both your client-side and server-side environments match. If you’re testing in a sandbox environment, make sure your client-side code is also generating nonces in the sandbox environment. Similarly, if you’re in a production environment, ensure your client-side code is generating nonces in the production environment.

Also, make sure that the access token you’re using on the server-side matches the environment you’re working in. If you’re in a sandbox environment, use your sandbox access token, and if you’re in a production environment, use your production access token.

This answer was reviewed by @Bryan-Square.