Below is a simplified version of a sign-up page I’m working on.
When the user clicks the button I need to collect money from a credit card. Then, if the transaction is successful, create the new user account.
My problem is handling the scenario where the credit card transaction succeeds but then the create-user transaction fails. If the user needs to correct some information in the form I don’t want to charge their card again.
My PaymentStatus variable contains a 1 or a 0 depending on whether or not the transaction succeeds.
Will this persist across multiple button clicks? And if it does, is this a reliable way of avoiding duplicate transactions in scenarios where the user needs to correct some data in the form?
I could persist the information to the database, but my preference would be to do that later in the transaction when there exists a UserID to associate the payment with.
protected void btn_Click(object sender, ImageClickEventArgs e)
{
int PaymentStatus = ChargeCardForSignUpFee(userInfo);
if (PaymentStatus == 0)
{
Label1.Text = "Your credit card is invalid. Please try again.";
}
else
{
try
{
MembershipUser newUser = Membership.CreateUser(userInfo);
if (newUser == null)
{
lblStatus.Text = GetErrorMessage(status);
}
else
{
CreateCustomer(userInfo);
SendWelcomeEmail(userInfo.email);
FormsAuthentication.SetAuthCookie(userName, true);
Response.Redirect("welcomepage.aspx");
}
}
catch
{
lblStatus.Text = "An error occurred while creating your account. "+
"Please check your information and try again. "+
ex.Message;
}
}
}
Validate the data first, then charge the card. It doesn’t make sense to charge the card before validating the data.
Also, save the data to the database before charging the card.