Reputation: 3964
Since Stripe.Net
doesn't work anymore in the PCL side, we need to use DependencyServices<>
. However, when I'm trying to generate a token from credit card information, it seems like... It's missing in the doc, I just can't find anything on the web or in the doc, is that normal?
I would like to achieve something like that :
public string CardToToken()
{
var card = new Card()
{
Number = "4242424242424242",
ExpiryMonth = 12,
ExpiryYear = 16,
CVC = 123
};
try
{
token = await Stripe.CreateToken(card);
}
catch (Exception ex)
{
return null;
}
return token;
}
So then I can just simply send it to my server. Any idea to achieve something that easy? That's the last point I need to finish for my project...
This example is in my Android side.
Thank for any help...
Upvotes: 1
Views: 653
Reputation: 3964
I found a good workaround (only Android at the moment but I will update the answer for UWP soon).
Step 1
Stripe.Net
over each subplatforms, and I mean by that Android, iOS, UWP, not the PCL part (the code you share).Step 2
The infos you will get should be stored in that class, I did like that, you can achieve it by another way, of course.
In your PCL, declare a CreditCard.cs:
public class CreditCard
{
public string Numbers { get; set; }
public string HolderName { get; set; }
public string Month { get; set; }
public string Year { get; set; }
public string Cvc { get; set; }
/// <summary>
/// Initializes a new instance of the CreditCard class.
/// </summary>
public CreditCard()
{
Numbers = "";
Month = "";
Year = "";
Cvc = "";
HolderName = "";
}
/// <summary>
/// Verifies the credit card info.
/// However, if the data provided aren't matching an existing card,
/// it will still return `true` since that function only checks the basic template of a credit card data.
/// </summary>
/// <returns>True if the card data match the basic card information. False otherwise</returns>
public bool VerifyCreditCardInfo()
{
if (Numbers == ""
|| Month == ""
|| Year == ""
|| Cvc == ""
|| HolderName == "")
return false;
try
{
int month = 0;
int year = 0;
int cvc = 0;
if (!Int32.TryParse(Month, out month)
|| !Int32.TryParse(Year, out year)
|| !Int32.TryParse(Year, out cvc))
return false;
if (month < 1 || month > 12)
return false;
else if (year < 1990 || year > new DateTime().Year)
return false;
else if (Cvc.Length != 3)
return false;
}
catch (Exception) { return false; }
return true;
}
}
Step 3
DependencyServices<>
. So we nee an interface IStripeServices
in the shared code (PCL), and a service that inherit of it, in the subplatform.In your PCL, declare a IStripeServices
public interface IStripeServices
{
string CardToToken(CreditCard creditCard);
}
Android: Create a StripeServices class like that:
public class StripeServices : IStripeServices
{
public string CardToToken(CreditCard creditCard)
{
var stripeTokenCreateOptions = new StripeTokenCreateOptions
{
Card = new StripeCreditCardOptions
{
Number = creditCard.Numbers,
ExpirationMonth = Int32.Parse(creditCard.Month),
ExpirationYear = Int32.Parse(creditCard.Year),
Cvc = creditCard.Cvc,
Name = creditCard.HolderName
}
};
var tokenService = new StripeTokenService();
var stripeToken = tokenService.Create(stripeTokenCreateOptions);
return stripeToken.Id;
}
}
Step 4
You can now generate a Stripe token from your credit card just by using this piece of code in your shared code (PCL)
if (CreditCardData.VerifyCreditCardInfo())
string cardToken = DependencyService.Get<IStripeServices>().CardToToken(CreditCardData);
else
Debug.WriteLine("The information are either missing or wrong.");
I hope this answer will help, I will try create a solution soon on a public github repo for people who wants to test it
Upvotes: 2