Created
March 2, 2022 20:07
-
-
Save sethdorris/a406cc2b29b70048dfccbab29fcbffe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Stripe; | |
using hdl.Infrastructure.Interfaces; | |
using Microsoft.Extensions.Configuration; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace hdl.Infrastructure.Services | |
{ | |
public class PaymentService : IPayment | |
{ | |
private readonly IConfiguration _config; | |
public bool UseSandbox { get; set; } | |
public string ApiKey | |
{ | |
get; | |
set; | |
} | |
public PaymentService(IConfiguration config) | |
{ | |
_config = config; | |
if (string.IsNullOrWhiteSpace(ApiKey)) | |
{ | |
var env = System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | |
if (env.Equals("Production", StringComparison.CurrentCultureIgnoreCase)) | |
{ | |
ApiKey = System.Environment.GetEnvironmentVariable("StripeConfig:ApiKey"); | |
} | |
else if (env.Equals("Testing", StringComparison.CurrentCultureIgnoreCase)) | |
{ | |
ApiKey = System.Environment.GetEnvironmentVariable("TestStripeConfig:ApiKey"); | |
UseSandbox = true; | |
} | |
else | |
{ | |
ApiKey = config["StripeConfig:ApiKey"]; | |
UseSandbox = true; | |
} | |
StripeConfiguration.ApiKey = ApiKey; | |
} | |
} | |
public async Task<PaymentIntent> CreatePaymentIntent(long amount, string gamertag, string email) | |
{ | |
var options = new PaymentIntentCreateOptions | |
{ | |
Amount = amount * 100, | |
Currency = "usd", | |
ReceiptEmail = email, | |
Description = $"Event registration fee for {gamertag}", | |
Metadata = UseSandbox ? | |
new Dictionary<string, string> | |
{ | |
{ "integration_check", "accept_a_payment" } | |
} | |
: null | |
}; | |
var service = new PaymentIntentService(); | |
return await service.CreateAsync(options); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment