|
|
|
|
@@ -27,6 +27,18 @@ var tpl = `<!DOCTYPE html>
|
|
|
|
|
</html>
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (a *App) getGrainFromOrder(order *CheckoutOrder) (*cart.CartGrain, error) {
|
|
|
|
|
cartId, ok := cart.ParseCartId(order.MerchantReference1)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("invalid cart id in order reference: %s", order.MerchantReference1)
|
|
|
|
|
}
|
|
|
|
|
grain, err := a.pool.Get(uint64(cartId))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to get cart grain: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return grain, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux, inventoryService inventory.InventoryService) {
|
|
|
|
|
conn, err := amqp.Dial(amqpUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -117,6 +129,38 @@ func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux, invento
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
fmt.Fprintf(w, tpl, order.HTMLSnippet)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/notification", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log.Printf("Klarna order notification, method: %s", r.Method)
|
|
|
|
|
logger.InfoContext(r.Context(), "Klarna order notification received", "method", r.Method)
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
order := &CheckoutOrder{}
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(order)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Klarna order notification: %s", order.ID)
|
|
|
|
|
logger.InfoContext(r.Context(), "Klarna order notification received", "order_id", order.ID)
|
|
|
|
|
grain, err := a.getGrainFromOrder(order)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.ErrorContext(r.Context(), "Unable to get grain from klarna order", "error", err.Error())
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inventoryService != nil {
|
|
|
|
|
inventoryRequests := getInventoryRequests(grain.Items)
|
|
|
|
|
err = inventoryService.ReserveInventory(inventoryRequests...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.WarnContext(r.Context(), "placeorder inventory reservation failed")
|
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("POST /validate", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
log.Printf("Klarna order validation, method: %s", r.Method)
|
|
|
|
|
if r.Method != "POST" {
|
|
|
|
|
@@ -128,24 +172,18 @@ func (a *App) HandleCheckoutRequests(amqpUrl string, mux *http.ServeMux, invento
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
log.Printf("Klarna order validation: %s, cart id: %s", order.ID, order.MerchantReference1)
|
|
|
|
|
cartId, ok := cart.ParseCartId(order.MerchantReference1)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Printf("Invalid cart id in order reference: %s", order.MerchantReference1)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grain, err := a.pool.Get(uint64(cartId))
|
|
|
|
|
logger.InfoContext(r.Context(), "Klarna order validation received", "order_id", order.ID, "cart_id", order.MerchantReference1)
|
|
|
|
|
grain, err := a.getGrainFromOrder(order)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.ErrorContext(r.Context(), "Unable to get grain from klarna order", "error", err.Error())
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if inventoryService != nil {
|
|
|
|
|
inventoryRequests := getInventoryRequests(grain.Items)
|
|
|
|
|
err = inventoryService.ReserveInventory(inventoryRequests...)
|
|
|
|
|
_, err = inventoryService.ReservationCheck(inventoryRequests...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.WarnContext(r.Context(), "placeorder inventory reservation failed")
|
|
|
|
|
logger.WarnContext(r.Context(), "placeorder inventory check failed")
|
|
|
|
|
w.WriteHeader(http.StatusNotAcceptable)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|