Skip to content

Instantly share code, notes, and snippets.

@jimdanz
Created December 7, 2013 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimdanz/7838912 to your computer and use it in GitHub Desktop.
Save jimdanz/7838912 to your computer and use it in GitHub Desktop.
Java multi-subs example
@Test
public void testNewStyleSubscriptionAPI() throws StripeException {
Plan plan = Plan.create(getUniquePlanParams(), MULTI_SUBS_API_KEY);
Plan plan2 = Plan.create(getUniquePlanParams(), MULTI_SUBS_API_KEY);
Customer customer = Customer.create(defaultCustomerParams, MULTI_SUBS_API_KEY);
// Create
Map<String, Object> subCreateParams = new HashMap<String, Object>();
subCreateParams.put("plan", plan.getId());
Subscription sub = customer.createSubscription(subCreateParams, MULTI_SUBS_API_KEY);
assertEquals(plan.getId(), sub.getPlan().getId());
customer = Customer.retrieve(customer.getId(), MULTI_SUBS_API_KEY);
assertEquals(1, customer.getSubscriptions().getCount().intValue());
assertEquals(sub.getId(), customer.getSubscriptions().getData().get(0).getId());
// Retrieve
Subscription retrievedSub = customer.getSubscriptions().retrieve(sub.getId(), MULTI_SUBS_API_KEY);
assertEquals(sub.getId(), retrievedSub.getId());
// List
CustomerSubscriptionCollection list = customer.getSubscriptions().all(null, MULTI_SUBS_API_KEY);
assertEquals(1, list.getCount().intValue());
assertEquals(sub.getId(), list.getData().get(0).getId());
// Update
Map<String, Object> subUpdateParams = new HashMap<String, Object>();
subUpdateParams.put("plan", plan2.getId());
sub = sub.update(subUpdateParams, MULTI_SUBS_API_KEY);
assertEquals(plan2.getId(), sub.getPlan().getId());
// Cancel
sub = sub.cancel(null, MULTI_SUBS_API_KEY);
assertNotNull(sub.getCanceledAt());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment