0

I'm coding a payment gateway plugin for a webshop. I'm having trouble redirecting the users to two different pages based on the order status.

In the payment sent to the gateway I can add one parameter as return URL. This is where users are redirected back to the site by the gateway. After redirection, I first have a check if the order-id is paid or not. Based on the output I want to redirect the user to the succes or failure page. I tried this using wp_redirect but it doesn't seem to work.

I use two functions. One to listen for GET requests matching the pattern. The other function should retrieve the order-id, check if the status is 'publish' and redirect the user to the correct page.

Update I refactored the code below and now it redirects the users to the correct URL based on the status of the order. The main thing I changed was one inconsistency in the code thanks to @czerspalace and changed the second function. The first function now calls the second function directly instead of using do_action(mollie_payments_redirect).

Because the check_for_mollie_payments() function is called in 'init', I can still redirect the user to the correct page.

function check_for_mollie_payments() {
  if (!isset($_GET["payment-notification"])) {
    return;
  }
  $id = $_GET["payment-id"];
  $params = array(
    'payment-notification'  => $_GET["payment-notification"],
    'payment-id'            => $_GET["payment-id"]
  );

    mollie_payments_redirect($params);
  }
add_action('init', 'check_for_mollie_payments');

function mollie_payments_redirect($params)
{
  global $edd_options;

  if(!is_array($params)) {
    return;
  }

  if(count($params) !== 2) {
    return;
  }

  $mollie_payment_id = $params['payment-id'];

  $status = get_post_status($mollie_payment_id);

  if($status === false) {
    return;
  }

  if ($status == 'complete' || $status == 'publish') {
    edd_send_to_success_page();
  } else {
    edd_send_to_failed_page();
  }
}
  • You should be getting a PHP error message with debugging enabled if it's a headers already sent error. – Milo Nov 9 '15 at 18:30
  • I have debugging enabled and this error hasn't occurred. Basically I'm looking for a way to redirect users to another page if the URL matches certain parameters. But do a check first to determine which URL they should be sent to. – SdeWijs Nov 9 '15 at 19:05
  • It's not clear where the failure may be. An answer may require intimate knowledge of a 3rd party plugin, which is off-topic here. Have you determined exactly what parts of your code are executing? Does it fire your action? Does it successfully retrieve the status? Does it make it to the switch statement? – Milo Nov 9 '15 at 19:25
  • A redirect after content is sent to the browser won't work. – s_ha_dum Nov 10 '15 at 2:01
  • Did you mean to use both $_GET['payment-notification'] and $_GET['payment-confirmation']? – czerspalace Nov 10 '15 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.