-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🔥added webhook support #61
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||
package handlers | ||||||
|
||||||
import ( | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"log" | ||||||
"time" | ||||||
|
||||||
"github.com/gofiber/fiber/v2" | ||||||
"github.com/stripe/stripe-go/v74" | ||||||
|
@@ -10,6 +13,7 @@ import ( | |||||
"github.com/shurco/litecart/internal/models" | ||||||
"github.com/shurco/litecart/internal/queries" | ||||||
"github.com/shurco/litecart/pkg/security" | ||||||
"github.com/shurco/litecart/pkg/webhook" | ||||||
"github.com/shurco/litecart/pkg/webutil" | ||||||
) | ||||||
|
||||||
|
@@ -41,6 +45,7 @@ func Checkout(c *fiber.Ctx) error { | |||||
|
||||||
lineItems := []*stripe.CheckoutSessionLineItemParams{} | ||||||
for _, item := range products.Products { | ||||||
|
||||||
images := []string{} | ||||||
for _, image := range item.Images { | ||||||
path := fmt.Sprintf("https://%s/uploads/%s_md.%s", domain, image.Name, image.Ext) | ||||||
|
@@ -93,6 +98,38 @@ func Checkout(c *fiber.Ctx) error { | |||||
PaymentStatus: string(stripeSession.PaymentStatus), | ||||||
}) | ||||||
|
||||||
if err = settingStripe.Payment.Validate(); err != nil { | ||||||
log.Printf("update payment webhook url", err) | ||||||
} else { | ||||||
resData := map[string]any{ | ||||||
"event": "payment_initiation", | ||||||
"timestamp": stripeSession.Created, | ||||||
"data": map[string]any{ | ||||||
"payment_id": stripeSession.ID, | ||||||
"total_amount": stripeSession.AmountTotal, | ||||||
"currency": stripeSession.Currency, | ||||||
"cart_items": items, | ||||||
}, | ||||||
} | ||||||
|
||||||
jsonData, err := json.Marshal(resData) | ||||||
if err != nil { | ||||||
log.Println("Error:", err) | ||||||
} | ||||||
|
||||||
go func() { | ||||||
res, err := webhook.SendHook(settingStripe.Payment.WebhookUrl, jsonData) | ||||||
|
||||||
if err != nil { | ||||||
log.Println(err) | ||||||
} | ||||||
if res.Status != "200 OK" { | ||||||
log.Print("An issue has been identified with the payment webhook URL. Please verify that it responds with a status code of 200 OK.") | ||||||
} | ||||||
}() | ||||||
|
||||||
} | ||||||
|
||||||
return webutil.Response(c, fiber.StatusOK, "Checkout url", stripeSession.URL) | ||||||
} | ||||||
|
||||||
|
@@ -101,6 +138,8 @@ func Checkout(c *fiber.Ctx) error { | |||||
func CheckoutSuccess(c *fiber.Ctx) error { | ||||||
db := queries.DB() | ||||||
|
||||||
settingStripe, err := db.SettingStripe() | ||||||
|
||||||
cartID := c.Params("cart_id") | ||||||
sessionID := c.Params("session_id") | ||||||
|
||||||
|
@@ -122,6 +161,40 @@ func CheckoutSuccess(c *fiber.Ctx) error { | |||||
return webutil.StatusBadRequest(c, err) | ||||||
} | ||||||
|
||||||
if err = settingStripe.Payment.Validate(); err != nil { | ||||||
log.Printf("update payment webhook url", err) | ||||||
} else { | ||||||
resData := map[string]any{ | ||||||
"event": "payment_success", | ||||||
"timestamp": sessionStripe.Created, | ||||||
"data": map[string]any{ | ||||||
"payment_system": "stripe", | ||||||
"cart_id": cartID, | ||||||
"payment_id": sessionStripe.PaymentIntent.ID, | ||||||
"total_amount": sessionStripe.PaymentIntent.Amount, | ||||||
"currency": sessionStripe.PaymentIntent.Currency, | ||||||
"user_email": sessionStripe.Customer.Email, | ||||||
}, | ||||||
} | ||||||
|
||||||
jsonData, err := json.Marshal(resData) | ||||||
if err != nil { | ||||||
log.Println("Error:", err) | ||||||
} | ||||||
|
||||||
go func() { | ||||||
res, err := webhook.SendHook(settingStripe.Payment.WebhookUrl, jsonData) | ||||||
|
||||||
if err != nil { | ||||||
log.Println(err) | ||||||
} | ||||||
|
||||||
if res.Status != "200 OK" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may be use
Suggested change
|
||||||
log.Print("An issue has been identified with the payment webhook URL. Please verify that it responds with a status code of 200 OK.") | ||||||
} | ||||||
}() | ||||||
} | ||||||
|
||||||
return c.Render("success", nil, "layouts/main") | ||||||
} | ||||||
|
||||||
|
@@ -130,15 +203,55 @@ func CheckoutSuccess(c *fiber.Ctx) error { | |||||
func CheckoutCancel(c *fiber.Ctx) error { | ||||||
cartID := c.Params("cart_id") | ||||||
db := queries.DB() | ||||||
err := db.UpdateCart(&models.Cart{ | ||||||
settingStripe, err := db.SettingStripe() | ||||||
cartStripe, err := db.Cart(cartID) | ||||||
|
||||||
err = db.UpdateCart(&models.Cart{ | ||||||
Core: models.Core{ | ||||||
ID: cartID, | ||||||
}, | ||||||
PaymentStatus: "cancel", | ||||||
}) | ||||||
|
||||||
if err != nil { | ||||||
return webutil.StatusBadRequest(c, err) | ||||||
} | ||||||
|
||||||
currentTime := time.Now().UTC() | ||||||
|
||||||
if err = settingStripe.Payment.Validate(); err != nil { | ||||||
log.Print("update payment webhook url", err) | ||||||
} else { | ||||||
resData := map[string]any{ | ||||||
"event": "payment_error", | ||||||
"timestamp": currentTime.Unix(), | ||||||
"data": map[string]any{ | ||||||
"payment_system": "stripe", | ||||||
"cart_id": cartID, | ||||||
"payment_id": cartStripe.PaymentID, | ||||||
"total_amount": cartStripe.AmountTotal, | ||||||
"currency": cartStripe.Currency, | ||||||
"user_email": cartStripe.Email, | ||||||
}, | ||||||
} | ||||||
|
||||||
jsonData, err := json.Marshal(resData) | ||||||
if err != nil { | ||||||
log.Println("Error:", err) | ||||||
} | ||||||
|
||||||
go func() { | ||||||
res, err := webhook.SendHook(settingStripe.Payment.WebhookUrl, jsonData) | ||||||
|
||||||
if err != nil { | ||||||
log.Println(err) | ||||||
} | ||||||
|
||||||
if res.Status != "200 OK" { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
log.Print("An issue has been identified with the payment webhook URL. Please verify that it responds with a status code of 200 OK.") | ||||||
} | ||||||
}() | ||||||
} | ||||||
|
||||||
return c.Render("cancel", nil, "layouts/main") | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,72 @@ func (q *CartQueries) Carts() ([]*models.Cart, error) { | |
return carts, nil | ||
} | ||
|
||
// Carts is ... | ||
func (q *CartQueries) Cart(cartId string) (*models.Cart, error) { | ||
|
||
query := ` | ||
SELECT | ||
id, | ||
email, | ||
name, | ||
amount_total, | ||
currency, | ||
payment_id, | ||
payment_status, | ||
strftime('%s', created), | ||
strftime('%s', updated) | ||
FROM cart | ||
WHERE id = ? | ||
` | ||
|
||
rows, err := q.DB.QueryContext(context.TODO(), query, cartId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var email, name, paymentID sql.NullString | ||
var updated sql.NullInt64 | ||
cart := &models.Cart{} | ||
|
||
err = rows.Scan( | ||
&cart.ID, | ||
&email, | ||
&name, | ||
&cart.AmountTotal, | ||
&cart.Currency, | ||
&paymentID, | ||
&cart.PaymentStatus, | ||
&cart.Created, | ||
&updated, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if email.Valid { | ||
cart.Email = email.String | ||
} | ||
|
||
if name.Valid { | ||
cart.Name = name.String | ||
} | ||
|
||
if paymentID.Valid { | ||
cart.PaymentID = paymentID.String | ||
} | ||
|
||
if updated.Valid { | ||
cart.Updated = updated.Int64 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that validating data from the database here is unnecessary. |
||
|
||
if err := rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cart, nil | ||
} | ||
|
||
// AddCart is ... | ||
func (q *CartQueries) AddCart(cart *models.Cart) error { | ||
byteCart, err := json.Marshal(cart.Cart) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
INSERT INTO setting (id, key, value) VALUES | ||
('7HkP2nYgR4sL8Qo', 'payment_webhook_url', ''); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DELETE FROM setting WHERE id = '7HkP2nYgR4sL8Qo'; | ||
-- +goose StatementEnd |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package webhook | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"fmt" | ||
|
||
) | ||
|
||
func SendHook(url string, payload []byte) (*http.Response, error) { | ||
|
||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating request: %v", err) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error making request: %v", err) | ||
} | ||
|
||
return resp, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.