0

Oh dear paypal,

I'd like to know why the "return redirect Url" page won't load if I call Response.AppendHeader. You see, I'm trying to make a file download automatically after the user has payed on paypal's page so I put the following code on my Page_Load

If Not IsNothing(Request.QueryString("paymentId")) Then
            Dim paypalUtils As New PayPalUtils
            paypalUtils.ProcessPayPalResponse(Request.QueryString("paymentId").ToString(), Request.QueryString("PayerID").ToString())
            TransmitFile()
        End If

My TransmitFile implementation is very simple

Protected Sub TransmitFile()
    Response.ContentType = "application/sla"
    Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileNameWithoutExtension(hidFileName.Value) & ".stl")
    Response.TransmitFile(Sistema.SavePath & Path.GetFileNameWithoutExtension(hidFileName.Value) & ".stl")
    Response.End()
End Sub

The problem is that somehow calling Response.AppendHeader or any of the following lines prevents paypal from correctly redirecting to the proper url that I specified on the API.

If I comment the TransmitFile on Page_Load everything works as expected (Paypal redirects me to my site). If not, I sucessfully make the file download automatically but I get stuck on this page

enter image description here

What is that I'm doing wrong?

Alejandro Lozdziejski
  • 1,063
  • 4
  • 12
  • 30
  • If I understand your code correctly, you're transmitting the file in the `Response` from the PayPal redirect? Or does your `paypalUtils.ProcessPayPalResponse(...)` call complete the received response and redirect to another page in your app prior to calling `TransmitFile()`? – Jason Z Mar 10 '15 at 00:03
  • Yes. I I'm transmitting the file in the `Response` from paypal's redirect.. The only thing that `paypalUtils.ProcessPayPalResponse(...)` does is execute the payment – Alejandro Lozdziejski Mar 10 '15 at 01:16
  • 1
    In that case, when you set `Response.TransmitFile()`, your application is attempting to send the file as a response back to PayPal when your application handles the incoming request. The reason why it's stuck on 'Loading' is likely because there's nothing on the PayPal site that is setup to respond to that type of response. You need a `Response.Redirect()` to an internal page that responds with the file so the buyer gets the file. – Jason Z Mar 10 '15 at 01:26
  • Ok, I created a page called PaymentProcessor.aspx that only does `Response.Redirect()` to Default.asp (the one having the `Response.TransmitFile()`).I changed paypal's return redirect Url to PaymentProcessor.aspx and I'm still getting the same error. Paypal loading page forever. I debugged and PaymentProcessor.aspx Page_Load executes fine. It redirects to Default.aspx without problems, but. I never get to see any page again (apart form the paypal loading thing) – Alejandro Lozdziejski Mar 11 '15 at 17:09
  • After some more testing, I realized my previous comment may not have been entirely correct. :P The file does look to be downloaded; however, the problem is in redirecting to a page after the download. The key is actually to first display a page with a `` tag in the `` portion of the page that then refreshes the page and initiates the download. I'll see if I can put together an answer for you that goes into more details on this. – Jason Z Mar 11 '15 at 18:52

1 Answers1

0

Your code is actually correct in how the download is being delivered - it's just how .NET handles the file transmission, redirection, and page display that's causing the confusion.

You have a few options at your disposal, depending on how you want the experience to be on your site.

  1. Use AJAX and iFrames to asynchronously download the file when your redirect page is displayed. See this StackOverflow question for more answers. The link to a walkthrough on Encosia is very detailed and should make for a nice user experience.

  2. Redirect to a page that is set to auto-refresh and downloads when auto-refreshed. This bypasses the .NET limitation that using Response.TransmitFile() and Response.End() makes it impossible to then use Response.Redirect(). To do the auto-refresh, you add the following line to the <head> portion of your HTML source code:

    <meta http-equiv="refresh" content="1; url=<%= this.RedirectUrl %>">
    

    this.RedirectUrl references a property in the code behind the page that is set to wherever you want the redirect to go. After that redirect is handled is when you run your code to send the file.

I was able to test out #2 and got it working. I'm sure there's more possibilities as well, but hopefully that should get things working for you.

Community
  • 1
  • 1
Jason Z
  • 854
  • 6
  • 15
  • Ok, lets focus on the 2nd solution (the one you tested). I used this line `` as you said and I'm able to have paypal rederiect me to `PaymentProcessor.aspx` the problem is that as I get redirected to `Default.aspx` , `Default.aspx.vb` `Page_Load` is executed and the file is correctly downloaded BUT Default.aspx is never redndered and the browser Url still sows `PaymentProcessor.aspx` What is wrong? – Alejandro Lozdziejski Mar 11 '15 at 20:41
  • This is where you'll need to play around with which pages are shown to get it to work exactly the way you want. The `url` in the `` tag is essentially the page that delivers the file, so I wouldn't rely on that to be a landing page for the user. The AJAX solution may be a better solution regarding this because you could potentially script a redirect to occur after the file is delivered, but I'm not overly familiar with doing that, unfortunately. – Jason Z Mar 11 '15 at 20:51