Skip to content

Commit

Permalink
fix(payment): product map should have stripe product id as key instea…
Browse files Browse the repository at this point in the history
…d of product alias in stripe webhook
  • Loading branch information
vihangpatil committed Sep 27, 2023
1 parent 88b73ee commit c224652
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.k33.platform.payment.stripe

import com.k33.platform.utils.analytics.Log
import com.k33.platform.utils.config.loadConfig
import com.k33.platform.utils.logging.NotifySlack
import com.k33.platform.utils.logging.getLogger
import com.stripe.exception.ApiConnectionException
Expand Down Expand Up @@ -62,6 +63,13 @@ object StripeClient {
val cancelUrl: String,
)

private val isProd by lazy { System.getenv("GCP_PROJECT_ID").endsWith("prod", ignoreCase = true) }

val productMap: Map<String, ProductConfig> by loadConfig(
"researchApp",
"apps.research.products",
)

/**
* [Stripe API - Create Checkout Session](https://stripe.com/docs/api/checkout/sessions/create)
*/
Expand Down Expand Up @@ -114,7 +122,10 @@ object StripeClient {
.setCustomer(customerId)
.setPaymentMethodCollection(CheckoutSessionCreateParams.PaymentMethodCollection.IF_REQUIRED)
.apply {
if (customerEmail.endsWith("@k33.com", ignoreCase = true)) {
if (isProd
&& customerEmail.endsWith("@k33.com", ignoreCase = true)
&& productMap["pro"]?.stripeProductId == productId
) {
addDiscount(
CheckoutSessionCreateParams
.Discount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.k33.platform.identity.auth.gcp.FirebaseAuthService
import com.k33.platform.user.UserId
import com.k33.platform.user.UserService.fetchUser
import com.k33.platform.utils.analytics.Log
import com.k33.platform.utils.config.loadConfig
import com.k33.platform.utils.config.loadConfigEager
import com.k33.platform.utils.logging.NotifySlack
import com.k33.platform.utils.logging.logWithMDC
import com.stripe.exception.SignatureVerificationException
Expand All @@ -32,10 +32,12 @@ fun Application.module() {

val endpointSecret by lazy { System.getenv("STRIPE_WEBHOOK_ENDPOINT_SECRET") }

val productMap by loadConfig<Map<String, ProductConfig>>(
"researchApp",
"apps.research.products",
)
val productMap: Map<String, ProductConfig> by lazy {
loadConfigEager<Map<String, ProductConfig>>(
"researchApp",
"apps.research.products",
).values.associateBy { it.stripeProductId }
}

val authService by lazy { FirebaseAuthService }
val emailService by getEmailService()
Expand Down

0 comments on commit c224652

Please sign in to comment.