/
customer.R
195 lines (165 loc) · 5.13 KB
/
customer.R
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
#' Create a customer
#'
#' @param idempotency A random string to ensure no repeat charges
#' @param account_balance Integer amount in pence
#' @param coupon A discount on recurring charges
#' @param description Arbitary string
#' @param email customer's email address
#' @param metadata A named list
#' @param plan ID of the plan to subscribe customer to
#' @param quantity Quantity to apply to subscription. Needs plan.
#' @param shipping Optional dictionary
#' @param source A token from \link{get_token}
#' @param tax_percent A positive decimal. Percentage added as tax.
#' @param trial_end Unix timestamp when trial period ends. Needs plan.
#'
#' @details
#' Setting \code{account_balance} to negative means the customer will have a credit.
#' A positive amount means it will be added to the next invoice.
#'
#' \code{description, email, metadata} will be set to empty if you POST an empty value.
#'
#' @return List object
#'
#' @family customers
#' @export
create_customer <- function(idempotency,
account_balance=NULL,
coupon=NULL,
description=NULL,
email=NULL,
metadata=NULL,
plan=NULL,
quantity=NULL,
shipping=NULL,
source=NULL,
tax_percent=NULL,
trial_end=NULL){
the_body <- list(
account_balance=account_balance,
coupon=coupon,
description=description,
email=email,
plan=plan,
quantity=quantity,
shipping=shipping,
source=source,
tax_percent=tax_percent,
trial_end=trial_end
)
if(!is.null(metadata)){
the_body <- c(the_body, make_meta(metadata))
}
req <- do_request("https://api.stripe.com/v1/customers",
request_type="POST",
idempotency=idempotency,
the_body = the_body)
req
}
#' Retrieve a customer
#'
#' @param customerId The ID of the customer
#'
#' @return List object
#'
#' @family customers
#' @export
get_customer <- function(customerId){
url <- sprintf("https://api.stripe.com/v1/customers/%s", customerId)
req <- do_request(url, "GET")
req
}
#' Update a customer
#'
#' @param customerId The ID of the customer
#' @param account_balance Integer amount in pence
#' @param coupon A discount on recurring charges
#' @param default_source ID of source to make customer new default
#' @param description Arbitary string
#' @param email customer's email address
#' @param metadata A named list
#' # @param plan ID of the plan to subscribe customer to
#' # @param quantity Quantity you'd like to apply to subscription. Needs plan.
#' @param shipping Optional dictionary
#' @param source A token from \link{get_token}
#' # @param tax_percent A positive decimal. Percentage added as tax.
#' # @param trail_end Unix timestamp when trial period ends. Needs plan.
#'
#' @details
#' Setting \code{account_balance} to negative means the customer will have a credit.
#' A positive amount means it will be added to the next invoice.
#'
#' \code{description, email, metadata} will be set to empty if you POST an empty value.
#'
#' @return List object
#'
#' @family customers
#' @export
update_customer <- function(customerId,
account_balance=NULL,
coupon=NULL,
default_source=NULL,
description=NULL,
email=NULL,
metadata=NULL,
shipping=NULL,
source=NULL){
url <- sprintf("https://api.stripe.com/v1/customers/%s", customerId)
the_body <- list(
account_balance=account_balance,
coupon=coupon,
default_source=default_source,
description=description,
email=email,
shipping=shipping,
source=source
)
if(!is.null(metadata)){
the_body <- c(the_body, make_meta(metadata))
}
req <- do_request(url,
"POST",
the_body = the_body)
req
}
#' Delete a customer
#'
#' @param customerId The ID of the customer
#'
#' @return List object
#'
#' @family customers
#' @export
delete_customer <- function(customerId){
url <- sprintf("https://api.stripe.com/v1/customers/%s", customerId)
req <- do_request(url, "DELETE")
req
}
#' List all customers
#'
#' @param created filter on created
#' @param ending_before filter for pagination
#' @param limit Between 1 and 100
#' @param starting_before filter for pagination
#'
#' @return List object
#'
#' @family customers
#' @export
#' @importFrom httr GET
list_customers <- function(created=NULL,
ending_before=NULL,
limit=10,
starting_before=NULL){
params <- list(
created=created,
ending_before=ending_before,
limit=limit,
starting_before=starting_before
)
url <- httr::modify_url("https://api.stripe.com/v1/customers",
query = params)
req <- do_request(url, "GET", limit=limit)
content <- req$data
req
}