Can I Use Square’s Checkout API (Not SDK) to Retrieve My Shopping Cart’s Checkout Page Grand Total?

Introduction

For many years I’ve been using merchant gateway providers such as Secure Trading and Cardnet, to process payments that I receive from customers via my shopping cart. Hosted payments, means customers are redirected to the provider’s platform for payment processing, in contrast to using the above-mentioned providers’ API option to process the payment directly on my website/shopping cart. However, I’ve always used their easiest option, which is hosted payments.

Having front-end programming experience and sufficient PHP experience, it was quite an easy task to learn how to add the little bit of PHP code required, and the HTML form which is submitted to the gateway company for them to process the card payment.

For example, this is the form input for the grand total…

<input type="hidden" name="hash" value="<php echo createHash( strval($grand_total), "826" ) >"/>

Note for users on this Square forum: I’m getting a 403 error when trying to include the “?” of the “php” tag in the above code snippet, when trying to post it here! Anyway let’s move on…

As you can see, to retrieve the grand total to be submitted, is as simple as injecting the PHP variable $grand_total

Incidentally, I programmed my own shopping cart, i.e. it’s not a known brand such as Shopify or WooCommerce, which I appreciate must be much more sophisticated.

My Current Situation with Square

I’m currently using “Create a link” >> “Collect a payment” (and selecting “Allow buyer to set the price”) >> “Create link” >> “Buy button

I’ve then placed the button on my checkout page, such that it appears only after the customer has entered their details and clicked “Make Card Payment”.

That works okay, but the problem is, the customer has to enter the grand total themselves as the payment amount, in the payment window that has opened in a new tab. Ideally, and I guess to be more professional, I need the payment amount to be set programmatically so that it becomes the grand total… it’d be nice if I could achieve this as simply as is done via injecting the PHP variable in my above example, or for example by using JavaScript to set the payment value.

Does Square Provide A Simple Solution?

I’m currently looking at the following page: Online Payment API for Developers

I’m hoping to use “Square Checkout API”, for which it states, “Opt for our simple, Square-hosted solution to integrate payments into any workflow with minimal coding.

When I ask Google…

Can I use Square Checkout API to programmatically set the payment value?”

It answers…

Yes, the Square Checkout API allows you to programmatically set the payment amount. You can create a payment link or use the Payments API with the desired amount when creating the payment object.

When I click through to the “Create payment link” page here: POST /v2/online-checkout/payment-links - Square API Reference, I can see the script via cURL, and beneath that the Response JSON, but I suspect I’m in over my head because the script itself starting “POST /v2/online-checkout/payment-links”, I have no idea about, i.e. I mean, what language is it? How would I go about learning how to implement this from my perspective of having just the essential PHP experience that I’ve needed so far?

Obviously I’m missing something fundamental, I realise that, but what is it?

Square, as I’ve quoted above, has stated “… with minimal coding.”. But is that minimal coding based on having to use some kind of external payment processing platform? I.e. so is it not something I can achieve myself via my own programming experience and just Square’s functionality?

Sorry if that’s a long read, but I just want to try and provide a full picture of where I’m at.

Cheers, Gary.

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

https://developer.squareup.com/docs/squarebridge/business-central/payments/troubleshooting

Additional Documentation

Get Started
Postman
Get Started

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.

So, I’ve been reading that cURL, although originated as a command-line tool, later became widely used in backend programming languages, e.g. PHP cURL, and that “most people who want to send page requests from PHP use the cURL library”.

The following page includes a “Create a quick pay checkout page” example: Quick Pay Checkout

Please if someone could help with the following two questions…

  1. Is the 16-line cURL code example on the following page correct for use as PHP? Quick Pay Checkout
  2. I already have a Square account, but do I need a Square development account in order to use the Checkout API?

I moved on to here: HTTPS via curl_init() versus HTTPS via -X POST - Questions - Square Developer Forums