#kristiyan_webhooks

1 messages ¡ Page 1 of 1 (latest)

umbral elkBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1245655319460184096

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

tribal gust
#
func (wh *Webhook) Fulfill(w http.ResponseWriter, r *http.Request) {
    const MaxBodyBytes = int64(65536)

    r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)

    body, err := io.ReadAll(r.Body)
    if err != nil {
        internalHttp.SendErrorResponse(w, "failed to parse body", http.StatusServiceUnavailable)
        return
    }

    event, err := webhook.ConstructEvent(body, r.Header.Get("Stripe-Signature"), wh.config.CheckoutWebhookEndpointSecret)
    if err != nil {
        internalHttp.SendErrorResponse(w, fmt.Sprintf("error verifying webhook signature: %v", err), http.StatusBadRequest)
        return
    }

    if event.Type != "checkout.session.completed" {
        internalHttp.SendErrorResponse(w, fmt.Sprintf("unexpected event type: %s", event.Type), http.StatusBadRequest)
        return
    }

    var session stripe.CheckoutSession
    err = json.Unmarshal(event.Data.Raw, &session)
    if err != nil {
        internalHttp.SendErrorResponse(w, "error parsing session data", http.StatusBadRequest)
        return
    }

    productID := session.Metadata["productID"]
    if productID == "" {
        internalHttp.SendErrorResponse(w, "productID not found", http.StatusBadRequest)
        return
    }

    userID := session.Metadata["userID"]
    if userID == "" {
        internalHttp.SendErrorResponse(w, "userID not found", http.StatusBadRequest)
        return
    }

    dto := &checkout.WebhookDTO{
        TransactionID: session.PaymentIntent.ID,
        UserID:        userID,
        ProductID:     productID,
    }

    // Respond with 200 status code immediately
    w.WriteHeader(http.StatusOK)

    // Create a new background context
    bgCtx := context.Background()

    // Process the webhook logic asynchronously
    go func() {
        err := wh.service.Process(bgCtx, dto)
        if err != nil {
            // Log the error
            fmt.Printf("Error processing webhook: %v\n", err)
        }
    }()
}
crude mica
#

Hi
This is really specific to your own integration framework, it's not really related to Stripe APIs. I encourage you narrow this with the community of the framework you are using.