protected StripeWebhookEvent GetWebhookStripeEvent(HttpRequestBase request, string webhookSigningSecret) { StripeWebhookEvent stripeEvent = null; if (HttpContext.Current.Items["Vendr_StripeEvent"] != null) { stripeEvent = (StripeWebhookEvent)HttpContext.Current.Items["Vendr_StripeEvent"]; } else { try { if (request.InputStream.CanSeek) { request.InputStream.Seek(0, SeekOrigin.Begin); } using (var reader = new StreamReader(request.InputStream)) { var json = reader.ReadToEnd(); // Just validate the webhook signature EventUtility.ValidateSignature(json, request.Headers["Stripe-Signature"], webhookSigningSecret); // Parse the event ourselves to our custom webhook event model // as it only captures minimal object information. stripeEvent = JsonConvert.DeserializeObject <StripeWebhookEvent>(json); // We manually fetch the event object type ourself as it means it will be fetched // using the same API version as the payment providers is coded against. // NB: Only supports a number of object types we are likely to be interested in. if (stripeEvent?.Data?.Object != null) { switch (stripeEvent.Data.Object.Type) { case "checkout.session": var sessionService = new SessionService(); stripeEvent.Data.Object.Instance = sessionService.Get(stripeEvent.Data.Object.Id); break; case "charge": var chargeService = new ChargeService(); stripeEvent.Data.Object.Instance = chargeService.Get(stripeEvent.Data.Object.Id); break; case "payment_intent": var paymentIntentService = new PaymentIntentService(); stripeEvent.Data.Object.Instance = paymentIntentService.Get(stripeEvent.Data.Object.Id); break; case "subscription": var subscriptionService = new SubscriptionService(); stripeEvent.Data.Object.Instance = subscriptionService.Get(stripeEvent.Data.Object.Id); break; case "invoice": var invoiceService = new InvoiceService(); stripeEvent.Data.Object.Instance = invoiceService.Get(stripeEvent.Data.Object.Id); break; } } HttpContext.Current.Items["Vendr_StripeEvent"] = stripeEvent; } } catch (Exception ex) { Vendr.Log.Error <StripePaymentProviderBase <TSettings> >(ex, "Stripe - GetWebhookStripeEvent"); } } return(stripeEvent); }
protected async Task <StripeWebhookEvent> GetWebhookStripeEventAsync(PaymentProviderContext <TSettings> ctx, string webhookSigningSecret) { StripeWebhookEvent stripeEvent = null; if (ctx.AdditionalData.ContainsKey("Vendr_StripeEvent")) { stripeEvent = (StripeWebhookEvent)ctx.AdditionalData["Vendr_StripeEvent"]; } else { try { var json = await ctx.Request.Content.ReadAsStringAsync(); var stripeSignature = ctx.Request.Headers.GetValues("Stripe-Signature").FirstOrDefault(); // Just validate the webhook signature EventUtility.ValidateSignature(json, stripeSignature, webhookSigningSecret); // Parse the event ourselves to our custom webhook event model // as it only captures minimal object information. stripeEvent = JsonConvert.DeserializeObject <StripeWebhookEvent>(json); // We manually fetch the event object type ourself as it means it will be fetched // using the same API version as the payment providers is coded against. // NB: Only supports a number of object types we are likely to be interested in. if (stripeEvent?.Data?.Object != null) { switch (stripeEvent.Data.Object.Type) { case "checkout.session": var sessionService = new SessionService(); stripeEvent.Data.Object.Instance = sessionService.Get(stripeEvent.Data.Object.Id); break; case "charge": var chargeService = new ChargeService(); stripeEvent.Data.Object.Instance = chargeService.Get(stripeEvent.Data.Object.Id); break; case "payment_intent": var paymentIntentService = new PaymentIntentService(); stripeEvent.Data.Object.Instance = paymentIntentService.Get(stripeEvent.Data.Object.Id); break; case "subscription": var subscriptionService = new SubscriptionService(); stripeEvent.Data.Object.Instance = subscriptionService.Get(stripeEvent.Data.Object.Id); break; case "invoice": var invoiceService = new InvoiceService(); stripeEvent.Data.Object.Instance = invoiceService.Get(stripeEvent.Data.Object.Id); break; case "review": var reviewService = new ReviewService(); stripeEvent.Data.Object.Instance = reviewService.Get(stripeEvent.Data.Object.Id); break; } } ctx.AdditionalData.Add("Vendr_StripeEvent", stripeEvent); } catch (Exception ex) { _logger.Error(ex, "Stripe - GetWebhookStripeEvent"); } } return(stripeEvent); }