all the refactor
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/cart"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/mats/platform/tax"
|
||||
adyenCheckout "github.com/adyen/adyen-go-api-library/v21/src/checkout"
|
||||
"github.com/adyen/adyen-go-api-library/v21/src/common"
|
||||
)
|
||||
@@ -66,7 +67,7 @@ type CheckoutMeta struct {
|
||||
// CartItem / Delivery to expose that data and propagate it here.
|
||||
// tp is an optional TaxProvider; when non-nil, its DefaultTaxRate is used for
|
||||
// delivery lines instead of the hardcoded 2500 (25 % as CartItem format).
|
||||
func BuildCheckoutOrderPayload(grain *checkout.CheckoutGrain, meta *CheckoutMeta, tp cart.TaxProvider) ([]byte, *CheckoutOrder, error) {
|
||||
func BuildCheckoutOrderPayload(grain *checkout.CheckoutGrain, meta *CheckoutMeta, tp tax.Provider) ([]byte, *CheckoutOrder, error) {
|
||||
if grain == nil {
|
||||
return nil, nil, fmt.Errorf("nil grain")
|
||||
}
|
||||
@@ -177,19 +178,22 @@ func BuildCheckoutOrderPayload(grain *checkout.CheckoutGrain, meta *CheckoutMeta
|
||||
}
|
||||
|
||||
// defaultKlarnaTaxRate returns the default tax rate for the purchase country, in Klarna
|
||||
// format (percent x 100, e.g. 2500 = 25.00 %). When tp is nil, falls back to 2500 (25 %).
|
||||
func defaultKlarnaTaxRate(tp cart.TaxProvider, country string) int {
|
||||
// format (percent x 100, e.g. 2500 = 25.00 %). This matches the platform basis-point
|
||||
// scale exactly, so DefaultTaxRate is passed through. When tp is nil, falls back to 2500.
|
||||
func defaultKlarnaTaxRate(tp tax.Provider, country string) int {
|
||||
if tp == nil {
|
||||
return 2500
|
||||
}
|
||||
return tp.DefaultTaxRate(country) * 100
|
||||
return tp.DefaultTaxRate(country)
|
||||
}
|
||||
|
||||
// defaultAdyenTaxRate returns the default tax rate for the purchase country, in Adyen
|
||||
// format (raw percent, e.g. 25 = 25 %). When tp is nil, falls back to 25.
|
||||
func defaultAdyenTaxRate(tp cart.TaxProvider, country string) int64 {
|
||||
// format (taxPercentage in basis points, e.g. 2500 = 25 %). This matches the platform
|
||||
// basis-point scale exactly, so DefaultTaxRate is passed through. When tp is nil, falls
|
||||
// back to 2500.
|
||||
func defaultAdyenTaxRate(tp tax.Provider, country string) int64 {
|
||||
if tp == nil {
|
||||
return 25
|
||||
return 2500
|
||||
}
|
||||
return int64(tp.DefaultTaxRate(country))
|
||||
}
|
||||
@@ -211,7 +215,7 @@ func GetCheckoutMetaFromRequest(r *http.Request) *CheckoutMeta {
|
||||
}
|
||||
}
|
||||
|
||||
func BuildAdyenCheckoutSession(grain *checkout.CheckoutGrain, meta *CheckoutMeta, tp cart.TaxProvider) (*adyenCheckout.CreateCheckoutSessionRequest, error) {
|
||||
func BuildAdyenCheckoutSession(grain *checkout.CheckoutGrain, meta *CheckoutMeta, tp tax.Provider) (*adyenCheckout.CreateCheckoutSessionRequest, error) {
|
||||
if grain == nil {
|
||||
return nil, fmt.Errorf("nil grain")
|
||||
}
|
||||
@@ -237,10 +241,10 @@ func BuildAdyenCheckoutSession(grain *checkout.CheckoutGrain, meta *CheckoutMeta
|
||||
}
|
||||
lineItems = append(lineItems, adyenCheckout.LineItem{
|
||||
Quantity: common.PtrInt64(int64(it.Quantity)),
|
||||
AmountIncludingTax: common.PtrInt64(it.TotalPrice.IncVat),
|
||||
AmountIncludingTax: common.PtrInt64(it.TotalPrice.IncVat.Int64()),
|
||||
Description: common.PtrString(it.Meta.Name),
|
||||
AmountExcludingTax: common.PtrInt64(it.TotalPrice.ValueExVat()),
|
||||
TaxAmount: common.PtrInt64(it.TotalPrice.TotalVat()),
|
||||
AmountExcludingTax: common.PtrInt64(it.TotalPrice.ValueExVat().Int64()),
|
||||
TaxAmount: common.PtrInt64(it.TotalPrice.TotalVat().Int64()),
|
||||
TaxPercentage: common.PtrInt64(int64(it.Tax)),
|
||||
})
|
||||
}
|
||||
@@ -263,8 +267,8 @@ func BuildAdyenCheckoutSession(grain *checkout.CheckoutGrain, meta *CheckoutMeta
|
||||
if d == nil {
|
||||
continue
|
||||
}
|
||||
amountIncTax := d.Price.IncVat
|
||||
amountExTax := d.Price.ValueExVat()
|
||||
amountIncTax := d.Price.IncVat.Int64()
|
||||
amountExTax := d.Price.ValueExVat().Int64()
|
||||
if hasFreeShipping {
|
||||
amountIncTax = 0
|
||||
amountExTax = 0
|
||||
@@ -284,7 +288,7 @@ func BuildAdyenCheckoutSession(grain *checkout.CheckoutGrain, meta *CheckoutMeta
|
||||
return &adyenCheckout.CreateCheckoutSessionRequest{
|
||||
Reference: grain.Id.String(),
|
||||
Amount: adyenCheckout.Amount{
|
||||
Value: total.IncVat,
|
||||
Value: total.IncVat.Int64(),
|
||||
Currency: currency,
|
||||
},
|
||||
CountryCode: common.PtrString(country),
|
||||
|
||||
Reference in New Issue
Block a user