From 045c15b41a9f738a2df7c51b9e65ac256881da23 Mon Sep 17 00:00:00 2001 From: matst80 Date: Tue, 13 May 2025 19:46:06 +0200 Subject: [PATCH] implement validate callback --- cart-grain.go | 3 +- klarna-types.go | 2 + main.go | 89 +++++++++++++++++++++++++++++++------------- proto/messages.pb.go | 39 ++++++++++++------- proto/messages.proto | 1 + 5 files changed, 93 insertions(+), 41 deletions(-) diff --git a/cart-grain.go b/cart-grain.go index c1812c0..ecd247a 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -515,6 +515,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa Terms: msg.Terms, Checkout: msg.Checkout, Confirmation: msg.Confirmation, + Validation: msg.Validation, Push: msg.Push, }, } @@ -529,7 +530,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa } else { klarnaOrder, err = KlarnaInstance.CreateOrder(bytes.NewReader(orderPayload)) } - log.Printf("Order result: %+v", klarnaOrder) + // log.Printf("Order result: %+v", klarnaOrder) if nil != err { log.Printf("error from klarna: %v", err) return nil, err diff --git a/klarna-types.go b/klarna-types.go index b04e75c..393e972 100644 --- a/klarna-types.go +++ b/klarna-types.go @@ -33,6 +33,8 @@ type ( GUI *GUI `json:"gui,omitempty"` MerchantRequested *AdditionalCheckBox `json:"merchant_requested,omitempty"` SelectedShippingOption *ShippingOption `json:"selected_shipping_option,omitempty"` + ErrorCode string `json:"error_code,omitempty"` + ErrorMessages []string `json:"error_messages,omitempty"` } // GUI type wraps the GUI options diff --git a/main.go b/main.go index 676ad26..1ea0c86 100644 --- a/main.go +++ b/main.go @@ -220,6 +220,7 @@ func main() { Terms: "https://slask-finder.tornberg.me/terms", Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}", Confirmation: "https://slask-finder.tornberg.me/confirmation/{checkout.order.id}", + Validation: "https://cart.tornberg.me/validation", Push: "https://cart.tornberg.me/push?order_id={checkout.order.id}", }, }) @@ -262,6 +263,31 @@ func main() { w.WriteHeader(http.StatusOK) w.Write([]byte(fmt.Sprintf(tpl, order.HTMLSnippet))) }) + mux.HandleFunc("/validate", func(w http.ResponseWriter, r *http.Request) { + 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) + } + err = confirmOrder(order, orderHandler) + if err != nil { + log.Printf("Error confirming order: %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + err = triggerOrderCompleted(err, syncedServer, order) + if err != nil { + log.Printf("Error processing cart message: %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + }) mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { @@ -276,39 +302,21 @@ func main() { if err != nil { log.Printf("Error creating request: %v\n", err) w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) return } - orderToSend, err := json.Marshal(order) + err = confirmOrder(order, orderHandler) if err != nil { - log.Printf("Error marshaling order: %v\n", err) - } else { - err = orderHandler.Connect() - if err != nil { - log.Printf("Error connecting to order handler: %v\n", err) - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } - defer orderHandler.Close() - err = orderHandler.OrderCompleted(orderToSend) - if err != nil { - log.Printf("Error sending order: %v\n", err) - w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) - return - } + log.Printf("Error confirming order: %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + return } - _, err = syncedServer.pool.Process(ToCartId(order.MerchantReference1), Message{ - Type: OrderCompletedType, - Content: &messages.OrderCreated{ - OrderId: order.ID, - Status: order.Status, - }, - }) + + err = triggerOrderCompleted(err, syncedServer, order) if err != nil { log.Printf("Error processing cart message: %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + return } w.WriteHeader(http.StatusOK) @@ -334,3 +342,32 @@ func main() { <-done } + +func triggerOrderCompleted(err error, syncedServer *PoolServer, order *CheckoutOrder) error { + _, err = syncedServer.pool.Process(ToCartId(order.MerchantReference1), Message{ + Type: OrderCompletedType, + Content: &messages.OrderCreated{ + OrderId: order.ID, + Status: order.Status, + }, + }) + return err +} + +func confirmOrder(order *CheckoutOrder, orderHandler *AmqpOrderHandler) error { + orderToSend, err := json.Marshal(order) + if err != nil { + return err + } + err = orderHandler.Connect() + if err != nil { + return err + } + defer orderHandler.Close() + err = orderHandler.OrderCompleted(orderToSend) + if err != nil { + return err + } + + return nil +} diff --git a/proto/messages.pb.go b/proto/messages.pb.go index e4671cc..b04b575 100644 --- a/proto/messages.pb.go +++ b/proto/messages.pb.go @@ -727,6 +727,7 @@ type CreateCheckoutOrder struct { Checkout string `protobuf:"bytes,2,opt,name=checkout,proto3" json:"checkout,omitempty"` Confirmation string `protobuf:"bytes,3,opt,name=confirmation,proto3" json:"confirmation,omitempty"` Push string `protobuf:"bytes,4,opt,name=push,proto3" json:"push,omitempty"` + Validation string `protobuf:"bytes,5,opt,name=validation,proto3" json:"validation,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -789,6 +790,13 @@ func (x *CreateCheckoutOrder) GetPush() string { return "" } +func (x *CreateCheckoutOrder) GetValidation() string { + if x != nil { + return x.Validation + } + return "" +} + type OrderCreated struct { state protoimpl.MessageState `protogen:"open.v1"` OrderId string `protobuf:"bytes,1,opt,name=orderId,proto3" json:"orderId,omitempty"` @@ -941,20 +949,23 @@ var file_messages_proto_rawDesc = string([]byte{ 0x79, 0x42, 0x06, 0x0a, 0x04, 0x5f, 0x7a, 0x69, 0x70, 0x42, 0x0a, 0x0a, 0x08, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x22, 0x20, 0x0a, 0x0e, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x44, 0x65, 0x6c, 0x69, 0x76, 0x65, 0x72, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x22, 0x7f, 0x0a, 0x13, 0x43, 0x72, 0x65, 0x61, 0x74, - 0x65, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x14, - 0x0a, 0x05, 0x74, 0x65, 0x72, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, - 0x65, 0x72, 0x6d, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, - 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, - 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x75, 0x73, 0x68, 0x18, 0x04, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x04, 0x70, 0x75, 0x73, 0x68, 0x22, 0x40, 0x0a, 0x0c, 0x4f, 0x72, 0x64, 0x65, - 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, - 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, - 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x3b, - 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x22, 0x9f, 0x01, 0x0a, 0x13, 0x43, 0x72, 0x65, 0x61, + 0x74, 0x65, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, + 0x14, 0x0a, 0x05, 0x74, 0x65, 0x72, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, + 0x74, 0x65, 0x72, 0x6d, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, + 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, + 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x75, 0x73, 0x68, 0x18, 0x04, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x75, 0x73, 0x68, 0x12, 0x1e, 0x0a, 0x0a, 0x76, 0x61, 0x6c, + 0x69, 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x76, + 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x40, 0x0a, 0x0c, 0x4f, 0x72, 0x64, + 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, + 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, + 0x72, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, + 0x3b, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, }) var ( diff --git a/proto/messages.proto b/proto/messages.proto index b5278c6..6d102e0 100644 --- a/proto/messages.proto +++ b/proto/messages.proto @@ -81,6 +81,7 @@ message CreateCheckoutOrder { string checkout = 2; string confirmation = 3; string push = 4; + string validation = 5; } message OrderCreated {