public void OnStripeSubscriptionEvent(StripeSubscriptionEventArgs args)
 {
     StripeSubscriptionEvent?.Invoke(this, args);
 }
        public override string ProcessRequest(Order order, HttpRequest request, IDictionary <string, string> settings)
        {
            var response = "";

            try
            {
                order.MustNotBeNull("order");
                request.MustNotBeNull("request");
                settings.MustNotBeNull("settings");

                // If in test mode, write out the form data to a text file
                if (settings.ContainsKey("mode") && settings["mode"] == "test")
                {
                    LogRequest <StripeSubscription>(request, logPostData: true);
                }

                // Stripe supports webhooks
                var stripeEvent = GetStripeEvent(request);

                // With subscriptions, Stripe creates an invoice for each payment
                // so to ensure subscription is live, we'll listen for successful invoice payment
                if (stripeEvent.Type.StartsWith("invoice."))
                {
                    var invoice = Mapper <Invoice> .MapFromJson(stripeEvent.Data.Object.ToString());

                    if (order.Properties["stripeCustomerId"] == invoice.CustomerId &&
                        order.Properties["stripeSubscriptionId"] == invoice.SubscriptionId)
                    {
                        var eventArgs = new StripeSubscriptionEventArgs
                        {
                            Order        = order,
                            Subscription = invoice.Subscription,
                            Invoice      = invoice
                        };

                        switch (stripeEvent.Type)
                        {
                        case "invoice.payment_succeeded":
                            if (order.TransactionInformation.PaymentState == PaymentState.Initialized ||
                                order.TransactionInformation.PaymentState == PaymentState.Authorized)
                            {
                                order.TransactionInformation.TransactionId = invoice.ChargeId;
                                order.TransactionInformation.PaymentState  = PaymentState.Captured;
                                order.Save();

                                eventArgs.Type = StripeSubscriptionEventType.SubscriptionStarted;
                            }
                            else
                            {
                                eventArgs.Type = StripeSubscriptionEventType.SubscriptionRenewed;
                            }
                            break;

                        case "invoice.payment_failed":
                            if (!string.IsNullOrWhiteSpace(order.TransactionInformation.TransactionId) &&
                                invoice.Status == "past_due")
                            {
                                eventArgs.Type = StripeSubscriptionEventType.SubscriptionPastDue;
                            }
                            break;

                        case "invoice.upcoming":
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionRenewing;
                            break;
                        }

                        OnStripeSubscriptionEvent(eventArgs);
                    }
                }
                else if (stripeEvent.Type.StartsWith("customer.subscription."))
                {
                    var subscription = Mapper <Subscription> .MapFromJson(stripeEvent.Data.Object.ToString());

                    if (order.Properties["stripeCustomerId"] == subscription.CustomerId &&
                        order.Properties["stripeSubscriptionId"] == subscription.Id)
                    {
                        var eventArgs = new StripeSubscriptionEventArgs
                        {
                            Order        = order,
                            Subscription = subscription
                        };

                        switch (stripeEvent.Type)
                        {
                        case "customer.subscription.trial_will_end":
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionTrialEnding;
                            break;

                        case "customer.subscription.created":
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionCreated;
                            break;

                        case "customer.subscription.updated":
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionUpdated;
                            break;

                        case "customer.subscription.deleted":
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionDeleted;
                            break;
                        }

                        OnStripeSubscriptionEvent(eventArgs);
                    }
                }
            }
            catch (Exception exp)
            {
                LoggingService.Instance.Error <StripeSubscription>("StripeSubscription(" + order.CartNumber + ") - ProcessRequest", exp);
            }

            return(response);
        }
        private string ProcessWebhookRequest(Order order, HttpRequest request, IDictionary <string, string> settings)
        {
            // Stripe supports webhooks
            var webhookSecret = settings[settings["mode"] + "_webhook_secret"];
            var stripeEvent   = GetWebhookStripeEvent(request, webhookSecret);

            // With subscriptions, Stripe creates an invoice for each payment
            // so to ensure subscription is live, we'll listen for successful invoice payment
            if (stripeEvent.Type.StartsWith("invoice."))
            {
                var invoice = (Invoice)stripeEvent.Data.Object;
                if (order.Properties["stripeCustomerId"] == invoice.CustomerId &&
                    order.Properties["stripeSubscriptionId"] == invoice.SubscriptionId)
                {
                    var eventArgs = new StripeSubscriptionEventArgs
                    {
                        Order        = order,
                        Subscription = invoice.Subscription,
                        Invoice      = invoice
                    };

                    switch (stripeEvent.Type)
                    {
                    case "invoice.payment_succeeded":
                        if (order.TransactionInformation.PaymentState != PaymentState.Authorized &&
                            order.TransactionInformation.PaymentState != PaymentState.Captured)
                        {
                            FinalizeOrUpdateOrder(order, invoice);

                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionStarted;
                        }
                        else
                        {
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionRenewed;
                        }
                        break;

                    case "invoice.payment_failed":
                        if (!string.IsNullOrWhiteSpace(order.TransactionInformation.TransactionId) &&
                            invoice.Status == "past_due")
                        {
                            eventArgs.Type = StripeSubscriptionEventType.SubscriptionPastDue;
                        }
                        break;

                    case "invoice.upcoming":
                        eventArgs.Type = StripeSubscriptionEventType.SubscriptionRenewing;
                        break;
                    }

                    OnStripeSubscriptionEvent(eventArgs);
                }
            }
            else if (stripeEvent.Type.StartsWith("customer.subscription."))
            {
                var subscription = (Subscription)stripeEvent.Data.Object;
                if (order.Properties["stripeCustomerId"] == subscription.CustomerId &&
                    order.Properties["stripeSubscriptionId"] == subscription.Id)
                {
                    var eventArgs = new StripeSubscriptionEventArgs
                    {
                        Order        = order,
                        Subscription = subscription
                    };

                    switch (stripeEvent.Type)
                    {
                    case "customer.subscription.trial_will_end":
                        eventArgs.Type = StripeSubscriptionEventType.SubscriptionTrialEnding;
                        break;

                    case "customer.subscription.created":
                        eventArgs.Type = StripeSubscriptionEventType.SubscriptionCreated;
                        break;

                    case "customer.subscription.updated":
                        eventArgs.Type = StripeSubscriptionEventType.SubscriptionUpdated;
                        break;

                    case "customer.subscription.deleted":
                        eventArgs.Type = StripeSubscriptionEventType.SubscriptionDeleted;
                        break;
                    }

                    OnStripeSubscriptionEvent(eventArgs);
                }
            }

            return(null);
        }