[SOLVED] - Square Credit Card implementation - Unhandled Promise Rejection Warning: TypeError: Do not know how to serialize a BigInt

sometime after calling

    const { result, statusCode } = await paymentsApi.createPayment(payment);

which is successfully returning the expected result

I’m getting the following error “TypeError: Do not know how to serialize a BigInt”

W20220901-17:55:03.993(9.5)? (STDERR) (node:10322) UnhandledPromiseRejectionWarning: TypeError: Do not know how to serialize a BigInt
W20220901-17:55:03.994(9.5)? (STDERR)     at JSON.stringify (<anonymous>)
W20220901-17:55:03.994(9.5)? (STDERR)     at Object.DDPCommon.stringifyDDP (packages/ddp-common/utils.js:116:15)
W20220901-17:55:03.994(9.5)? (STDERR)     at Session.send (packages/ddp-server/livedata_server.js:514:34)
W20220901-17:55:03.994(9.5)? (STDERR)     at packages/ddp-server/livedata_server.js:792:14
W20220901-17:55:03.994(9.5)? (STDERR)     at /Users/robgordon/.meteor/packages/promise/.0.12.0.qsgjpe.bwik++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
W20220901-17:55:03.994(9.5)? (STDERR) (node:10322) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)

Any suggestions?

Try this. It might work.

const  createPayment = Meteor.wrapAsync(paymentsApi.createPayment);
const {result, statusCode} = createPayment(payment);
1 Like

Good idea, but

(node:14066) UnhandledPromiseRejectionWarning: TypeError: this.createRequest is not a function

Server method …

Meteor.methods({
  'create.payment': async function createSquarePayment(token, locationId) {
    console.log('createSquarePayment');
    check(token, String);
    check(locationId, String);
    try {
      const { square } = Meteor.settings.private;
      const client = new Client({
        accessToken: square.SQUARE_ACCESS_TOKEN,
        environment: Environment.Sandbox,
      });
      const payment = {
        idempotencyKey: nanoid(),
        locationId,
        sourceId: token,
        amountMoney: {
          amount: '100',
          currency: 'AUD',
        },
      };
      const { result, statusCode } = await client.paymentsApi.createPayment(payment);
      console.log('Result:', result, statusCode);
      return { result, statusCode };
    } catch (error) {
      throw new Meteor.Error('error:', error);
    }
  },
});

Client

  const handleClick = async () => {
    const token = await tokenize(card);
    Meteor.call('create.payment', token, locationId, (err, result) => {
      if (err) return console.log(err);
      return console.log(result);
    });
  };

Console Output

 createSquarePayment
 Result: {
  payment: {
    id: 'dm6Hgk0B5kkPyMZa366W1mC8RYJZY',
    createdAt: '2022-09-02T01:57:41.815Z',
    amountMoney: { amount: 100n, currency: 'AUD' },
    status: 'COMPLETED',
  },
  }
 } 200

(node:14485) UnhandledPromiseRejectionWarning: TypeError: Do not know how to serialize a BigInt
   at JSON.stringify (<anonymous>)
   at Object.DDPCommon.stringifyDDP (packages/ddp-common/utils.js:116:15)
   at Session.send (packages/ddp-server/livedata_server.js:514:34)
   at packages/ddp-server/livedata_server.js:792:14
   at /Users/robgordon/.meteor/packages/promise/.0.12.0.qsgjpe.bwik++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
 (Use `node --trace-warnings ...` to show where the warning was created)
 (node:14485) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
 (node:14485) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

SOLUTION: In case anyone else requires this …

    const { result } = await paymentsApi.createPayment(payment);
    const paymentDetails = {
      id: result.payment.id,
      status: result.payment.status,
      receiptUrl: result.payment.receiptUrl,
      orderId: result.payment.orderId,
    };

By limiting the results I returned, the error is not triggered.
A {key:value} pair within the payment object was triggering the error.
I found it by stringifying the object

JSONBig.parse(JSONBig.stringify(result.payment)