protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
#region Paramert Binding
var email = txtEmailId.Text != null ? txtEmailId.Text : null;
var firstname = txtFirstName.Text != null ? txtFirstName.Text : null;
var Lastname = txtLastName.Text != null ? txtLastName.Text : null;
var country = txtCountry.Text != null ? txtCountry.Text : null;
var state = txtState.Text != null ? txtState.Text : null;
var city = txtCity.Text != null ? txtCity.Text : null;
var Purpose = txtPurposeDonation.Text != null ? txtPurposeDonation.Text : null;
var phone = txtCellPhoneNo.Text != null ? txtCellPhoneNo.Text : null;
var donation = txtDonationAmount.Text != null ? long.Parse(txtDonationAmount.Text) : 0;
var ZipCode = txtZipcode.Text != null ? txtZipcode.Text : null;
var Address1 = txtAddress1.Text != null ? txtAddress1.Text : null;
var Address2 = txtAddress2.Text != null ? txtAddress2.Text : null;
var cardOnName = txtCardName.Text;
var cardNumber = txtCard.Text;
var cardMonth = txtExp.Text != null ? long.Parse(txtExp.Text) : 0;
var cardYear = txtYear.Text != null ? long.Parse(txtYear.Text) : 0;
var cardCVV = txtCV.Text;
#endregion
#region Stripe Payment
string Strieppublickey = "";
string striepSecretKey = "";
Strieppublickey = ConfigurationManager.AppSettings["StripePublishKey"].ToString();
striepSecretKey = ConfigurationManager.AppSettings["StripeSecretKey"].ToString();
Stripe.StripeConfiguration.SetApiKey(Strieppublickey);
Stripe.StripeConfiguration.ApiKey = striepSecretKey;
#region Strip token
CreditCardOptions card = new Stripe.CreditCardOptions();
card.Name = cardOnName;
card.Number = cardNumber;
card.ExpYear = cardYear;
card.ExpMonth = cardMonth;
card.Cvc = cardCVV;
//Assign Card to Token Object and create Token
Stripe.TokenCreateOptions token = new Stripe.TokenCreateOptions();
token.Card = card;
Stripe.TokenService serviceToken = new Stripe.TokenService();
Stripe.Token newToken = serviceToken.Create(token);
#endregion
#region Customer Service
Stripe.CustomerCreateOptions myCustomer = new Stripe.CustomerCreateOptions();
myCustomer.Email = email;
myCustomer.Source = newToken.Id;
myCustomer.Address = new AddressOptions
{
Line1 = Address1 + "" + Address2,
PostalCode = ZipCode,
City = city,
State = state,
Country = country,
};
var customerService = new Stripe.CustomerService();
Stripe.Customer stripeCustomer = customerService.Create(myCustomer);
#endregion
var myCharge = new Stripe.ChargeCreateOptions
{
Amount = Convert.ToInt32(txtDonationAmount.Text) * 100,
Currency = "USD",
ReceiptEmail = email,
Description = "Donation agaist " + Purpose,
Customer = stripeCustomer.Id,
Capture = true,
};
var chargeService = new Stripe.ChargeService();
Charge stripeCharge = chargeService.Create(myCharge);
#endregion
if (stripeCharge.Status == "succeeded")
{
//After payment, you can do Database operation here
Response.Redirect("ThankYou.aspx");//redirect to thank you page
}
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}