Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.80 KB | None | 0 0
  1. /*
  2. Copyright (c) 2013-2015, John Berquist
  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. component {
  17.  
  18. variables.integerFields = [ "quantity","limit","interval_count","trial_period_days","duration_in_months","max_redemptions", "percent_off","exp_month","exp_year","cvc" ];
  19. variables.numericFields = [ "application_fee_percent","tax_percent" ];
  20. variables.currencyFields = [ "amount","amount_refunded","amount_off","account_balance","fee","application_fee","net" ];
  21. variables.timestampFields = [ "created","trial_start","trial_end","redeem_by","start","end","ended_at","period_start","period_end","date","next_payment_attempt","available_on","current_period_start","current_period_end","canceled_at","gt","gte","lt","lte" ];
  22. variables.booleanFields = [ "active","at_period_end","capture","closed","debit_negative_balances","discountable","filled","forgiven","prorate","refund_application_fee","refund_mispayments","managed","uncaptured_funds" ];
  23. variables.arrayFields = [ "expand","include","additional_owners" ];
  24. variables.dictionaryFields = {
  25. source = { required = [ "object","number","exp_month","exp_year" ], optional = [ "cvc","name","address_line1","address_line2","address_city","address_zip","address_state","address_country" ] },
  26. card = { required = [ "number","exp_month","exp_year" ], optional = [ "cvc","name","address_line1","address_line2","address_city","address_zip","address_state","address_country" ] },
  27. evidence = { required = [ ], optional = [ "access_activity_log","billing_address","cancellation_policy","cancellation_policy_disclosure","cancellation_rebuttal","customer_communication","customer_email_address","customer_name","customer_purchase_ip","customer_signature","duplicate_charge_documentation","duplicate_charge_explanation","duplicate_charge_id","product_description","receipt","refund_policy","refund_policy_disclosure","refund_refusal_explanation","service_date","service_documentation","shipping_address","shipping_carrier","shipping_date","shipping_documentation","shipping_tracking_number","uncategorized_file","uncategorized_text" ] },
  28. bank_account = { required = [ "country","currentcy","account_number" ], optional = [ "routing_number" ] },
  29. legal_entity = { required = [ "" ], optional = [ "type","address","business_name","business_tax_id","business_vat_id","dob","first_name","last_name","personal_address","personal_id_number","ssn_last_4","verification","additional_owners" ] },
  30. dob = { required = [ "day","month","year" ], optional = [ ] },
  31. personal_address = { required = [ ], optional = [ "line1","line2","city","state","postal_code","country" ] },
  32. verification = { required = [ "" ], optional = [ "document" ] },
  33. tos_acceptance = { required = [ "date","ip" ], optional = [ "user_agent" ] },
  34. transfer_schedule = { required = [ "" ], optional = [ "delay_days","interval","monthly_anchor","weekly_anchor" ] },
  35. available_on = { required = [ ], optional = [ "gt","gte","lt","lte" ] },
  36. created = { required = [ ], optional = [ "gt","gte","lt","lte" ] },
  37. date = { required = [ ], optional = [ "gt","gte","lt","lte" ] },
  38. shipping = { required = [ ], optional = [ "address","carrier","phone","name","tracking_number" ] },
  39. address = { required = [ ], optional = [ "line1","line2","city","state","postal_code","country" ] },
  40. metadata = { required = [ ], optional = [ ] }
  41. };
  42.  
  43. public any function init( required string apiKey, boolean convertUTCTimestamps = true, boolean convertToCents = false, string defaultCurrency = "usd", boolean includeRaw = false, string apiBaseUrl = "https://api.stripe.com/v3/", string fileUploadBaseUrl = "https://uploads.stripe.com/v3/" ) {
  44. structAppend( variables, arguments );
  45.  
  46. variables.utcBaseDate = dateAdd( "l", createDate( 1970,1,1 ).getTime() * -1, createDate( 1970,1,1 ) );
  47.  
  48. return this;
  49. }
  50.  
  51. // Charges
  52.  
  53. public struct function createCharge( required numeric amount, string currency = variables.defaultCurrency, numeric application_fee, boolean capture, string customer, string description, string destination, struct metadata, string receipt_email, struct shipping, any source, string statement_descriptor ) {
  54. return apiCall( "charges", setupParams( arguments ), "post" );
  55. }
  56.  
  57. public struct function getCharge( required string id ) {
  58. return apiCall( "charges/#trim( arguments.id )#", setupParams( arguments ) );
  59. }
  60.  
  61. public struct function updateCharge( required string id, string description, struct metadata ) {
  62. return apiCall( "charges/#trim( arguments.id )#", setupParams( arguments ), "post" );
  63. }
  64.  
  65. public struct function captureCharge( required string id, numeric amount, numeric application_fee ) {
  66. return apiCall( "charges/#trim( arguments.id )#/capture", setupParams( arguments ), "post" );
  67. }
  68.  
  69. public struct function listCharges( any created, string customer, string ending_before, numeric limit, string starting_after ) {
  70. return apiCall( "charges", setupParams( arguments ) );
  71. }
  72.  
  73. // Refunds
  74.  
  75. public struct function createChargeRefund( required string id, numeric amount, struct metadata, string reason, boolean refund_application_fee ) {
  76. return apiCall( "charges/#trim( arguments.id )#/refunds", setupParams( arguments ), "post" );
  77. }
  78.  
  79. public struct function getChargeRefund( required string charge_id, required string id ) {
  80. return apiCall( "charges/#trim( arguments.charge_id )#/refunds/#trim( arguments.id )#", setupParams( arguments ) );
  81. }
  82.  
  83. public struct function updateChargeRefund( required string charge_id, required string id, struct metadata ) {
  84. return apiCall( "charges/#trim( arguments.charge_id )#/refunds/#trim( arguments.id )#", setupParams( arguments ), "post" );
  85. }
  86.  
  87. public struct function listChargeRefunds( required string charge_id, string ending_before, numeric limit, string starting_after ) {
  88. return apiCall( "charges/#trim( arguments.charge_id )#/refunds", setupParams( arguments ) );
  89. }
  90.  
  91. // Customers
  92.  
  93. public struct function createCustomer( numeric account_balance, string coupon, string description, string email, struct metadata, string plan, numeric quantity, any source, any trial_end ) {
  94. return apiCall( "customers", setupParams( arguments ), "post" );
  95. }
  96.  
  97. public struct function getCustomer( required string id ) {
  98. return apiCall( "customers/#trim( arguments.id )#", setupParams( arguments ) );
  99. }
  100.  
  101. public struct function updateCustomer( required string id, numeric account_balance, string coupon, string default_source, string description, string email, struct metadata, any source ) {
  102. return apiCall( "customers/#trim( arguments.id )#", setupParams( arguments ), "post" );
  103. }
  104.  
  105. public struct function deleteCustomer( required string id ) {
  106. return apiCall( "customers/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  107. }
  108.  
  109. public struct function listCustomers( any created, string ending_before, numeric limit, string starting_after ) {
  110. return apiCall( "customers", setupParams( arguments ) );
  111. }
  112.  
  113. // Subscriptions
  114.  
  115. public struct function createCustomerSubscription( required string customer_id, required string plan, numeric application_fee_percent, string coupon, struct metadata, boolean prorate, numeric quantity, any source, numeric tax_percent, any trial_end ) {
  116. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions", setupParams( arguments ), "post" );
  117. }
  118.  
  119. public struct function getCustomerSubscription( required string customer_id, required string id ) {
  120. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions/#trim( arguments.id )#", setupParams( arguments ) );
  121. }
  122.  
  123. public struct function updateCustomerSubscription( required string customer_id, required string id, numeric application_fee_percent, string coupon, struct metadata, string plan, boolean prorate, numeric quantity, any source, numeric tax_percent, any trial_end ) {
  124. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions/#trim( arguments.id )#", setupParams( arguments ), "post" );
  125. }
  126.  
  127. public struct function cancelCustomerSubscription( required string customer_id, required string id, boolean at_period_end ) {
  128. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  129. }
  130.  
  131. public struct function listCustomerSubscriptions( required string customer_id, string ending_before, numeric limit, string starting_after ) {
  132. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions", setupParams( arguments ) );
  133. }
  134.  
  135. // Cards / Sources
  136.  
  137. public struct function createCustomerSource( required string customer_id, any source ) {
  138. return apiCall( "customers/#trim( arguments.customer_id )#/sources", setupParams( arguments ), "post" );
  139. }
  140.  
  141. public struct function getCustomerSource( required string customer_id, required string id ) {
  142. return apiCall( "customers/#trim( arguments.customer_id )#/sources/#trim( arguments.id )#", setupParams( arguments ) );
  143. }
  144.  
  145. public struct function updateCustomerSource( required string customer_id, required string id, string address_city, string address_country, string address_line1, string address_line2, string address_state, string address_zip, numeric exp_month, numeric exp_year, struct metadata, string name ) {
  146. return apiCall( "customers/#trim( arguments.customer_id )#/sources/#trim( arguments.id )#", setupParams( arguments ), "post" );
  147. }
  148.  
  149. public struct function deleteCustomerSource( required string customer_id, required string id ) {
  150. return apiCall( "customers/#trim( arguments.customer_id )#/sources/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  151. }
  152.  
  153. public struct function listCustomerSources( required string customer_id, string ending_before, numeric limit, string starting_after ) {
  154. return apiCall( "customers/#trim( arguments.customer_id )#/sources", setupParams( arguments ) );
  155. }
  156.  
  157. // Plans
  158.  
  159. public struct function createPlan( required string id, required numeric amount, string currency = variables.defaultCurrency, required string interval, required string name, numeric interval_count, struct metadata, string statement_descriptor, numeric trial_period_days ) {
  160. return apiCall( "plans", setupParams( arguments, true ), "post" );
  161. }
  162.  
  163. public struct function getPlan( required string id ) {
  164. return apiCall( "plans/#trim( arguments.id )#", setupParams( arguments ) );
  165. }
  166.  
  167. public struct function updatePlan( required string id, struct metadata, string name, string statement_descriptor ) {
  168. return apiCall( "plans/#trim( arguments.id )#", setupParams( arguments ), "post" );
  169. }
  170.  
  171. public struct function deletePlan( required string id ) {
  172. return apiCall( "plans/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  173. }
  174.  
  175. public struct function listPlans( any created, string ending_before, numeric limit, string starting_after ) {
  176. return apiCall( "plans", setupParams( arguments ) );
  177. }
  178.  
  179. // Coupons
  180.  
  181. public struct function createCoupon( required string duration, numeric amount_off, string currency = variables.defaultCurrency, numeric duration_in_months, string id, numeric max_redemptions, struct metadata, numeric percent_off, any redeem_by ) {
  182. // only one of percent_off and amount_off can be provided to the createCoupon function
  183. if ( !( structKeyExists( arguments, "amount_off" ) xor structKeyExists( arguments, "percent_off" ) ) ) {
  184. throwError( "createCoupon: please provide one and only one of amount_off and percent_off params" );
  185. }
  186. // duration_in_months is required only when duration equals "repeating"
  187. if ( arguments.duration == "repeating" xor structKeyExists( arguments, "duration_in_months" ) ) {
  188. throwError( "createCoupon: duration_in_months is required when and only when duration equals 'repeating'" );
  189. }
  190. return apiCall( "coupons", setupParams( arguments, true ), "post" );
  191. }
  192.  
  193. public struct function getCoupon( required string id ) {
  194. return apiCall( "coupons/#trim( arguments.id )#", setupParams( arguments ) );
  195. }
  196.  
  197. public struct function updateCoupon( required string id, struct metadata ) {
  198. return apiCall( "coupons/#trim( arguments.id )#", setupParams( arguments ), "post" );
  199. }
  200.  
  201. public struct function deleteCoupon( required string id ) {
  202. return apiCall( "coupons/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  203. }
  204.  
  205. public struct function listCoupons( any created, string ending_before, numeric limit, string starting_after ) {
  206. return apiCall( "coupons", setupParams( arguments ) );
  207. }
  208.  
  209. // Discounts
  210.  
  211. public struct function deleteCustomerDiscount( required string id ) {
  212. return apiCall( "customers/#trim( arguments.id )#/discount", setupParams( arguments ), "delete" );
  213. }
  214.  
  215. public struct function deleteCustomerSubscriptionDiscount( required string customer_id, required string id ) {
  216. return apiCall( "customers/#trim( arguments.customer_id )#/subscriptions/#trim( arguments.id )#/discount", setupParams( arguments ), "delete" );
  217. }
  218.  
  219. // Invoices
  220.  
  221. public struct function createInvoice( required string customer, numeric application_fee, string description, struct metadata, string statement_descriptor, string subscription, numeric tax_percent ) {
  222. return apiCall( "invoices", setupParams( arguments ), "post" );
  223. }
  224.  
  225. public struct function getInvoice( required string id ) {
  226. return apiCall( "invoices/#trim( arguments.id )#", setupParams( arguments ) );
  227. }
  228.  
  229. public struct function getInvoiceLineItems( required string id, string customer, string ending_before, numeric limit, string starting_after, string subscription ) {
  230. return apiCall( "invoices/#trim( arguments.id )#/lines", setupParams( arguments ) );
  231. }
  232.  
  233. public struct function getUpcomingInvoice( required string customer, string subscription ) {
  234. return apiCall( "invoices/upcoming", setupParams( arguments ) );
  235. }
  236.  
  237. public struct function updateInvoice( required string id, boolean closed, string description, boolean forgiven, struct metadata, string statement_descriptor, numeric tax_percent ) {
  238. return apiCall( "invoices/#trim( arguments.id )#", setupParams( arguments ), "post" );
  239. }
  240.  
  241. public struct function payInvoice( required string id ) {
  242. return apiCall( "invoices/#trim( arguments.id )#/pay", setupParams( arguments ), "post" );
  243. }
  244.  
  245. public struct function listInvoices( string customer, any date, string ending_before, numeric limit, string starting_after ) {
  246. return apiCall( "invoices", setupParams( arguments ) );
  247. }
  248.  
  249. // Invoice Items
  250.  
  251. public struct function createInvoiceItem( required string customer, required numeric amount, string currency = variables.defaultCurrency, string description, boolean discountable, string invoice, struct metadata, string subscription ) {
  252. return apiCall( "invoiceitems", setupParams( arguments ), "post" );
  253. }
  254.  
  255. public struct function getInvoiceItem( required string id ) {
  256. return apiCall( "invoiceitems/#trim( arguments.id )#", setupParams( arguments ) );
  257. }
  258.  
  259. public struct function updateInvoiceItem( required string id, numeric amount, string description, boolean discountable, struct metadata ) {
  260. return apiCall( "invoiceitems/#trim( arguments.id )#", setupParams( arguments ), "post" );
  261. }
  262.  
  263. public struct function deleteInvoiceItem( required string id ) {
  264. return apiCall( "invoiceitems/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  265. }
  266.  
  267. public struct function listInvoiceItems( any created, string customer, string ending_before, numeric limit, string starting_after ) {
  268. return apiCall( "invoiceitems", setupParams( arguments ) );
  269. }
  270.  
  271. // Disputes
  272.  
  273. public struct function updateChargeDispute( required string id, struct evidence, struct metadata ) {
  274. return apiCall( "charges/#trim( arguments.id )#/dispute", setupParams( arguments ), "post" );
  275. }
  276.  
  277. public struct function closeChargeDispute( required string id ) {
  278. return apiCall( "charges/#trim( arguments.id )#/dispute/close", setupParams( arguments ), "post" );
  279. }
  280.  
  281. // Transfers
  282.  
  283. public struct function createTransfer( required numeric amount, string currency = variables.defaultCurrency, required string destination, string description, struct metadata, string source_transaction, string statement_descriptor ) {
  284. return apiCall( "transfers", setupParams( arguments ), "post" );
  285. }
  286.  
  287. public struct function getTransfer( required string id ) {
  288. return apiCall( "transfers/#trim( arguments.id )#", setupParams( arguments ) );
  289. }
  290.  
  291. public struct function updateTransfer( required string id, string description, struct metadata ) {
  292. return apiCall( "transfers/#trim( arguments.id )#", setupParams( arguments ), "post" );
  293. }
  294.  
  295. public struct function listTransfers( any created, any date, string ending_before, numeric limit, string recipient, string starting_after, string status ) {
  296. return apiCall( "transfers", setupParams( arguments ) );
  297. }
  298.  
  299. // Transfer Reversals
  300.  
  301. public struct function createTransferReversal( required string transfer_id, numeric amount, string description, struct metadata, boolean refund_application_fee ) {
  302. return apiCall( "transfers/#trim( arguments.transfer_id )#/reversals", setupParams( arguments ), "post" );
  303. }
  304.  
  305. public struct function getTransferReversal( required string transfer_id, required string id ) {
  306. return apiCall( "transfers/#trim( arguments.transfer_id )#/reversals/#trim( arguments.id )#", setupParams( arguments ) );
  307. }
  308.  
  309. public struct function updateTransferReversal( required string transfer_id, required string id, string description, struct metadata ) {
  310. return apiCall( "transfers/#trim( arguments.transfer_id )#/reversals/#trim( arguments.id )#", setupParams( arguments ), "post" );
  311. }
  312.  
  313. public struct function listTransferReversals( required string transfer_id, string ending_before, numeric limit, string starting_after ) {
  314. return apiCall( "transfers/#trim( arguments.transfer_id )#/reversals", setupParams( arguments ) );
  315. }
  316.  
  317. // Recipients - Deprecated
  318.  
  319. /*
  320. public struct function createRecipient( required string name, required string type, any bank_account, string email, string description, struct metadata, string tax_id ) {
  321. return apiCall( "recipients", setupParams( arguments ), "post" );
  322. }
  323.  
  324. public struct function getRecipient( required string id ) {
  325. return apiCall( "recipients/#trim( arguments.id )#", setupParams( arguments ) );
  326. }
  327.  
  328. public struct function updateRecipient( required string id, any bank_account, string email, string description, struct metadata, string name, string tax_id ) {
  329. return apiCall( "recipients/#trim( arguments.id )#", setupParams( arguments ), "post" );
  330. }
  331.  
  332. public struct function deleteRecipient( required string id ) {
  333. return apiCall( "recipients/#trim( arguments.id )#", setupParams( arguments ), "delete" );
  334. }
  335.  
  336. public struct function listRecipients( string ending_before, numeric limit, string starting_after, boolean verified ) {
  337. return apiCall( "recipients", setupParams( arguments ) );
  338. }
  339. */
  340.  
  341. // Application Fees
  342.  
  343. public struct function getApplicationFee( required string id ) {
  344. return apiCall( "application_fees/#trim( arguments.id )#", setupParams( arguments ) );
  345. }
  346.  
  347. public struct function listApplicationFees( string charge, any created, string ending_before, numeric limit, string starting_after ) {
  348. return apiCall( "application_fees", setupParams( arguments ) );
  349. }
  350.  
  351. // Application Fee Refunds
  352.  
  353. public struct function createApplicationFeeRefund( required string application_fee_id, numeric amount, struct metadata = { } ) {
  354. return apiCall( "application_fees/#trim( arguments.application_fee_id )#/refunds", setupParams( arguments ) );
  355. }
  356.  
  357. public struct function getApplicationFeeRefund( required string application_fee_id, required string id ) {
  358. return apiCall( "application_fees/#trim( arguments.transfer_id )#/refunds/#trim( arguments.id )#", setupParams( arguments ) );
  359. }
  360.  
  361. public struct function updateApplicationFeeRefund( required string application_fee_id, required string id, struct metadata ) {
  362. return apiCall( "application_fees/#trim( arguments.application_fee_id )#/refunds/#trim( arguments.id )#", setupParams( arguments ), "post" );
  363. }
  364.  
  365. public struct function listApplicationFeeRefunds( required string application_fee_id, string ending_before, numeric limit, string starting_after ) {
  366. return apiCall( "application_fees/#trim( arguments.application_fee_id )#/refunds", setupParams( arguments ) );
  367. }
  368.  
  369. // Accounts
  370.  
  371. public struct function createAccount( any bank_account, string business_name, string business_url, string country, boolean debit_negative_balances, string default_currency, string email, struct legal_entity, boolean managed, struct metadata, string product_description, string statement_descriptor, string support_phone, struct tos_acceptance, struct transfer_schedule ) {
  372. return apiCall( "accounts", setupParams( arguments ), "post" );
  373. }
  374.  
  375. public struct function getAccount( string id = '' ) {
  376. return apiCall( "accounts/#trim( arguments.id )#" );
  377. }
  378.  
  379. public struct function updateAccount( required string id, any bank_account, string business_name, string business_url, boolean debit_negative_balances, string default_currency, string email, struct legal_entity, struct metadata, string product_description, string statement_descriptor, string support_phone, struct tos_acceptance, struct transfer_schedule ) {
  380. return apiCall( "accounts/#trim( arguments.id )#", setupParams( arguments ), "post" );
  381. }
  382.  
  383. public struct function listAccounts( string ending_before, numeric limit, string starting_after ) {
  384. return apiCall( "accounts", setupParams( arguments ) );
  385. }
  386.  
  387. // Balance
  388.  
  389. public struct function getBalance() {
  390. return apiCall( "balance", setupParams( arguments ) );
  391. }
  392.  
  393. public struct function getBalanceTransaction( required string id ) {
  394. return apiCall( "balance/history/#trim( arguments.id )#", setupParams( arguments ) );
  395. }
  396.  
  397. public struct function listBalanceHistory( any available_on, any created, string currency, string ending_before, numeric limit, string source, string starting_after, string transfer, string type ) {
  398. return apiCall( "balance/history", setupParams( arguments ) );
  399. }
  400.  
  401. // Events
  402.  
  403. public struct function getEvent( required string id ) {
  404. return apiCall( "events/#trim( arguments.id )#", setupParams( arguments ) );
  405. }
  406.  
  407. public struct function listEvents( any created, string ending_before, numeric limit, string starting_after, string type ) {
  408. return apiCall( "events", setupParams( arguments ) );
  409. }
  410.  
  411. // Tokens
  412.  
  413. public struct function createCardToken( any card, string customer ) {
  414. return apiCall( "tokens", setupParams( arguments ), "post" );
  415. }
  416.  
  417. public struct function createBankAccountToken( struct bank_account ) {
  418. return apiCall( "tokens", setupParams( arguments ), "post" );
  419. }
  420.  
  421. public struct function getToken( required string id ) {
  422. return apiCall( "tokens/#trim( arguments.id )#", setupParams( arguments ) );
  423. }
  424.  
  425. // Bitcoin
  426.  
  427. public struct function createBitcoinReceiver( required numeric amount, string currency = variables.defaultCurrency, string email, string description, struct metadata, boolean refund_mispayments ) {
  428. return apiCall( "bitcoin/receivers", setupParams( arguments ), "post" );
  429. }
  430.  
  431. public struct function getBitcoinReceiver( required string id ) {
  432. return apiCall( "bitcoin/receivers/#trim( arguments.id )#", setupParams( arguments ) );
  433. }
  434.  
  435. public struct function listBitcoinReceivers( boolean active, string ending_before, boolean filled, numeric limit, string starting_after, boolean uncaptured_funds ) {
  436. return apiCall( "bitcoin/receivers", setupParams( arguments ) );
  437. }
  438.  
  439. // File Uploads
  440.  
  441. public struct function createFileUpload( required string file, required string purpose ) {
  442. return apiCall( "files", setupParams( arguments ), "post", true );
  443. }
  444.  
  445. public struct function getFileUpload( required string id ) {
  446. return apiCall( "files/#trim( arguments.id )#", setupParams( arguments ), "get", true );
  447. }
  448.  
  449. public struct function listFileUploads( any created, string ending_before, numeric limit, string purpose, string starting_after ) {
  450. return apiCall( "files", setupParams( arguments ), "get", true );
  451. }
  452.  
  453. // PRIVATE FUNCTIONS
  454.  
  455. private struct function apiCall( required string path, array params = [ ], string method = "get", boolean fileUpload = false ) {
  456. var fullApiPath = ( fileUpload ? variables.fileUploadBaseUrl : variables.apiBaseUrl ) & arguments.path;
  457. var requestStart = getTickCount();
  458. var apiResponse = makeHttpRequest( urlPath = fullApiPath, params = arguments.params, method = arguments.method );
  459. var result = { "api_request_time" = getTickCount() - requestStart, "status_code" = listFirst( apiResponse.statuscode, " " ), "status_text" = listRest( apiResponse.statuscode, " " ) };
  460. if ( variables.includeRaw ) {
  461. result[ "raw" ] = { "method" = ucase( arguments.method ), "path" = fullApiPath, "params" = serializeJSON( arguments.params ), "response" = apiResponse.fileContent };
  462. }
  463. structAppend( result, deserializeJSON( apiResponse.fileContent ), true );
  464. if ( variables.convertUTCTimestamps || variables.convertToCents ) parseResult( result );
  465. return result;
  466. }
  467.  
  468. private any function makeHttpRequest( required string urlPath, required array params, required string method ) {
  469. var http = new http( url = arguments.urlPath, method = arguments.method, username = variables.apiKey, password = "" );
  470.  
  471. // adding a user agent header so that Adobe ColdFusion doesn't get mad about empty HTTP posts
  472. http.addParam( type = "header", name = "User-Agent", value = "stripe.cfc" );
  473.  
  474. var qs = [ ];
  475.  
  476. // look for special params 'idempotencyKey','stripeAccount','apiKey'
  477. for ( var param in arguments.params ) {
  478.  
  479. if ( param.name == "idempotencyKey" ) {
  480. http.addParam( type = "header", name = "Idempotency-Key", value = param.value );
  481. } else if ( param.name == "apiKey" ) {
  482. http.setUsername( param.value );
  483. } else if ( param.name == "stripeAccount" ) {
  484. http.addParam( type = "header", name = "Stripe-Account", value = param.value );
  485. } else if ( arguments.method == "post" ) {
  486. if ( param.name == "file" ) {
  487. http.addParam( type = "file", name = lcase( param.name ), file = param.value );
  488. } else {
  489. http.addParam( type = "formfield", name = lcase( param.name ), value = param.value );
  490. }
  491. } else if ( arrayFind( [ "get","delete" ], arguments.method ) ) {
  492. arrayAppend( qs, lcase( param.name ) & "=" & encodeurl( param.value ) );
  493. }
  494.  
  495. }
  496.  
  497. if ( arrayLen( qs ) ) {
  498. http.setUrl( arguments.urlPath & "?" & arrayToList( qs, "&" ) );
  499. }
  500.  
  501. return http.send().getPrefix();
  502. }
  503.  
  504. private array function setupParams( required struct params, boolean includeIds = false ) {
  505. var filteredParams = { };
  506. var paramKeys = structKeyArray( arguments.params );
  507. for ( var paramKey in paramKeys ) {
  508. if ( structKeyExists( arguments.params, paramKey ) && !isNull( arguments.params[ paramKey ] ) && ( ( paramKey != "id" && right( paramKey, 3 ) != "_id" ) || arguments.includeIds ) ) {
  509. filteredParams[ paramKey ] = params[ paramKey ];
  510. }
  511. }
  512. return parseDictionary( filteredParams );
  513. }
  514.  
  515. private array function parseDictionary( required struct dictionary, string name = '', string root = '' ) {
  516. var result = [ ];
  517. var structFieldExists = structKeyExists( variables.dictionaryFields, arguments.name );
  518.  
  519. // validate required dictionary keys based on variables.dictionaries
  520. if ( structFieldExists ) {
  521. for ( var field in variables.dictionaryFields[ arguments.name ].required ) {
  522. if ( !structKeyExists( arguments.dictionary, field ) ) {
  523. throwError( "'#arguments.name#' dictionary missing required field: #field#" );
  524. }
  525. }
  526. }
  527.  
  528. // special metadata handling -- metadata has 20 key limit and is cleared by passing empty metadata struct
  529. if ( arguments.name == "metadata" ) {
  530. if ( arrayLen( structKeyArray( arguments.dictionary ) ) > 20 ) {
  531. throwError( "There can be a maximum of 20 keys in a metadata struct." );
  532. }
  533. if ( structIsEmpty( arguments.dictionary ) ) {
  534. arrayAppend( result, { name = arguments.root, value = "" } );
  535. }
  536. }
  537.  
  538. for ( var key in arguments.dictionary ) {
  539.  
  540. // confirm that key is a valid one based on variables.dictionaries
  541. if ( structFieldExists && arguments.name != "metadata" && !( arrayFindNoCase( variables.dictionaryFields[ arguments.name ].required, key ) || arrayFindNoCase( variables.dictionaryFields[ arguments.name ].optional, key ) ) ) {
  542. throwError( "'#arguments.name#' dictionary has invalid field: #key#" );
  543. }
  544.  
  545. var fullKey = len( arguments.root ) ? arguments.root & '[' & lcase( key ) & ']' : lcase( key );
  546. if ( isStruct( arguments.dictionary[ key ] ) ) {
  547. for ( var item in parseDictionary( arguments.dictionary[ key ], key, fullKey ) ) {
  548. arrayAppend( result, item );
  549. }
  550. } else if ( isArray( arguments.dictionary[ key ] ) ) {
  551. for ( var item in parseArray( arguments.dictionary[ key ], key, fullKey ) ) {
  552. arrayAppend( result, item );
  553. }
  554. } else {
  555. // note: metadata struct is special - no validation is done on it
  556. arrayAppend( result, { name = fullKey, value = getValidatedParam( key, arguments.dictionary[ key ], arguments.name != "metadata" ) } );
  557. }
  558.  
  559. }
  560.  
  561. return result;
  562. }
  563.  
  564. private array function parseArray( required array list, string name = '', string root = '' ) {
  565. var result = [ ];
  566. var index = 0;
  567. var arrayFieldExists = arrayFindNoCase( variables.arrayFields, arguments.name );
  568.  
  569. if ( !arrayFieldExists ) {
  570. throwError( "'#arguments.name#' is not an allowed list variable." );
  571. }
  572.  
  573. for ( var item in arguments.list ) {
  574. if ( isStruct( item ) ) {
  575. var fullKey = len( arguments.root ) ? arguments.root & "[" & index & "]" : arguments.name & "[" & index & "]";
  576. for ( var item in parseDictionary( item, '', fullKey ) ) {
  577. arrayAppend( result, item );
  578. }
  579. ++index;
  580. } else {
  581. var fullKey = len( arguments.root ) ? arguments.root & "[]" : arguments.name & "[]";
  582. arrayAppend( result, { name = fullKey, value = getValidatedParam( arguments.name, item ) } );
  583. }
  584. }
  585.  
  586. return result;
  587. }
  588.  
  589. private any function getValidatedParam( required string paramName, required any paramValue, boolean validate = true ) {
  590. // only simple values
  591. if ( !isSimpleValue( paramValue ) ) throwError( "'#paramName#' is not a simple value." );
  592.  
  593. // if not validation just result trimmed value
  594. if ( !arguments.validate ) {
  595. return trim( paramValue );
  596. }
  597.  
  598. // integer
  599. if ( arrayFindNoCase( variables.integerFields, paramName ) ) {
  600. if ( !isInteger( paramValue ) ) {
  601. throwError( "field '#paramName#' requires an integer value" );
  602. }
  603. return paramValue;
  604. }
  605. // numeric
  606. if ( arrayFindNoCase( variables.numericFields, paramName ) ) {
  607. if ( !isNumeric( paramValue ) ) {
  608. throwError( "field '#paramName#' requires a numeric value" );
  609. }
  610. return paramValue;
  611. }
  612.  
  613. // currency
  614. if ( arrayFindNoCase( variables.currencyFields, paramName ) ) {
  615. if ( !( isNumeric( paramValue ) && ( variables.convertToCents || isValid( "integer", paramValue ) ) ) ) {
  616. throwError( "field '#paramName#' requires an numeric/integer value" );
  617. }
  618. // added rounding due to possible inaccuracies in multiplication - see https://issues.jboss.org/browse/RAILO-767
  619. return ( variables.convertToCents ? round( paramValue * 100 ) : paramValue );
  620. }
  621.  
  622. // boolean
  623. if ( arrayFindNoCase( variables.booleanFields, paramName ) ) {
  624. return ( paramValue ? "true" : "false" );
  625. }
  626.  
  627. // timestamp
  628. if ( arrayFindNoCase( variables.timestampFields, paramName ) ) {
  629. return parseUTCTimestampField( paramValue, paramName );
  630. }
  631.  
  632. // default is string
  633. return trim( paramValue );
  634. }
  635.  
  636. private any function parseUTCTimestampField( required any utcField, required string utcFieldName ) {
  637. if ( isInteger( arguments.utcField ) ) return arguments.utcField;
  638. if ( isDate( arguments.utcField ) ) return getUTCTimestamp( arguments.utcField );
  639. if ( arguments.utcField == 'now' && arguments.utcFieldName == 'trial_end' ) return 'now';
  640. throwError( "utc timestamp field '#utcFieldName#' is in an invalid format" );
  641. }
  642.  
  643. private void function parseResult( required struct result ) {
  644. var resultKeys = structKeyArray( result );
  645. for ( var key in resultKeys ) {
  646. if ( structKeyExists( result, key ) && !isNull( result[ key ] ) ) {
  647. if ( isStruct( result[ key ] ) ) parseResult( result[ key ] );
  648. if ( isArray( result[ key ] ) ) {
  649. for ( var item in result[ key ] ) {
  650. if ( isStruct( item ) ) parseResult( item );
  651. }
  652. }
  653. if ( variables.convertUTCTimestamps && arrayFindNoCase( variables.timestampFields, key ) ) result[ key ] = parseUTCTimestamp( result[ key ] );
  654. if ( variables.convertToCents && arrayFindNoCase( variables.currencyFields, key ) && isInteger( result[ key ] ) ) result[ key ] = result[ key ] / 100;
  655. }
  656. }
  657. }
  658.  
  659. private string function encodeurl( required string str ) {
  660. return replacelist( urlEncodedFormat( arguments.str, "utf-8" ), "%2D,%2E,%5F,%7E", "-,.,_,~" );
  661. }
  662.  
  663. private numeric function getUTCTimestamp( required date dateToConvert ) {
  664. return dateDiff( "s", variables.utcBaseDate, dateToConvert );
  665. }
  666.  
  667. private date function parseUTCTimestamp( required numeric utcTimestamp ) {
  668. var parsed_date = dateAdd( "s", arguments.utcTimestamp, variables.utcBaseDate );
  669. return parsed_date;
  670. }
  671.  
  672. private boolean function isInteger( required any varToValidate ) {
  673. return ( isNumeric( arguments.varToValidate ) && isValid( "integer", arguments.varToValidate ) );
  674. }
  675.  
  676. private void function throwError( required string errorMessage ) {
  677. throw( type = "Stripe", message = "(stripe.cfc) " & arguments.errorMessage );
  678. }
  679.  
  680. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement