Creating a Payment Link in Google App Script

Here is the function from my script:

> function generatePaymentLink(amount, form_org) {
>   var accessToken = debugMode ? "SANDBOX_ACCESS_TOKEN" : "PRODUCTION_ACCESS_TOKEN";
>   var apiBaseUrl = debugMode ? "https://connect.squareupsandbox.com" : "https://connect.squareup.com";
>   var locationId = debugMode ? "SANDBOX_LOCATION_ID" : "PRODUCTION_LOCATION_ID";
> 
>   var requestBody = {
>   idempotency_key: Utilities.getUuid(),
>   order: {
>     location_id: locationId,
>     line_items: [{
>       name: "Parking Pass Purchase",
>       quantity: "1",
>       base_price_money: {
>         amount: amount * 100,
>         currency: "CAD"
>       }
>     }]
>   },
>   redirect_url: "https://pridewinnipeg.com/",
>   ask_for_shipping_address: false
> };
> 
>   var options = {
>     method: "post",
>     contentType: "application/json",
>     headers: {
>       "Authorization": "Bearer " + accessToken
>     },
>     payload: JSON.stringify(requestBody),
>     muteHttpExceptions: true
>   };
> 
>   // Log the request body
>   Logger.log("Request Body: " + JSON.stringify(requestBody));
> 
>   var response = UrlFetchApp.fetch(apiBaseUrl + "/v2/locations/" + locationId + "/checkouts", options);
> 
>   // Log the entire API response
>   Logger.log("API Response: " + response.getContentText());
> 
>   var jsonResponse = JSON.parse(response.getContentText());
> 
>   if (response.getResponseCode() == 200) {
>     return jsonResponse.checkout.checkout_page_url;
>   } else {
>     // Handle errors
>     Logger.log("Error creating Square checkout: " + jsonResponse.errors[0].detail);
>     return null;
>   }
> }

The response I keep getting however is:

Mar 24, 2024, 4:07:18 PM

Info

API Response: {“errors”:[{“category”:“INVALID_REQUEST_ERROR”,“code”:“MISSING_REQUIRED_PARAMETER”,“detail”:“Missing required parameter.”,“field”:“order”}]}

REFRESH

I am really struggling with how to interpret or troubleshoot this and would appreciate if anybody could weigh in…

:wave: 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/get-started/make-api-request
https://developer.squareup.com/docs/checkout-api/square-order-checkout

Additional Documentation

https://developer.squareup.com/docs/checkout-api/quick-pay-checkout
https://developer.squareup.com/docs/checkout-api
https://developer.squareup.com/docs/checkout-api-overview

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.

The problem I am running into appears to be that the .JSON being sent is missing data in the order field… it’s clearly there in the sent request for my script but not being received by square…

Cannot see anything in the documentation that indicates that I am formatting this incorrectly.

What is your application ID? :slightly_smiling_face:

I figured out what the issue was, basically came down to brute force repetition.

Firstly, what works better for this use case is quickpay.

WIthin that I received repetative confusing errors which came down to the headers needing to be in snake_case over camelCase. This caused some confusion because the error message I receive from square refers to these headers in camelCase, so I pretty much had to disover this through trial and error.,

Glad to hear that you figured out the issue. :slightly_smiling_face: