diff --git a/CRM/Core/Payment/MJWTrait.php b/CRM/Core/Payment/MJWTrait.php
index 2e236b767630efdd71b65718a32c10fc17ce70cb..c7bee78fcbc93a24432685d057593a7aaae4ef70 100644
--- a/CRM/Core/Payment/MJWTrait.php
+++ b/CRM/Core/Payment/MJWTrait.php
@@ -278,17 +278,25 @@ trait CRM_Core_Payment_MJWTrait {
    */
   public function getErrorUrl($params) {
     // Get proper entry URL for returning on error.
-    if (!$params->has('qfKey')) {
+    if (!$params->has('qfKey') || !$params->has('entryURL')) {
       // Probably not called from a civicrm form (e.g. webform) -
       // will return error object to original api caller.
-      return NULL;
+      $errorUrl = NULL;
     }
     else {
       $qfKey = $params->getCustomProperty('qfKey');
-      $parsed_url = parse_url($params->getCustomProperty('entryURL'));
-      $url_path = substr($parsed_url['path'], 1);
-      return CRM_Utils_System::url($url_path, $parsed_url['query'] . "&_qf_Main_display=1&qfKey={$qfKey}", FALSE, NULL, FALSE);
+      $parsedUrl = parse_url($params->getCustomProperty('entryURL'));
+      $urlPath = substr($parsedUrl['path'], 1);
+      $query = $parsedUrl['query'];
+      if (strpos($query, '_qf_Main_display=1') === FALSE) {
+        $query .= '&_qf_Main_display=1';
+      }
+      if (strpos($query, 'qfKey=') === FALSE) {
+        $query .= "&qfKey={$qfKey}";
+      }
+      $errorUrl = CRM_Utils_System::url($urlPath, $query, FALSE, NULL, FALSE);
     }
+    return $errorUrl;
   }
 
   /**