2

I am working on an asp.net core MVC project with PayPal integration. After completing the payment, the PayPal correctly redirect to the success URL (.../PayPal/PaymentSuccess). Please see the method given below:

PayPalController

    public class PayPalController 
     {
            public IActionResult PaymentSuccess()
            {
            //code for business logic here
            TempData["Amount"] = amount; 
            return RedirectToAction("PaymentCompleted", "Home");
            }
     }

HomeController

         public class HomeController
         {
                public IActionResult PaymentCompleted()
                {
                var amount = Convert.ToDecimal(TempData["Amount"]);
                //code for business logic here
                return View();
                }
         }

I have tried a payment using PayPal in the hosted environment. After completing the payment the PayPal successfully invoked the PaymentSuccess() method. In this method, we have added the business logic for database updation. After this we need to redirect to another page "PaymentCompleted" to show the payment success message.

The issue is that the redirection to the view page "PaymentCompleted" which is in another controller (HomeController) is not working after successfully executing the code in PaymentSuccess method.

But, I have tried to invoke the PaymentSuccess() method directly in the browser and now the redirection is working.

EDIT: I have used a 'TempData' on 'PaymentSuccess()' method for passing paid amount to display on the 'PaymentCompleted' view page.

Why does this happen? Can anyone specify the reason behind this problem?

Any help would be appreciable. Thank You!

Vignesh VS
  • 921
  • 1
  • 14
  • 30
  • You don't get any exception? Can you add explicitly [HttpGet] to the PaymentCompleted controller action, maybe that will help. – Wouter May 06 '20 at 06:47
  • It's not showing any exception. OK I will try it. Thank you:) – Vignesh VS May 06 '20 at 06:52
  • @Wouter I tried it. But still showing the same. – Vignesh VS May 06 '20 at 07:02
  • How did you trigger the PaymentSuccess method? (This may be the key point). It is recommended that you debug the code in PaymentSuccess to confirm whether the breakpoint can go to the RedirectToAction code. – LouraQ May 07 '20 at 02:06
  • @YongqingYu .We have created the payment buttons using the button generator feature in Paypal. During button generation, we can specify the necessary URLs such as payment success, cancel, and other optional URLs. So when we do a payment the Paypal itself invokes the corresponding method. – Vignesh VS May 07 '20 at 04:41

2 Answers2

0

I had same with your issue before.

The problem the code handle business logic is exception and code runing can not reach RedirectToAction

You can add try/catch to handle exception, and log information to see detail

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Thank you for your answer. I have already added try/catch. But it's not showing any exception. I have tried debugging in the hosted environment(Using IIS) and it points to the RedirectToAction() method. But not redirecting :( – Vignesh VS May 06 '20 at 06:14
0

Finally, I got the solution. The issue is happened because of the way we used the TempData. We have to serialize the amount before assigning it to TempData in ASP.Net Core.

TempData["Amount"] = JsonConvert.SerializeObject(amount);

Also, we have to deserialize it before retrieving the TempData. No need to deserialize if it is string. I just showing the procedure. You can follow the below code:

var amount = JsonConvert.DeserializeObject<string>(TempData["Amount"].ToString());

But in MVC application, we don't need any serialization before assigning data to TempData.

Now the RedirectionToAction() method worked and I retrieved the paid amount on the PaymentCompleted() method using TempData.

Vignesh VS
  • 921
  • 1
  • 14
  • 30