-
Notifications
You must be signed in to change notification settings - Fork 3
/
customers.go
330 lines (291 loc) · 12 KB
/
customers.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package chartmogul
import "strings"
// Customer is the customer as represented in the API.
type Customer struct {
ID uint32 `json:"id,omitempty"`
// Basic info
DataSourceUUID string `json:"data_source_uuid,omitempty"`
DataSourceUUIDs []string `json:"data_source_uuids,omitempty"`
UUID string `json:"uuid,omitempty"`
ExternalID string `json:"external_id,omitempty"`
ExternalIDs []string `json:"external_ids,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Status string `json:"status,omitempty"`
CustomerSince string `json:"customer-since,omitempty"`
Attributes *Attributes `json:"attributes,omitempty"`
Address *Address `json:"address,omitempty"`
// Other info
Mrr float64 `json:"mrr,omitempty"`
Arr float64 `json:"arr,omitempty"`
BillingSystemURL string `json:"billing-system-url,omitempty"`
ChartmogulURL string `json:"chartmogul-url,omitempty"`
BillingSystemType string `json:"billing-system-type,omitempty"`
Currency string `json:"currency,omitempty"`
CurrencySign string `json:"currency-sign,omitempty"`
// For update
Company string `json:"company,omitempty"`
Country string `json:"country,omitempty"`
State string `json:"state,omitempty"`
City string `json:"city,omitempty"`
Zip string `json:"zip,omitempty"`
LeadCreatedAt string `json:"lead_created_at,omitempty"`
FreeTrialStartedAt string `json:"free_trial_started_at,omitempty"`
WebsiteUrl string `json:"website_url,omitempty"`
Errors Errors `json:"errors,omitempty"`
}
// UpdateCustomer allows updating customer on the update endpoint.
type UpdateCustomer struct {
Name *string `json:"name,omitempty"`
Email *string `json:"email,omitempty"`
Company *string `json:"company,omitempty"`
Country *string `json:"country,omitempty"`
State *string `json:"state,omitempty"`
City *string `json:"city,omitempty"`
Zip *string `json:"zip,omitempty"`
LeadCreatedAt *string `json:"lead_created_at,omitempty"`
FreeTrialStartedAt *string `json:"free_trial_started_at,omitempty"`
Attributes *Attributes `json:"attributes,omitempty"`
WebsiteUrl *string `json:"website_url,omitempty"`
}
// NewCustomer allows creating customer on a new endpoint.
type NewCustomer struct {
// Obligatory
DataSourceUUID string `json:"data_source_uuid"`
ExternalID string `json:"external_id,omitempty"`
Name string `json:"name,omitempty"`
//Optional
Email string `json:"email,omitempty"`
Attributes *NewAttributes `json:"attributes,omitempty"`
// Address
Company string `json:"company,omitempty"`
Country string `json:"country,omitempty"`
State string `json:"state,omitempty"`
City string `json:"city,omitempty"`
Zip string `json:"zip,omitempty"`
// Lead/Trial
LeadCreatedAt string `json:"lead_created_at,omitempty"`
FreeTrialStartedAt string `json:"free_trial_started_at,omitempty"`
// Website
WebsiteUrl string `json:"website_url,omitempty"`
}
// Attributes is subdocument of Customer.
type Attributes struct {
Tags []string `json:"tags,omitempty"`
Stripe map[string]interface{} `json:"stripe,omitempty"`
Clearbit map[string]interface{} `json:"clearbit,omitempty"`
Custom map[string]interface{} `json:"custom,omitempty"`
}
// NewAttributes is subdocument of NewCustomer.
type NewAttributes struct {
Tags []string `json:"tags,omitempty"`
Custom []*CustomAttribute `json:"custom,omitempty"`
}
// Address is subdocument of Customer.
type Address struct {
AddressZIP string `json:"address_zip,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
Country string `json:"country,omitempty"`
}
// ListCustomersParams = parameters for listing customers in API.
type ListCustomersParams struct {
DataSourceUUID string `json:"data_source_uuid,omitempty"`
Status string `json:"status,omitempty"`
System string `json:"system,omitempty"`
ExternalID string `json:"external_id,omitempty"`
Cursor
}
// SearchCustomersParams - just email now.
type SearchCustomersParams struct {
Email string `json:"email,omitempty"`
Cursor
}
// Customers is result of listing customers in API.
type Customers struct {
Entries []*Customer `json:"entries,omitempty"`
Pagination
}
// MergeCustomersParams - identify source and target for merging.
type MergeCustomersParams struct {
From CustID `json:"from"`
Into CustID `json:"into"`
}
// UnmergeCustomersParams - identify target for unmerging.
type UnmergeCustomersParams struct {
CustomerUUID string `json:"customer_uuid"`
ExternalID string `json:"external_id"`
DataSourceUUID string `json:"data_source_uuid"`
MoveToNewCustomer []string `json:"move_to_new_customer,omitempty"`
}
// CustID - use either DataSourceUUID & ExternalID or CustomerUUID
type CustID struct {
DataSourceUUID string `json:"data_source_uuid,omitempty"`
ExternalID string `json:"external_id,omitempty"`
CustomerUUID string `json:"customer_uuid,omitempty"`
}
// DeleteCustomerInvoicesParams - optional param for deleting all customer invoices.
type DeleteCustomerInvoicesParams struct {
CustomerExternalID string
}
const (
singleCustomerEndpoint = "customers/:uuid"
deleteCustomerInvoicesEndpoint = "data_sources/:data_source_uuid/customers/:uuid/invoices"
customersEndpoint = "customers"
searchCustomersEndpoint = "customers/search"
mergeCustomersEndpoint = "customers/merges"
unmergeCustomersEndpoint = "customers/unmerges"
customerContactsEndpoint = "customers/:uuid/contacts"
)
// CreateCustomer loads the customer to Chartmogul. New endpoint - with attributes.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) CreateCustomer(newCustomer *NewCustomer) (*Customer, error) {
result := &Customer{}
return result, api.create(customersEndpoint, newCustomer, result)
}
// RetrieveCustomer returns one customer as in API.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) RetrieveCustomer(customerUUID string) (*Customer, error) {
result := &Customer{}
return result, api.retrieve(singleCustomerEndpoint, customerUUID, result)
}
// UpdateCustomer updates one customer in API.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) UpdateCustomer(customer *Customer, customerUUID string) (*Customer, error) {
result := &Customer{}
return result, api.update(singleCustomerEndpoint,
customerUUID,
customer,
result)
}
// UpdateCustomerV2 updates one customer in API.
//
// See https://dev.chartmogul.com/v1.0/reference#update-a-customer
func (api API) UpdateCustomerV2(input *UpdateCustomer, customerUUID string) (*Customer, error) {
output := &Customer{}
return output, api.update(singleCustomerEndpoint, customerUUID, input, output)
}
// ListCustomers lists all Customers for cutomer of given UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) ListCustomers(listCustomersParams *ListCustomersParams) (*Customers, error) {
result := &Customers{}
query := make([]interface{}, 0, 1)
if listCustomersParams != nil {
query = append(query, *listCustomersParams)
}
return result, api.list(customersEndpoint, result, query...)
}
// SearchCustomers lists all Customers for cutomer of given UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) SearchCustomers(searchCustomersParams *SearchCustomersParams) (*Customers, error) {
result := &Customers{}
return result, api.list(searchCustomersEndpoint, result, *searchCustomersParams)
}
// MergeCustomers merges two cutomers.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) MergeCustomers(mergeCustomersParams *MergeCustomersParams) error {
return api.merge(mergeCustomersEndpoint, *mergeCustomersParams)
}
// UnmergeCustomers unmerges two cutomers.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) UnmergeCustomers(unmergeCustomersParams *UnmergeCustomersParams) error {
return api.unmerge(unmergeCustomersEndpoint, *unmergeCustomersParams)
}
// DeleteCustomer deletes one customer by UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) DeleteCustomer(customerUUID string) error {
return api.delete(singleCustomerEndpoint, customerUUID)
}
// DeleteCustomerInvoices deletes all customer's invoices by UUID for given data source UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) DeleteCustomerInvoices(dataSourceUUID, customerUUID string) error {
path := strings.Replace(deleteCustomerInvoicesEndpoint, ":data_source_uuid", dataSourceUUID, 1)
return api.delete(path, customerUUID)
}
// DeleteCustomerInvoicesV2 deletes all customer's invoices by UUID & ExternalID for given data source UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) DeleteCustomerInvoicesV2(dataSourceUUID, customerUUID string, deleteCustomerInvoicesParams *DeleteCustomerInvoicesParams) error {
path := strings.Replace(deleteCustomerInvoicesEndpoint, ":data_source_uuid", dataSourceUUID, 1)
if deleteCustomerInvoicesParams != nil && len(deleteCustomerInvoicesParams.CustomerExternalID) > 0 {
path += "?customer_external_id=" + deleteCustomerInvoicesParams.CustomerExternalID
}
return api.delete(path, customerUUID)
}
// ListCustomersContacts
//
// See https://dev.chartmogul.com/reference/list-customers-contacts
func (api API) ListCustomersContacts(listContactsParams *ListContactsParams, customerUUID string) (*Contacts, error) {
result := &Contacts{}
query := make([]interface{}, 0, 1)
if listContactsParams != nil {
query = append(query, *listContactsParams)
}
path := strings.Replace(customerContactsEndpoint, ":uuid", customerUUID, 1)
return result, api.list(path, result, query...)
}
// CreateCustomersContacts
//
// See https://dev.chartmogul.com/reference/create-a-contact
func (api API) CreateCustomersContact(newContact *NewContact, customerUUID string) (*Contact, error) {
result := &Contact{}
path := strings.Replace(customerContactsEndpoint, ":uuid", customerUUID, 1)
return result, api.create(path, newContact, result)
}
// ListCustomerNotes
//
// See https://dev.chartmogul.com/reference/list-customer-notes
func (api API) ListCustomerNotes(listCustomerNotesParams *ListNotesParams, customerUUID string) (*Notes, error) {
result := &Notes{}
query := make([]interface{}, 0, 1)
if listCustomerNotesParams != nil {
if listCustomerNotesParams.CustomerUUID == "" {
listCustomerNotesParams.CustomerUUID = customerUUID
}
query = append(query, *listCustomerNotesParams)
}
return result, api.list(customerNotesEndpoint, result, query...)
}
// CreateCustomerNote
//
// See https://dev.chartmogul.com/reference/create-a-customer-note
func (api API) CreateCustomerNote(input *NewNote, customerUUID string) (*Note, error) {
result := &Note{}
if input.CustomerUUID == "" {
input.CustomerUUID = customerUUID
}
return result, api.create(customerNotesEndpoint, input, result)
}
// ListCustomerOpporunities
//
// See https://dev.chartmogul.com/reference/list-opportunities
func (api API) ListCustomerOpporunities(listOpportunitiesParams *ListOpportunitiesParams, customerUUID string) (*Opportunities, error) {
result := &Opportunities{}
query := make([]interface{}, 0, 1)
if listOpportunitiesParams != nil {
if listOpportunitiesParams.CustomerUUID == "" {
listOpportunitiesParams.CustomerUUID = customerUUID
}
query = append(query, *listOpportunitiesParams)
}
return result, api.list(opportunitiesEndpoint, result, query...)
}
// CreateCustomerOpportunity
//
// See https://dev.chartmogul.com/reference/create-an-opportunity
func (api API) CreateCustomerOpportunity(input *NewOpportunity, customerUUID string) (*Opportunity, error) {
result := &Opportunity{}
if input.CustomerUUID == "" {
input.CustomerUUID = customerUUID
}
return result, api.create(opportunitiesEndpoint, input, result)
}