├── README └── Stripe.cfc /README: -------------------------------------------------------------------------------- 1 | This is a ColdFusion wrapper for the Stripe.com API (http://stripe.com) 2 | 3 | It supports all the methods of the Stripe API. 4 | 5 | Compatible with Railo and Adobe ColdFusion. 6 | 7 | 8 | BASIC USAGE - works great with Stripe.js (https://gist.github.com/1084146) although it is not necessary 9 | 10 | Within your Application.cfc place the following code: 11 | 12 | application.stripeApiKey = 'YOUR STRIPE SECRET PUBLISHABLE KEY'; 13 | application.Stripe = createObject('component','path.to.Stripe').init(stripeApiKey=application.stripeApiKey); 14 | 15 | 16 | On the page you want to process payments: 17 | 18 | local.StripeResult = application.Stripe.createCharge(amount=form.amount,card=form.stripeToken,description='Payment for products'); 19 | 20 | 21 | NOTE: 22 | 23 | If you receive the following error: 24 | 25 | 401 Unauthorized 26 | { "error": { "type": "invalid_request_error", "message": "Stripe no longer supports API requests made with TLS 1.0. Please initiate HTTPS connections with TLS 1.2 or later. You can learn more about this at https://stripe.com/blog/upgrading-tls." } } 27 | 28 | Try updating to Java 8. This should fix this issue. 29 | -------------------------------------------------------------------------------- /Stripe.cfc: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2012 James Eisenlohr 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | component name="Stripe" output=false accessors=true description="ColdFusion Wrapper for Stripe.com API" { 18 | 19 | property string stripeApiKey; // Stripe Secret Key 20 | property string baseUrl; 21 | property string currency; // Stripe currently has only 'usd' support 22 | 23 | public stripe function init(required string stripeApiKey, string baseUrl='https://api.stripe.com/v1/', string currency='usd') { 24 | 25 | setStripeApiKey(arguments.stripeApiKey); 26 | setBaseUrl(arguments.baseUrl); 27 | setCurrency(arguments.currency); 28 | 29 | return this; 30 | } 31 | 32 | 33 | /* CHARGES */ 34 | 35 | public struct function createCharge(required numeric amount, string currency=getCurrency(), string customer='', any card, string description='') { 36 | 37 | local.HTTPService = createHTTPService('POST'); 38 | 39 | local.HTTPService.setUrl(getBaseUrl() & 'charges'); 40 | local.amount = amountToCents(arguments.amount); // convert amount to cents for Stripe 41 | local.HTTPService.addParam(type='formfield',name='amount',value=local.amount); 42 | local.HTTPService.addParam(type='formfield',name='currency',value=arguments.currency); 43 | if (Len(Trim(arguments.customer))) { 44 | local.HTTPService.addParam(type='formfield',name='customer',value=Trim(arguments.customer)); 45 | } 46 | if (StructKeyExists(arguments,'card') AND isStruct(arguments.card)) { 47 | local.HTTPService.addParam(type='formfield',name='card[number]',value=arguments.card.number); 48 | local.HTTPService.addParam(type='formfield',name='card[exp_month]',value=arguments.card.exp_month); 49 | local.HTTPService.addParam(type='formfield',name='card[exp_year]',value=arguments.card.exp_year); 50 | if (StructKeyExists(arguments,'card.cvc')) { 51 | local.HTTPService.addParam(type='formfield',name='card[cvc]',value=arguments.card.cvc); 52 | } 53 | if (StructKeyExists(arguments,'card.name') AND Len(Trim(arguments.card.name))) { 54 | local.HTTPService.addParam(type='formfield',name='card[name]',value=arguments.card.name); 55 | } 56 | if (StructKeyExists(arguments,'card.address_line1') AND Len(Trim(arguments.card.address_line1))) { 57 | local.HTTPService.addParam(type='formfield',name='card[address_line1]',value=arguments.card.address_line1); 58 | } 59 | if (StructKeyExists(arguments,'card.address_line2') AND Len(Trim(arguments.card.address_line2))) { 60 | local.HTTPService.addParam(type='formfield',name='card[address_line2]',value=arguments.card.address_line2); 61 | } 62 | if (StructKeyExists(arguments,'card.address_zip') AND Len(Trim(arguments.card.address_zip))) { 63 | local.HTTPService.addParam(type='formfield',name='card[address_zip]',value=arguments.card.address_zip); 64 | } 65 | if (StructKeyExists(arguments,'card.address_state') AND Len(Trim(arguments.card.address_state))) { 66 | local.HTTPService.addParam(type='formfield',name='card[address_state]',value=arguments.card.address_state); 67 | } 68 | if (StructKeyExists(arguments,'card.address_country') AND Len(Trim(arguments.card.address_country))) { 69 | local.HTTPService.addParam(type='formfield',name='card[address_country]',value=arguments.card.address_country); 70 | } 71 | } else if (StructKeyExists(arguments,'card')) { 72 | local.HTTPService.addParam(type='formfield',name='card',value=Trim(arguments.card)); 73 | } 74 | if (Len(Trim(arguments.description))) { 75 | local.HTTPService.addParam(type='formfield',name='description',value=Trim(arguments.description)); 76 | } 77 | local.HTTPResult = local.HTTPService.send().getPrefix(); 78 | 79 | if (NOT isDefined("local.HTTPResult.statusCode")) { 80 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 81 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 82 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 83 | } 84 | return deserializeJSON(local.HTTPResult.filecontent); 85 | } 86 | 87 | // Can get X number of charges (0 to 100, 10 is default), a specific charge or all charges for a specific customer 88 | public struct function readCharge(string chargeid='', string customerid='', numeric count, numeric offset) { 89 | 90 | local.HTTPService = createHTTPService('GET'); 91 | 92 | local.url = getBaseUrl() & 'charges'; 93 | if (Len(Trim(arguments.chargeid))) { 94 | local.url = local.url & '/' & arguments.chargeid; 95 | } else if (Len(Trim(arguments.customerid))) { 96 | local.url = local.url & '?customer=' & arguments.customerid; 97 | } 98 | if (StructKeyExists(arguments,'count') AND (arguments.count GT 0 AND arguments.count LTE 100)) { 99 | // Regex to find ? 100 | if (Find('?',local.url) GT 0) { 101 | local.url = local.url & '&count=' & arguments.count; 102 | } else { 103 | local.url = local.url & '?count=' & arguments.count; 104 | } 105 | } 106 | if (StructKeyExists(arguments,'offset') AND (arguments.offset GT 0)) { 107 | // Regex to find ? 108 | if (Find('?',local.url) GT 0) { 109 | local.url = local.url & '&offset=' & arguments.offset; 110 | } else { 111 | local.url = local.url & '?offset=' & arguments.offset; 112 | } 113 | } 114 | local.HTTPService.setUrl(local.url); 115 | local.HTTPResult = local.HTTPService.send().getPrefix(); 116 | 117 | if (NOT isDefined("local.HTTPResult.statusCode")) { 118 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 119 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 120 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 121 | } 122 | return deserializeJSON(local.HTTPResult.filecontent); 123 | } 124 | 125 | public struct function refundCharge(required string chargeid, numeric amount) { 126 | 127 | local.HTTPService = createHTTPService('POST'); 128 | 129 | local.HTTPService.setUrl(getBaseUrl() & 'charges/' & arguments.chargeid & '/refund'); 130 | if (StructKeyExists(arguments,'amount') AND arguments.amount GT 0) { 131 | local.HTTPService.addParam(type='formfield',name='amount',value=arguments.amount); 132 | } 133 | local.HTTPResult = local.HTTPService.send().getPrefix(); 134 | 135 | if (NOT isDefined("local.HTTPResult.statusCode")) { 136 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 137 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 138 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 139 | } 140 | return deserializeJSON(local.HTTPResult.filecontent); 141 | } 142 | 143 | 144 | /* CUSTOMERS */ 145 | 146 | public struct function createCustomer(any card, string coupon='', string email='', string description='', string plan='', timestamp trial_end) { 147 | 148 | local.HTTPService = createHTTPService('POST'); 149 | 150 | local.HTTPService.setUrl(getBaseUrl() & 'customers'); 151 | if (StructKeyExists(arguments,'card') AND isStruct(arguments.card)) { 152 | local.HTTPService.addParam(type='formfield',name='card[number]',value=arguments.card.number); 153 | local.HTTPService.addParam(type='formfield',name='card[exp_month]',value=arguments.card.exp_month); 154 | local.HTTPService.addParam(type='formfield',name='card[exp_year]',value=arguments.card.exp_year); 155 | if (StructKeyExists(arguments,'card.cvc')) { 156 | local.HTTPService.addParam(type='formfield',name='card[cvc]',value=arguments.card.cvc); 157 | } 158 | if (StructKeyExists(arguments,'card.name') AND Len(Trim(arguments.card.name))) { 159 | local.HTTPService.addParam(type='formfield',name='card[name]',value=arguments.card.name); 160 | } 161 | if (StructKeyExists(arguments,'card.address_line1') AND Len(Trim(arguments.card.address_line1))) { 162 | local.HTTPService.addParam(type='formfield',name='card[address_line1]',value=Trim(arguments.card.address_line1)); 163 | } 164 | if (StructKeyExists(arguments,'card.address_line2') AND Len(Trim(arguments.card.address_line2))) { 165 | local.HTTPService.addParam(type='formfield',name='card[address_line2]',value=Trim(arguments.card.address_line2)); 166 | } 167 | if (StructKeyExists(arguments,'card.address_zip') AND Len(Trim(arguments.card.address_zip))) { 168 | local.HTTPService.addParam(type='formfield',name='card[address_zip]',value=Trim(arguments.card.address_zip)); 169 | } 170 | if (StructKeyExists(arguments,'card.address_state') AND Len(Trim(arguments.card.address_state))) { 171 | local.HTTPService.addParam(type='formfield',name='card[address_state]',value=Trim(arguments.card.address_state)); 172 | } 173 | if (StructKeyExists(arguments,'card.address_country') AND Len(Trim(arguments.card.address_country))) { 174 | local.HTTPService.addParam(type='formfield',name='card[address_country]',value=Trim(arguments.card.address_country)); 175 | } 176 | } else if (StructKeyExists(arguments,'card')) { 177 | local.HTTPService.addParam(type='formfield',name='card',value=Trim(arguments.card)); 178 | } 179 | if (Len(Trim(arguments.coupon))) { 180 | local.HTTPService.addParam(type='formfield',name='coupon',value=Trim(arguments.coupon)); 181 | } 182 | if (Len(Trim(arguments.email))) { 183 | local.HTTPService.addParam(type='formfield',name='email',value=Trim(arguments.email)); 184 | } 185 | if (Len(Trim(arguments.description))) { 186 | local.HTTPService.addParam(type='formfield',name='description',value=Trim(arguments.description)); 187 | } 188 | if (Len(Trim(arguments.plan))) { 189 | local.HTTPService.addParam(type='formfield',name='plan',value=Trim(arguments.plan)); 190 | } 191 | if (StructKeyExists(arguments,'trial_end') AND IsDate(arguments.trial_end)) { 192 | local.intUTCDate = timeToUTCInt(arguments.trial_end); 193 | local.HTTPService.addParam(type='formfield',name='trial_end',value=local.intUTCDate); 194 | } 195 | local.HTTPResult = local.HTTPService.send().getPrefix(); 196 | 197 | if (NOT isDefined("local.HTTPResult.statusCode")) { 198 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 199 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 200 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 201 | } 202 | return deserializeJSON(local.HTTPResult.filecontent); 203 | } 204 | 205 | public struct function readCustomer(string customerid='', numeric count, numeric offset) { 206 | 207 | local.HTTPService = createHTTPService('GET'); 208 | 209 | local.url = getBaseUrl() & 'customers'; 210 | if (Len(Trim(arguments.customerid))) { 211 | local.HTTPService.setUrl(local.url & '/' & arguments.customerid); 212 | } else if (StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 213 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count & '&offset=' & arguments.offset); 214 | } else if (StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 215 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count); 216 | } else if (NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 217 | local.HTTPService.setUrl(local.url & '?offset=' & arguments.offset); 218 | } else { 219 | local.HTTPService.setUrl(local.url); 220 | } 221 | local.HTTPResult = local.HTTPService.send().getPrefix(); 222 | 223 | if (NOT isDefined("local.HTTPResult.statusCode")) { 224 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 225 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 226 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 227 | } 228 | return deserializeJSON(local.HTTPResult.filecontent); 229 | } 230 | 231 | public struct function updateCustomer(required string customerid, any card, string coupon='',string description='',string email='') { 232 | 233 | local.HTTPService = createHTTPService('POST'); 234 | 235 | local.HTTPService.setUrl(getBaseUrl() & 'customers/' & arguments.customerid); 236 | if (StructKeyExists(arguments,'card') AND isStruct(arguments.card)) { 237 | local.HTTPService.addParam(type='formfield',name='card[number]',value=arguments.card.number); 238 | local.HTTPService.addParam(type='formfield',name='card[exp_month]',value=arguments.card.exp_month); 239 | local.HTTPService.addParam(type='formfield',name='card[exp_year]',value=arguments.card.exp_year); 240 | if (StructKeyExists(arguments,'card.cvc')) { 241 | local.HTTPService.addParam(type='formfield',name='card[cvc]',value=arguments.card.cvc); 242 | } 243 | if (StructKeyExists(arguments,'card.name') AND Len(Trim(arguments.card.name))) { 244 | local.HTTPService.addParam(type='formfield',name='card[name]',value=arguments.card.name); 245 | } 246 | if (StructKeyExists(arguments,'card.address_line1') AND Len(Trim(arguments.card.address_line1))) { 247 | local.HTTPService.addParam(type='formfield',name='card[address_line1]',value=Trim(arguments.card.address_line1)); 248 | } 249 | if (StructKeyExists(arguments,'card.address_line2') AND Len(Trim(arguments.card.address_line2))) { 250 | local.HTTPService.addParam(type='formfield',name='card[address_line2]',value=Trim(arguments.card.address_line2)); 251 | } 252 | if (StructKeyExists(arguments,'card.address_zip') AND Len(Trim(arguments.card.address_zip))) { 253 | local.HTTPService.addParam(type='formfield',name='card[address_zip]',value=Trim(arguments.card.address_zip)); 254 | } 255 | if (StructKeyExists(arguments,'card.address_state') AND Len(Trim(arguments.card.address_state))) { 256 | local.HTTPService.addParam(type='formfield',name='card[address_state]',value=Trim(arguments.card.address_state)); 257 | } 258 | if (StructKeyExists(arguments,'card.address_country') AND Len(Trim(arguments.card.address_country))) { 259 | local.HTTPService.addParam(type='formfield',name='card[address_country]',value=Trim(arguments.card.address_country)); 260 | } 261 | } else if (StructKeyExists(arguments,'card')) { 262 | local.HTTPService.addParam(type='formfield',name='card',value=Trim(arguments.card)); 263 | } 264 | if (Len(Trim(arguments.coupon))) { 265 | local.HTTPService.addParam(type='formfield',name='coupon',value=Trim(arguments.coupon)); 266 | } 267 | if (Len(Trim(arguments.description))) { 268 | local.HTTPService.addParam(type='formfield',name='description',value=Trim(arguments.description)); 269 | } 270 | if (Len(Trim(arguments.email))) { 271 | local.HTTPService.addParam(type='formfield',name='email',value=Trim(arguments.email)); 272 | } 273 | local.HTTPResult = local.HTTPService.send().getPrefix(); 274 | 275 | if (NOT isDefined("local.HTTPResult.statusCode")) { 276 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 277 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 278 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 279 | } 280 | return deserializeJSON(local.HTTPResult.filecontent); 281 | } 282 | 283 | public struct function deleteCustomer(required string customerid) { 284 | 285 | local.HTTPService = createHTTPService('DELETE'); 286 | 287 | local.HTTPService.setUrl(getBaseUrl() & 'customers/' & arguments.customerid); 288 | local.HTTPResult = local.HTTPService.send().getPrefix(); 289 | 290 | if (NOT isDefined("local.HTTPResult.statusCode")) { 291 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 292 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 293 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 294 | } 295 | return deserializeJSON(local.HTTPResult.filecontent); 296 | } 297 | 298 | 299 | /* CARD TOKENS */ 300 | 301 | public struct function createToken(required struct card, numeric amount, string currency=getCurrency()) { 302 | 303 | local.HTTPService = createHTTPService('POST'); 304 | 305 | local.HTTPService.setUrl(getBaseUrl() & 'tokens'); 306 | 307 | if (StructKeyExists(arguments,'card') AND isStruct(arguments.card)) { 308 | local.HTTPService.addParam(type='formfield',name='card[number]',value=arguments.card.number); 309 | local.HTTPService.addParam(type='formfield',name='card[exp_month]',value=arguments.card.exp_month); 310 | local.HTTPService.addParam(type='formfield',name='card[exp_year]',value=arguments.card.exp_year); 311 | if (StructKeyExists(arguments,'card.cvc')) { 312 | local.HTTPService.addParam(type='formfield',name='card[cvc]',value=arguments.card.cvc); 313 | } 314 | if (StructKeyExists(arguments,'card.name') AND Len(Trim(arguments.card.name))) { 315 | local.HTTPService.addParam(type='formfield',name='card[name]',value=arguments.card.name); 316 | } 317 | if (StructKeyExists(arguments,'card.address_line1') AND Len(Trim(arguments.card.address_line1))) { 318 | local.HTTPService.addParam(type='formfield',name='card[address_line1]',value=Trim(arguments.card.address_line1)); 319 | } 320 | if (StructKeyExists(arguments,'card.address_line2') AND Len(Trim(arguments.card.address_line2))) { 321 | local.HTTPService.addParam(type='formfield',name='card[address_line2]',value=Trim(arguments.card.address_line2)); 322 | } 323 | if (StructKeyExists(arguments,'card.address_zip') AND Len(Trim(arguments.card.address_zip))) { 324 | local.HTTPService.addParam(type='formfield',name='card[address_zip]',value=Trim(arguments.card.address_zip)); 325 | } 326 | if (StructKeyExists(arguments,'card.address_state') AND Len(Trim(arguments.card.address_state))) { 327 | local.HTTPService.addParam(type='formfield',name='card[address_state]',value=Trim(arguments.card.address_state)); 328 | } 329 | if (StructKeyExists(arguments,'card.address_country') AND Len(Trim(arguments.card.address_country))) { 330 | local.HTTPService.addParam(type='formfield',name='card[address_country]',value=Trim(arguments.card.address_country)); 331 | } 332 | } 333 | if (StructKeyExists(arguments,'amount')) { 334 | local.amount = amountToCents(arguments.amount); // convert amount to cents for Stripe 335 | local.HTTPService.addParam(type='formfield',name='amount',value=local.amount); 336 | } 337 | local.HTTPService.addParam(type='formfield',name='currency',value=arguments.currency); 338 | local.HTTPResult = local.HTTPService.send().getPrefix(); 339 | 340 | if (NOT isDefined("local.HTTPResult.statusCode")) { 341 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 342 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 343 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 344 | } 345 | return deserializeJSON(local.HTTPResult.filecontent); 346 | } 347 | 348 | public struct function readToken(required string tokenid) { 349 | 350 | local.HTTPService = createHTTPService('GET'); 351 | 352 | local.HTTPService.setUrl(getBaseUrl() & 'tokens/' & arguments.tokenid); 353 | local.HTTPResult = local.HTTPService.send().getPrefix(); 354 | 355 | if (NOT isDefined("local.HTTPResult.statusCode")) { 356 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 357 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 358 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 359 | } 360 | return deserializeJSON(local.HTTPResult.filecontent); 361 | } 362 | 363 | 364 | /* PLANS */ 365 | 366 | public struct function createPlan(required numeric amount, required string interval, required string name, string planid=CreateUUID(), string currency=getCurrency(), numeric trial_period_days) { 367 | 368 | local.HTTPService = createHTTPService('POST'); 369 | 370 | local.HTTPService.setUrl(getBaseUrl() & 'plans'); 371 | local.HTTPService.addParam(type='formfield',name='id',value=Trim(arguments.planid)); 372 | local.amount = amountToCents(arguments.amount); 373 | local.HTTPService.addParam(type='formfield',name='amount',value=local.amount); 374 | local.HTTPService.addParam(type='formfield',name='currency',value=Trim(arguments.currency)); 375 | local.HTTPService.addParam(type='formfield',name='interval',value=Trim(arguments.interval)); 376 | local.HTTPService.addParam(type='formfield',name='name',value=Trim(arguments.name)); 377 | if (StructKeyExists(arguments,'trial_period_days')) { 378 | local.HTTPService.addParam(type='formfield',name='trial_period_days',value=arguments.trial_period_days); 379 | } 380 | local.HTTPResult = local.HTTPService.send().getPrefix(); 381 | 382 | if (NOT isDefined("local.HTTPResult.statusCode")) { 383 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 384 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 385 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 386 | } 387 | return deserializeJSON(local.HTTPResult.filecontent); 388 | } 389 | 390 | public struct function readPlan(string planid='', numeric count, numeric offset) { 391 | 392 | local.HTTPService = createHTTPService('GET'); 393 | 394 | local.url = getBaseUrl() & 'plans'; 395 | if (Len(Trim(arguments.planid))) { 396 | local.HTTPService.setUrl(local.url & '/' & arguments.planid); 397 | } else if (StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 398 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count & '&offset=' & arguments.offset); 399 | } else if (StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 400 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count); 401 | } else if (NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 402 | local.HTTPService.setUrl(local.url & '?offset=' & arguments.offset); 403 | } else { 404 | local.HTTPService.setUrl(local.url); 405 | } 406 | local.HTTPResult = local.HTTPService.send().getPrefix(); 407 | 408 | if (NOT isDefined("local.HTTPResult.statusCode")) { 409 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 410 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 411 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 412 | } 413 | return deserializeJSON(local.HTTPResult.filecontent); 414 | } 415 | 416 | public struct function updatePlan(required string planid, required string name) { 417 | 418 | local.HTTPService = createHTTPService('POST'); 419 | 420 | local.HTTPService.setUrl(getBaseUrl() & 'plans/' & arguments.planid); 421 | local.HTTPService.addParam(type='formfield',name='name',value=Trim(arguments.name)); 422 | local.HTTPResult = local.HTTPService.send().getPrefix(); 423 | 424 | if (NOT isDefined("local.HTTPResult.statusCode")) { 425 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 426 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 427 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 428 | } 429 | return deserializeJSON(local.HTTPResult.filecontent); 430 | } 431 | 432 | public struct function deletePlan(required string planid) { 433 | 434 | local.HTTPService = createHTTPService('DELETE'); 435 | 436 | local.HTTPService.setUrl(getBaseUrl() & 'plans/' & arguments.planid); 437 | local.HTTPResult = local.HTTPService.send().getPrefix(); 438 | 439 | if (NOT isDefined("local.HTTPResult.statusCode")) { 440 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 441 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 442 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 443 | } 444 | return deserializeJSON(local.HTTPResult.filecontent); 445 | } 446 | 447 | 448 | /* SUBCRIPTIONS */ 449 | 450 | public struct function updateSubscription(required string customerid, required string planid, string coupon='', boolean prorate=true, timestamp trial_end, any card) { 451 | 452 | local.HTTPService = createHTTPService('POST'); 453 | local.HTTPService.addParam(type='formfield',name='plan',value=arguments.planid); 454 | 455 | local.HTTPService.setUrl(getBaseUrl() & 'customers/' & arguments.customerid & '/subscription'); 456 | if (Len(Trim(arguments.coupon))) { 457 | local.HTTPService.addParam(type='formfield',name='coupon',value=Trim(arguments.coupon)); 458 | } 459 | local.HTTPService.addParam(type='formfield',name='prorate',value=arguments.prorate); 460 | if (StructKeyExists(arguments,'trial_end') AND IsDate(arguments.trial_end)) { 461 | local.intUTCDate = timeToUTCInt(arguments.trial_end); 462 | local.HTTPService.addParam(type='formfield',name='trial_end',value=local.intUTCDate); 463 | } 464 | if (StructKeyExists(arguments,'card') AND isStruct(arguments.card)) { 465 | local.HTTPService.addParam(type='formfield',name='card[number]',value=arguments.card.number); 466 | local.HTTPService.addParam(type='formfield',name='card[exp_month]',value=arguments.card.exp_month); 467 | local.HTTPService.addParam(type='formfield',name='card[exp_year]',value=arguments.card.exp_year); 468 | if (StructKeyExists(arguments,'card.cvc')) { 469 | local.HTTPService.addParam(type='formfield',name='card[cvc]',value=arguments.card.cvc); 470 | } 471 | if (StructKeyExists(arguments,'card.name') AND Len(Trim(arguments.card.name))) { 472 | local.HTTPService.addParam(type='formfield',name='card[name]',value=arguments.card.name); 473 | } 474 | if (StructKeyExists(arguments,'card.address_line1') AND Len(Trim(arguments.card.address_line1))) { 475 | local.HTTPService.addParam(type='formfield',name='card[address_line1]',value=Trim(arguments.card.address_line1)); 476 | } 477 | if (StructKeyExists(arguments,'card.address_line2') AND Len(Trim(arguments.card.address_line2))) { 478 | local.HTTPService.addParam(type='formfield',name='card[address_line2]',value=Trim(arguments.card.address_line2)); 479 | } 480 | if (StructKeyExists(arguments,'card.address_zip') AND Len(Trim(arguments.card.address_zip))) { 481 | local.HTTPService.addParam(type='formfield',name='card[address_zip]',value=Trim(arguments.card.address_zip)); 482 | } 483 | if (StructKeyExists(arguments,'card.address_state') AND Len(Trim(arguments.card.address_state))) { 484 | local.HTTPService.addParam(type='formfield',name='card[address_state]',value=Trim(arguments.card.address_state)); 485 | } 486 | if (StructKeyExists(arguments,'card.address_country') AND Len(Trim(arguments.card.address_country))) { 487 | local.HTTPService.addParam(type='formfield',name='card[address_country]',value=Trim(arguments.card.address_country)); 488 | } 489 | } else if (StructKeyExists(arguments,'card')) { 490 | local.HTTPService.addParam(type='formfield',name='card',value=Trim(arguments.card)); 491 | } 492 | local.HTTPResult = local.HTTPService.send().getPrefix(); 493 | 494 | if (NOT isDefined("local.HTTPResult.statusCode")) { 495 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 496 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 497 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 498 | } 499 | return deserializeJSON(local.HTTPResult.filecontent); 500 | } 501 | 502 | public struct function deleteSubscription(required string customerid, boolean at_period_end=false) { 503 | 504 | local.HTTPService = createHTTPService('DELETE'); 505 | 506 | local.HTTPService.setUrl(getBaseUrl() & 'customers/' & arguments.customerid & '/subscription'); 507 | local.HTTPService.addParam(type='formfield',name='at_period_end',value=arguments.at_period_end); 508 | local.HTTPResult = local.HTTPService.send().getPrefix(); 509 | 510 | if (NOT isDefined("local.HTTPResult.statusCode")) { 511 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 512 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 513 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 514 | } 515 | return deserializeJSON(local.HTTPResult.filecontent); 516 | } 517 | 518 | 519 | /* INVOICE ITEMS */ 520 | 521 | public struct function createInvoiceItem(required string customerid, required numeric amount, string currency=getCurrency(), string description='') { 522 | 523 | local.HTTPService = createHTTPService('POST'); 524 | 525 | local.HTTPService.setUrl(getBaseUrl() & 'invoiceitems'); 526 | local.HTTPService.addParam(type='formfield',name='customer',value=Trim(arguments.customerid)); 527 | local.amount = amountToCents(arguments.amount); // convert amount to cents for Stripe 528 | local.HTTPService.addParam(type='formfield',name='amount',value=local.amount); 529 | local.HTTPService.addParam(type='formfield',name='currency',value=arguments.currency); 530 | if (Len(Trim(arguments.description))) { 531 | local.HTTPService.addParam(type='formfield',name='description',value=Trim(arguments.description)); 532 | } 533 | local.HTTPResult = local.HTTPService.send().getPrefix(); 534 | 535 | if (NOT isDefined("local.HTTPResult.statusCode")) { 536 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 537 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 538 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 539 | } 540 | return deserializeJSON(local.HTTPResult.filecontent); 541 | } 542 | 543 | public struct function readInvoiceItem(string invoiceitemid='', string customerid='', numeric count, numeric offset) { 544 | 545 | local.HTTPService = createHTTPService('GET'); 546 | 547 | local.url = getBaseUrl() & 'invoiceitems'; 548 | if (Len(Trim(arguments.invoiceitemid))) { 549 | local.HTTPService.setUrl(local.url & '/' & arguments.invoiceitemid); 550 | } else if (Len(Trim(arguments.customerid)) AND StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 551 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customer & 'count=' & arguments.count & '&offset=' & arguments.offset); 552 | } else if (Len(Trim(arguments.customerid)) AND StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 553 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customer & 'count=' & arguments.count); 554 | } else if (Len(Trim(arguments.customerid)) AND NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 555 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customer & '&offset=' & arguments.offset); 556 | } else if (Len(Trim(arguments.customerid))) { 557 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customer); 558 | } else if (StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 559 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count & '&offset=' & arguments.offset); 560 | } else if (StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 561 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count); 562 | } else if (NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 563 | local.HTTPService.setUrl(local.url & '?offset=' & arguments.offset); 564 | } else { 565 | local.HTTPService.setUrl(local.url); 566 | } 567 | local.HTTPResult = local.HTTPService.send().getPrefix(); 568 | 569 | if (NOT isDefined("local.HTTPResult.statusCode")) { 570 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 571 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 572 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 573 | } 574 | return deserializeJSON(local.HTTPResult.filecontent); 575 | } 576 | 577 | public struct function updateInvoiceItem(required string invoiceitemid, required numeric amount, string currency=getCurrency(), string description='') { 578 | 579 | local.HTTPService = createHTTPService('POST'); 580 | 581 | local.HTTPService.setUrl(getBaseUrl() & 'invoiceitems/' & arguments.invoiceitemid); 582 | local.amount = amountToCents(arguments.amount); 583 | local.HTTPService.addParam(type='formfield',name='amount',value=local.amount); 584 | local.HTTPService.addParam(type='formfield',name='currency',value=Trim(arguments.currency)); 585 | if (Len(Trim(arguments.description))) { 586 | local.HTTPService.addParam(type='formfield',name='description',value=Trim(arguments.description)); 587 | } 588 | local.HTTPResult = local.HTTPService.send().getPrefix(); 589 | 590 | if (NOT isDefined("local.HTTPResult.statusCode")) { 591 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 592 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 593 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 594 | } 595 | return deserializeJSON(local.HTTPResult.filecontent); 596 | } 597 | 598 | public struct function deleteInvoiceItem(required string invoiceitemid) { 599 | 600 | local.HTTPService = createHTTPService('DELETE'); 601 | 602 | local.HTTPService.setUrl(getBaseUrl() & 'invoiceitems/' & arguments.invoiceitemid); 603 | local.HTTPResult = local.HTTPService.send().getPrefix(); 604 | 605 | if (NOT isDefined("local.HTTPResult.statusCode")) { 606 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 607 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 608 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 609 | } 610 | return deserializeJSON(local.HTTPResult.filecontent); 611 | } 612 | 613 | 614 | /* INVOICES */ 615 | 616 | public struct function readInvoice(string invoiceid='', string customerid='', numeric count, numeric offset, boolean upcoming=false) { 617 | 618 | local.HTTPService = createHTTPService('GET'); 619 | 620 | local.url = getBaseUrl() & 'invoices'; 621 | if (Len(Trim(arguments.invoiceid))) { 622 | local.HTTPService.setUrl(local.url & '/' & arguments.invoiceid); 623 | } else if (Len(Trim(arguments.customerid)) AND StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 624 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customerid & 'count=' & arguments.count & '&offset=' & arguments.offset); 625 | } else if (Len(Trim(arguments.customerid)) AND StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 626 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customerid & 'count=' & arguments.count); 627 | } else if (Len(Trim(arguments.customerid)) AND NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 628 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customerid & '&offset=' & arguments.offset); 629 | } else if (Len(Trim(arguments.customerid)) AND arguments.upcoming) { 630 | local.HTTPService.setUrl(local.url & '/upcoming/?customer=' & arguments.customerid); 631 | } else if (Len(Trim(arguments.customerid)) AND NOT arguments.upcoming) { 632 | local.HTTPService.setUrl(local.url & '?customer=' & arguments.customerid); 633 | } else if (StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 634 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count & '&offset=' & arguments.offset); 635 | } else if (StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 636 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count); 637 | } else if (NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 638 | local.HTTPService.setUrl(local.url & '?offset=' & arguments.offset); 639 | } else { 640 | local.HTTPService.setUrl(local.url); 641 | } 642 | local.HTTPResult = local.HTTPService.send().getPrefix(); 643 | 644 | if (NOT isDefined("local.HTTPResult.statusCode")) { 645 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 646 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 647 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 648 | } 649 | return deserializeJSON(local.HTTPResult.filecontent); 650 | } 651 | 652 | 653 | /* COUPONS */ 654 | 655 | public struct function createCoupon(required numeric percent_off, required string duration, string couponid='', numeric duration_in_months, numeric max_redemptions, timestamp redeem_by) { 656 | 657 | local.HTTPService = createHTTPService('POST'); 658 | 659 | local.HTTPService.setUrl(getBaseUrl() & 'coupons'); 660 | local.HTTPService.addParam(type='formfield',name='percent_off',value=arguments.percent_off); 661 | local.HTTPService.addParam(type='formfield',name='duration',value=arguments.duration); 662 | if (Len(Trim(arguments.couponid))) { 663 | local.HTTPService.addParam(type='formfield',name='id',value=arguments.couponid); 664 | } 665 | if (StructKeyExists(arguments,'duration_in_months')) { 666 | local.HTTPService.addParam(type='formfield',name='duration_in_months',value=arguments.duration_in_months); 667 | } 668 | if (StructKeyExists(arguments,'max_redemptions')) { 669 | local.HTTPService.addParam(type='formfield',name='max_redemptions',value=arguments.max_redemptions); 670 | } 671 | if (StructKeyExists(arguments,'redeem_by')) { 672 | local.redeem_by = timeToUTCInt(arguments.redeem_by); 673 | local.HTTPService.addParam(type='formfield',name='redeem_by',value=local.redeem_by); 674 | } 675 | local.HTTPResult = local.HTTPService.send().getPrefix(); 676 | 677 | if (NOT isDefined("local.HTTPResult.statusCode")) { 678 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 679 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 680 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 681 | } 682 | return deserializeJSON(local.HTTPResult.filecontent); 683 | } 684 | 685 | public struct function readCoupon(string couponid='', numeric count, numeric offset) { 686 | 687 | local.HTTPService = createHTTPService('GET'); 688 | 689 | local.url = getBaseUrl() & 'coupons'; 690 | if (Len(Trim(arguments.couponid))) { 691 | local.HTTPService.setUrl(local.url & '/' & arguments.couponid); 692 | } else if (StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 693 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count & '&offset=' & arguments.offset); 694 | } else if (StructKeyExists(arguments,'count') AND NOT StructKeyExists(arguments,'offset')) { 695 | local.HTTPService.setUrl(local.url & '?count=' & arguments.count); 696 | } else if (NOT StructKeyExists(arguments,'count') AND StructKeyExists(arguments,'offset')) { 697 | local.HTTPService.setUrl(local.url & '?offset=' & arguments.offset); 698 | } else { 699 | local.HTTPService.setUrl(local.url); 700 | } 701 | local.HTTPResult = local.HTTPService.send().getPrefix(); 702 | 703 | if (NOT isDefined("local.HTTPResult.statusCode")) { 704 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 705 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 706 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 707 | } 708 | return deserializeJSON(local.HTTPResult.filecontent); 709 | } 710 | 711 | public struct function deleteCoupon(required string couponid) { 712 | 713 | local.HTTPService = createHTTPService('DELETE'); 714 | local.HTTPService.setUrl(getBaseUrl() & 'coupons/' & arguments.couponid); 715 | local.HTTPResult = local.HTTPService.send().getPrefix(); 716 | 717 | if (NOT isDefined("local.HTTPResult.statusCode")) { 718 | throw(type='Stripe',errorcode="stripe_unresponsive", message="The Stripe server did not respond.", detail="The Stripe server did not respond."); 719 | } else if (left(local.HTTPResult.statusCode,3) NEQ "200") { 720 | throw(type='Stripe',errorcode=local.HTTPResult.statusCode, message=local.HTTPResult.statuscode, detail=local.HTTPResult.filecontent); 721 | } 722 | return deserializeJSON(local.HTTPResult.filecontent); 723 | } 724 | 725 | 726 | /* HELPER FUNCTIONS */ 727 | 728 | private HTTP function createHTTPService(string urlmethod='POST', numeric httptimeout=1000) { 729 | local.HTTPService = new HTTP(); 730 | local.HTTPService.setUsername(getStripeApiKey()); 731 | local.HTTPService.setPassword(''); 732 | local.HTTPService.setMethod(arguments.urlmethod); 733 | local.HTTPService.setCharset('utf-8'); 734 | local.HTTPService.setTimeout(arguments.httptimeout); 735 | 736 | return local.HTTPService; 737 | } 738 | 739 | private numeric function amountToCents(required numeric amount) { 740 | local.amount = arguments.amount * 100; // convert amount to cents for Stripe 741 | return local.amount; 742 | } 743 | 744 | public numeric function centsToAmount(required numeric cents) { 745 | local.amount = arguments.cents / 100; // convert cents to dollars 746 | return local.amount; 747 | } 748 | 749 | private numeric function timeToUTCInt(required timestamp date) { 750 | local.currUTCDate = DateConvert('local2utc',arguments.date); 751 | local.baseDate = CreateDateTime(1970,1,1,0,0,0); 752 | local.intUTCDate = DateDiff('s',local.baseDate,local.currUTCDate); 753 | return local.intUTCDate; 754 | } 755 | 756 | public datetime function utcIntToTime(required numeric intUtcTime) { 757 | local.baseDate = CreateDateTime(1970,1,1,0,0,0); 758 | return DateAdd('s',arguments.intUtcTime,DateConvert('utc2Local',local.baseDate));; 759 | } 760 | 761 | } 762 | --------------------------------------------------------------------------------