Invalid error source id

If i generate nonce from square react native sdk and then call API in app with this nonce to create card, then ccof is returned which is working fine.

However, if i send this generated nonce to my backend which is using square nodejs SDK then that SDK createCard endpoint returns error invalid source ID.

The app API call is like this

const res = await axios.post(
    `${baseURL}cards`,
    {
      idempotency_key: key,
      source_id: nonce,
      card: {
        customer_id: user_square_customer_id,
      },
    },
    {
      headers: {
        Authorization: `Bearer ${authorizationToken}`,
        'Content-Type': 'application/json',
      },
    }
  );

The SDK call is this

const res = await client.cardsApi.createCard({
    idempotencyKey: key,
    sourceId: nonce,
    card: { customerId: sqCustomerId },
  })

Note that, SDK uses camelcase while API uses snake case.

App using direct API call (so no version)

backend using SDK: 34.0.1

If I try the SDK by sending cnon:card-nonce-ok then card is created fine. but that ccof can’t be charged as createPayment API says card not found.

Only thing working is, create nonce from React native SDK. Then save it via square API call from app (not backend). and then i can use this saved token with my backend.

Need solution for this issue? Why is my SDK rejecting the nonce?

applicationID: sandbox-sq0idb-FquB-I6tsHka31FnNqH4sA

:waving_hand: 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

Customers API Workflows
Sandbox Payments
Create a Card on File and a Payment

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.

useless reply. doesn’t help

The issue you’re experiencing with the Square Node.js SDK rejecting the nonce likely stems from a mismatch in the environment or the way the nonce is being generated and used. Here are a few things to consider and check:

  1. Environment Consistency: Ensure that both your React Native application and your backend are using the same environment (Sandbox or Production). If the nonce is generated in the Sandbox environment, it must be used in the Sandbox environment on the backend as well.

  2. Nonce Validity: The nonce generated by the React Native SDK is a one-time-use token. Ensure that it is not being reused or expired by the time it reaches your backend. Nonces are designed to be used immediately after generation.

  3. SDK Version: You mentioned using version 34.0.1 of the Node.js SDK. Consider updating to the latest version of the SDK, as there might be bug fixes or improvements that could resolve your issue.

  4. Source ID Format: The error “invalid source ID” suggests that the nonce might not be in the expected format or is not recognized as valid. Double-check that the nonce is correctly passed from the frontend to the backend without any alterations.

  5. Permissions and Configuration: Ensure that your backend has the necessary permissions and is correctly configured to handle card creation. The access token used should have the appropriate scopes, such as PAYMENTS_WRITE and CUSTOMERS_WRITE.

  6. Testing with Known Values: You mentioned that using cnon:card-nonce-ok works fine. This indicates that the backend setup is correct, and the issue might be with the nonce generation or transmission from the React Native app.

By ensuring these aspects are correctly set up, you should be able to resolve the issue with the SDK rejecting the nonce. If the problem persists, consider logging the nonce and other request details to further diagnose the issue.

This answer was reviewed by @Bryan-Square.

Also have you initialized the Web Payments SDK with the same application ID and your using to call CreateCard? :slight_smile: