Skip to content

Instantly share code, notes, and snippets.

@sethdorris
Created March 2, 2022 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sethdorris/a406cc2b29b70048dfccbab29fcbffe6 to your computer and use it in GitHub Desktop.
Save sethdorris/a406cc2b29b70048dfccbab29fcbffe6 to your computer and use it in GitHub Desktop.
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