From 24cabf825be553f06d2c7a8fa5166292bc4bc7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Soares?= Date: Sat, 1 Apr 2017 13:30:57 +0100 Subject: [PATCH 1/4] Charge a credit card with Stripe Spring Boot project that includes Stripe Java library and does a charge to a credit card. BAEL-742 --- stripe/pom.xml | 68 +++++++++++++++++++ .../java/com/baeldung/ChargeController.java | 33 +++++++++ .../main/java/com/baeldung/ChargeRequest.java | 20 ++++++ .../java/com/baeldung/CheckoutController.java | 21 ++++++ .../java/com/baeldung/StripeApplication.java | 12 ++++ .../main/java/com/baeldung/StripeService.java | 30 ++++++++ stripe/src/main/resources/static/index.html | 7 ++ .../main/resources/templates/checkout.html | 35 ++++++++++ .../src/main/resources/templates/result.html | 12 ++++ .../com/baeldung/StripeApplicationTests.java | 16 +++++ 10 files changed, 254 insertions(+) create mode 100644 stripe/pom.xml create mode 100644 stripe/src/main/java/com/baeldung/ChargeController.java create mode 100644 stripe/src/main/java/com/baeldung/ChargeRequest.java create mode 100644 stripe/src/main/java/com/baeldung/CheckoutController.java create mode 100644 stripe/src/main/java/com/baeldung/StripeApplication.java create mode 100644 stripe/src/main/java/com/baeldung/StripeService.java create mode 100644 stripe/src/main/resources/static/index.html create mode 100644 stripe/src/main/resources/templates/checkout.html create mode 100644 stripe/src/main/resources/templates/result.html create mode 100644 stripe/src/test/java/com/baeldung/StripeApplicationTests.java diff --git a/stripe/pom.xml b/stripe/pom.xml new file mode 100644 index 000000000000..7a4b182fa202 --- /dev/null +++ b/stripe/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + com.baeldung + stripe + 0.0.1-SNAPSHOT + jar + + Stripe + Demo project for Stripe API + + + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.projectlombok + lombok + 1.16.16 + + + + com.stripe + stripe-java + 4.2.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/stripe/src/main/java/com/baeldung/ChargeController.java b/stripe/src/main/java/com/baeldung/ChargeController.java new file mode 100644 index 000000000000..b34541c1ee55 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/ChargeController.java @@ -0,0 +1,33 @@ +package com.baeldung; + +import com.baeldung.ChargeRequest.Currency; +import com.stripe.exception.StripeException; +import java.util.logging.Level; +import lombok.extern.java.Log; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import static org.springframework.web.bind.annotation.RequestMethod.POST; + +@Log +@Controller +public class ChargeController { + + @Autowired + StripeService paymentsService; + + @RequestMapping(value = "/charge", method = POST) + public String charge(ChargeRequest chargeRequest, Model model) { + try { + chargeRequest.setDescription("Example charge"); + chargeRequest.setCurrency(Currency.EUR); + log.log(Level.INFO, "Executing {0}", chargeRequest); + paymentsService.charge(chargeRequest); + } catch (StripeException ex) { + log.severe(ex.getMessage()); + model.addAttribute("message", ex.getMessage()); + } + return "result"; + } +} diff --git a/stripe/src/main/java/com/baeldung/ChargeRequest.java b/stripe/src/main/java/com/baeldung/ChargeRequest.java new file mode 100644 index 000000000000..95d6889bd9ef --- /dev/null +++ b/stripe/src/main/java/com/baeldung/ChargeRequest.java @@ -0,0 +1,20 @@ +package com.baeldung; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +@Getter +@Setter +@ToString(includeFieldNames = false) +public class ChargeRequest { + + public enum Currency { + EUR, USD; + } + String description; + int amount; // cents + Currency currency; + String stripeEmail; + String stripeToken; +} diff --git a/stripe/src/main/java/com/baeldung/CheckoutController.java b/stripe/src/main/java/com/baeldung/CheckoutController.java new file mode 100644 index 000000000000..303a20607f27 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/CheckoutController.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class CheckoutController { + + @Value("${STRIPE_PUBLIC_KEY}") + private String stripePublicKey; + + @RequestMapping("/checkout") + public String checkout(Model model) { + model.addAttribute("amount", 50 * 100); // in cents + model.addAttribute("stripePublicKey", stripePublicKey); + model.addAttribute("currency", ChargeRequest.Currency.EUR); + return "checkout"; + } +} diff --git a/stripe/src/main/java/com/baeldung/StripeApplication.java b/stripe/src/main/java/com/baeldung/StripeApplication.java new file mode 100644 index 000000000000..35f17814825d --- /dev/null +++ b/stripe/src/main/java/com/baeldung/StripeApplication.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class StripeApplication { + + public static void main(String[] args) { + SpringApplication.run(StripeApplication.class, args); + } +} diff --git a/stripe/src/main/java/com/baeldung/StripeService.java b/stripe/src/main/java/com/baeldung/StripeService.java new file mode 100644 index 000000000000..08866ee640b6 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/StripeService.java @@ -0,0 +1,30 @@ +package com.baeldung; + +import com.stripe.exception.APIConnectionException; +import com.stripe.exception.APIException; +import com.stripe.exception.AuthenticationException; +import com.stripe.exception.CardException; +import com.stripe.exception.InvalidRequestException; +import com.stripe.model.Charge; +import com.stripe.net.RequestOptions; +import java.util.HashMap; +import java.util.Map; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class StripeService { + + @Value("${STRIPE_SECRET_KEY}") + private String secretKey; + + public void charge(ChargeRequest chargeRequest) + throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException { + Map chargeParams = new HashMap<>(); + chargeParams.put("amount", chargeRequest.getAmount()); + chargeParams.put("currency", chargeRequest.getCurrency()); + chargeParams.put("description", chargeRequest.getDescription()); + chargeParams.put("source", chargeRequest.getStripeToken()); + Charge.create(chargeParams, RequestOptions.builder().setApiKey(secretKey).build()); + } +} diff --git a/stripe/src/main/resources/static/index.html b/stripe/src/main/resources/static/index.html new file mode 100644 index 000000000000..090a01e91d1b --- /dev/null +++ b/stripe/src/main/resources/static/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/stripe/src/main/resources/templates/checkout.html b/stripe/src/main/resources/templates/checkout.html new file mode 100644 index 000000000000..4af336ce9991 --- /dev/null +++ b/stripe/src/main/resources/templates/checkout.html @@ -0,0 +1,35 @@ + + + + Checkout + + + +
+ + + + +
+ + diff --git a/stripe/src/main/resources/templates/result.html b/stripe/src/main/resources/templates/result.html new file mode 100644 index 000000000000..7ace3c6972da --- /dev/null +++ b/stripe/src/main/resources/templates/result.html @@ -0,0 +1,12 @@ + + + + Result + + +
+
Success!
+ + Checkout + + diff --git a/stripe/src/test/java/com/baeldung/StripeApplicationTests.java b/stripe/src/test/java/com/baeldung/StripeApplicationTests.java new file mode 100644 index 000000000000..5024b810d6a6 --- /dev/null +++ b/stripe/src/test/java/com/baeldung/StripeApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class StripeApplicationTests { + + @Test + public void contextLoads() { + } + +} From edb05e201a895ed126e3f790aa5e9813ccd84e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lui=CC=81s=20Soares?= Date: Tue, 11 Apr 2017 16:17:58 +0100 Subject: [PATCH 2/4] Use ExceptionHandler for Stripe exceptions BAEL-742 --- .../java/com/baeldung/ChargeController.java | 22 +++++++++---------- .../src/main/resources/templates/result.html | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/stripe/src/main/java/com/baeldung/ChargeController.java b/stripe/src/main/java/com/baeldung/ChargeController.java index b34541c1ee55..5bdddb659c80 100644 --- a/stripe/src/main/java/com/baeldung/ChargeController.java +++ b/stripe/src/main/java/com/baeldung/ChargeController.java @@ -2,11 +2,11 @@ import com.baeldung.ChargeRequest.Currency; import com.stripe.exception.StripeException; -import java.util.logging.Level; import lombok.extern.java.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import static org.springframework.web.bind.annotation.RequestMethod.POST; @@ -18,16 +18,16 @@ public class ChargeController { StripeService paymentsService; @RequestMapping(value = "/charge", method = POST) - public String charge(ChargeRequest chargeRequest, Model model) { - try { - chargeRequest.setDescription("Example charge"); - chargeRequest.setCurrency(Currency.EUR); - log.log(Level.INFO, "Executing {0}", chargeRequest); - paymentsService.charge(chargeRequest); - } catch (StripeException ex) { - log.severe(ex.getMessage()); - model.addAttribute("message", ex.getMessage()); - } + public String charge(ChargeRequest chargeRequest, Model model) throws StripeException { + chargeRequest.setDescription("Example charge"); + chargeRequest.setCurrency(Currency.EUR); + paymentsService.charge(chargeRequest); + return "result"; + } + + @ExceptionHandler(StripeException.class) + public String handleError(Model model, StripeException ex) { + model.addAttribute("error", ex.getMessage()); return "result"; } } diff --git a/stripe/src/main/resources/templates/result.html b/stripe/src/main/resources/templates/result.html index 7ace3c6972da..0f3f02bc3fdf 100644 --- a/stripe/src/main/resources/templates/result.html +++ b/stripe/src/main/resources/templates/result.html @@ -4,8 +4,8 @@ Result -
-
Success!
+
+
Success!
Checkout From 8b7a2ba94e88c1c09bb364af4b4fbc502c572e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Soares?= Date: Sun, 16 Apr 2017 21:56:55 +0100 Subject: [PATCH 3/4] Show more detail at result page BAEL-742 --- stripe/.gitignore | 27 +++++++++++++++++++ stripe/pom.xml | 8 +++--- .../{ => stripe}/ChargeController.java | 11 +++++--- .../baeldung/{ => stripe}/ChargeRequest.java | 2 +- .../{ => stripe}/CheckoutController.java | 2 +- .../{ => stripe}/StripeApplication.java | 2 +- .../baeldung/{ => stripe}/StripeService.java | 16 +++++++---- .../src/main/resources/templates/result.html | 10 ++++--- .../com/baeldung/StripeApplicationTests.java | 16 ----------- 9 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 stripe/.gitignore rename stripe/src/main/java/com/baeldung/{ => stripe}/ChargeController.java (70%) rename stripe/src/main/java/com/baeldung/{ => stripe}/ChargeRequest.java (91%) rename stripe/src/main/java/com/baeldung/{ => stripe}/CheckoutController.java (95%) rename stripe/src/main/java/com/baeldung/{ => stripe}/StripeApplication.java (91%) rename stripe/src/main/java/com/baeldung/{ => stripe}/StripeService.java (76%) delete mode 100644 stripe/src/test/java/com/baeldung/StripeApplicationTests.java diff --git a/stripe/.gitignore b/stripe/.gitignore new file mode 100644 index 000000000000..be941a016b3a --- /dev/null +++ b/stripe/.gitignore @@ -0,0 +1,27 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +### +desktop.ini \ No newline at end of file diff --git a/stripe/pom.xml b/stripe/pom.xml index 7a4b182fa202..5bb3d4207a5b 100644 --- a/stripe/pom.xml +++ b/stripe/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung + com.baeldung.stripe stripe 0.0.1-SNAPSHOT jar @@ -38,8 +38,8 @@ org.projectlombok lombok 1.16.16 - @@ -63,6 +63,4 @@ - - diff --git a/stripe/src/main/java/com/baeldung/ChargeController.java b/stripe/src/main/java/com/baeldung/stripe/ChargeController.java similarity index 70% rename from stripe/src/main/java/com/baeldung/ChargeController.java rename to stripe/src/main/java/com/baeldung/stripe/ChargeController.java index 5bdddb659c80..cd3f057cd804 100644 --- a/stripe/src/main/java/com/baeldung/ChargeController.java +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeController.java @@ -1,7 +1,8 @@ -package com.baeldung; +package com.baeldung.stripe; -import com.baeldung.ChargeRequest.Currency; +import com.baeldung.stripe.ChargeRequest.Currency; import com.stripe.exception.StripeException; +import com.stripe.model.Charge; import lombok.extern.java.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -21,7 +22,11 @@ public class ChargeController { public String charge(ChargeRequest chargeRequest, Model model) throws StripeException { chargeRequest.setDescription("Example charge"); chargeRequest.setCurrency(Currency.EUR); - paymentsService.charge(chargeRequest); + Charge charge = paymentsService.charge(chargeRequest); + model.addAttribute("id", charge.getId()); + model.addAttribute("status", charge.getStatus()); + model.addAttribute("chargeId", charge.getId()); + model.addAttribute("balance_transaction", charge.getBalanceTransaction()); return "result"; } diff --git a/stripe/src/main/java/com/baeldung/ChargeRequest.java b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java similarity index 91% rename from stripe/src/main/java/com/baeldung/ChargeRequest.java rename to stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java index 95d6889bd9ef..a9f881377e74 100644 --- a/stripe/src/main/java/com/baeldung/ChargeRequest.java +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.stripe; import lombok.Getter; import lombok.Setter; diff --git a/stripe/src/main/java/com/baeldung/CheckoutController.java b/stripe/src/main/java/com/baeldung/stripe/CheckoutController.java similarity index 95% rename from stripe/src/main/java/com/baeldung/CheckoutController.java rename to stripe/src/main/java/com/baeldung/stripe/CheckoutController.java index 303a20607f27..2d525843b0ae 100644 --- a/stripe/src/main/java/com/baeldung/CheckoutController.java +++ b/stripe/src/main/java/com/baeldung/stripe/CheckoutController.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.stripe; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; diff --git a/stripe/src/main/java/com/baeldung/StripeApplication.java b/stripe/src/main/java/com/baeldung/stripe/StripeApplication.java similarity index 91% rename from stripe/src/main/java/com/baeldung/StripeApplication.java rename to stripe/src/main/java/com/baeldung/stripe/StripeApplication.java index 35f17814825d..735c67dda5e7 100644 --- a/stripe/src/main/java/com/baeldung/StripeApplication.java +++ b/stripe/src/main/java/com/baeldung/stripe/StripeApplication.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.stripe; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/stripe/src/main/java/com/baeldung/StripeService.java b/stripe/src/main/java/com/baeldung/stripe/StripeService.java similarity index 76% rename from stripe/src/main/java/com/baeldung/StripeService.java rename to stripe/src/main/java/com/baeldung/stripe/StripeService.java index 08866ee640b6..8784b604c25b 100644 --- a/stripe/src/main/java/com/baeldung/StripeService.java +++ b/stripe/src/main/java/com/baeldung/stripe/StripeService.java @@ -1,14 +1,15 @@ -package com.baeldung; +package com.baeldung.stripe; +import com.stripe.Stripe; import com.stripe.exception.APIConnectionException; import com.stripe.exception.APIException; import com.stripe.exception.AuthenticationException; import com.stripe.exception.CardException; import com.stripe.exception.InvalidRequestException; import com.stripe.model.Charge; -import com.stripe.net.RequestOptions; import java.util.HashMap; import java.util.Map; +import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -16,15 +17,20 @@ public class StripeService { @Value("${STRIPE_SECRET_KEY}") - private String secretKey; + String secretKey; - public void charge(ChargeRequest chargeRequest) + @PostConstruct + public void init() { + Stripe.apiKey = secretKey; + } + + public Charge charge(ChargeRequest chargeRequest) throws AuthenticationException, InvalidRequestException, APIConnectionException, CardException, APIException { Map chargeParams = new HashMap<>(); chargeParams.put("amount", chargeRequest.getAmount()); chargeParams.put("currency", chargeRequest.getCurrency()); chargeParams.put("description", chargeRequest.getDescription()); chargeParams.put("source", chargeRequest.getStripeToken()); - Charge.create(chargeParams, RequestOptions.builder().setApiKey(secretKey).build()); + return Charge.create(chargeParams); } } diff --git a/stripe/src/main/resources/templates/result.html b/stripe/src/main/resources/templates/result.html index 0f3f02bc3fdf..4d179b46d41d 100644 --- a/stripe/src/main/resources/templates/result.html +++ b/stripe/src/main/resources/templates/result.html @@ -5,8 +5,12 @@
-
Success!
- - Checkout +
Success! +
Id.:
+
Status:
+
Charge id.:
+
Balance transaction:
+
+ Checkout again diff --git a/stripe/src/test/java/com/baeldung/StripeApplicationTests.java b/stripe/src/test/java/com/baeldung/StripeApplicationTests.java deleted file mode 100644 index 5024b810d6a6..000000000000 --- a/stripe/src/test/java/com/baeldung/StripeApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class StripeApplicationTests { - - @Test - public void contextLoads() { - } - -} From 1a3cbe6ee8d35a17fbf3b77f1e174f2665149fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Soares?= Date: Sun, 16 Apr 2017 23:58:12 +0100 Subject: [PATCH 4/4] Show more detail at result page BAEL-742 --- .../main/java/com/baeldung/stripe/ChargeRequest.java | 10 +++++----- stripe/src/main/resources/templates/result.html | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java index a9f881377e74..76cdea7bb429 100644 --- a/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java @@ -12,9 +12,9 @@ public class ChargeRequest { public enum Currency { EUR, USD; } - String description; - int amount; // cents - Currency currency; - String stripeEmail; - String stripeToken; + private String description; + private int amount; // cents + private Currency currency; + private String stripeEmail; + private String stripeToken; } diff --git a/stripe/src/main/resources/templates/result.html b/stripe/src/main/resources/templates/result.html index 4d179b46d41d..57f02b74a4e2 100644 --- a/stripe/src/main/resources/templates/result.html +++ b/stripe/src/main/resources/templates/result.html @@ -4,12 +4,13 @@ Result -
-
Success! +

+
+

Success!

Id.:
Status:
Charge id.:
-
Balance transaction:
+
Balance transaction id.:
Checkout again