I am currently facing an issue with an update code in test project. Initially when I tested the code it was working; the update was working. I was doing a test run on my project and I found that after a payment has been done, an update which was supposed to take effect did not update. The thing is, after a user is redirected to a payment gateway to purchase token for services rendered, and after successful payment the user is redirected back to a certain page (tokenconfirmation.aspx) in the project and on page load, the user’s wallet is credited with the unit amount paid for. But on the page load event of the tokencofirmation.aspx page the wallet does not update. I don’t know why it is not working again. Here is the code on the page load event that updates the wallet table in the database.
Also, after successful payment, a reference number is sent as QueryString which is used to make the update.
Please how I get this work again as before?
On my browser tab the reference is showing: e.g
http://localhost:54245/tokenconfirmation.aspx?reference=522162531
Namespaces
using RestSharp;
using System;
using System.Net;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Data;
tokenconfirmation.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("http://localhost:54245/login.aspx");
}
else
{
}
VerifyTransaction();
Response.Redirect("http://localhost:54245/dashboard.aspx ");
}
//This event is where the transaction will be verified
private void VerifyTransaction()
{
//This is where it gets the reference number
var tranxRef = Request.QueryString["reference"];
//Calling paystack API for transaction verification
var verifyUrl = "https://api.paystack.co/transaction/verify/" + tranxRef;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var client = new RestClient(verifyUrl);
var request = new RestRequest(Method.GET);
client.AddDefaultHeader("Authorization", "Bearer sk_test_***************************************");
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
var jsonResponse = response.Content;
var verificationResponse = JsonConvert.DeserializeObject<PaymentResponse>(jsonResponse);
if (verificationResponse.status && verificationResponse.data.status == "success")
{
var amountPaid = (verificationResponse.data.amount / 100);
var customerEmail = verificationResponse.data.customer.email;
//update wallet is done here
UpdateWallet(customerEmail, amountPaid);
}
else
{
}
}
else
{
}
}
//This is the update wallet code.
private void UpdateWallet(string email, decimal amount)
{
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
string query = "";
//Checks to see if the account exists
if (AccountExist(email))
{
query = "UPDATE UserWallet SET amount += @amount WHERE email=@email";
}
else
{
query = "INSERT INTO UserWallet (email,amount) VALUES (@email,@amount)";
}
var cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@email", email);
cmd.Parameters.AddWithValue("@amount", amount);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
//Checking the UserWallet Table to see if the account exist.
private bool AccountExist(string email)
{
SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
string query = "SELECT COUNT(*) FROM UserWallet WHERE email=@email";
var cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("@email", email);
conn.Open();
int count = (int)cmd.ExecuteScalar();
return count > 0;
}