update push to update the cart
This commit is contained in:
46
main.go
46
main.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user