public void Index()
        {
            var json = new StreamReader(HttpContext.Request.Body).ReadToEnd();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    //handlePaymentIntentSucceeded(paymentIntent);
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                    //handlePaymentMethodAttached(paymentMethod);
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                }
            }
            catch (StripeException e)
            {
            }
        }
Exemple #2
0
        public static async Task Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
            HttpRequestMessage req,
            [Queue("success-charges", Connection = "AzureWebJobsStorage")] IAsyncCollector <Transaction> queue,
            ILogger log)
        {
            log.LogInformation("OnSuccessCharge HTTP trigger function processed a request.");

            var jsonEvent = await req.Content.ReadAsStringAsync();

            var @event = EventUtility.ParseEvent(jsonEvent);

            var charge = @event.Data.Object as Charge;
            var card   = charge.Source as Card;

            var transaction = new Transaction
            {
                Id               = Guid.NewGuid().ToString(),
                ChargeId         = charge.Id,
                Amount           = charge.Amount,
                Currency         = charge.Currency,
                DateCreated      = charge.Created,
                StripeCustomerId = charge.CustomerId,
                CustomerEmail    = card.Name,
                CardType         = card.Brand,
                CustomerId       = int.Parse(charge.Metadata["id"]),
                CustomerName     = charge.Metadata["name"],
                Product          = charge.Metadata["product"]
            };

            await queue.AddAsync(transaction);
        }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    Console.WriteLine(paymentIntent);
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                    Console.WriteLine(paymentMethod);
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                    return(BadRequest());
                }
                return(Ok());
            }
            catch (StripeException e)
            {
                return(BadRequest());
            }
        }
Exemple #4
0
        protected Event GetStripeEvent(HttpRequest request)
        {
            Event stripeEvent = null;

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

                    using (StreamReader reader = new StreamReader(request.InputStream))
                    {
                        stripeEvent = EventUtility.ParseEvent(reader.ReadToEnd());

                        HttpContext.Current.Items["TC_StripeEvent"] = stripeEvent;
                    }
                }
                catch
                {
                }
            }

            return(stripeEvent);
        }
Exemple #5
0
        public string GetEventType(string json)
        {
            var stripeEvent     = EventUtility.ParseEvent(json);
            var stripeEventType = stripeEvent.Type;

            return(stripeEventType);
        }
Exemple #6
0
        public void ThrowsOnApiVersionMismatch()
        {
            var exception = Assert.Throws <StripeException>(() =>
                                                            EventUtility.ParseEvent(this.json));

            Assert.Contains("Received event with API version 2017-05-25", exception.Message);
        }
Exemple #7
0
        public async Task <ActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.InputStream).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    Console.WriteLine("PaymentIntent was successful!");
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                    Console.WriteLine("PaymentMethod was attached to a Customer!");
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (StripeException)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
        }
Exemple #8
0
        public async Task <IActionResult> ListenStripeEvents()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                if (stripeEvent.Type.Contains(TEST_TYPES_SUBSCRIPTION))
                {
                    var eventResult = stripeEvent.Data.Object as Subscription;
                    _logger.LogInformation("Subscription event: {0}, Type: {1} Status: {2}", stripeEvent.Id, stripeEvent.Type, eventResult.Status);
                }
                else if (stripeEvent.Type.Contains(TEST_TYPES_INVOICE))
                {
                    var eventResult = stripeEvent.Data.Object as Invoice;
                    _logger.LogInformation("Invoice event: {0}, Type: {1} Status: {2}", stripeEvent.Id, stripeEvent.Type, eventResult.Status);
                }
                else
                {
                    return(BadRequest());
                }

                return(Ok());
            }
            catch (StripeException e)
            {
                return(BadRequest());
            }
        }
Exemple #9
0
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    // Then define and call a method to handle the successful payment intent.
                    // handlePaymentIntentSucceeded(paymentIntent);
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                    // Then define and call a method to handle the successful attachment of a PaymentMethod.
                    // handlePaymentMethodAttached(paymentMethod);
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                    return((IActionResult)BadRequest());
                }
                return((IActionResult)Ok());
            }
            catch (StripeException e)
            {
                return((IActionResult)BadRequest());
            }
        }
Exemple #10
0
    public string GetBillingReason(string json)
    {
        var stripeEvent = EventUtility.ParseEvent(json);
        var invoice     = stripeEvent.Data.Object as Invoice;

        var billingReason = invoice !.BillingReason;

        return(billingReason);
    }
Exemple #11
0
    public string GetSubscriptionId(string json)
    {
        var stripeEvent = EventUtility.ParseEvent(json);
        var invoice     = stripeEvent.Data.Object as Invoice;

        var subscriptionId = invoice !.Subscription.Id;

        return(subscriptionId);
    }
Exemple #12
0
    public decimal GetPaymentAmount(string json)
    {
        var stripeEvent = EventUtility.ParseEvent(json);
        var invoice     = stripeEvent.Data.Object as Invoice;

        var amount = (decimal)invoice !.Total;

        return(amount);
    }
Exemple #13
0
    public string GetCustomerId(string json)
    {
        var stripeEvent = EventUtility.ParseEvent(json);
        var invoice     = stripeEvent.Data.Object as Invoice;

        var customerId = invoice !.Customer.Id;

        return(customerId);
    }
    public PaymentHandlerEvent FromJson(string json)
    {
        var    stripeEvent     = EventUtility.ParseEvent(json);
        string stripeEventType = stripeEvent.Type;
        string invoiceId       = GetInvoiceId(stripeEvent);
        string subscriptionId  = GetSubscriptionId(stripeEvent);

        return(new PaymentHandlerEvent(stripeEventType, invoiceId, subscriptionId));
    }
Exemple #15
0
        public void AcceptsExpectedApiVersion()
        {
            var evt = Event.FromJson(this.json);

            evt.ApiVersion = StripeConfiguration.ApiVersion;
            var serialized = evt.ToJson();

            evt = EventUtility.ParseEvent(serialized);
            Assert.Equal(StripeConfiguration.ApiVersion, evt.ApiVersion);
        }
Exemple #16
0
    public void ParseJsonToSubscriptionId()
    {
        string json        = System.IO.File.ReadAllText(_jsonFile);
        var    stripeEvent = EventUtility.ParseEvent(json);

        var    invoice        = (Invoice)stripeEvent.Data.Object;
        string subscriptionId = invoice.SubscriptionId;

        Assert.Equal("sub_K1hzaxOt9gb2TB", subscriptionId);
    }
        public async Task <ActionResult> Capture()
        {
            Logger.LogInformation("Capturing webhook");

            var json        = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
            var stripeEvent = EventUtility.ParseEvent(json);

            await TransactionService.CaptureIntentHook(stripeEvent);

            return(Ok());
        }
Exemple #18
0
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            Log.Information(json);
            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);
                if (stripeEvent.Type == Stripe.Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    if (paymentIntent.Metadata.Count == 2)
                    {
                        var e = new SignUpEventSuccessEvent()
                        {
                            UserId  = new Guid(paymentIntent.Metadata["UserId"]),
                            EventId = new Guid(paymentIntent.Metadata["EventId"])
                        };
                        Log.Information("User: "******" signed up to eventId: " + e.EventId);
                        await _eventService.SaveEventAndDbContextChangesAsync(e);

                        await _eventService.PublishEventAsync(e);
                    }
                }
                else if (stripeEvent.Type == Stripe.Events.CustomerSubscriptionCreated)
                {
                    var sub = stripeEvent.Data.Object as Subscription;
                    if (sub.Metadata.Count == 2)
                    {
                        var e = new SignUpSubscriptionSuccessEvent()
                        {
                            UserId             = new Guid(sub.Metadata["UserId"]),
                            ClubSubscriptionId = new Guid(sub.Metadata["ClubSubscriptionId"])
                        };
                        Log.Information("User: "******" signed up to subscription: " + e.ClubSubscriptionId);
                        await _eventService.SaveEventAndDbContextChangesAsync(e);

                        await _eventService.PublishEventAsync(e);
                    }
                }
                else
                {
                    Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
                }
                return(Ok());
            }
            catch
            {
            }

            return(BadRequest());
        }
Exemple #19
0
        public string GetSubscriptionId(string json)
        {
            var stripeEvent    = EventUtility.ParseEvent(json);
            var subscriptionId = "";

            if (stripeEvent.Type.Contains("subscription"))
            {
                var subscription = stripeEvent.Data.Object as Subscription;

                subscriptionId = subscription !.Id;
            }
            return(subscriptionId);
        }
Exemple #20
0
        public string GetInvoiceId(string json)
        {
            var stripeEvent = EventUtility.ParseEvent(json);
            var invoiceId   = "";

            if (stripeEvent.Type.StartsWith("invoice"))
            {
                var invoice = stripeEvent.Data.Object as Invoice;

                invoiceId = invoice !.Id;
            }
            return(invoiceId);
        }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    Console.WriteLine("PaymentIntent was successful!");
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as PaymentMethod;
                    Console.WriteLine("PaymentMethod was attached to a Customer!");
                }
                else if (stripeEvent.Type == Events.ChargeSucceeded)
                {
                    Charge charge         = stripeEvent.Data.Object as Charge;
                    short  subscriptionId = _LoveMirroringcontext.Subscriptions.
                                            Where(s => s.SubscriptionPrice == (charge.Amount / 100)).
                                            Select(s => s.SubscriptionId).
                                            SingleOrDefault();

                    UserSubscription subscription = new UserSubscription {
                        UserId = charge.Metadata["UserId"],
                        UserSubscriptionsAmount = charge.Amount / 100,
                        UserSubscriptionsDate   = DateTime.Now,
                        SubscriptionsId         = subscriptionId
                    };

                    _LoveMirroringcontext.UserSubscriptions.Add(subscription);
                    await _LoveMirroringcontext.SaveChangesAsync();
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                    return(BadRequest());
                }

                return(Ok());
            }
            catch (StripeException e)
            {
                return(BadRequest());
            }
        }
Exemple #22
0
        public void AcceptsExpectedApiVersion()
        {
            var origApiVersion = StripeConfiguration.StripeApiVersion;

            try
            {
                StripeConfiguration.StripeApiVersion = "2017-05-25";
                var evt = EventUtility.ParseEvent(this.json);
                Assert.Equal("2017-05-25", evt.ApiVersion);
            }
            finally
            {
                StripeConfiguration.StripeApiVersion = origApiVersion;
            }
        }
Exemple #23
0
        public async Task <IActionResult> UpdateOrderAfterStripeResponse(
            )
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    var updateOrder   = await _orderService
                                        .GetByPaymentIntentIdAsync(paymentIntent.Id);

                    if (updateOrder == null)
                    {
                        return(BadRequest());
                    }
                    var isChangeStatusSucceeded = await _orderService
                                                  .ChangeOrderToPayed(updateOrder);

                    if (!isChangeStatusSucceeded)
                    {
                        return(BadRequest());
                    }

                    return(Ok());
                }
                else if (stripeEvent.Type == Events.PaymentMethodAttached)
                {
                    var paymentMethod = stripeEvent.Data.Object as Stripe.PaymentMethod;
                }
                // ... handle other event types
                else
                {
                    // Unexpected event type
                    return(BadRequest());
                }

                return(Ok());
            }
            catch (StripeException)
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> StripePaymentIntentWebHook()
        {
            var json           = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
            var endpointSecret = _config.GetSection("Stripe")["EndpointSecret"];

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                var signatureHeader = Request.Headers["Stripe-Signature"];
                stripeEvent = EventUtility.ConstructEvent(json, signatureHeader, endpointSecret);

                // Handle the event
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    // Then define and call a method to handle the successful payment intent. handlePaymentIntentSucceeded(paymentIntent);
                    await _paymentService.HandlePaymentIntentSucceeded(paymentIntent);
                }
                else if (stripeEvent.Type == Events.PaymentIntentPaymentFailed)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    await _paymentService.HandlePaymentIntentFailed(paymentIntent);
                }
                else if (stripeEvent.Type == Events.PaymentIntentCanceled)
                {
                    var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                    await _paymentService.HandlePaymentIntentCanceled(paymentIntent);
                }
                else
                {
                    // Unexpected event type
                    Console.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
                }
                return(Ok());
            }
            catch (StripeException e)
            {
                Console.WriteLine(e.Message);
                return(BadRequest());
            }
        }
Exemple #25
0
        public async Task <IActionResult> StripeHookAsync()
        {
            var json = await new StreamReader(HttpContext.Request.Body)
                       .ReadToEndAsync();

            var stripeEvent = EventUtility.ParseEvent(json);

            switch (stripeEvent.Type)
            {
            case Events.PaymentIntentSucceeded:
                await _paymentService.SendEmailAsync(stripeEvent);

                break;

            default:
                return(BadRequest());
            }

            return(Ok());
        }
    // TODO: Add tests
    public string GetSubscriptionId(string json)
    {
        var stripeEvent = EventUtility.ParseEvent(json);

        var invoice = stripeEvent.Data.Object as Invoice;

        if (invoice != null)
        {
            return(invoice.SubscriptionId);
        }

        var subscription = stripeEvent.Data.Object as Subscription;

        if (subscription != null)
        {
            return(subscription.Id);
        }

        return(string.Empty);
    }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            _webhook.Content = $"{json}";
            await _webhook.Send();

            return(Ok());

            try
            {
                var stripeEvent     = EventUtility.ParseEvent(json);
                var stripeEventType = stripeEvent.Type;

                if (stripeEventType.Equals("customer.subscription.created"))
                {
                    var subscription   = stripeEvent.Data.Object as Stripe.Subscription;
                    var subscriptionId = subscription !.Id;
                    var customerId     = subscription.CustomerId;
                    var email          = _paymentHandlerCustomer.GetCustomerEmail(customerId);

                    Invitation invite = await _newMemberService.CreateInvitationAsync(email, subscriptionId);

                    await _newMemberService.SendRegistrationEmailAsync(invite);
                }
                else
                {
                    _logger.LogError("Unhandled event type: {0}", stripeEvent.Type);
                }
                return(Ok());
            }
            catch (StripeException)
            {
                return(BadRequest());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                var stripeEvent = EventUtility.ParseEvent(json);

                // Handle the event
                if (stripeEvent.Type == Events.CustomerSubscriptionCreated)
                {
                    var _subscription = stripeEvent.Data.Object as Stripe.Subscription;

                    var items = _subscription.Items.Data;
                }

                return(Ok());
            }
            catch (StripeException e)
            {
                return(BadRequest(e.Message));
            }
        }
        public async Task <IActionResult> Index()
        {
            var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();

            try
            {
                // var stripeEvent = EventUtility.ConstructEvent(json,
                //     Request.Headers["Stripe-Signature"], _endpointSecret);
                var stripeEvent = EventUtility.ParseEvent(json, false);

                // Handle the event

                var paymentIntent = stripeEvent.Data.Object as PaymentIntent;
                // Always log all!
                _paymentService.UpdatePayment(new PaymentStatusResponse
                {
                    PaymentId     = paymentIntent.Id,
                    Amount        = paymentIntent.Amount.Value,
                    Created       = paymentIntent.Created,
                    PaymentStatus = paymentIntent.Status,
                    ErrorMessage  = paymentIntent.LastPaymentError?.ToString()
                });
                // Only fullfilment when succes
                if (stripeEvent.Type == Events.PaymentIntentSucceeded)
                {
                    await _voucherService.HandleFulfillment(paymentIntent.Id);
                }

                return(Ok());
            }
            catch (Exception e)
            {
                Log.Error($"{e}");
                return(BadRequest());
            }
        }
Exemple #30
0
        public void CanDisableThrowOnApiVersionMismatch()
        {
            var evt = EventUtility.ParseEvent(this.json, false);

            Assert.Equal("2017-05-25", evt.ApiVersion);
        }