1 let square = require("square")
4 let config = await env.site.get_json('/_config/config.json')
6 // Initialize the authorization for Square
7 let client = new square.Client(
9 accessToken: config.square_access_token,
11 config.environment === 'production' ?
12 square.Environment.Production :
13 square.Environment.Sandbox
17 let transaction_id = env.parsed_url.query.transactionId
18 let response = await client.ordersApi.retrieveOrder(transaction_id);
19 console.log('response', response)
21 // If there was an error with the request we will
22 // print them to the browser screen here
23 if (response.isError) {
25 _out.push('<!DOCTYPE html>')
28 'Api response has Errors'
30 for (let i = 0; i < response.errors.length; ++i)
31 li {`❌ ${error.detail}`}
35 env.site.serve(env, 200, Buffer.from(_out.join('')), 'checkout.html.jst')
39 let order = response.result.order
42 _out.push('<!DOCTYPE html>')
46 'Checkout Confirmation'
48 meta(name="description" content="An example of Square Checkout on Glitch") {}
49 link(rel="stylesheet" href="/main.css") {}
50 link(rel="stylesheet" href="/normalize.css") {}
51 link#favicon(rel="icon" href="https://cdn.glitch.com/4c9bc573-ca4c-48de-8afe-501eddad0b79%2Fsquare-logo.svg?1521834224783" type="image/x-icon") {}
52 meta(charset="utf-8") {}
53 meta(http-equiv="X-UA-Compatible" content="IE=edge") {}
54 meta(name="viewport" content="width=device-width,initial-scale=1") {}
60 'Custom Checkout Confirmation'
63 div.container#confirmation {
69 `Status: ${order.state}`
73 for (let i = 0; i < order.lineItems.length; ++i) {
74 let line_item = order.lineItems[i]
75 console.log('line_item.totalMoney', line_item.totalMoney)
76 console.log('line_item.totalMoney.amount', line_item.totalMoney.amount)
77 // Display each line item in the order, you may want to consider formatting the money amount using different currencies
79 div.item-label {`${line_item.name} x ${line_item.quantity}`}
80 div.item-amount {`$${line_item.totalMoney.amount / BigInt('100')}.${('0' + line_item.totalMoney.amount % BigInt('100')).slice(-2)}`}
84 // Display total amount paid for the order, you may want to consider formatting the money amount using different currencies
85 div.item-line.total-line {
86 div.item-label {'Total'}
87 div.item-amount {`$${order.totalMoney.amount / BigInt('100')}.${('0' + order.totalMoney.amount % BigInt('100')).slice(-2)}`}
95 a(href="http://localhost:8080") {
102 env.site.serve(env, 200, Buffer.from(_out.join('')), 'confirmation.html.jst')