Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #166 - Using Stripe tokenization to send credit card information for processing #167

Merged
merged 1 commit into from
May 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions Libraries/Hotcakes.Payment/Gateways/StripeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,40 @@ public override MethodSettings BaseSettings
get { return Settings; }
}

/// <summary>
/// Creates the token required by Stripe in order to send credit card information to processed for a transaction
/// </summary>
/// <param name="t">The transaction object</param>
/// <returns>A StripeToken object that can be associated with the charge that is sent to Stripe</returns>
private StripeToken CreateCardToken(Transaction t)
{
var tokenOptions = new StripeTokenCreateOptions()
{
Card = new StripeCreditCardOptions()
{
Number = t.Card.CardNumber,
ExpirationYear = t.Card.ExpirationYear.ToString(),
ExpirationMonth = t.Card.ExpirationMonthPadded,
}
};

if (!string.IsNullOrEmpty(t.Card.SecurityCode))
{
tokenOptions.Card.Cvc = t.Card.SecurityCode; // optional
}

if (t.Customer.Street.Length > 0)
tokenOptions.Card.AddressLine1 = t.Customer.Street; // optional

if (t.Customer.PostalCode.Length > 0)
tokenOptions.Card.AddressZip = t.Customer.PostalCode; // optional
tokenOptions.Card.Name = t.Card.CardHolderName;

var tokenService = new StripeTokenService();
StripeToken stripeToken = tokenService.Create(tokenOptions);
return stripeToken;
}

/// <summary>
/// Creates the charge.
/// </summary>
Expand All @@ -79,21 +113,16 @@ private void CreateCharge(Transaction t, bool capture)
chargeOptions.Source = new StripeSourceOptions();

// set these properties if using a card
chargeOptions.Source.Number = t.Card.CardNumber;
chargeOptions.Source.ExpirationYear = t.Card.ExpirationYear.ToString();
chargeOptions.Source.ExpirationMonth = t.Card.ExpirationMonthPadded;
//set the charge token
var chargeToken = CreateCardToken(t);

chargeOptions.Source.TokenId = chargeToken.Id;

//myCharge.CardAddressCountry = "US"; // optional
if (t.Customer.Street.Length > 0)
chargeOptions.Source.AddressLine1 = t.Customer.Street; // optional
//myCharge.CardAddressLine2 = "Apt 24"; // optional
//myCharge.CardAddressState = "NC"; // optional
if (t.Customer.PostalCode.Length > 0)
chargeOptions.Source.AddressZip = t.Customer.PostalCode; // optional
chargeOptions.Source.Name = t.Card.CardHolderName; // optional
if (!string.IsNullOrEmpty(t.Card.SecurityCode))
{
chargeOptions.Source.Cvc = t.Card.SecurityCode; // optional
}

/* chargeOptions.Source.Name = t.Card.CardHolderName;*/ // optional

// set this property if using a customer
//myCharge.CustomerId = *customerId*;
Expand Down