Reputation: 646
I am integrating Paypal's payment processing REST API into a Next.js app with vercel. The payments successfully runs on the localhost (dev). However, on the staging (review) environment the request fails upon fetching the PayPal access token. The client credentials are correctly shown in the logs.
Logs:
info: Request body {"type":"form","content":[{"key":"grant_type","value":"client_credentials"}]}
info: Response 401 77 application/json
info: Response headers {"connection":"keep-alive","content-length":"77","access-control-expose-headers":"**Redacted**","server-timing":"**Redacted**","server":"nginx","paypal-debug-id":"**Redacted**","x-paypal-token-service":"**Redacted**","content-type":"application/json","traceparent":"**Redacted**","strict-transport-security":"**Redacted**","cache-control":"max-age=0, no-cache, no-store, must-revalidate","http_x_pp_az_locator":"**Redacted**","pragma":"no-cache","x-backend-info":"**Redacted**","edge-control":"**Redacted**","accept-ranges":"**Redacted**","date":"Sat, 14 Dec 2024 01:56:00 GMT","via":"1.1 varnish","x-served-by":"**Redacted**","x-cache":"**Redacted**","x-cache-hits":"**Redacted**","x-timer":"**Redacted**","vary":"Accept-Encoding"}
Failed to create order: Error
at v (/var/task/.next/server/app/api/orders/route.js:1:1699)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async g (/var/task/.next/server/app/api/orders/route.js:1:1884)
at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:36957
at async eC.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:27552)
at async eC.handle (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:38291)
at async es (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25262)
at async en.responseCache.get.routeKind (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:1026)
at async r6.renderToResponseWithComponentsImpl (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:508)
at async r6.renderPageComponent (/var/task/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:17:5085)
Route handler code:
import { NextRequest, NextResponse } from 'next/server';
import { Client, Environment, LogLevel, OrdersController, ApiError, CheckoutPaymentIntent } from '@paypal/paypal-server-sdk';
const PAYPAL_CLIENT_ID = process.env.PAYPAL_CLIENT_ID || '';
const PAYPAL_CLIENT_SECRET = process.env.PAYPAL_CLIENT_SECRET || '';
console.info("PayPal Client ID:", process.env.PAYPAL_CLIENT_ID);
console.info("PayPal Client Secret:", process.env.PAYPAL_CLIENT_SECRET);
const client = new Client({
clientCredentialsAuthCredentials: {
oAuthClientId: PAYPAL_CLIENT_ID,
oAuthClientSecret: PAYPAL_CLIENT_SECRET,
},
environment: Environment.Sandbox,
logging: {
logLevel: LogLevel.Info,
logRequest: { logBody: true },
logResponse: { logHeaders: true },
},
});
const ordersController = new OrdersController(client);
/* Create payment order through PayPal SDK */
const createOrder = async (totalCost: string) => {
const payload = {
body: {
intent: CheckoutPaymentIntent.CAPTURE,
purchaseUnits: [
{
amount: {
currencyCode: 'USD',
value: totalCost,
},
},
],
},
prefer: 'return=minimal',
};
try {
const { body, ...httpResponse } = await ordersController.ordersCreate(payload);
const jsonResponse = JSON.parse(body.toString());
return {
jsonResponse,
httpStatusCode: httpResponse.statusCode,
};
} catch (error) {
if (error instanceof ApiError) {
throw new Error(error.message);
}
return {
jsonResponse: { error: 'Unknown error occurred' },
httpStatusCode: 500,
};
}
};
export async function POST(req: NextRequest) {
const {totalCost} = await req.json();
try {
const { jsonResponse, httpStatusCode } = await createOrder(totalCost);
return NextResponse.json(jsonResponse, { status: httpStatusCode });
} catch (error) {
console.error('Failed to create order:', error);
return NextResponse.json({ error: 'Failed to create order.' }, { status: 500 });
}
}
Upvotes: 0
Views: 45