refactor klarna stuff
Some checks failed
Build and Publish / BuildAndDeploy (push) Successful in 3m42s
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled

This commit is contained in:
matst80
2025-04-18 20:41:25 +02:00
parent b51fc78dd5
commit 6c2328495b
8 changed files with 302 additions and 145 deletions

View File

@@ -5,24 +5,25 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
messages "git.tornberg.me/go-cart-actor/proto"
"golang.org/x/exp/rand"
)
type PoolServer struct {
pod_name string
pool GrainPool
pod_name string
pool GrainPool
klarnaClient *KlarnaClient
}
func NewPoolServer(pool GrainPool, pod_name string) *PoolServer {
func NewPoolServer(pool GrainPool, pod_name string, klarnaClient *KlarnaClient) *PoolServer {
return &PoolServer{
pod_name: pod_name,
pool: pool,
pod_name: pod_name,
pool: pool,
klarnaClient: klarnaClient,
}
}
@@ -215,38 +216,23 @@ func (s *PoolServer) HandleAddRequest(w http.ResponseWriter, r *http.Request, id
return s.WriteResult(w, reply)
}
var (
APIUsername = os.Getenv("KLARNA_API_USERNAME")
APIPassword = os.Getenv("KLARNA_API_PASSWORD")
)
func (s *PoolServer) HandleConfirmation(w http.ResponseWriter, r *http.Request, id CartId) error {
orderId := r.PathValue("orderId")
if orderId == "" {
return fmt.Errorf("orderId is empty")
}
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.playground.klarna.com/checkout/v3/orders/%s", orderId), nil)
order, err := s.klarnaClient.GetOrder(orderId)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(APIUsername, APIPassword)
res, err := http.DefaultClient.Do(req)
if nil != err {
return err
}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Pod-Name", s.pod_name)
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(res.StatusCode)
w.Write(buf.Bytes())
return nil
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(order)
}
func (s *PoolServer) HandleCheckout(w http.ResponseWriter, r *http.Request, id CartId) error {
@@ -267,29 +253,19 @@ func (s *PoolServer) HandleCheckout(w http.ResponseWriter, r *http.Request, id C
return s.WriteResult(w, reply)
}
req, err := http.NewRequest("POST", "https://api.playground.klarna.com/checkout/v3/orders", bytes.NewReader(reply.Payload))
if err != nil {
return err
}
order, err := s.klarnaClient.CreateOrder(bytes.NewReader(reply.Payload))
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(APIUsername, APIPassword)
res, err := http.DefaultClient.Do(req)
if nil != err {
return err
}
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Pod-Name", s.pod_name)
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(res.StatusCode)
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
return nil
return json.NewEncoder(w).Encode(order)
}
func NewCartId() CartId {