view stripe/src/luan/modules/stripe/StripeLuan.java @ 402:62b457c50594

add stripe; change Luan.values to only return values, not indexes;
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 28 Apr 2015 22:38:31 -0600
parents
children
line wrap: on
line source

package luan.modules.stripe;

import luan.Luan;
import luan.LuanTable;
import luan.LuanProperty;
import com.stripe.model.Customer;
import com.stripe.model.Charge;
import com.stripe.model.Subscription;
import java.util.List;


public final class StripeLuan {

	public static LuanTable table(final Customer customer) {

		LuanTable tbl = Luan.newPropertyTable();

		tbl.put( "id", new LuanProperty() {
			@Override public Object get() {
				return customer.getId();
			}
		} );

		tbl.put( "subscription_status", new LuanProperty() {
			@Override public Object get() {
				Subscription s = getSubscription(customer);
				return s==null ? null : s.getStatus();
			}
		} );

		return tbl;
	}

	public static LuanTable table(final Charge charge) {
		LuanTable tbl = Luan.newPropertyTable();

		tbl.put( "id", new LuanProperty() {
			@Override public Object get() {
				return charge.getId();
			}
		} );

		tbl.put( "amount", new LuanProperty() {
			@Override public Object get() {
				return charge.getAmount();
			}
/*
			@Override public boolean set(Object value) {
				charge.setAmount(check_integer(value));  return true;
			}
*/
		} );

		return tbl;
	}

	public static Subscription getSubscription(Customer customer) {
		List<Subscription> list = customer.getSubscriptions().getData();
		switch(list.size()) {
		case 0:
			return null;
		case 1:
			return list.get(0);
		default:
			throw new RuntimeException("more than 1 subscription");
		}
	}
/*
	private static Integer check_integer(Object value) {
		if( value==null )
			return (Integer)null;
		Integer i = Luan.asInteger(value);
		if( i==null )
			throw new IllegalArgumentException("value must be an integer");
		return i;
	}
*/
}

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