1

I am developing a plugin where I have started a session on init hook. I have a form which when submit calls a function which uses SDK (i.e payment is handled here). If this function returns FALSE then I need to redirect user to the previous page (or given URL) where I need the values stored in the session earlier. But when I try call them, I get none. It seems as if session is getting unset.

Can any one help me on this?

The below function handles the SDK part which either returns true or false.

function authorize_net_payment_process($order_metas)
{
    $options = get_option('wp_travel_engine_settings', true);
    $pno = $_SESSION['travelers'];
    $errors = '';
    if ( ! isset( $_POST['authorize']['card_number'] ) || $_POST['authorize']['card_number'] == '' ) {
        $errors = __( 'You must enter a card number', 'wte-authorize-net');
    }
    if ( ! isset( $_POST['authorize']['card_name'] ) || $_POST['authorize']['card_name'] == '' ) {
        $errors = __( 'You must enter the name on your card', 'wte-authorize-net');
    }
    if ( ! isset( $_POST['authorize']['card_exp_month'] ) || $_POST['authorize']['card_exp_month'] == '' ) {
        $errors = __( 'You must enter an expiration month', 'wte-authorize-net');
    }
    if ( ! isset( $_POST['authorize']['card_exp_year'] ) || $_POST['authorize']['card_exp_year'] == '' ) {
        $errors = __( 'You must enter an expiration year', 'wte-authorize-net');
    }
    if ( ! isset( $_POST['authorize']['card_cvc'] ) || $_POST['authorize']['card_cvc'] == '' || strlen( $_POST['authorize']['card_cvc'] ) < 3 )
    {
        $errors = __( 'You must enter a valid CVC', 'wte-authorize-net' );
    }

    if ( ! $errors || $errors=='' ) {
        require_once( dirname( __FILE__ ) . '/anet_php_sdk/AuthorizeNet.php' );
        $transaction = new AuthorizeNetAIM( $options['authorize_api_id'], $options['authorize_transaction_key'] );
        $card_names = explode( ' ', $_POST['authorize']['card_name'] );
        $first_name = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['fname'];
        $last_name = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['lname'];

        $transaction->amount      = $_SESSION['trip-cost'];
        $transaction->card_num    = strip_tags( trim( $_POST['authorize']['card_number'] ) );
        $transaction->card_code   = strip_tags( trim( $_POST['authorize']['card_cvc'] ) );
        $transaction->exp_date    = strip_tags( trim( $_POST['authorize']['card_exp_month'] ) ) . '/' . strip_tags( trim($_POST['authorize']['card_exp_year'] ) );

        $transaction->first_name  = $first_name;
        $transaction->last_name   = $last_name;

        $transaction->address     = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['address'];
        $transaction->city        = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['city'];
        $transaction->country     = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['country'];
        $transaction->email       = $_POST['wp_travel_engine_booking_setting']['place_order']['booking']['email'];

        $cost = $_SESSION['trip-cost'];
        $cost = str_replace(',', '', $cost);
        $response = $transaction->authorizeAndCapture();
        if ( $response->approved ) { 

If false is returned then user is redirected to the previous page where he/she puts the values again as previous one was returned false by the above function.

I need the $_SESSION inside that function again (user submits the form again in case of false after redirection).

Thank you!

  • Are you talking about PHP sessions or WordPress user sessions using cookies? Please be a bit more specific and append any code you use to accomplish this. – swissspidy Feb 1 '18 at 10:15
  • Its php sessions using $_SESSION. And I am editing my question to put some of my codes. @swissspidy – saurav.rox Feb 1 '18 at 10:26
  • And you're running session_start() on init? Have you tried running that directly, without hooking into an action? – swissspidy Feb 1 '18 at 11:57

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.