How to charge a customer in Stripe using Stripe.net C# library

Note: In a production environment, you’ll have to write more code to see if the credit card is expired or not
You’ll also have to know what credit card to charge (some customers have more than one credit card).

 

StripeConfiguration.SetApiKey(private_key);

// Variables
string cardId = string.Empty;

// Retrieve the customer
var customerService = new StripeCustomerService();
StripeCustomer stripeCustomer = customerService.Get("cus_AZEhoCpoCVFiNK");

// Go through the customers to see if we have the credit card on file.
StripeList<Source> source = stripeCustomer.Sources;
if (source != null)
{
foreach (var dt in source.Data)
{
if (dt == null)
continue;

if (dt.Card != null)
{
// See if they have a card. Note: you'll probably have to write some logic to check if the card is expired or not
cardId = dt.Card.Id;
break;
}
}
}

StripeChargeCreateOptions chargeOpt = new StripeChargeCreateOptions()
{
Amount = 25000
,
Currency = "USD"
,
Description = "Gift Card Purchase"
,
ReceiptEmail = "myemail@mailinator.com"
,
CustomerId = stripeCustomer.Id
,
SourceTokenOrExistingSourceId = cardId
};

StripeChargeService chargeSVC = new StripeChargeService();
var charge = chargeSVC.Create(chargeOpt);



 

 

Leave a Comment