Skip to content

Commit c83cea0

Browse files
committed
feat: Add Razorpay integration for cart-order page payments
1 parent 952b028 commit c83cea0

3 files changed

Lines changed: 127 additions & 8 deletions

File tree

internal/handler/user.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,18 @@ func CartOrderPost(c echo.Context) error {
223223

224224
var cart = utils.GetSessionAll(c, "cart")
225225
var orderId = uuid.New()
226+
body.Id = orderId.String()
226227
var total int = 0
227228
for id, size := range cart {
228229
var product, _ = mysql.GetProductById(id.(string))
229230
var order models.Order = body
230231

231232
order.ProductId = product.Id
232-
order.Status = "ordered"
233+
if body.Payment == "online" {
234+
order.Status = "pending"
235+
} else {
236+
order.Status = "ordered"
237+
}
233238
order.Quantity = 1
234239
order.Total = product.Price
235240
order.Size = size.(string)
@@ -240,8 +245,23 @@ func CartOrderPost(c echo.Context) error {
240245
}
241246
total += order.Total
242247
}
248+
if body.Payment == "online" {
249+
var key = os.Getenv("RAZORPAY_KEY")
250+
var secret = os.Getenv("RAZORPAY_SECRET")
251+
var client = razorpay.NewClient(key, secret)
252+
var ord = map[string]interface{}{
253+
"amount": total * 100,
254+
"currency": "INR",
255+
}
256+
order, err := client.Order.Create(ord, nil)
257+
if err != nil {
258+
fmt.Println(err)
259+
}
260+
fmt.Println("submit cart")
243261

244-
utils.DeleteSession(c, "cart")
262+
var component = user.OnlinePaymentCart(body, order, key)
263+
return utils.Render(c, component)
264+
}
245265

246266
if c.Request().Header.Get("HX-Request") == "true" {
247267
c.Response().Header().Set("HX-Location", "/cart")

view/user/cart-order.templ

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
templ CartOrder(products []models.Product) {
1212
@layout.User() {
13+
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
1314
<link rel="stylesheet" href="/static/styles/user/order.css">
1415
<div class={Container()}>
1516
<h1>Place Order</h1>
@@ -25,7 +26,7 @@ templ CartOrder(products []models.Product) {
2526
<hr>
2627
}
2728
</div>
28-
<form hx-post="/cart/order">
29+
<form hx-post="/cart/order" hx-target="closest form" hx-swap="afterend">
2930
<input type="text" name="name" placeholder="Name"/>
3031
<textarea name="address" placeholder="Adress"></textarea>
3132
<input type="text" name="house" placeholder="House no. or House name"/>
@@ -41,4 +42,61 @@ templ CartOrder(products []models.Product) {
4142
</div>
4243
</div>
4344
}
45+
}
46+
templ OnlinePaymentCart(order models.Order,ord map[string]interface{},key string) {
47+
@templ.JSONScript("order-data", order)
48+
@templ.JSONScript("ord-data", ord)
49+
@templ.JSONScript("key-data", key)
50+
51+
<script id="order-data" type="application/json"></script>
52+
<script id="ord-data" type="application/json"></script>
53+
<script id="key-data" type="application/json"></script>
54+
55+
<script>
56+
var order = JSON.parse(document.getElementById("order-data").textContent)
57+
var ord = JSON.parse(document.getElementById("ord-data").textContent)
58+
var key = JSON.parse(document.getElementById("key-data").textContent)
59+
console.log(order)
60+
61+
var options = {
62+
key: key,
63+
amount: order.Total,
64+
currency: "INR",
65+
name: "Stitch",
66+
description: "Cloth Shop",
67+
"order_id": ord.id,
68+
handler: async function (response) {
69+
var url = "/payment-verification/" + order.id
70+
var body = JSON.stringify({"orderId": response.razorpay_order_id, "paymentId": response.razorpay_payment_id, "razorpaySignature": response.razorpay_signature})
71+
console.log(body)
72+
var res = await fetch(url,{method: "POST", headers: {"Content-Type": "application/json"}, body:body})
73+
if (res.ok) {
74+
var json = await res.json()
75+
alert(json)
76+
} else {
77+
var json = await res.json()
78+
alert(json)
79+
}
80+
},
81+
modal: {
82+
ondismiss: async function() {
83+
var url = "/delete-order/" + order.id
84+
var res = await fetch(url)
85+
if (res.ok) {
86+
var json = await res.json()
87+
alert(json)
88+
}
89+
}
90+
},
91+
prefil: {
92+
name: order.Name,
93+
contact: order.Phone
94+
},
95+
theme: "#ff0000"
96+
}
97+
98+
var raz = new Razorpay(options)
99+
raz.open();
100+
101+
</script>
44102
}

view/user/cart-order_templ.go

Lines changed: 46 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)