I am using Square’s API to create a checkout link. The API request succeeds, and I receive a valid response, including the checkout URL. However, when I attempt to open the generated URL, I see a blank page with the error message: “Something went wrong”.
Code Snippet:
Below is the CreateCheckout
function used to create the checkout link:
func CreateCheckout(amount int64, buyerEmail string) (*CreateCheckoutResponse, error) {
idempotencyKey := uuid.NewString()
var redirectURL string
if config.Env.Environment == "development" {
redirectURL = "http://localhost:4321/"
} else {
redirectURL = "{PROD_URL}"
}
reqBody := createCheckoutRequest{
CheckoutOptions: checkoutOptions{
AllowTipping: false,
AskForShippingAddress: false,
EnableCoupon: false,
EnableLoyalty: false,
MerchantSupportEmail: "support@{DOMAIN}",
RedirectURL: redirectURL,
},
IdempotencyKey: idempotencyKey,
QuickPay: quickPay{
LocationID: "{LOCATION_ID}",
Name: "Deposit",
PriceMoney: priceMoney{
Amount: amount,
Currency: "USD",
},
},
PrePopulatedData: prePopulatedData{
BuyerEmail: buyerEmail,
},
}
data, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
var url string
if config.Env.Environment == "development" {
url = "https://connect.squareupsandbox.com/v2/online-checkout/payment-links"
} else {
url = "https://connect.squareup.com/v2/online-checkout/payment-links"
}
req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Square-Version", "2024-11-20")
req.Header.Set("Authorization", "Bearer "+config.Env.SquareAccessToken)
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("received non-2xx response: %d", resp.StatusCode)
}
var result CreateCheckoutResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
Error Page Screenshot:
Issue Details:
- The API request to create the checkout link returns a successful response with a
200 OK
status code. - The returned URL appears valid.
- When I visit the URL in a browser, I receive a “Something went wrong” error and the page remains blank.
I cannot continue development until this is solved, any help is appreciated thank you!