-
Notifications
You must be signed in to change notification settings - Fork 1
/
order.go
159 lines (138 loc) · 4.19 KB
/
order.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package go_certcentral
import (
"bytes"
"crypto/x509"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"time"
)
const orderURL = "/order/certificate/"
type OrderType string
func (o OrderType) String() string {
return string(o)
}
var OrderTypes = struct {
SSLPlus,
SSLMultiDomain,
SSLWildcard,
PrivateSSLPlus,
PrivateSSLWildcard,
SecureSiteProSSL,
SecureSiteOV,
SecureSiteProEVSSL,
SecureSiteEV OrderType
}{
"ssl_plus",
"ssl_multi_domain",
"ssl_wildcard",
"private_ssl_plus",
"private_ssl_wildcard",
"ssl_securesite_pro",
"ssl_securesite_flex",
"ssl_ev_securesite_pro",
"ssl_ev_securesite_flex",
}
type PaymentMethod string
func (p PaymentMethod) String() string {
return string(p)
}
var PaymentMethods = struct {
Balance,
Card,
Profile,
WireTransfer PaymentMethod
}{
"balance",
"card",
"profile",
"wire_transfer",
}
type (
Order struct {
Certificate Certificate `json:"certificate,omitempty"`
Organization *Organization `json:"organization,omitempty"`
ValidityYears int `json:"validity_years,omitempty"`
CustomExpirationDate string `json:"custom_expiration_date,omitempty"`
Comments string `json:"comments,omitempty"`
ProcessorComment string `json:"processor_comment,omitempty"`
DisableRenewalNotifications bool `json:"disable_renewal_notifications,omitempty"`
RenewalOfOrderID int `json:"renewal_of_order_id,omitempty"`
PaymentMethod PaymentMethod `json:"payment_method,omitempty"`
SkipApproval bool `json:"skip_approval,omitempty"`
Product *Product `json:"product,omitempty"`
OrganizationContact *User `json:"organization_contact,omitempty"`
TechnicalContact *User `json:"technical_contact,omitempty"`
User *User `json:"user,omitempty"`
CsProvisioningMethod string `json:"cs_provisioning_method,omitempty"`
DisableCT bool `json:"disable_ct,omitempty"`
Requests []OrderRequest `json:"requests,omitempty"`
ID int `json:"id"`
Domains []Domain `json:"domains,omitempty"`
CertificateID int `json:"certificate_id,omitempty"`
CertificateChain []CertificateChain `json:"certificate_chain,omitempty"`
Container *Container `json:"container,omitempty"`
}
OrderRequest struct {
ID int `json:"id"`
Date *time.Time `json:"date,omitempty"`
Type string `json:"type,omitempty"`
Status Status `json:"status,omitempty"`
Comments string `json:"comments,omitempty"`
}
)
func (o Order) DecodeCertificateChain() ([]*x509.Certificate, error) {
crtChain := make([]*x509.Certificate, 0)
for _, crt := range o.CertificateChain {
decodedCrt, err := crt.DecodePEM()
if err != nil {
return crtChain, err
}
crtChain = append(crtChain, decodedCrt...)
}
return crtChain, nil
}
func (c *Client) SubmitOrder(order Order, orderType OrderType) (*Order, error) {
body, err := json.Marshal(order)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, makeURL(orderURL, orderType.String()), bytes.NewReader(body))
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err = ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var orderRes Order
err = json.Unmarshal(body, &orderRes)
return &orderRes, err
}
func (c *Client) GetOrder(orderID string) (*Order, error) {
if orderID == "" {
return nil, errors.New("cannot get order without ID")
}
req, err := http.NewRequest(http.MethodGet, makeURL(orderURL, orderID), nil)
if err != nil {
return nil, err
}
res, err := c.do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var o Order
err = json.Unmarshal(resBody, &o)
return &o, err
}