view stripe/src/luan/modules/stripe/Stripe.luan @ 452:56c23aa70045 0.7

Add cancel_subscription() function to Stripe customer.
author Hugo Teixeira <hugo.tech@gmail.com>
date Tue, 05 May 2015 18:15:21 -0300
parents 7fd9f1b7b878
children 92c3d22745b8
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

	function this.cancel_subscription()
		local s = subscription()
		s and s.cancel(nil)
	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