I'm totally new to Drupal. I created a form in Drupal using pure HTML when the user clicks on pay button I call a PHP file just to get some database information subsequently this PHP redirects to another PHP that puts the Paypal payment together.


pay

the click on the button will trigger the following code:

var samount;
var sqty;
var sprice;
var desc;
function myFunction(){
$("#ba2").load("paymentpp.php?description=" + desc +"&samount=0.005" + "&sqty=" + amount + "&sprice=" + usd_amount.toFixed(2));
}

For some reason the paypal payment PHP does not redirect to Paypal when it is called from under Drupal. My layers are as follows:

https://mysite.com/test/?q=node/11 --> press pay button --> call savedatatoDB.php --> call paypalpreparepayment.php --> redirect to paypal

If I call the paypal payment PHP from the browser directly like this:

https://mysite.com/phalanx/paymentpp.php?description=1234XRP&samount=10&...

it works! I thought the problem could be an output before header() but it is not the case otherwise it would not work directly from the browser, right?

I wrote a line to write to a log file right before the redirect like this:

$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$log = "{$today} ready to redirect {$sale_description}, {$sale_amount}, {$sale_quantity}, {$sale_price}\n";
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);
header("Location: $redirectUrl");
exit;

And the log shows that I'm always getting that far but no redirect! Again if I call that PHP directly from the browser then I see Paypal asking me to authorize payment which is what I'm expecting.

So when I'm under Drupal UI, I'm wondering if Drupal could be blocking the redirect somehow but other than the log file to see my traces I don't know another way to debug this.

The reason why I have a PHP to call another PHP is because the second PHP is a Paypal sample code with little modification to read passed args to prepare the payment. it is very easy to maintain if Paypal releases newer versions of their sdk.

Anyone any ideas please? Thanks for your help.

Comments

jaypan’s picture

The problem is that your redirect is on the server side, but you are triggering it with JavaScript. If the trigger is in your JS, then the redirect also needs to happen in the JS. If the trigger is non-JS (submits to the server), then your redirect can be on the server side.

Contact me to contract me for D7 -> D10/11 migrations.

gilmotta’s picture

That's an interesting problem. I understand that my code to trigger is JS and now that you said that I think if I issue a form submit in JS it will work as expected because the code is expecting a response from the server.

Anyway, in my paypal prepare payment PHP code I resolved the problem as follows:

I replaced:
header('Location: ' . $url, TRUE, $http_response_code);

with:

	echo '<script type="text/javascript">
           window.location = "'. $redirectUrl . '"
      </script>';

And now it is redirecting fine. I still feel this is a workaround rather than a permanent solution. The problem for me is that I don't have a real html form, I created a <input text> and a <button> then in my JS I do what you saw in my first question. And I lack the knowledge in HTML for a smarter solution.