Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
8.9k views
in Technique[技术] by (71.8m points)

php - HTTP redirect status code must be a redirection codes 3xx. Problem with wp_redirect() call

I have a wordpress/woocommerce website that is integrated with a 3rd party credit card payment company via their provided plugin.

WordPress: 5.6 Woocommerce: 4.9

When testing with their test environment I get an error generated when I make a test purchase using their environment. The site if trying to redirect to the default thank you page but instead, it goes to an almost blank page with the message shown below.

8751374076 HTTP redirect status code must be a redirection code 3xx.

This happens after a wp_direct call is made back to the main site when a successful order is placed. An example of the parameter passed back (a string), is:

https://soccerexsports.com/checkout/order-received/7227/?key=wc_order_4eTWoSHcELZ8I70

Now the order status is changed to Processing and the emails confirming the order are sent, but the user is not returned to the Shop with a thank you message. Instead, they get the error above.

The code for the call is in the response.php file with the 3rd party plugin. I have listed it below:

<?php 

    require('../../../wp-load.php');
    global $woocommerce;
    //$id = $_GET["order"];
    session_start(); 
    $id = $_SESSION['Order_ID'];
    
    $order = wc_get_order($id);
    //print $order;
    $response_curlerror = ""; 
    $response_curl = ""; 
    
    
    $ch = curl_init($_SESSION['GatewayURL']); 
    $arr_sendRequest = array();
    $arr_sendRequest['TransactionID'] = $_SESSION['TransactionID'] ;
    $arr_sendRequest['Customer'] = $_SESSION['MerchantID'];
    $arr_sendRequest['UserName'] = $_SESSION['UserName'];
    $arr_sendRequest['Password'] = $_SESSION['Password'];
    $jonRequest = json_encode(array('Finalization' => $arr_sendRequest), JSON_FORCE_OBJECT);
    
    //Logs enabling
    custom_logs("--------Initialize the Transaction Finalization Process---------");
    custom_logs("TransactionID - ".$_SESSION['TransactionID']);
    custom_logs("OrderID - ".$_SESSION['Order_ID']);
    

    curl_setopt($ch, CURLOPT_HEADER, 0); /* Set to 1 to see HTTP headers, otherwise 0 or XML reading will not work */
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Accept:text/xml-standard-api'));
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_PORT, 2443);
    curl_setopt($ch, CURLOPT_SSLVERSION, 6);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $jonRequest );
    curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . "/ca.crt"); /* Location in same folder as this file */
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response_curl = curl_exec($ch);
    
    if (curl_errno($ch)) {
        $response_curlerror = curl_error($ch);
    }
    curl_close($ch);
    
    if( !is_wp_error( $response ) ) 
    {
            $response = json_decode($response_curl, true);
        
            custom_logs("Response Code - ".$response['Transaction']['ResponseCode']);
            custom_logs("Response Description - ".$response['Transaction']['ResponseDescription']);

        if ($response['Transaction']['ResponseCode'] == 0) 
        { 
            $order->payment_complete();
            $order->reduce_order_stock();
            $message = "Payment has been processed successfully" . "
" . "TransactionID: " .$_SESSION['TransactionID']. "
".  "Response Code: " .$response['Transaction']['ResponseCode'] ."
". "ApprovalCode: "  .$response['Transaction']['ApprovalCode'] ;
            $order->add_order_note(nl2br($message), true );
            $woocommerce->cart->empty_cart();
            $return_url = $order->get_checkout_order_received_url();

            //print the value of $return_url
            print $return_url;
            
            
            wp_redirect($return_url);
            custom_logs("--------Process Completed with success---------");
            exit();
        }
        else    
        {
            //wp_redirect(get_permalink( wc_get_page_id( 'cart' ) ));
            //exit();
            
            print "<br />Error Details:<br />";
            print "Response Code: " .$response['Transaction']['ResponseCode'] . "<br />"; 
            print "Desctiption: " .$response['Transaction']['ResponseDescription'] . "<br />"; 
            print "Class Desctiption: " .$response['Transaction']['ResponseClassDescription'] . "<br />"; 
            $message = "Payment Failed for TransactionID " .$_SESSION['TransactionID']. "
".  "Response Code: " .$response['Transaction']['ResponseCode'] ;
            $order->add_order_note(nl2br($message), true );
            custom_logs("--------Process Completed with failure---------");
            echo "<br>";
            echo '<form name="Request" action="'.get_permalink( wc_get_page_id( 'cart' ) ).'" method="get">';
            echo '<input type="submit" value="Back to Cart">';
            echo '</form>';
        }
    }
else
    {

            Print "Connection Error - Please Try Again";
            echo "<br>";
            echo '<form name="Request" action="'.get_permalink( wc_get_page_id( 'cart' ) ).'" method="get">';
            echo '<input type="submit" value="Back to Cart">';
            echo '</form>';
    }
    
    function custom_logs($logmessage) 
    { 
        if(is_array($logmessage)) 
        { 
            $logmessage = json_encode($logmessage); 
        } 
        $file = fopen("../Transactionlogs.log","a"); 
        echo fwrite($file, "
" . date('Y-m-d h:i:s') . " : " . $logmessage); 
        fclose($file); 
    }
    

The location is the wp_redirect($return_url) call just before the custom_logs("--------Process Completed with success---------") message

I am baffled by the issue. The data being passed seems legitimate, and the emails are being generated correctly but the user is not being sent back to the shop.

Any ideas?

question from:https://stackoverflow.com/questions/65934421/http-redirect-status-code-must-be-a-redirection-codes-3xx-problem-with-wp-redir

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...