Search code examples
salesforceapexsquare

How to create customer card in Square using Apex in salesforce?


We had a requirement to create customers and save their credit card information in Square using salesforce and them we had to charge payments of customer later through apex code(in salesforce). So for this, we read and understand the Square API doc as a developer do. After reading this link https://developer.squareup.com/reference/square/customers-api/create-customer we were able to create the customer from salesforce to square using Square API. the Square API documentation is ready good easy to read. But when we started to implement the functionality to save credit card for customers Square we got stuck because while creating/saving customer card in Square there were two things required "customer_id" and "card_nonce". So we had customers Id because we had created the customer already in Square using apex code in salesforce. But we did not have "nonce" What does a Square nonce represent? and the only way to generate the nonce as per Square API documentation is "Square Payment Form" https://developer.squareup.com/docs/payment-form/payment-form-walkthrough, How to get Nonce for Card using Square API from Backend So I spent more much time to find workarounds but all the efforts went wrong. I also tried to create VF page and pass all the customer and card info we already had in salesforce object to that Square payment form from apex code but JS did not execute and Square payment form only work with JS SDK.

Does anyone have an idea about how can we generate "nonce" from backed (apex code) to save cards for a customer in Square?


Solution

  • There is no way to create a nonce without using the Square Payment Form. You also can not pass raw data to the Square Payment Form, as it does not contain functions to fill in card data (other than the postal code). Someone would need to manually type that information into a site using HTTPS hosting the Square Payment Form in order to generate a legitiate