let square = require("square") return async env => { let config = await env.site.get_json('/_config/config.json') // Initialize the authorization for Square let client = new square.Client( { accessToken: config.square_access_token, environment: config.environment === 'production' ? square.Environment.Production : square.Environment.Sandbox } ) let transaction_id = env.parsed_url.query.transactionId let response = await client.ordersApi.retrieveOrder(transaction_id); console.log('response', response) // If there was an error with the request we will // print them to the browser screen here if (response.isError) { let _out = [] _out.push('') html { body { 'Api response has Errors' ul { for (let i = 0; i < response.errors.length; ++i) li {`❌ ${error.detail}`} } } } env.site.serve(env, 200, Buffer.from(_out.join('')), 'checkout.html.jst') return } let order = response.result.order let _out = [] _out.push('') html { head { title { 'Checkout Confirmation' } meta(name="description" content="An example of Square Checkout on Glitch") {} link(rel="stylesheet" href="/main.css") {} link(rel="stylesheet" href="/normalize.css") {} link#favicon(rel="icon" href="https://cdn.glitch.com/4c9bc573-ca4c-48de-8afe-501eddad0b79%2Fsquare-logo.svg?1521834224783" type="image/x-icon") {} meta(charset="utf-8") {} meta(http-equiv="X-UA-Compatible" content="IE=edge") {} meta(name="viewport" content="width=device-width,initial-scale=1") {} } body { header.container { div#square-logo {} h1.header { 'Custom Checkout Confirmation' } } div.container#confirmation { div { div { `Order ${order.id}` } div { `Status: ${order.state}` } } div { for (let i = 0; i < order.lineItems.length; ++i) { let line_item = order.lineItems[i] console.log('line_item.totalMoney', line_item.totalMoney) console.log('line_item.totalMoney.amount', line_item.totalMoney.amount) // Display each line item in the order, you may want to consider formatting the money amount using different currencies div.item-line { div.item-label {`${line_item.name} x ${line_item.quantity}`} div.item-amount {`$${line_item.totalMoney.amount / BigInt('100')}.${('0' + line_item.totalMoney.amount % BigInt('100')).slice(-2)}`} } } // Display total amount paid for the order, you may want to consider formatting the money amount using different currencies div.item-line.total-line { div.item-label {'Total'} div.item-amount {`$${order.totalMoney.amount / BigInt('100')}.${('0' + order.totalMoney.amount % BigInt('100')).slice(-2)}`} } } div { span { 'Payment Successful!' } ' ' a(href="http://localhost:8080") { 'Back to home page' } } } } } env.site.serve(env, 200, Buffer.from(_out.join('')), 'confirmation.html.jst') }