Mercurial Hosting > luan
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 |
rev | line source |
---|---|
402 | 1 java() |
2 local Luan = require "luan:Luan" | |
3 local error = Luan.error | |
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 | 6 local Stripe = require "java:com.stripe.Stripe" |
7 local Customer = require "java:com.stripe.model.Customer" | |
8 local Charge = require "java:com.stripe.model.Charge" | |
9 | |
503 | 10 local M = {} |
402 | 11 |
503 | 12 M.currency = "usd" |
13 | |
14 function M.init(api_key) | |
402 | 15 Stripe.apiKey = api_key |
16 end | |
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 | 58 function M.create_customer(params) |
402 | 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 | 61 end |
62 | |
503 | 63 function M.retrieve_customer(id) |
402 | 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 | 66 end |
67 | |
503 | 68 function M.create_charge(params) |
402 | 69 params.amount or error "missing parameter 'amount'" |
70 params.amount = assert_integer(params.amount) | |
503 | 71 params.currency = params.currency or M.currency |
402 | 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 | 74 end |
407
7fd9f1b7b878
replace LuanPropertyTable with LuanPropertyMeta
Franklin Schmidt <fschmidt@gmail.com>
parents:
402
diff
changeset
|
75 |
503 | 76 return M |
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 |