Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Updates js
Browse files Browse the repository at this point in the history
  • Loading branch information
fayderflorez committed Oct 2, 2017
1 parent 30ca9a9 commit 22c9f59
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ target/
backup/
.idea/
*.iml
google*.html
src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java
google*.html
61 changes: 61 additions & 0 deletions src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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();
}
}

0 comments on commit 22c9f59

Please sign in to comment.