从支付网关重定向后未变更未初始化

This is a very weird situation I am in. Here is the scenario: After a payment is complete, the user lands on the thankyou page of my site. Here there are some functions on the page that do the following:

  1. Send order email.
  2. Store data in the DB of the payment
  3. Add some marketing pixels so that the sale is tracked.

The problem I am facing is:

  1. After the redirect from the payment gateway I get the transaction_id from the url.
  2. I use it to get the sales order details and send out the order email.
  3. While showing the pixels, the sales order details is gone. the variable dont exist no more I tried to get the transaction ID and get the details from the DB again but with no luck.

Now after I do a refresh on this page, everything works fine. Just like it should.

My question is:

  1. Why is it when its redirected from the gateway, this variable is un initialized?
  2. Why does it work after a manual refresh (Considering it shouldn't if there was a problem in my code)?

Thanks.