Skip to content

Commit

Permalink
Added charge policy (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb authored Oct 18, 2024
1 parent 499ac4a commit abfc639
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions utils/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
82 changes: 82 additions & 0 deletions v1/policy/charge.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions v1/policy/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()...)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit abfc639

Please sign in to comment.