From abfc639cbe434441292cb412b460fffe40b5e729 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Fri, 18 Oct 2024 17:40:45 +0100 Subject: [PATCH] Added charge policy (#217) --- utils/generate.go | 4 +++ v1/policy/charge.go | 82 +++++++++++++++++++++++++++++++++++++++++++++ v1/policy/init.go | 9 +++++ 3 files changed, 95 insertions(+) create mode 100644 v1/policy/charge.go diff --git a/utils/generate.go b/utils/generate.go index af04d80..ab7ca36 100644 --- a/utils/generate.go +++ b/utils/generate.go @@ -53,6 +53,7 @@ const ( // policy confPolCascade Template = "pol_cascade" confPolChargeExpiry Template = "pol_chargeExpiry" + confPolCharge Template = "pol_charge" confPolFraud Template = "pol_fraud" confPolMethodLock Template = "pol_methodLock" confPolMethodUpgrade Template = "pol_methodUpgrade" @@ -92,6 +93,7 @@ var Templates = map[Template]string{ confConnFlexPay: "Connector: FlexPay", confIntSlack: "Integration: Slack", confPolCascade: "Policy: Cascade", + confPolCharge: "Policy: Charge", confPolChargeExpiry: "Policy: Charge Expiry", confPolFraud: "Policy: Fraud", confPolMethodLock: "Policy: Method Lock", @@ -350,6 +352,8 @@ func buildSpec(conf Template) (object.Specification, error) { return policy.CascadePolicy{Rules: []policy.CascadeRule{{Library: connectorconfig.Library(chg), OriginalResponseCode: chg}}}, nil case confPolChargeExpiry: return policy.ChargeExpiryPolicy{}, nil + case confPolCharge: + return policy.ChargePolicy{}, nil case confPolFraud: return policy.FraudPolicy{ConnectorIDs: []string{chg}, CheckTime: "preauth-first", CheckType: "all"}, nil case confPolMethodLock: diff --git a/v1/policy/charge.go b/v1/policy/charge.go new file mode 100644 index 0000000..481e83f --- /dev/null +++ b/v1/policy/charge.go @@ -0,0 +1,82 @@ +package policy + +import ( + "encoding/json" + "errors" + "github.com/chargehive/configuration/object" +) + +// KindPolicyCharge is the identifier for a PolicyCharge config +const KindPolicyCharge object.Kind = "PolicyCharge" + +// Restriction is the type of API involvement +type Restriction string + +const ( + // RestrictionNone requires no api involvement + RestrictionNone Restriction = "" + + // RestrictionAPIInitiate requires the api to initiate the request + RestrictionAPIInitiate Restriction = "api-initiate" + + // RestrictionAPIVerify requires the api to be involved before proceeding + RestrictionAPIVerify Restriction = "api-verify" + + // RestrictionBlock should block the request + RestrictionBlock Restriction = "block" +) + +// ChargePolicy defines the constraints that when exceeded a charge will not expire +type ChargePolicy struct { + // MaxAuthAttempts is the number of auths that can be processed, 0 for unlimited + MaxAuthAttempts int64 `json:"authAttempts" yaml:"authAttempts" validate:"min=0"` + + // AmountUpperLimit is the maximum amount that can be processed + AmountUpperLimit int64 `json:"amountUpperLimit" yaml:"amountUpperLimit" validate:"min=0"` + + // AmountLowerLimit is the minimum amount that can be processed + AmountLowerLimit int64 `json:"amountLowerLimit" yaml:"amountLowerLimit" validate:"min=0"` + + // AllowAmounts is a list of amounts that are allowed to be processed + AllowAmounts []int64 `json:"allowAmounts" yaml:"allowAmounts"` + + // OnCreation is the restriction on charge creation + OnCreation Restriction `json:"onCreation" yaml:"onCreation" validate:"omitempty,oneof='' api-initiate api-verify block"` + + // OnAuth is the restriction on charge auth + OnAuth Restriction `json:"onAuth" yaml:"onAuth" validate:"omitempty,oneof='' api-initiate api-verify block"` + + // OnCapture is the restriction on charge capture + OnCapture Restriction `json:"onCapture" yaml:"onCapture" validate:"omitempty,oneof='' api-initiate api-verify block"` + + // OnRefund is the restriction on charge refund + OnRefund Restriction `json:"onRefund" yaml:"onRefund" validate:"omitempty,oneof='' api-initiate api-verify block"` +} + +// GetKind returns the ChargePolicy kind +func (ChargePolicy) GetKind() object.Kind { return KindPolicyCharge } + +// GetVersion returns the ChargePolicy version +func (ChargePolicy) GetVersion() string { return "v1" } + +// ChargePolicyDefinition is the Charge config object definition +type ChargePolicyDefinition struct{ def *object.Definition } + +// NewChargePolicyDefinition creates a new ChargePolicyDefinition +func NewChargePolicyDefinition(d *object.Definition) (*ChargePolicyDefinition, error) { + if _, ok := d.Spec.(*ChargePolicy); ok { + return &ChargePolicyDefinition{def: d}, nil + } + return nil, errors.New("invalid Charge policy object") +} + +// Definition returns the ChargePolicyDefinition structure +func (d *ChargePolicyDefinition) Definition() *object.Definition { return d.def } + +// MarshalJSON returns the JSON value for the ChargePolicyDefinition +func (d *ChargePolicyDefinition) MarshalJSON() ([]byte, error) { return json.Marshal(d.def) } + +// Spec returns the CascadePolicy contained within the ChargePolicyDefinition +func (d *ChargePolicyDefinition) Spec() *ChargePolicy { + return d.def.Spec.(*ChargePolicy) +} diff --git a/v1/policy/init.go b/v1/policy/init.go index 8de10a5..503343b 100644 --- a/v1/policy/init.go +++ b/v1/policy/init.go @@ -8,6 +8,7 @@ func GetHandlers() []object.KindHandler { funcs = append(funcs, scaPolicy()...) funcs = append(funcs, fraudPolicy()...) funcs = append(funcs, chargeExpiryPolicy()...) + funcs = append(funcs, chargePolicy()...) funcs = append(funcs, methodUpgradePolicy()...) funcs = append(funcs, cascadePolicy()...) funcs = append(funcs, methodLockPolicy()...) @@ -40,6 +41,14 @@ func chargeExpiryPolicy() []object.KindHandler { } } +func chargePolicy() []object.KindHandler { + o := ChargePolicy{} + return []object.KindHandler{ + object.NewKindHandler(o.GetKind(), object.KindHandlerDefaultVersion, func() object.Specification { return &ChargePolicy{} }), + object.NewKindHandler(o.GetKind(), o.GetVersion(), func() object.Specification { return &ChargePolicy{} }), + } +} + func methodUpgradePolicy() []object.KindHandler { o := MethodUpgradePolicy{} return []object.KindHandler{