-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.js
272 lines (252 loc) · 9.05 KB
/
checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { useState } from 'react'
import Head from 'next/head'
import { SiteContext, ContextProviderComponent } from "../context/mainContext"
import DENOMINATION from "../utils/currencyProvider"
import { FaLongArrowAltLeft } from "react-icons/fa"
import Link from "next/link"
import Image from "../components/Image"
import { v4 as uuid } from "uuid"
import {
CardElement,
Elements,
useStripe,
useElements,
} from "@stripe/react-stripe-js"
import { loadStripe } from "@stripe/stripe-js"
// Make sure to call `loadStripe` outside of a component’s render to avoid
// recreating the `Stripe` object on every render.
const stripePromise = loadStripe("xxx-xxx-xxx")
function CheckoutWithContext(props) {
return (
<ContextProviderComponent>
<SiteContext.Consumer>
{context => (
<Elements stripe={stripePromise}>
<Checkout {...props} context={context} />
</Elements>
)}
</SiteContext.Consumer>
</ContextProviderComponent>
)
}
const calculateShipping = () => {
return 0
}
const Input = ({ onChange, value, name, placeholder }) => (
<input
onChange={onChange}
value={value}
className="mt-2 text-sm shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
type="text"
placeholder={placeholder}
name={name}
/>
)
const Checkout = ({ context }) => {
const [errorMessage, setErrorMessage] = useState(null)
const [orderCompleted, setOrderCompleted] = useState(false)
const [input, setInput] = useState({
name: "",
email: "",
street: "",
city: "",
postal_code: "",
state: "",
})
const stripe = useStripe()
const elements = useElements()
const onChange = e => {
setErrorMessage(null)
setInput({ ...input, [e.target.name]: e.target.value })
}
const handleSubmit = async event => {
event.preventDefault()
const { name, email, street, city, postal_code, state } = input
const { total, clearCart } = context
if (!stripe || !elements) {
// Stripe.js has not loaded yet. Make sure to disable
// form submission until Stripe.js has loaded.
return
}
// Validate input
if (!street || !city || !postal_code || !state) {
setErrorMessage("Please fill in the form!")
return
}
// Get a reference to a mounted CardElement. Elements knows how
// to find your CardElement because there can only ever be one of
// each type of element.
const cardElement = elements.getElement(CardElement)
// Use your card Element with other Stripe.js APIs
const { error, paymentMethod } = await stripe.createPaymentMethod({
type: "card",
card: cardElement,
billing_details: { name: name },
})
if (error) {
setErrorMessage(error.message)
return
}
const order = {
email,
amount: total,
address: state, // should this be {street, city, postal_code, state} ?
payment_method_id: paymentMethod.id,
receipt_email: "[email protected]",
id: uuid(),
}
// TODO call API
setOrderCompleted(true)
clearCart()
}
const { numberOfItemsInCart, cart, total } = context
const cartEmpty = numberOfItemsInCart === Number(0)
if (orderCompleted) {
return (
<div>
<h3>Thanks! Your order has been successfully processed.</h3>
</div>
)
}
return (
<div className="flex flex-col items-center pb-10">
<Head>
<title>Jamstack ECommerce - Checkout</title>
<meta name="description" content={`Check out`} />
<meta property="og:title" content="Jamstack ECommerce - Checkpit" key="title" />
</Head>
<div
className="
flex flex-col w-full
c_large:w-c_large
"
>
<div className="pt-10 pb-8">
<h1 className="text-5xl font-light mb-6">Checkout</h1>
<Link href="/cart">
<a aria-label="Cart">
<div className="cursor-pointer flex items-center">
<FaLongArrowAltLeft className="mr-2 text-gray-600" />
<p className="text-gray-600 text-sm">Edit Cart</p>
</div>
</a>
</Link>
</div>
{cartEmpty ? (
<h3>No items in cart.</h3>
) : (
<div className="flex flex-col">
<div className="">
{cart.map((item, index) => {
return (
<div className="border-b py-10" key={index}>
<div className="flex items-center">
<Image
className="w-32 m-0"
src={item.image}
alt={item.name}
/>
<p className="m-0 pl-10 text-gray-600">
{item.name}
</p>
<div className="flex flex-1 justify-end">
<p className="m-0 pl-10 text-gray-900 font-semibold">
{DENOMINATION + item.price}
</p>
</div>
</div>
</div>
)
})}
</div>
<div className="flex flex-1 flex-col md:flex-row">
<div className="flex flex-1 pt-8 flex-col">
<div className="mt-4 border-t pt-10">
<form onSubmit={handleSubmit}>
{errorMessage ? <span>{errorMessage}</span> : ""}
<Input
onChange={onChange}
value={input.name}
name="name"
placeholder="Cardholder name"
/>
<CardElement className="mt-2 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" />
<Input
onChange={onChange}
value={input.email}
name="email"
placeholder="Email"
/>
<Input
onChange={onChange}
value={input.street}
name="street"
placeholder="Street"
/>
<Input
onChange={onChange}
value={input.city}
name="city"
placeholder="City"
/>
<Input
onChange={onChange}
value={input.state}
name="state"
placeholder="State"
/>
<Input
onChange={onChange}
value={input.postal_code}
name="postal_code"
placeholder="Postal Code"
/>
<button
type="submit"
disabled={!stripe}
onClick={handleSubmit}
className="hidden md:block bg-primary hover:bg-black text-white font-bold py-2 px-4 mt-4 rounded focus:outline-none focus:shadow-outline"
type="button"
>
Confirm order
</button>
</form>
</div>
</div>
<div className="md:pt-20">
<div className="pl-4 flex flex-1 pt-2 md:pt-8 mt-2 sm:mt-0">
<p className="text-sm pr-10 text-left">Subtotal</p>
<p className="w-38 flex text-right justify-end">
{DENOMINATION + total}
</p>
</div>
<div className="pl-4 flex flex-1 my-2">
<p className="text-sm pr-10">Shipping</p>
<p className="w-38 flex justify-end">
FREE SHIPPING
</p>
</div>
<div className="md:ml-4 pl-2 flex flex-1 bg-gray-200 pr-4 pb-1 pt-2 mt-2">
<p className="text-sm pr-10">Total</p>
<p className="font-semibold w-38 flex justify-end">
{DENOMINATION + (total + calculateShipping())}
</p>
</div>
<button
type="submit"
disabled={!stripe}
onClick={handleSubmit}
className="md:hidden bg-primary hover:bg-black text-white font-bold py-2 px-4 mt-4 rounded focus:outline-none focus:shadow-outline"
type="button"
>
Confirm order
</button>
</div>
</div>
</div>
)}
</div>
</div>
)
}
export default CheckoutWithContext