Search code examples
javascriptwebpaypalpaymentpaypal-rest-sdk

Why do I keep getting INVALID_RESOURCE_ID error in production when trying to set up PayPal payment?


I want to set up an one-time PayPal payment. I am calling <script data-page-type="checkout" src="https://www.paypal.com/sdk/js?client-id={my client id}&currency=CZK&commit=true&intent=capture&debug=true"></script> from HTML to get the button to show.

I then set it up in the client-side javascript like this

        createOrder: function(data, actions) {
            return fetch('/.netlify/functions/transactionProcessor?orderAction=create', {
                method: 'post',
            }).then(function(functionResponse) {
                console.log("order created");
                return functionResponse.json();
            }).then(function(functionResponseJson) {
                return functionResponseJson.id;
            });
        },

When the user clicks the button, createOrder is called. CreateOrder then calls this server-side code

async function createOrder(client) {
    // Call PayPal to set up a transaction.
    const request = new paypal.orders.OrdersCreateRequest();
    request.prefer('return=minimal');
    request.requestBody({
        intent: 'CAPTURE',
        purchase_units: [{
            amount: {
                currency_code: 'CZK',
                value: '69.99'
            }
        }]
    });
    
    const order = await client.execute(request);
    console.log('order successfuly created');

    // Return a successful serverResponse to the client with the order ID.
    return { statusCode: 200, body: JSON.stringify({ serverResponse: order, id: 
    order.result.id }) }
}

That returns absolutely fine. The client-side code then handles it by returning it in it's createOrder method - and here the error occurs.

RESOURCE_NOT_FOUND (https://justpaste.it/resource_not_found_paypal_err)

INVALID_RESOURCE_ID (https://justpaste.it/invalid_resource_id_err)

I have absolutely no idea why it happens and I've been trying to fix it for days already. It WORKS IN SANDBOX.


Solution

  • Solved. I was accidentally using sandbox environment on the server. The logic was process.env.SANDBOX ? sandboxEnv : liveEnv, but the process.env variable doesn't work for some reason (Netlify + Netlify dev). I replaced it with local const sandbox = false.

    Can't believe I spent so much time on this. I'd never find this if I didn't fiddle with it so stupidly much.