Skip to content

Commit ab98579

Browse files
committed
22. Back to the Client | 16. Paying for an Order
1 parent 220694a commit ab98579

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

22_Back_to_the_Client.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ If chome will block page for security reasons, type: **thisisunsafe**
8383

8484
<br/>
8585

86+
### 14. Configuring Stripe
87+
88+
<br/>
89+
90+
### 15. Test Credit Card Numbers
91+
92+
<br/>
93+
94+
### 16. Paying for an Order
95+
96+
<br/>
97+
8698
---
8799

88100
<br/>

22_Back_to_the_Client/app/client/hooks/use-request.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import { useState } from 'react';
55
export default ({ url, method, body, onSuccess }) => {
66
const [errors, setErrros] = useState(null);
77

8-
const doRequest = async () => {
8+
const doRequest = async (props = {}) => {
99
try {
1010
setErrros(null);
11-
const response = await axios[method](url, body);
11+
const response = await axios[method](url, {
12+
...body,
13+
...props,
14+
});
1215

1316
if (onSuccess) {
1417
onSuccess(response.data);

22_Back_to_the_Client/app/client/pages/orders/[orderId].js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { useEffect, useState } from 'react';
2+
import StripeCheckout from 'react-stripe-checkout';
3+
import useRequest from '../../hooks/use-request';
24

3-
const OrderShow = ({ order }) => {
5+
const OrderShow = ({ order, currentUser }) => {
46
const [timeLeft, setTimeLeft] = useState(0);
57

8+
const { doRequest, errors } = useRequest({
9+
url: '/api/payments',
10+
method: 'post',
11+
body: {
12+
orderId: order.id,
13+
},
14+
onSuccess: (payment) => console.log(payment),
15+
});
16+
617
useEffect(() => {
718
const findTimeLeft = () => {
819
const msLeft = new Date(order.expiresAt) - new Date();
@@ -20,7 +31,18 @@ const OrderShow = ({ order }) => {
2031
return <div>Order Expired</div>;
2132
}
2233

23-
return <div>TIme left to pay: {timeLeft} seconds</div>;
34+
return (
35+
<div>
36+
Time left to pay: {timeLeft} seconds <br />
37+
<StripeCheckout
38+
token={({ id }) => doRequest({ token: id })}
39+
stripeKey="pk_test_LQWgtToPhlyDhBDmdn9ULxmn00WVghorjW"
40+
amount={order.ticket.price * 100}
41+
email={currentUser.email}
42+
/>
43+
{errors}
44+
</div>
45+
);
2446
};
2547

2648
OrderShow.getInitialProps = async (context, client) => {

22_Back_to_the_Client/app/client/pages/tickets/[ticketId].js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const TicketShow = ({ ticket }) => {
1717
<h1>{ticket.title}</h1>
1818
<h4>Price: {ticket.price}</h4>
1919
{errors}
20-
<button className="btn btn-primary" onClick={doRequest}>
20+
<button className="btn btn-primary" onClick={() => doRequest()}>
2121
Purchase
2222
</button>
2323
</div>

0 commit comments

Comments
 (0)