@JsonRequest @ProtectEndpoint(writeOperation = true, requiresProject = false) @Path("/coupon") public RakamCoupon checkCoupon( @ApiParam("coupon") String coupon, @HeaderParam("X-Requested-With") String csrfHeader, @javax.inject.Named("user_id") UIPermissionParameterProvider.Project project) { if (!"XMLHttpRequest".equals(csrfHeader)) { throw new RakamException(FORBIDDEN); } Optional<WebUser> webUser = service.getUser(project.userId); if (!webUser.isPresent() || webUser.get().readOnly) { throw new RakamException("User is not allowed to perform this operation", UNAUTHORIZED); } try { Coupon retrieve = Coupon.retrieve(coupon, requestOptions); return new RakamCoupon(retrieve.getPercentOff(), retrieve.getAmountOff()); } catch (InvalidRequestException e) { if (e.getStatusCode() == 404) { throw new RakamException(NOT_FOUND); } throw Throwables.propagate(e); } catch (StripeException e) { throw Throwables.propagate(e); } }
/** * Create a coupon. */ public static Coupon create(Map<String, Object> params) throws StripeException { return create(params, (RequestOptions) null); }
/** * Delete a coupon. */ public Coupon delete() throws StripeException { return delete((RequestOptions) null); }
Optional.ofNullable(subs.getDiscount()) .map(e -> e.getCoupon()) .map(e -> new RakamCoupon(e.getPercentOff(), e.getAmountOff())).orElse(null))) .collect(Collectors.toList());
/** * Retrieve a coupon. */ public static Coupon retrieve(String id) throws StripeException { return retrieve(id, (RequestOptions) null); }
@Test public void testDelete() throws StripeException { final Coupon coupon = getCouponFixture(); final Coupon deletedCoupon = coupon.delete(); assertNotNull(deletedCoupon); assertTrue(deletedCoupon.getDeleted()); verifyRequest( ApiResource.RequestMethod.DELETE, String.format("/v1/coupons/%s", coupon.getId()) ); } }
@Test public void testUpdate() throws StripeException { final Coupon coupon = getCouponFixture(); final Map<String, String> metadata = new HashMap<>(); metadata.put("key", "value"); final Map<String, Object> params = new HashMap<>(); params.put("metadata", metadata); final Coupon updatedCoupon = coupon.update(params); assertNotNull(updatedCoupon); verifyRequest( ApiResource.RequestMethod.POST, String.format("/v1/coupons/%s", coupon.getId()), params ); }
/** * List all coupons. */ public static CouponCollection list(Map<String, Object> params) throws StripeException { return list(params, null); }
/** * Update a coupon. */ @Override public Coupon update(Map<String, Object> params) throws StripeException { return update(params, (RequestOptions) null); }
/** * List all coupons. */ public static CouponCollection list(Map<String, Object> params, RequestOptions options) throws StripeException { return requestCollection(classUrl(Coupon.class), params, CouponCollection.class, options); } // </editor-fold>
@Test public void testDeserialize() throws Exception { final String data = getFixture("/v1/coupons/COUPON_ID"); final Coupon resource = ApiResource.GSON.fromJson(data, Coupon.class); assertNotNull(resource); assertNotNull(resource.getId()); } }
/** * Retrieve a coupon. */ public static Coupon retrieve(String id, RequestOptions options) throws StripeException { return retrieve(id, null, options); }
Optional.ofNullable(subs.getDiscount()) .map(e -> e.getCoupon()) .map(e -> new RakamCoupon(e.getPercentOff(), e.getAmountOff())).orElse(null))) .collect(Collectors.toList());
@Test public void testList() throws StripeException { final Map<String, Object> params = new HashMap<>(); params.put("limit", 1); final CouponCollection coupons = Coupon.list(params); assertNotNull(coupons); verifyRequest( ApiResource.RequestMethod.GET, String.format("/v1/coupons") ); }
private Coupon getCouponFixture() throws StripeException { final Coupon coupon = Coupon.retrieve(COUPON_ID); resetNetworkSpy(); return coupon; }
@Test public void testCreate() throws StripeException { final Map<String, Object> params = new HashMap<>(); params.put("percent_off", 25); params.put("duration", "forever"); final Coupon coupon = Coupon.create(params); assertNotNull(coupon); verifyRequest( ApiResource.RequestMethod.POST, String.format("/v1/coupons"), params ); }
@Test public void testRetrieve() throws StripeException { final Coupon coupon = Coupon.retrieve(COUPON_ID); assertNotNull(coupon); verifyRequest( ApiResource.RequestMethod.GET, String.format("/v1/coupons/%s", COUPON_ID) ); }