diff --git a/cas.go b/cas.go index 1dde934..c086388 100644 --- a/cas.go +++ b/cas.go @@ -6,6 +6,26 @@ func listItems(e *Envelope) { e.Error(9, "Unable to obtain title.", err) } + attrs, err := e.getKeys("AttributeFilters") + if err != nil { + e.Error(5, "AttributeFilters key did not exist!", err) + } + + var licenceStr string + for _, attr := range attrs { + name, value := parseNameValue(attr.InnerText()) + if name == "TitleKind" { + licenceStr = value + } + } + + // Now validate + licenceKind, err := GetLicenceKind(licenceStr) + if err != nil { + e.Error(5, "Invalid TitleKind was passed by SOAP", err) + } + + // TODO(SketchMaster2001): Query database for items e.AddKVNode("ListResultTotalSize", "1") e.AddCustomType(Items{ TitleId: titleId, @@ -35,8 +55,8 @@ func listItems(e *Envelope) { Amount: 0, Currency: "POINTS", }, - Limits: LimitStruct(DR), - LicenseKind: RENTAL, + Limits: LimitStruct(PR), + LicenseKind: *licenceKind, }, }) } diff --git a/constants.go b/constants.go index b90e8c2..c4e9a4c 100644 --- a/constants.go +++ b/constants.go @@ -1,5 +1,7 @@ package main +import "errors" + // LimitKinds represents various limits applied to the current ticket. type LimitKinds int @@ -25,6 +27,23 @@ const ( SERVICE LicenceKinds = "SERVICE" ) +func GetLicenceKind(kind string) (*LicenceKinds, error) { + names := map[string]LicenceKinds{ + "PERMANENT": PERMANENT, + "DEMO": DEMO, + "TRIAL": TRIAL, + "RENTAL": RENTAL, + "SUBSCRIPT": SUBSCRIPT, + "SERVICE": SERVICE, + } + + if value, exists := names[kind]; exists { + return &value, nil + } else { + return nil, errors.New("invalid LicenceKind") + } +} + // LimitStruct returns a Limits struct filled for the given kind. func LimitStruct(kind LimitKinds) Limits { names := map[LimitKinds]string{ diff --git a/ecs.go b/ecs.go index ca4382b..b6eda55 100644 --- a/ecs.go +++ b/ecs.go @@ -169,18 +169,18 @@ func purchaseTitle(e *Envelope) { func listPurchaseHistory(e *Envelope) { e.AddCustomType([]Transactions{ { - TransactionId: "12345678", + TransactionId: "00000000", Date: e.Timestamp(), - Type: "SERVICE", - TotalPaid: 7, + Type: string(SERVICE), + TotalPaid: 0, Currency: "POINTS", ItemId: 0, TitleId: "000100014843494A", ItemPricing: []Limits{ LimitStruct(DR), }, - ReferenceId: 1, - ReferenceValue: 19224, + ReferenceId: "01234567890123456789012345678912", + ReferenceValue: 1, }, }) diff --git a/structure.go b/structure.go index 5c49266..26abff7 100644 --- a/structure.go +++ b/structure.go @@ -121,7 +121,7 @@ type Transactions struct { ItemPricing []Limits `xml:"ItemPricing"` TitleId string `xml:"TitleId,omitempty"` ItemCode int `xml:"ItemCode,omitempty"` - ReferenceId int `xml:"ReferenceId,omitempty"` + ReferenceId string `xml:"ReferenceId,omitempty"` ReferenceValue int `xml:"ReferenceValue,omitempty"` } diff --git a/utils.go b/utils.go index a3fc945..db004d7 100644 --- a/utils.go +++ b/utils.go @@ -209,6 +209,15 @@ func (e *Envelope) Error(errorCode int, reason string, err error) { e.AddKVNode("ErrorMessage", fmt.Sprintf("%s: %v", reason, err)) } +// parseNameValue parses the output of *xmlquery.Node.InnerText when it is a nested Name and Value node. +func parseNameValue(s string) (string, string) { + s = strings.TrimSpace(s) + s = strings.Replace(s, " ", "", strings.Count(s, " ")-1) + decoded := strings.Split(s, " ") + + return strings.TrimSuffix(decoded[0], "\n"), decoded[1] +} + // normalise parses a document, returning a document with only the request type's child nodes, stripped of prefix. func normalise(service string, action string, reader io.Reader) (*xmlquery.Node, error) { doc, err := xmlquery.Parse(reader) @@ -247,6 +256,17 @@ func (e *Envelope) getKey(key string) (string, error) { } } +// getKeys returns a list of xmlquery.Node, if documented. +func (e *Envelope) getKeys(key string) ([]*xmlquery.Node, error) { + node := xmlquery.Find(e.doc, "//"+key) + + if node == nil { + return nil, errors.New("missing mandatory key named " + key) + } else { + return node, nil + } +} + // Derived from https://stackoverflow.com/a/31832326, adding numbers const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"