diff --git a/Libraries/Hotcakes.Payment/Gateways/StripeProcessor.cs b/Libraries/Hotcakes.Payment/Gateways/StripeProcessor.cs index 6dcef0a37..7c85a07bb 100644 --- a/Libraries/Hotcakes.Payment/Gateways/StripeProcessor.cs +++ b/Libraries/Hotcakes.Payment/Gateways/StripeProcessor.cs @@ -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> @@ -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*;