-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from Sunagatov/feature/stripe-integration
Stripe POC
- Loading branch information
Showing
18 changed files
with
356 additions
and
12 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
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
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,27 @@ | ||
@startuml | ||
'https://plantuml.com/sequence-diagram | ||
|
||
autonumber | ||
|
||
title Checkout Sequence, Logged-In User | ||
participant "Frontend" as fe | ||
participant "Backend" as be | ||
participant "Stripe" as stripe | ||
database "PostgreSQL" as db | ||
|
||
fe -> be: GET /api/v1/payment | ||
be -> db: Create new order (status CREATED) | ||
be -> stripe: Create session and POST /v1/checkout/sessions | ||
be <-- stripe: Return client_secret | ||
be -> db: Save client_secret | ||
be --> fe: Return client_secret | ||
fe -> stripe: Embed Stripe's payment page using client_secret | ||
fe <[#red]-- stripe: Redirect /orders?sessionId={CHECKOUT_SESSION_ID} <font color=red><b>REDIRECT TO USER!!! | ||
fe -> be: GET /stripe/session-status?sessionId={CHECKOUT_SESSION_ID} | ||
group <font color=red><b>!!! Inconsistency danger: what if step 6 doesn't happen | ||
be -> stripe: GET /v1/checkout/sessions/CHECKOUT_SESSION_ID | ||
be <-- stripe: Session status (OPEN / COMPLETE / EXPIRED) | ||
be -> db: Update order status if COMPLETE (status PAID) | ||
fe <-- be: Response with order status and id | ||
end | ||
@enduml |
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,15 @@ | ||
@startuml | ||
|
||
title Order States | ||
|
||
[*] --> CREATED : BE receives payment request | ||
CREATED -right-> PAID : successful payment | ||
CREATED --> UNSUCCESSFUL_PAYMENT : rejected payment | ||
UNSUCCESSFUL_PAYMENT --> PAID : successful payment | ||
UNSUCCESSFUL_PAYMENT -> UNSUCCESSFUL_PAYMENT : rejected payment | ||
|
||
CREATED: New Order | ||
PAID : Stripe redirected success=true | ||
UNSUCCESSFUL_PAYMENT : Stripe redirected success=false | ||
|
||
@enduml |
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
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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/zufar/icedlatte/payment/api/StripeLineItemsConverter.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,43 @@ | ||
package com.zufar.icedlatte.payment.api; | ||
|
||
import com.stripe.param.checkout.SessionCreateParams; | ||
import com.zufar.icedlatte.openapi.dto.ShoppingCartDto; | ||
import com.zufar.icedlatte.openapi.dto.ShoppingCartItemDto; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class StripeLineItemsConverter { | ||
|
||
// FIXME: use JPA entities instead of converted DTO | ||
public List<SessionCreateParams.LineItem> getLineItems(ShoppingCartDto shoppingCart) { | ||
var result = new ArrayList<SessionCreateParams.LineItem>(); | ||
for (ShoppingCartItemDto item : shoppingCart.getItems()) { | ||
var quantity = item.getProductQuantity(); | ||
// convert to cents | ||
var unitAmount = item.getProductInfo().getPrice().multiply(BigDecimal.valueOf(100)).longValue(); | ||
var productName = item.getProductInfo().getName(); | ||
var productData = SessionCreateParams.LineItem.PriceData.ProductData.builder() | ||
.setName(productName) | ||
.build(); | ||
var priceData = SessionCreateParams.LineItem.PriceData.builder() | ||
.setUnitAmount(unitAmount) | ||
.setCurrency("USD") | ||
.setProductData(productData) | ||
.build(); | ||
var lineItem = SessionCreateParams.LineItem.builder() | ||
.setQuantity((long) quantity) | ||
.setPriceData(priceData) | ||
.build(); | ||
result.add(lineItem); | ||
} | ||
return result; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/java/com/zufar/icedlatte/payment/api/StripeShippingOptionsProvider.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,94 @@ | ||
package com.zufar.icedlatte.payment.api; | ||
|
||
import com.stripe.param.checkout.SessionCreateParams; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class StripeShippingOptionsProvider { | ||
|
||
public List<SessionCreateParams.ShippingOption> getShippingOptions() { | ||
var result = new ArrayList<SessionCreateParams.ShippingOption>(); | ||
var option1 = SessionCreateParams.ShippingOption.builder() | ||
.setShippingRateData( | ||
SessionCreateParams.ShippingOption.ShippingRateData.builder() | ||
.setType( | ||
SessionCreateParams.ShippingOption.ShippingRateData.Type.FIXED_AMOUNT | ||
) | ||
.setFixedAmount( | ||
SessionCreateParams.ShippingOption.ShippingRateData.FixedAmount.builder() | ||
.setAmount(0L) | ||
.setCurrency("usd") | ||
.build() | ||
) | ||
.setDisplayName("Free shipping") | ||
.setDeliveryEstimate( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.builder() | ||
.setMinimum( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Minimum.builder() | ||
.setUnit( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Minimum.Unit.BUSINESS_DAY | ||
) | ||
.setValue(5L) | ||
.build() | ||
) | ||
.setMaximum( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Maximum.builder() | ||
.setUnit( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Maximum.Unit.BUSINESS_DAY | ||
) | ||
.setValue(7L) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build(); | ||
result.add(option1); | ||
var option2 = SessionCreateParams.ShippingOption.builder() | ||
.setShippingRateData( | ||
SessionCreateParams.ShippingOption.ShippingRateData.builder() | ||
.setType( | ||
SessionCreateParams.ShippingOption.ShippingRateData.Type.FIXED_AMOUNT | ||
) | ||
.setFixedAmount( | ||
SessionCreateParams.ShippingOption.ShippingRateData.FixedAmount.builder() | ||
.setAmount(1500L) | ||
.setCurrency("usd") | ||
.build() | ||
) | ||
.setDisplayName("Next day air") | ||
.setDeliveryEstimate( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.builder() | ||
.setMinimum( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Minimum.builder() | ||
.setUnit( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Minimum.Unit.BUSINESS_DAY | ||
) | ||
.setValue(1L) | ||
.build() | ||
) | ||
.setMaximum( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Maximum.builder() | ||
.setUnit( | ||
SessionCreateParams.ShippingOption.ShippingRateData.DeliveryEstimate.Maximum.Unit.BUSINESS_DAY | ||
) | ||
.setValue(1L) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build() | ||
) | ||
.build(); | ||
result.add(option2); | ||
return result; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/zufar/icedlatte/payment/dto/PaymentSession.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,4 @@ | ||
package com.zufar.icedlatte.payment.dto; | ||
|
||
public record PaymentSession(String clientSecret) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/zufar/icedlatte/payment/dto/PaymentSessionStatus.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,4 @@ | ||
package com.zufar.icedlatte.payment.dto; | ||
|
||
public record PaymentSessionStatus(String status, String customerEmail) { | ||
} |
Oops, something went wrong.