Пример #1
0
        //
        // POST api/stripe/webhooks

        public async Task <HttpResponseMessage> Post(StripeWebhookEvent stripeEvent)
        {
            if (string.IsNullOrEmpty(stripeEvent.Id))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.BadRequest));
            }

            await _billingEventHandler.HandleEventAsync(stripeEvent.Id);

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
        protected StripeWebhookEvent GetWebhookStripeEvent(HttpRequest request, string webhookSecret)
        {
            StripeWebhookEvent stripeEvent = null;

            if (HttpContext.Current.Items["TC_StripeEvent"] != null)
            {
                stripeEvent = (StripeWebhookEvent)HttpContext.Current.Items["TC_StripeEvent"];
            }
            else
            {
                try
                {
                    if (request.InputStream.CanSeek)
                    {
                        request.InputStream.Seek(0, SeekOrigin.Begin);
                    }

                    using (StreamReader reader = new StreamReader(request.InputStream))
                    {
                        var json = reader.ReadToEnd();

                        // Just validate the webhook signature
                        EventUtility.ValidateSignature(json, request.Headers["Stripe-Signature"], webhookSecret);

                        // 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 "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["TC_StripeEvent"] = stripeEvent;
                    }
                }
                catch (Exception exp)
                {
                    LoggingService.Instance.Error <BaseStripeProvider>("BaseStripeProvider - GetWebhookStripeEvent", exp);
                }
            }

            return(stripeEvent);
        }