question

Donald-0878 avatar image
0 Votes"
Donald-0878 asked Donald-0878 answered

ERROR: Update code for wallet table suddenly stops working

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;
 }



dotnet-csharpdotnet-aspnet-general
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code ignores errors returned from the payment gateway! It's very poorly designed code.

Please take a moment to run your code through the Visual Studio debugger to find the mistakes.

0 Votes 0 ·

Before now, it was working fine.
You mentiomed an error?! But on the browser addreass tab, it returned the reference number after successful payment. So the referencentre number is taken as the Querystring. So seeing the reference it should have updated the wallet

0 Votes 0 ·

Please AgaveJoe, how should I have written the code; you mentioned that it's poorly written. May I ask for good guidelines on how to write it well?

0 Votes 0 ·

Please AgaveJoe, how should I have written the code; you mentioned that it's poorly written. May I ask for good guidelines on how to write it well?

There are several potential issues with the design.

  • The design looks for the user's Session which is always null if the 3rd party service is making the call.

  • The UpdateWallet method is only called If the api call returns is successful. The design ignores any error returned from the api and does nothing to alert the user or log the error.

As recommended, please learn how to use the Visual Studio debugger to validate that your design is functioning as expected. Set a break point and single step through the code. You're looking for where the code produces unexpected results.

First look at the Visual Studio Debugger

If the code was functioning as expected at one time, then compare the working code with the updated code to find the mistake.


0 Votes 0 ·

Hi @Donald-0878,

ERROR: Update code for wallet table suddenly stops working

Are there any error messages displayed?
One thing to pay attention to is the data type of the amount.
It is necessary to ensure that the data type of the database is consistent with the defined data type.
Best regards,
Lan Huang

0 Votes 0 ·

iHi @ LanHuang-MSFT,
The datatype of the amount is decimal.
What surprises most is that for the past one year it has been working. It just suddenly stops updating

0 Votes 0 ·

Hi @Donald-0878,
No error messages at all?

0 Votes 0 ·
Show more comments

1 Answer

Donald-0878 avatar image
0 Votes"
Donald-0878 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.