Change paypal example to square
[square_example_site.git] / confirmation.html.jst
1 let square = require("square")
2
3 return async env => {
4   let config = await env.site.get_json('/_config/config.json')
5
6   // Initialize the authorization for Square
7   let client = new square.Client(
8     {
9       accessToken: config.square_access_token,
10       environment:
11         config.environment === 'production' ?
12           square.Environment.Production :
13           square.Environment.Sandbox
14     }
15   )
16
17   let transaction_id = env.parsed_url.query.transactionId
18   let response = await client.ordersApi.retrieveOrder(transaction_id);
19   console.log('response', response)
20
21   // If there was an error with the request we will
22   // print them to the browser screen here
23   if (response.isError) {
24     let _out = []
25     _out.push('<!DOCTYPE html>')
26     html {
27       body {
28         'Api response has Errors'
29         ul {
30           for (let i = 0; i < response.errors.length; ++i)
31             li {`❌ ${error.detail}`}
32         }
33       }
34     }
35     env.site.serve(env, 200, Buffer.from(_out.join('')), 'checkout.html.jst')
36     return
37   }
38
39   let order = response.result.order
40
41   let _out = []
42   _out.push('<!DOCTYPE html>')
43   html {
44     head {
45       title {
46         'Checkout Confirmation'
47       }
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") {}
55     }
56     body {
57       header.container {
58         div#square-logo {}
59         h1.header {
60           'Custom Checkout Confirmation'
61         }
62       }
63       div.container#confirmation {
64         div {
65           div {
66             `Order ${order.id}`
67           }
68           div {
69             `Status: ${order.state}`
70           }
71         }
72         div {
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
78             div.item-line {
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)}`}
81             }
82           }
83
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)}`}
88           }
89         }
90         div {
91           span {
92             'Payment Successful!'
93           }
94           ' '
95           a(href="http://localhost:8080") {
96             'Back to home page'
97           }
98         }
99       }
100     }
101   }
102   env.site.serve(env, 200, Buffer.from(_out.join('')), 'confirmation.html.jst')
103 }