-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplan_addons.go
73 lines (63 loc) · 2.24 KB
/
plan_addons.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gorecurly
import (
"encoding/xml"
"errors"
"time"
)
//Plan Add on fields struct
type PlanAddOnFields struct {
endpoint string
r *Recurly
Plan *PlanStub `xml:"plan,omitempty"`
Name string `xml:"name,omitempty"`
AddOnCode string `xml:"add_on_code,omitempty"`
DisplayQuantityOnHostedPage bool `xml:"display_quantity_on_hosted_page,omitempty"`
DefaultQuantity int `xml:"default_quantity,omitempty"`
CreatedAt *time.Time `xml:"created_at,omitempty"`
}
//Plan add on
type PlanAddOn struct {
XMLName xml.Name `xml:"add_on"`
PlanAddOnFields
UnitAmountInCents *CurrencyArray `xml:"unit_amount_in_cents,omitempty"`
}
type tempPlanAddOn struct {
XMLName xml.Name `xml:"add_on"`
PlanAddOnFields
UnitAmountInCents *CurrencyMarshalArray `xml:"unit_amount_in_cents,omitempty"`
}
//Create plan add on given a plan code
func (p *PlanAddOn) Create(plan_code string) error {
if p.CreatedAt != nil {
return RecurlyError{statusCode: 400, Description: "Add on Code Already in Use"}
}
return p.r.doCreate(&p, PLANS+"/"+plan_code+"/add_ons")
}
//Update a plan add on
func (p *PlanAddOn) Update() error {
newaddon := new(tempPlanAddOn)
newaddon.Name = p.Name
newaddon.DisplayQuantityOnHostedPage = p.DisplayQuantityOnHostedPage
newaddon.DefaultQuantity = p.DefaultQuantity
newaddon.CreatedAt = nil
//Total hack job
//due to limitation of XML.marshal not recognizing "any" tag
//could be fixed in future go releases
unitAmountInCents := make([]*Currency, len(p.UnitAmountInCents.CurrencyList))
newaddon.UnitAmountInCents = &CurrencyMarshalArray{unitAmountInCents}
for k, _ := range p.UnitAmountInCents.CurrencyList {
newaddon.UnitAmountInCents.CurrencyList[k] = &p.UnitAmountInCents.CurrencyList[k]
}
//end hack job
if len(newaddon.UnitAmountInCents.CurrencyList) <= 0 {
newaddon.UnitAmountInCents = nil
}
if p.Plan != nil {
return p.r.doUpdate(newaddon, PLANS+"/"+p.Plan.GetCode()+"/add_ons/"+p.AddOnCode)
}
return errors.New("Plan Does not exist")
}
//Delete plan add on
func (p *PlanAddOn) Delete() error {
return p.r.doDelete(PLANS + "/" + p.Plan.GetCode() + "/add_ons/" + p.AddOnCode)
}