view stripe/src/luan/modules/stripe/Stripe.luan @ 407:7fd9f1b7b878

replace LuanPropertyTable with LuanPropertyMeta
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 13:01:00 -0600
parents 62b457c50594
children 56c23aa70045
line wrap: on
line source

java()
local Luan = require "luan:Luan"
local error = Luan.error
local assert_integer = Luan.assert_integer
local Table = require "luan:Table"
local Stripe = require "java:com.stripe.Stripe"
local Customer = require "java:com.stripe.model.Customer"
local Charge = require "java:com.stripe.model.Charge"

currency = "usd"

function init(api_key)
	Stripe.apiKey = api_key
end


local function customer_table(java_customer)

	local function subscription()
		local list = java_customer.getSubscriptions().getData()
		local size = list.size()
		size <= 1 or error "more than 1 subscription"
		return size == 1 and list.get(0) or nil
	end

	local this = Table.new_property_table()
	local meta = Luan.get_metatable(this)

	meta.get.id = java_customer.getId

	function meta.get.subscription_status()
		local s = subscription()
		return s and s.getStatus()
	end

	return this
end


local function charge_table(java_charge)
	local this = Table.new_property_table()
	local meta = Luan.get_metatable(this)

	meta.get.id = java_charge.getId
	meta.get.amount = java_charge.getAmount

	return this
end


function create_customer(params)
	local java_customer = Customer.create(params)
	return customer_table(java_customer)
end

function retrieve_customer(id)
	local java_customer = Customer.retrieve(id)
	return customer_table(java_customer)
end

function create_charge(params)
	params.amount or error "missing parameter 'amount'"
	params.amount = assert_integer(params.amount)
	params.currency = params.currency or currency
	local java_charge = Charge.create(params)
	return charge_table(java_charge)
end

-- http://javadox.com/com.stripe/stripe-java/1.2.1/overview-summary.html