0

I am working on a checkout proces where if I submit the checkout I get an error "headers already send" because I use a PHP header() function to send the customer to my payment provider url.

My idea is now to put the checkout code in a Wordpress 'init' hook, do you think this will resolve the problem?

This is the handling code:

// Include Mollie
include "initialize.php";

if( isset( $_POST['submit'] ) ) {

    $order_id = time();

    // Create payment
    $payment = $mollie->payments->create( array(
        'amount'        => $data['price'] * ( $vat + 1 ),
        'description'   => __( 'Splashback order' ),
        'method'        => $_POST['sbg_payment'],
        'redirectUrl'   => site_url( '/success/?order_id=' . $order_id ),
        'metadata'      => array(
            'order_id'  => $order_id,
        ),
    )); 

    // Json product composition
    $composition = json_encode( $data );

    // Check fields and json
    $fields = array(
        'name'      => array( 'req' => false ), 
        'street'    => array( 'req' => false ), 
        'number'    => array( 'req' => false ), 
        'zipcode'   => array( 'req' => false ), 
        'city'      => array( 'req' => false ), 
        'country'   => array( 'req' => false ), 
        'email'     => array( 'req' => false ),
        'phone'     => array( 'req' => true ),
        'payment'   => array( 'req' => false ),
    );

    foreach( $fields as $key => $val ) {
        if( isset( $_POST['sbg_' . $key] ) ) {
            if( $val['req'] === false ) {
                if( empty( $_POST['sbg_' . $key] ) ) {
                    $errors = true;
                } else {
                    $customer[$key] = $_POST['sbg_' . $key];    
                }
            } else {
                $customer[$key] = $_POST['sbg_' . $key];    
            }
        }
    }

I have tried to make a function in the init hook of the piece below, but cannot get it work, because I give some arguments to the function to fill the variables I get errors for all the empty variables. Can anyone help me with this?

    if( ! $errors ) {

        // Json customer details
        $customer = json_encode( $customer );

        // Insert transaction to database
        $wpdb->insert( $wpdb->prefix . 'mollie_transactions',
            array(
                'transaction_id'        => $payment->id,
                'order_id'              => $order_id,
                'amount'                => $data['price'] * ( $vat + 1 ),
                'composition'           => $composition,
                'customer_details'      => $customer,
                'date'                  => current_time( 'mysql' ), 
                'status'                => __( 'Progress' ),
            )
        );

        // Redirect
        header( "Location: " . $payment->getPaymentUrl() );

    }

}
  • 1
    "do you think this will resolve the problem?" Have you tried it yet? – TheDeadMedic May 20 '14 at 11:28
  • @TheDeadMedic Yes off course, otherwise I would not post the question here. – Robbert May 20 '14 at 12:28
  • Yes, something like what you are doing should solve the problem but your code is too out of context for me to hazard a guess at an answer. I can't see how it all works together, and your "function" is incomplete as well. – s_ha_dum May 20 '14 at 13:34
  • @s_ha_dum Yeah my idea was to show a small piece of the code but most of the time people ask for more code then. But so I made a function in functions.php which I want to load if the checkout is submitted. But I am figuring out now which hook I need to use so I hope people can give me a step in the right direction. – Robbert May 20 '14 at 13:51
  • 1
    Again, your code is too out of context. Any hook before content is sent to the browser should solve the header problem, but also mention undefined variable issues. – s_ha_dum May 20 '14 at 13:54

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.