From c2246522ca83437bda834b22c4376209220ec758 Mon Sep 17 00:00:00 2001 From: Vihang Patil Date: Wed, 27 Sep 2023 12:09:32 +0200 Subject: [PATCH] fix(payment): product map should have stripe product id as key instead of product alias in stripe webhook --- .../com/k33/platform/payment/stripe/StripeClient.kt | 13 ++++++++++++- .../k33/platform/payment/stripe/StripeWebhook.kt | 12 +++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeClient.kt b/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeClient.kt index 8d3b985..9ff51da 100644 --- a/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeClient.kt +++ b/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeClient.kt @@ -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 @@ -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 by loadConfig( + "researchApp", + "apps.research.products", + ) + /** * [Stripe API - Create Checkout Session](https://stripe.com/docs/api/checkout/sessions/create) */ @@ -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 diff --git a/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeWebhook.kt b/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeWebhook.kt index 122948d..1dea26f 100644 --- a/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeWebhook.kt +++ b/libs/services/payment/stripe/src/main/kotlin/com/k33/platform/payment/stripe/StripeWebhook.kt @@ -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 @@ -32,10 +32,12 @@ fun Application.module() { val endpointSecret by lazy { System.getenv("STRIPE_WEBHOOK_ENDPOINT_SECRET") } - val productMap by loadConfig>( - "researchApp", - "apps.research.products", - ) + val productMap: Map by lazy { + loadConfigEager>( + "researchApp", + "apps.research.products", + ).values.associateBy { it.stripeProductId } + } val authService by lazy { FirebaseAuthService } val emailService by getEmailService()