-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vehicle-service): add stripe webhook to enable payment methods
- Loading branch information
vincenzo.corso
committed
Mar 23, 2024
1 parent
ca4b188
commit 46a3180
Showing
18 changed files
with
205 additions
and
29 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
5 changes: 3 additions & 2 deletions
5
...a/it/vincenzocorso/carsharing/paymentservice/adapters/persistence/jpa/CustomerEntity.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
16 changes: 8 additions & 8 deletions
16
...vincenzocorso/carsharing/paymentservice/adapters/persistence/jpa/PaymentMethodEntity.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 |
---|---|---|
@@ -1,29 +1,29 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.persistence.jpa; | ||
|
||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase; | ||
import jakarta.persistence.*; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "payment_methods") | ||
public class PaymentMethodEntity extends PanacheEntityBase { | ||
@NoArgsConstructor | ||
public class PaymentMethodEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
@Column(name = "payment_method_id") | ||
public String id; | ||
|
||
@Column(name = "external_id", nullable = false) | ||
@Column(name = "external_id") | ||
public String externalId; | ||
|
||
@Column(name = "customer_id", nullable = false) | ||
@Column(name = "customer_id") | ||
public String customerId; | ||
|
||
@Column(name = "state", nullable = false) | ||
@Column(name = "state") | ||
public String state; | ||
|
||
@Column(name = "metadata", nullable = false) | ||
@Column(name = "metadata") | ||
public String metadata; | ||
|
||
@Version | ||
@Column(name = "version", nullable = false) | ||
@Column(name = "version") | ||
public Long version; | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
...va/it/vincenzocorso/carsharing/paymentservice/adapters/stripe/SetupIntentMetadataKey.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,12 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.stripe; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum SetupIntentMetadataKey { | ||
PAYMENT_METHOD_ID("internal__payment_method_id"); | ||
|
||
private final String keyName; | ||
} |
10 changes: 10 additions & 0 deletions
10
...rc/main/java/it/vincenzocorso/carsharing/paymentservice/adapters/webhook/StripeEvent.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,10 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.webhook; | ||
|
||
public record StripeEvent( | ||
String id, | ||
String object, | ||
String apiVersion, | ||
String created, | ||
String type, | ||
StripeEventData data | ||
) {} |
5 changes: 5 additions & 0 deletions
5
...ain/java/it/vincenzocorso/carsharing/paymentservice/adapters/webhook/StripeEventData.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,5 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.webhook; | ||
|
||
public record StripeEventData( | ||
StripeEventDataObject object | ||
) {} |
9 changes: 9 additions & 0 deletions
9
...va/it/vincenzocorso/carsharing/paymentservice/adapters/webhook/StripeEventDataObject.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,9 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.webhook; | ||
|
||
import java.util.Map; | ||
|
||
public record StripeEventDataObject( | ||
String id, | ||
String object, | ||
Map<String, String> metadata | ||
) {} |
53 changes: 53 additions & 0 deletions
53
.../it/vincenzocorso/carsharing/paymentservice/adapters/webhook/StripeWebhookController.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,53 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.adapters.webhook; | ||
|
||
import it.vincenzocorso.carsharing.common.exceptions.InternalServerException; | ||
import it.vincenzocorso.carsharing.paymentservice.adapters.stripe.SetupIntentMetadataKey; | ||
import it.vincenzocorso.carsharing.paymentservice.domain.models.PaymentMethod; | ||
import it.vincenzocorso.carsharing.paymentservice.domain.ports.in.SavePaymentMethod; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.flogger.Flogger; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Optional; | ||
|
||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static jakarta.ws.rs.core.Response.Status.OK; | ||
|
||
@Path("/stripe/webhook") | ||
@Produces(APPLICATION_JSON) | ||
@Consumes(APPLICATION_JSON) | ||
@Slf4j | ||
@AllArgsConstructor | ||
public class StripeWebhookController { | ||
final SavePaymentMethod savePaymentMethod; | ||
|
||
@POST | ||
public Response handleStripeEvents(StripeEvent event) { | ||
log.info("Received event of type " + event.type() + ": " + event); | ||
|
||
switch (event.type()) { | ||
case "setup_intent.succeeded" -> handleSetupIntentSucceededEvent(event); | ||
default -> log.info("A stripe event was skipped: " + event); | ||
} | ||
|
||
return Response.status(OK).build(); | ||
} | ||
|
||
@Transactional | ||
void handleSetupIntentSucceededEvent(StripeEvent event) { | ||
String paymentMethodId = Optional.ofNullable(event.data()) | ||
.map(StripeEventData::object) | ||
.map(StripeEventDataObject::metadata) | ||
.map(metadata -> metadata.getOrDefault(SetupIntentMetadataKey.PAYMENT_METHOD_ID.getKeyName(), null)) | ||
.orElseThrow(() -> new RuntimeException("Could not find the payment method id")); | ||
|
||
PaymentMethod enabledPaymentMethod = savePaymentMethod.enablePaymentMethod(paymentMethodId); | ||
log.info("Enabled payment method {}", enabledPaymentMethod.getId()); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...cenzocorso/carsharing/paymentservice/domain/events/PaymentMethodStateTransitionEvent.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,10 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.domain.events; | ||
|
||
import it.vincenzocorso.carsharing.common.messaging.events.DomainEvent; | ||
|
||
public record PaymentMethodStateTransitionEvent(String oldState, String newState) implements DomainEvent { | ||
@Override | ||
public String getType() { | ||
return "PAYMENT_METHOD_STATE_TRANSITION_EVENT"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...haring/paymentservice/domain/exceptions/IllegalPaymentMethodStateTransitionException.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,7 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.domain.exceptions; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
public class IllegalPaymentMethodStateTransitionException extends RuntimeException { | ||
} |
10 changes: 10 additions & 0 deletions
10
...enzocorso/carsharing/paymentservice/domain/exceptions/PaymentMethodNotFoundException.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,10 @@ | ||
package it.vincenzocorso.carsharing.paymentservice.domain.exceptions; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class PaymentMethodNotFoundException extends RuntimeException { | ||
private final String paymentMethodId; | ||
} |
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
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 |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
public enum PaymentMethodState { | ||
PENDING, | ||
ACTIVE, | ||
INACTIVE | ||
ENABLED, | ||
DISABLED | ||
} |
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
Oops, something went wrong.