annotate stripe/src/luan/modules/stripe/Stripe.luan @ 503:92c3d22745b8

make _ENV optional
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 May 2015 23:24:46 -0600
parents 56c23aa70045
children ca169567ce07
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 java()
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local Luan = require "luan:Luan"
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local error = Luan.error
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local assert_integer = Luan.assert_integer
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
5 local Table = require "luan:Table"
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local Stripe = require "java:com.stripe.Stripe"
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Customer = require "java:com.stripe.model.Customer"
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local Charge = require "java:com.stripe.model.Charge"
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
10 local M = {}
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
12 M.currency = "usd"
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
13
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
14 function M.init(api_key)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 Stripe.apiKey = api_key
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 end
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
18
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
19 local function customer_table(java_customer)
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
20
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
21 local function subscription()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
22 local list = java_customer.getSubscriptions().getData()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
23 local size = list.size()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
24 size <= 1 or error "more than 1 subscription"
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
25 return size == 1 and list.get(0) or nil
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
26 end
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
27
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
28 local this = Table.new_property_table()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
29 local meta = Luan.get_metatable(this)
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
30
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
31 meta.get.id = java_customer.getId
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
32
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
33 function meta.get.subscription_status()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
34 local s = subscription()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
35 return s and s.getStatus()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
36 end
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
37
452
56c23aa70045 Add cancel_subscription() function to Stripe customer.
Hugo Teixeira <hugo.tech@gmail.com>
parents: 407
diff changeset
38 function this.cancel_subscription()
56c23aa70045 Add cancel_subscription() function to Stripe customer.
Hugo Teixeira <hugo.tech@gmail.com>
parents: 407
diff changeset
39 local s = subscription()
56c23aa70045 Add cancel_subscription() function to Stripe customer.
Hugo Teixeira <hugo.tech@gmail.com>
parents: 407
diff changeset
40 s and s.cancel(nil)
56c23aa70045 Add cancel_subscription() function to Stripe customer.
Hugo Teixeira <hugo.tech@gmail.com>
parents: 407
diff changeset
41 end
56c23aa70045 Add cancel_subscription() function to Stripe customer.
Hugo Teixeira <hugo.tech@gmail.com>
parents: 407
diff changeset
42
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
43 return this
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
44 end
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
45
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
46
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
47 local function charge_table(java_charge)
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
48 local this = Table.new_property_table()
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
49 local meta = Luan.get_metatable(this)
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
50
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
51 meta.get.id = java_charge.getId
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
52 meta.get.amount = java_charge.getAmount
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
53
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
54 return this
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
55 end
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
56
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
57
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
58 function M.create_customer(params)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 local java_customer = Customer.create(params)
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
60 return customer_table(java_customer)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
63 function M.retrieve_customer(id)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 local java_customer = Customer.retrieve(id)
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
65 return customer_table(java_customer)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 end
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
68 function M.create_charge(params)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 params.amount or error "missing parameter 'amount'"
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 params.amount = assert_integer(params.amount)
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
71 params.currency = params.currency or M.currency
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 local java_charge = Charge.create(params)
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
73 return charge_table(java_charge)
402
62b457c50594 add stripe;
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 end
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
75
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
76 return M
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 452
diff changeset
77
407
7fd9f1b7b878 replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents: 402
diff changeset
78 -- http://javadox.com/com.stripe/stripe-java/1.2.1/overview-summary.html