You will build an application that receives a subscription update stripe event by using StripeEventHandler
.
- About 15 minutes
- A favorite text editor or IDE
- JDK 11 or later
- Gradle
For all Spring applications, you should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies you need for an application and does a lot of the set up for you. This example needs only the Spring Web dependency, Java 11 and Gradle
Add the following to your build.gradle.kts
or build.gradle
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.4.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.4.21"
kotlin("plugin.spring") version "1.4.21"
}
repositories {
mavenCentral()
}
dependencies {
// add the stripe spring boot starter to your gradle build file
implementation("io.hndrs:stripe-spring-boot-starter:1.0.0")
// add the stripe java library
implementation("com.stripe:stripe-java:20.37.0")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
To configure the stripe webhook we need to set its signingSecret
and the webhook-path
. Add the following
properties to src/main/resources/application
hndrs.stripe.signingSecret=whsc_**********
hndrs.stripe.webhook-path=/stripe-events
The signing secret can be obtained on your Stripe Dashboard or with the Stripe CLI
With any webhook-event-based application, you need to create a receiver that responds to published webhook events. The following implementation shows how to do so:
@Component
open class ExampleReceiver : StripeEventReceiver<Subscription>(Subscription::class.java) {
override fun onCondition(event: Event): Boolean {
// check the event type
return event.type == "customer.subscription.updated"
}
companion object {
private val LOG = LoggerFactory.getLogger(ExampleReceiver::class.java)
}
}
Use the Stripe Cli to send a test event.
stripe listen
stripe trigger customer.subscription.updated