update push to update the cart
Some checks failed
Build and Publish / BuildAndDeploy (push) Successful in 3m9s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
matst80
2025-04-18 17:27:50 +02:00
parent 00e39cf7fa
commit 49cebad861
5 changed files with 159 additions and 124 deletions

46
main.go
View File

@@ -13,6 +13,7 @@ import (
"syscall"
"time"
messages "git.tornberg.me/go-cart-actor/proto"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -187,11 +188,46 @@ func main() {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var data OrderValidationBody
err := json.NewDecoder(r.Body).Decode(&data)
if err == nil {
cartId := data.MerchantReference1
log.Printf("Received push for cart %s, status: %s", cartId, data.Status)
orderId := r.URL.Query().Get("order_id")
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.playground.klarna.com/checkout/v3/orders/%s", orderId), nil)
if err != nil {
log.Printf("Error creating request: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(APIUsername, APIPassword)
res, err := http.DefaultClient.Do(req)
if nil != err {
log.Printf("Error making request: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
defer res.Body.Close()
var klarnaOrderResponse KlarnaOrderResponse
err = json.NewDecoder(res.Body).Decode(&klarnaOrderResponse)
if err != nil {
log.Printf("Error decoding response: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
_, err = syncedServer.pool.Process(ToCartId(klarnaOrderResponse.MerchantReference1), Message{
Type: OrderCompletedType,
Content: messages.OrderCreated{
OrderId: klarnaOrderResponse.OrderID,
Status: klarnaOrderResponse.Status},
})
if err != nil {
log.Printf("Error processing message: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)