This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30ca9a9
commit 22c9f59
Showing
2 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/eu/fayder/restcountries/v2/rest/StripeRest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |