let square = require("square") let uuid = require("uuid") 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 } ) // Monetary amounts are specified in the smallest unit of the applicable currency. // This amount is in cents. It's also hard-coded for $1.00, which isn't very useful. // Set currency to the currency for the location let currency = ( await client.locationsApi.retrieveLocation(config.square_location_id) ).result.location.currency let money_A = {currency: currency, amount: 500} let item_A = {quantity: '1', name: 'Test Item A', basePriceMoney: money_A} let money_B = {currency: currency, amount: 1000} let item_B = {quantity: '3', name: 'Test Item B', basePriceMoney: money_B} // Create a new order and add the line items as necessary. let order = { locationId: config.square_location_id, lineItems: [item_A, item_B] } let order_request = {order: order} // Similar to payments you must have a unique idempotency key. // Set a custom redirect URL, otherwise a default Square confirmation page will be used let checkout_request = { idempotencyKey: uuid.v4(), order: order_request, redirectUrl: 'http://localhost:8080/confirmation.html' } let response = await client.checkoutApi.createCheckout( config.square_location_id, checkout_request ) 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 } // This redirects to the Square hosted checkout page env.response.setHeader('Location', response.result.checkout.checkoutPageUrl) env.site.serve(env, 301, Buffer.alloc(0), 'checkout.html.jst') }