From 22c9f59a8c2ae7e05107b735003aedbf0432258a Mon Sep 17 00:00:00 2001 From: Fayder Florez Date: Mon, 2 Oct 2017 11:25:39 +0300 Subject: [PATCH] Updates js --- .gitignore | 3 +- .../restcountries/v2/rest/StripeRest.java | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java diff --git a/.gitignore b/.gitignore index a80f1bb1..c6bc1123 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,4 @@ target/ backup/ .idea/ *.iml -google*.html -src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java +google*.html \ No newline at end of file diff --git a/src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java b/src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java new file mode 100644 index 00000000..b14cf06a --- /dev/null +++ b/src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java @@ -0,0 +1,61 @@ +package eu.fayder.restcountries.v2.rest; + +import com.google.gson.Gson; +import com.stripe.Stripe; +import com.stripe.exception.*; +import com.stripe.model.Charge; +import eu.fayder.restcountries.domain.ResponseEntity; +import eu.fayder.restcountries.v2.domain.Contribution; +import org.apache.log4j.Logger; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by fayder on 24/02/2017. + */ +@Provider +@Path("/contribute") +@Consumes("application/json;charset=utf-8") +public class StripeRest { + + private static final Logger LOG = Logger.getLogger(StripeRest.class); + + @POST + public Object contribute(Contribution contribution) { + if (contribution == null || contribution.getToken() == null || contribution.getAmount() < 500) { + return getResponse(Response.Status.BAD_REQUEST); + } + + LOG.debug("Contribute: " + contribution.getAmount()); + + Stripe.apiKey = ""; + Map params = new HashMap<>(); + params.put("amount", contribution.getAmount()); + params.put("currency", "eur"); + params.put("description", "REST Countries"); + params.put("source", contribution.getToken()); + + try { + Charge.create(params); + } catch (AuthenticationException | InvalidRequestException | CardException | APIConnectionException | APIException e) { + LOG.error(e.getMessage(), e); + return getResponse(Response.Status.BAD_REQUEST); + } + + return getResponse(Response.Status.ACCEPTED); + } + + private Response getResponse(Response.Status status) { + Gson gson = new Gson(); + return Response + .status(status) + .entity(gson.toJson(new ResponseEntity(status.getStatusCode(), + status.getReasonPhrase()))).build(); + } +}