diff --git a/stripe/.gitignore b/stripe/.gitignore new file mode 100644 index 0000000000..be941a016b --- /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 new file mode 100644 index 0000000000..5bb3d4207a --- /dev/null +++ b/stripe/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.baeldung.stripe + 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/stripe/ChargeController.java b/stripe/src/main/java/com/baeldung/stripe/ChargeController.java new file mode 100644 index 0000000000..cd3f057cd8 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeController.java @@ -0,0 +1,38 @@ +package com.baeldung.stripe; + +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; +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; + +@Log +@Controller +public class ChargeController { + + @Autowired + StripeService paymentsService; + + @RequestMapping(value = "/charge", method = POST) + public String charge(ChargeRequest chargeRequest, Model model) throws StripeException { + chargeRequest.setDescription("Example charge"); + chargeRequest.setCurrency(Currency.EUR); + 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"; + } + + @ExceptionHandler(StripeException.class) + public String handleError(Model model, StripeException ex) { + model.addAttribute("error", ex.getMessage()); + return "result"; + } +} diff --git a/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java new file mode 100644 index 0000000000..76cdea7bb4 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/stripe/ChargeRequest.java @@ -0,0 +1,20 @@ +package com.baeldung.stripe; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +@Getter +@Setter +@ToString(includeFieldNames = false) +public class ChargeRequest { + + public enum Currency { + EUR, USD; + } + private String description; + private int amount; // cents + private Currency currency; + private String stripeEmail; + private String stripeToken; +} diff --git a/stripe/src/main/java/com/baeldung/stripe/CheckoutController.java b/stripe/src/main/java/com/baeldung/stripe/CheckoutController.java new file mode 100644 index 0000000000..2d525843b0 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/stripe/CheckoutController.java @@ -0,0 +1,21 @@ +package com.baeldung.stripe; + +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/stripe/StripeApplication.java b/stripe/src/main/java/com/baeldung/stripe/StripeApplication.java new file mode 100644 index 0000000000..735c67dda5 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/stripe/StripeApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.stripe; + +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/stripe/StripeService.java b/stripe/src/main/java/com/baeldung/stripe/StripeService.java new file mode 100644 index 0000000000..8784b604c2 --- /dev/null +++ b/stripe/src/main/java/com/baeldung/stripe/StripeService.java @@ -0,0 +1,36 @@ +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 java.util.HashMap; +import java.util.Map; +import javax.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class StripeService { + + @Value("${STRIPE_SECRET_KEY}") + String secretKey; + + @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()); + return Charge.create(chargeParams); + } +} diff --git a/stripe/src/main/resources/static/index.html b/stripe/src/main/resources/static/index.html new file mode 100644 index 0000000000..090a01e91d --- /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 0000000000..4af336ce99 --- /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 0000000000..57f02b74a4 --- /dev/null +++ b/stripe/src/main/resources/templates/result.html @@ -0,0 +1,17 @@ + + + + Result + + +

+
+

Success!

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