From 9f585eec2e498d9e2a46be02a54dcd9f1c8cd787 Mon Sep 17 00:00:00 2001 From: matst80 Date: Fri, 18 Apr 2025 16:38:24 +0200 Subject: [PATCH] push endpoint --- cart-grain.go | 23 +++++--- klarna-push-type.go | 135 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 17 +++++- 3 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 klarna-push-type.go diff --git a/cart-grain.go b/cart-grain.go index 6839aa0..0062c4b 100644 --- a/cart-grain.go +++ b/cart-grain.go @@ -60,15 +60,18 @@ type CartDelivery struct { } type CartGrain struct { - mu sync.RWMutex - lastItemId int - lastDeliveryId int - storageMessages []Message - Id CartId `json:"id"` - Items []*CartItem `json:"items"` - TotalPrice int64 `json:"totalPrice"` - Deliveries []*CartDelivery `json:"deliveries,omitempty"` - Processing bool `json:"processing"` + mu sync.RWMutex + lastItemId int + lastDeliveryId int + storageMessages []Message + Id CartId `json:"id"` + Items []*CartItem `json:"items"` + TotalPrice int64 `json:"totalPrice"` + Deliveries []*CartDelivery `json:"deliveries,omitempty"` + Processing bool `json:"processing"` + PaymentInProgress bool `json:"paymentInProgress"` + OrderReference string `json:"orderReference,omitempty"` + PaymentReference string `json:"paymentReference,omitempty"` } type Grain interface { @@ -413,6 +416,8 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) (*FrameWithPa } else { orderLines := make([]*klarna.Line, 0, len(c.Items)) totalTax := 0 + c.PaymentInProgress = true + c.Processing = true for _, item := range c.Items { total := int(item.Price) * item.Quantity taxAmount := GetTaxAmount(total, item.Tax) diff --git a/klarna-push-type.go b/klarna-push-type.go new file mode 100644 index 0000000..b2b58a1 --- /dev/null +++ b/klarna-push-type.go @@ -0,0 +1,135 @@ +package main + +import "time" + +type OrderValidationBody struct { + PurchaseCountry string `json:"purchase_country"` + PurchaseCurrency string `json:"purchase_currency"` + Locale string `json:"locale"` + Status string `json:"status"` + BillingAddress struct { + GivenName string `json:"given_name"` + FamilyName string `json:"family_name"` + Email string `json:"email"` + Title string `json:"title"` + StreetAddress string `json:"street_address"` + StreetAddress2 string `json:"street_address2"` + StreetName string `json:"street_name"` + StreetNumber string `json:"street_number"` + HouseExtension string `json:"house_extension"` + PostalCode string `json:"postal_code"` + City string `json:"city"` + Region string `json:"region"` + Phone string `json:"phone"` + Country string `json:"country"` + CareOf string `json:"care_of"` + } `json:"billing_address"` + ShippingAddress struct { + GivenName string `json:"given_name"` + FamilyName string `json:"family_name"` + Email string `json:"email"` + Title string `json:"title"` + StreetAddress string `json:"street_address"` + StreetAddress2 string `json:"street_address2"` + StreetName string `json:"street_name"` + StreetNumber string `json:"street_number"` + HouseExtension string `json:"house_extension"` + PostalCode string `json:"postal_code"` + City string `json:"city"` + Region string `json:"region"` + Phone string `json:"phone"` + Country string `json:"country"` + CareOf string `json:"care_of"` + } `json:"shipping_address"` + OrderAmount int `json:"order_amount"` + OrderTaxAmount int `json:"order_tax_amount"` + OrderLines []struct { + } `json:"order_lines"` + Customer struct { + Type string `json:"type"` + Gender string `json:"gender"` + DateOfBirth string `json:"date_of_birth"` + OrganizationRegistrationID string `json:"organization_registration_id"` + VatID string `json:"vat_id"` + } `json:"customer"` + MerchantUrls struct { + Terms string `json:"terms"` + Checkout string `json:"checkout"` + Confirmation string `json:"confirmation"` + Push string `json:"push"` + Validation string `json:"validation"` + Notification string `json:"notification"` + CancellationTerms string `json:"cancellation_terms"` + ShippingOptionUpdate string `json:"shipping_option_update"` + AddressUpdate string `json:"address_update"` + CountryChange string `json:"country_change"` + } `json:"merchant_urls"` + MerchantReference1 string `json:"merchant_reference1"` + MerchantReference2 string `json:"merchant_reference2"` + Options struct { + RequireValidateCallbackSuccess bool `json:"require_validate_callback_success"` + AcquiringChannel string `json:"acquiring_channel"` + VatRemoved bool `json:"vat_removed"` + AllowSeparateShippingAddress bool `json:"allow_separate_shipping_address"` + ColorButton string `json:"color_button"` + ColorButtonText string `json:"color_button_text"` + ColorCheckbox string `json:"color_checkbox"` + ColorCheckboxCheckmark string `json:"color_checkbox_checkmark"` + ColorHeader string `json:"color_header"` + ColorLink string `json:"color_link"` + DateOfBirthMandatory bool `json:"date_of_birth_mandatory"` + ShippingDetails string `json:"shipping_details"` + TitleMandatory bool `json:"title_mandatory"` + AdditionalCheckbox struct { + } `json:"additional_checkbox"` + NationalIdentificationNumberMandatory bool `json:"national_identification_number_mandatory"` + AdditionalMerchantTerms string `json:"additional_merchant_terms"` + PhoneMandatory bool `json:"phone_mandatory"` + RadiusBorder string `json:"radius_border"` + AllowedCustomerTypes []interface{} `json:"allowed_customer_types"` + ShowSubtotalDetail bool `json:"show_subtotal_detail"` + AdditionalCheckboxes []interface{} `json:"additional_checkboxes"` + VerifyNationalIdentificationNumber bool `json:"verify_national_identification_number"` + AutoCapture bool `json:"auto_capture"` + RequireClientValidation bool `json:"require_client_validation"` + EnableDiscountModule bool `json:"enable_discount_module"` + ShowVatRegistrationNumberField bool `json:"show_vat_registration_number_field"` + } `json:"options"` + Attachment struct { + Body time.Time `json:"body"` + ContentType string `json:"content_type"` + } `json:"attachment"` + ExternalPaymentMethods []struct { + } `json:"external_payment_methods"` + ExternalCheckouts []struct { + } `json:"external_checkouts"` + ShippingCountries []string `json:"shipping_countries"` + ShippingOptions []struct { + } `json:"shipping_options"` + MerchantData string `json:"merchant_data"` + Gui struct { + Options []interface{} `json:"options"` + } `json:"gui"` + MerchantRequested struct { + } `json:"merchant_requested"` + SelectedShippingOption struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + Promo string `json:"promo"` + Price int `json:"price"` + Preselected bool `json:"preselected"` + TaxAmount int `json:"tax_amount"` + TaxRate int `json:"tax_rate"` + ShippingMethod string `json:"shipping_method"` + DeliveryDetails struct { + } `json:"delivery_details"` + TmsReference string `json:"tms_reference"` + SelectedAddons []interface{} `json:"selected_addons"` + } `json:"selected_shipping_option"` + Recurring bool `json:"recurring"` + BillingCountries []string `json:"billing_countries"` + Tags []string `json:"tags"` + DiscountLines []struct { + } `json:"discount_lines"` +} diff --git a/main.go b/main.go index ef33359..eabfc1d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "log" "net" @@ -181,7 +182,21 @@ func main() { w.Write([]byte("ok")) }) mux.HandleFunc("/push", func(w http.ResponseWriter, r *http.Request) { - log.Print(r.Body) + log.Printf("KLARNA PUSH: search %v, body %s", r.URL.Query(), r.Body) + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } + var data OrderValidationBody + err := json.NewDecoder(r.Body).Decode(&data) + if err != nil { + log.Printf("Error decoding body: %v\n", err) + w.WriteHeader(http.StatusBadRequest) + return + } + cartId := data.MerchantReference1 + log.Printf("Received push for cart %s, status: %s", cartId, data.Status) + w.WriteHeader(http.StatusOK) }) mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK)