implement validate callback
Some checks failed
Build and Publish / BuildAndDeploy (push) Failing after 3m2s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
matst80
2025-05-13 19:46:06 +02:00
parent 00b465a8c9
commit 045c15b41a
5 changed files with 93 additions and 41 deletions

View File

@@ -515,6 +515,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
Terms: msg.Terms, Terms: msg.Terms,
Checkout: msg.Checkout, Checkout: msg.Checkout,
Confirmation: msg.Confirmation, Confirmation: msg.Confirmation,
Validation: msg.Validation,
Push: msg.Push, Push: msg.Push,
}, },
} }
@@ -529,7 +530,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa
} else { } else {
klarnaOrder, err = KlarnaInstance.CreateOrder(bytes.NewReader(orderPayload)) klarnaOrder, err = KlarnaInstance.CreateOrder(bytes.NewReader(orderPayload))
} }
log.Printf("Order result: %+v", klarnaOrder) // log.Printf("Order result: %+v", klarnaOrder)
if nil != err { if nil != err {
log.Printf("error from klarna: %v", err) log.Printf("error from klarna: %v", err)
return nil, err return nil, err

View File

@@ -33,6 +33,8 @@ type (
GUI *GUI `json:"gui,omitempty"` GUI *GUI `json:"gui,omitempty"`
MerchantRequested *AdditionalCheckBox `json:"merchant_requested,omitempty"` MerchantRequested *AdditionalCheckBox `json:"merchant_requested,omitempty"`
SelectedShippingOption *ShippingOption `json:"selected_shipping_option,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 // GUI type wraps the GUI options

89
main.go
View File

@@ -220,6 +220,7 @@ func main() {
Terms: "https://slask-finder.tornberg.me/terms", Terms: "https://slask-finder.tornberg.me/terms",
Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}", Checkout: "https://slask-finder.tornberg.me/checkout?order_id={checkout.order.id}",
Confirmation: "https://slask-finder.tornberg.me/confirmation/{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}", Push: "https://cart.tornberg.me/push?order_id={checkout.order.id}",
}, },
}) })
@@ -262,6 +263,31 @@ func main() {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(tpl, order.HTMLSnippet))) 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) { mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
@@ -276,39 +302,21 @@ func main() {
if err != nil { if err != nil {
log.Printf("Error creating request: %v\n", err) log.Printf("Error creating request: %v\n", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return return
} }
orderToSend, err := json.Marshal(order) err = confirmOrder(order, orderHandler)
if err != nil { if err != nil {
log.Printf("Error marshaling order: %v\n", err) log.Printf("Error confirming order: %v\n", err)
} else { w.WriteHeader(http.StatusInternalServerError)
err = orderHandler.Connect() return
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
}
} }
_, err = syncedServer.pool.Process(ToCartId(order.MerchantReference1), Message{
Type: OrderCompletedType, err = triggerOrderCompleted(err, syncedServer, order)
Content: &messages.OrderCreated{
OrderId: order.ID,
Status: order.Status,
},
})
if err != nil { if err != nil {
log.Printf("Error processing cart message: %v\n", err) log.Printf("Error processing cart message: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@@ -334,3 +342,32 @@ func main() {
<-done <-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
}

View File

@@ -727,6 +727,7 @@ type CreateCheckoutOrder struct {
Checkout string `protobuf:"bytes,2,opt,name=checkout,proto3" json:"checkout,omitempty"` Checkout string `protobuf:"bytes,2,opt,name=checkout,proto3" json:"checkout,omitempty"`
Confirmation string `protobuf:"bytes,3,opt,name=confirmation,proto3" json:"confirmation,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"` 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 unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
} }
@@ -789,6 +790,13 @@ func (x *CreateCheckoutOrder) GetPush() string {
return "" return ""
} }
func (x *CreateCheckoutOrder) GetValidation() string {
if x != nil {
return x.Validation
}
return ""
}
type OrderCreated struct { type OrderCreated struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
OrderId string `protobuf:"bytes,1,opt,name=orderId,proto3" json:"orderId,omitempty"` 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, 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, 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, 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, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x22, 0x9f, 0x01, 0x0a, 0x13, 0x43, 0x72, 0x65, 0x61,
0x65, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x14, 0x74, 0x65, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12,
0x0a, 0x05, 0x74, 0x65, 0x72, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x14, 0x0a, 0x05, 0x74, 0x65, 0x72, 0x6d, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05,
0x65, 0x72, 0x6d, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x74, 0x65, 0x72, 0x6d, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75, 0x74, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6f, 0x75,
0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f,
0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d, 0x61, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x72, 0x6d,
0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x75, 0x73, 0x68, 0x18, 0x04, 0x20, 0x01, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x70, 0x75, 0x73, 0x68, 0x18, 0x04, 0x20,
0x28, 0x09, 0x52, 0x04, 0x70, 0x75, 0x73, 0x68, 0x22, 0x40, 0x0a, 0x0c, 0x4f, 0x72, 0x64, 0x65, 0x01, 0x28, 0x09, 0x52, 0x04, 0x70, 0x75, 0x73, 0x68, 0x12, 0x1e, 0x0a, 0x0a, 0x76, 0x61, 0x6c,
0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x69, 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x76,
0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x61, 0x6c, 0x69, 0x64, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x40, 0x0a, 0x0c, 0x4f, 0x72, 0x64,
0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x02, 0x20, 0x01, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x72, 0x64,
0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x0c, 0x5a, 0x0a, 0x2e, 0x3b, 0x65, 0x72, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6f, 0x72, 0x64, 0x65,
0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, 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 ( var (

View File

@@ -81,6 +81,7 @@ message CreateCheckoutOrder {
string checkout = 2; string checkout = 2;
string confirmation = 3; string confirmation = 3;
string push = 4; string push = 4;
string validation = 5;
} }
message OrderCreated { message OrderCreated {