public WebhookEndpointServiceTest( StripeMockFixture stripeMockFixture, MockHttpClientFixture mockHttpClientFixture) : base(stripeMockFixture, mockHttpClientFixture) { this.service = new WebhookEndpointService(this.StripeClient); this.createOptions = new WebhookEndpointCreateOptions { EnabledEvents = new List <string> { "charge.succeeded", }, Url = "https://stripe.com", }; this.updateOptions = new WebhookEndpointUpdateOptions { EnabledEvents = new List <string> { "charge.succeeded", }, }; this.listOptions = new WebhookEndpointListOptions { Limit = 1, }; }
public WebhookEndpointServiceTest() { this.service = new WebhookEndpointService(); this.createOptions = new WebhookEndpointCreateOptions() { EnabledEvents = new string[] { "charge.succeeded", }, Url = "https://stripe.com", }; this.updateOptions = new WebhookEndpointUpdateOptions() { EnabledEvents = new string[] { "charge.succeeded", }, }; this.listOptions = new WebhookEndpointListOptions() { Limit = 1, }; }
static void Main(string[] args) { //Set this to a valid test API key. StripeConfiguration.ApiKey = "sk_test_XXX"; Console.WriteLine("Versioning in .NET!"); Console.WriteLine("The .NET library is using API Version: " + StripeConfiguration.ApiVersion); //Create and print a customer to see what the response from library's pinned version looks like. var customerOptions = new CustomerCreateOptions { Email = "*****@*****.**", Description = "Customer created with API version pinned to library", }; var customerService = new CustomerService(); var customer = customerService.Create(customerOptions); Console.WriteLine("Customer created:"); Console.WriteLine(customer); // Retrieve the customer.created event // or look at it in the Dashboard: https://dashboard.stripe.com/test/events // The customer object in the event payload will be based off of the version your // account is set to. var eventOptions = new EventListOptions { Limit = 1 }; var eventService = new EventService(); StripeList <Event> events = eventService.List(eventOptions); Console.WriteLine("customer.created event:"); Console.WriteLine(events.Data[0]); //Create a webhook endpoint and set it's API version. var endpointOptions = new WebhookEndpointCreateOptions { ApiVersion = StripeConfiguration.ApiVersion, Url = "https://example.com/my/webhook/endpoint", EnabledEvents = new List <String> { "customer.created", }, }; var endpointService = new WebhookEndpointService(); endpointService.Create(endpointOptions); customerOptions.Email = "*****@*****.**"; customerOptions.Description = "Customer created using version set on for webhook endpoint"; customer = customerService.Create(customerOptions); // Visit the Dashboard page for the endpoint you just created: // https://dashboard.stripe.com/test/webhooks/we_XXX // Under "Webhook Attempts" you'll see the event data Stripe has sent to the endpoint // for the customer that was just created. // Since we created the endpoint using the 2020-08-27 API version, the customer object // in the payload is using that version. Console.WriteLine("All done, visit https://dashboard.stripe.com/test/webhooks to see what was sent to the endpoint"); }
public async Task <string> Register(string baseUrl) { var options = new WebhookEndpointCreateOptions { Url = $"{baseUrl}/api/StripeWebHook", EnabledEvents = new List <String> { "charge.failed", "charge.succeeded", "invoice.payment_failed", "invoice.payment_succeeded" } }; var service = new WebhookEndpointService(); var webhookEndpoint = await service.CreateAsync(options); return("Ok"); }
public static void EnsureWebhookEndpointFor(Api.Models.PaymentMethod paymentMethod, string[] events) { // Check to see if we have run already in this request for this provider if (HttpContext.Current == null || HttpContext.Current.Items[$"{nameof(BaseStripeProvider)}_{nameof(EnsureWebhookEndpointFor)}_{paymentMethod.Id}"] != null) { return; } // We set a cache item so that this only runs once per request // because when we call .Save() in a moment, it's going to trigger // the Updated event handler again HttpContext.Current.Items[$"{nameof(BaseStripeProvider)}_{nameof(EnsureWebhookEndpointFor)}_{paymentMethod.Id}"] = 1; // Check to see if we have a configured mode var mode = paymentMethod.Settings.SingleOrDefault(x => x.Key == "mode")?.Value; if (string.IsNullOrWhiteSpace(mode)) { return; } // Configure stripe var secretKey = paymentMethod.Settings.SingleOrDefault(x => x.Key == $"{mode}_secret_key")?.Value; if (string.IsNullOrWhiteSpace(secretKey)) { return; } ConfigureStripe(secretKey); // Create the webhook service now as we'll need it a couple of times var service = new WebhookEndpointService(); // Build the webhook URL var req = HttpContext.Current.Request; if (req == null) { return; } var baseUrlSetting = paymentMethod.Settings.SingleOrDefault(x => x.Key == $"{mode}_base_url")?.Value; var baseUrl = !string.IsNullOrWhiteSpace(baseUrlSetting) ? new Uri(baseUrlSetting) : new UriBuilder(req.Url.Scheme, req.Url.Host, req.Url.Port).Uri; var webhookUrl = new Uri(baseUrl, "/base/TC/PaymentCommunicationWithoutOrderId/" + paymentMethod.StoreId + "/" + paymentMethod.PaymentProviderAlias + "/" + paymentMethod.Id + ".aspx").AbsoluteUri; // Check to see if we already have a registered webhook var webhookIdKey = $"{mode}_webhook_id"; var webhookSecretKey = $"{mode}_webhook_secret"; var webhookIdSetting = paymentMethod.Settings.SingleOrDefault(x => x.Key == webhookIdKey); var webhookSecretSetting = paymentMethod.Settings.SingleOrDefault(x => x.Key == webhookSecretKey); var webhookId = webhookIdSetting?.Value; var webhookSecret = webhookSecretSetting?.Value; if (!string.IsNullOrWhiteSpace(webhookId) && !string.IsNullOrWhiteSpace(webhookSecret)) { // We've found some credentials, so lets validate them try { var webhookEndpoint = service.Get(webhookId); if (webhookEndpoint != null && webhookEndpoint.ApiVersion == StripeConfiguration.ApiVersion && webhookEndpoint.Url == webhookUrl) { return; } } catch (StripeException ex) { // Somethings wrong with the webhook so lets keep going and create a new one } } // Create the webhook try { var options = new WebhookEndpointCreateOptions { Url = webhookUrl, EnabledEvents = new List <string>(events), ApiVersion = StripeConfiguration.ApiVersion }; var newWebhookEndpoint = service.Create(options); // Remove settings if they exist (if we got here, it must mean they are invalid in some way) if (webhookIdSetting != null) { paymentMethod.Settings.Remove(webhookIdSetting); } if (webhookSecretSetting != null) { paymentMethod.Settings.Remove(webhookSecretSetting); } // Save the settings paymentMethod.Settings.Add(new Api.Models.PaymentMethodSetting(webhookIdKey, newWebhookEndpoint.Id)); paymentMethod.Settings.Add(new Api.Models.PaymentMethodSetting(webhookSecretKey, newWebhookEndpoint.Secret)); paymentMethod.Save(); } catch (Exception exp) { LoggingService.Instance.Error <BaseStripeProvider>("BaseStripeProvider - EnsureWebhookEndpoint", exp); } }