WordPress Redirect Loop with CiviContribute
I solved the problem by using Ramesh's suggestion as a starting point. Instead of introducing hard coded Thank you &amp; Cancel pages I stored the permalink address of the WordPress page along the other invoice stuff during the creation of the payment in Paytrail extension:

public function doTransferCheckout(&amp;$params, $component) {
  $paymentHelper = new PaytrailPaymentHelper();

  // For now works only with WordPress &amp; permalinks
  if (function_exists('get_permalink') &amp;&amp; $contributionPageCmsUrl = get_permalink()) {
    $params['contributionPageCmsUrl'] = $contributionPageCmsUrl;
  } 
  //Save payment info for PaytrailIPN.php to retrieve
  $paymentHelper-&gt;insertInvoiceInfo($params, $component);


It's utilized in the IPN following way:

  static function main() {
    ...
    $invoiceData = self::readInvoiceData($invoiceID);
    $contributionPageCmsUrl = $invoiceData['contributionPageCmsUrl'] ?: '';
    ...
    if ($contributionPageCmsUrl) {
      $queryParams = array_merge(array(
        'page' =&gt; 'CiviCRM',
        'q' =&gt; $finalUrlAction,
      ), $finalUrlParams);
      $glue = strpos($contributionPageCmsUrl, '?') === FALSE ? '?' : '&amp;';
      $finalUrl = $contributionPageCmsUrl . $glue . http_build_query($queryParams, null, '&amp;');
      CRM_Utils_System::redirect($finalUrl);
    }
    else {
      CRM_Utils_System::redirect(CRM_Utils_System::url($finalUrlAction, $finalUrlParams, true, null, false, true));
    }
}


I guess there should be more logic for handling different situations and upgrade hook implemented. This behaviour could be made also optional and support for Joomla and Drupal introduced (if needed), but now it solves the problem for the client.

BTW, double title on the contribution WordPress page should be eliminated. Is the display:none way to go?