-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (49 loc) · 1.56 KB
/
server.js
File metadata and controls
56 lines (49 loc) · 1.56 KB
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
const express =require("express")
const cors =require("cors")
const stripe =require("stripe")("sk_test_51Jgto9SHO5t3rrNYzax7fLMFDdk9SiogX4UPzLBb0AcbI0DbykOzwEOjISHN2JC1yL7SSJRF2k0fvjntKJXOYb2r00R4sGGi1q")
const { v4: uuidv4 } =require('uuid');
const app=express();
app.use(cors());
app.use(express.json()) ;
app.get("/",(req,res)=>{
res.send("welcome into react shop")
})
app.post("/checkout",async(req,res)=>{
let error;
let status;
try{
const {product,token}=req.body ;
const customer= await stripe.customers.create({
email:token.email,
source:token.id
})
const key =uuidv4();
const charge =await stripe.charges.create(
{
amount:product.price*100,
currency:"usd",
customer:customer.id,
receipt_email:token.email,
description:"all products description",
shipping:{
name:token.card.name,
address:{
line1:token.card.address_line1,
line2:token.card.address_line2,
city:token.card.address_city,
country:token.card.address_country,
postal_code:token.card.address_zip
}
}
},
{idempotencyKey:key})
status="success";
}catch (error){
console.log(error)
status="error";
}
res.json({status})
} )
app.listen(8080,()=>{
console.log("Your app is running")
})