Thursday, 11 May 2017

How to set card_nonce in Node Unirest (Square)

I am in the process of converting the Square REST API found here: https://docs.connect.squareup.com/api/connect/v2/#endpoint-createcustomercard into backend code that runs with Node.js. Currently, I am trying to add a card to an existing customer with the CreateCustomerCard endpoint.

However, when trying to submit the card_nonce I am receiving the following error:

errors: 
   [{ category: 'INVALID_REQUEST_ERROR',
   code: 'MISSING_REQUIRED_PARAMETER',
   detail: 'Field must be set',
   field: 'card_nonce'
}]

I am running the function from test.js as follows:

const square = require('./square'),
  unirest = require('unirest');

let access_token = 'access-token-string';

square.CustomerCards.createCustomerCard({
  "customer_id": 'customer-id-string',
  "card_nonce": 'card-nonce-string',
  "cardholder_name": 'Name Name'
}).then( response => {
  console.log(response.raw_body);
})

Which references customerCard.js

const unirest = require('unirest');

module.exports = {
  // Adds a card on file to an existing customer
  // Requires access_token, customer_id, and card_nonce
  createCustomerCard: function({access_token, card_nonce, billing_address, cardholder_name, customer_id}) {
    return new Promise((resolve, reject) => {
      unirest.post(this.domain + '/customers/' + customer_id + '/cards')
        // Required permissions: CUSTOMERS_WRITE
        .headers({
          'Content-Type': 'application/json',
          "Authorization": "Bearer " + access_token
        })
        .send({
          // Required: A card nonce representing the credit card to link to the customer.
          "card_nonce": card_nonce,
          "billing_address": billing_address,
          "cardholder_name": cardholder_name
        })
        .end(function (response) {
          resolve(response)
      })
    });
  };

I seem to be making a typo somewhere, but I can't find it. Any help is appreciated...



via KVNA

No comments:

Post a Comment