ERROR: Update code for wallet table suddenly stops working

Donald Symmons 2,856 Reputation points
2022-11-14T08:45:00.517+00:00

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

using RestSharp;  
using System;  
using System.Net;  
using Newtonsoft.Json;  
using System.Data.SqlClient;  
using System.Data;  

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;  
}  
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,246 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Donald Symmons 2,856 Reputation points
    2022-11-21T01:07:20.067+00:00

    I finally got it to work. Since I used webhook to redirect to url after successful payment, the ID(int) Datatype for paymentresponse was changed from int to long and it worked

    0 comments No comments