Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support to Store and populate the Pricing details ( specially the fixed price details) at the time of License Creation
  • Loading branch information
anandrgitnirman authored and anandrgitnirman committed Sep 16, 2021
1 parent 7930ec2 commit 337603d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
24 changes: 23 additions & 1 deletion license_server/license_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,42 @@ type LockingLicenseService struct {
LicenseDetailsStorage storage.TypedAtomicStorage
LicenseUsageStorage storage.TypedAtomicStorage
Org *blockchain.OrganizationMetaData
ServiceMetaData *blockchain.ServiceMetadata
}

//Will be used in the components to create a new instance of LicenseService
func NewLicenseService(
detailsStorage storage.TypedAtomicStorage,
licenseStorage storage.TypedAtomicStorage, orgData *blockchain.OrganizationMetaData,
) *LockingLicenseService {
serviceMetaData *blockchain.ServiceMetadata) *LockingLicenseService {
return &LockingLicenseService{
LicenseDetailsStorage: detailsStorage,
LicenseUsageStorage: licenseStorage,
Org: orgData,
ServiceMetaData: serviceMetaData,
}
}

func (h *LockingLicenseService) CreateLicenseDetails(channelId *big.Int, serviceId string,
license License) (err error) {
key := LicenseDetailsKey{ServiceID: serviceId, ChannelID: channelId}
//Get the associated fixed Pricing details
fixedPricingDetails, err := h.getFixedPriceFromServiceMetadata(license)
if err != nil {
return err
}
data := LicenseDetailsData{License: license, FixedPricing: fixedPricingDetails}
return h.LicenseDetailsStorage.Put(key, data)
}

func (h *LockingLicenseService) getFixedPriceFromServiceMetadata(license License) (fixedPricingDetails ServiceMethodCostDetails, err error) {
fixedPricingDetails = ServiceMethodCostDetails{}
serviceMetadata := h.ServiceMetaData
fixedPricingDetails.Price = serviceMetadata.GetDefaultPricing().PriceInCogs
fixedPricingDetails.PlanName = serviceMetadata.GetDefaultPricing().PriceModel
return
}

func (h *LockingLicenseService) CreateOrUpdateLicense(channelId *big.Int, serviceId string) (err error) {
return h.UpdateLicenseUsage(channelId, serviceId, nil, PLANNED, SUBSCRIPTION)
}
Expand Down
25 changes: 19 additions & 6 deletions license_server/license_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func serializeLicenseDetailsKey(key interface{}) (serialized string, err error)

type LicenseDetailsData struct {
License License
//This is to capture the fixed price at the time of purchasing the license
//If there is no Dynamic Pricing involved , we will need to fall back on the fixed price
//that is available
// Keep this flexible to ensure we also support method level pricing in to the future
FixedPricing ServiceMethodCostDetails
}
type LicenseUsageTrackerKey struct {
ChannelID *big.Int
Expand Down Expand Up @@ -102,6 +107,7 @@ type Usage interface {
}

type License interface {
GetName() string
GetType() string
IsActive() bool
IsCallEligible() (bool, error)
Expand Down Expand Up @@ -206,14 +212,14 @@ type Tier struct {
AuthorizedAddresses []string
}

type ServiceMethodDetails struct {
type ServiceMethodCostDetails struct {
PlanName string
ServiceName string
MethodName string
Price *big.Int
}

func (s ServiceMethodDetails) String() string {
func (s ServiceMethodCostDetails) String() string {
return fmt.Sprintf("{Valididty:%v,Details:%v,Discount:%v}",
s.PlanName, s.ServiceName, s.MethodName)
}
Expand Down Expand Up @@ -248,16 +254,20 @@ type PricingDetails struct {
PlanName string
ValidityInDays uint8
ActualAmountSigned *big.Int
ServiceMethodDetails *ServiceMethodDetails //If this is null , implies it applies to all methods of the Service or just the one defined here
ServiceMethodDetails *ServiceMethodCostDetails //If this is null , implies it applies to all methods of the Service or just the one defined here
}

func (s PricingDetails) String() string {
return fmt.Sprintf("{CreditsInCogs:%v,FeeInCogs:%v,PlanName:%v"+
",ValidityInDays:%v,ActualAmountSigned:%v,ServiceMethodDetails:%v}",
",ValidityInDays:%v,ActualAmountSigned:%v,ServiceMethodCostDetails:%v}",
s.CreditsInCogs, s.FeeInCogs, s.PlanName, s.ValidityInDays, s.ActualAmountSigned,
s.ServiceMethodDetails)
}

func (s Subscription) GetName() string {
return s.GetName()
}

func (s Subscription) ValidFrom() time.Time {
return s.Validity.StartTimeUTC
}
Expand Down Expand Up @@ -294,6 +304,9 @@ func (s Subscription) String() string {
s.Validity.String(), s.Details.String(), s.Discount.String())
}

func (s Tier) GetName() string {
return s.GetName()
}
func (s Tier) GetType() string {
return TIER
}
Expand Down Expand Up @@ -338,7 +351,7 @@ func serializeLicenseDetailsData(value interface{}) (slice string, err error) {
gob.Register(&PricingDetails{})
gob.Register(&TierPricingDetails{})
gob.Register(&DiscountPercentage{})
gob.Register(&ServiceMethodDetails{})
gob.Register(&ServiceMethodCostDetails{})

err = e.Encode(value)

Expand All @@ -356,7 +369,7 @@ func deserializeLicenseDetailsData(slice string, value interface{}) (err error)
gob.Register(&PricingDetails{})
gob.Register(&TierPricingDetails{})
gob.Register(&DiscountPercentage{})
gob.Register(&ServiceMethodDetails{})
gob.Register(&ServiceMethodCostDetails{})

d := gob.NewDecoder(b)
err = d.Decode(value)
Expand Down
2 changes: 1 addition & 1 deletion license_server/license_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_serializeLicenseDetailsData(t *testing.T) {
ValidityInDays: 120,
FeeInCogs: big.NewInt(120),
CreditsInCogs: big.NewInt(130),
ServiceMethodDetails: &ServiceMethodDetails{MethodName: "M1", ServiceName: "S1"},
ServiceMethodDetails: &ServiceMethodCostDetails{MethodName: "M1", ServiceName: "S1"},
},
}
fmt.Println(license)
Expand Down

0 comments on commit 337603d

Please sign in to comment.