What Woocommerce function is called on PayPal IPN response? What Woocommerce function is called on PayPal IPN response? wordpress wordpress

What Woocommerce function is called on PayPal IPN response?


Checking the file /wp-content/plugins/woocommerce/classes/gateways/paypal/class-wc-paypal.php, we see that there's an action hook inside the function check_ipn_response:

if ($this->check_ipn_request_is_valid()) :    header('HTTP/1.1 200 OK');    do_action("valid-paypal-standard-ipn-request", $_POST);

You can hook into it like this:

add_action( 'valid-paypal-standard-ipn-request', 'so_12967331_ipn_response', 10, 1 );function so_12967331_ipn_response( $formdata ){    // do your stuff}


Building on @brasofilo's answer, I had to do extra work for each product for the current order.

Note: I'm new to (un)serializing data, so I don't know why I had to unescape the double quotes to get unserialize() to work. It threw an error, otherwise. Maybe there's a better way to handle this.

function so_12967331_ipn_response( $formdata ) {    if ( !empty( $formdata['invoice'] ) && !empty( $formdata['custom'] ) ) {        if( $formdata['payment_status'] == 'Completed' ) {            if( is_serialized( $posted['custom'] ) ) {                // backwards compatible                // unserialize data                $order_data = unserialize( str_replace('\"', '"', $posted['custom'] ) );                $order_id = $order_data[0];            } else {                // custom data was changed to JSON at some point                $order_data = (array)json_decode( $posted['custom'] );                $order_id = $order_data['order_id'];            }            // get order            $order = new WC_Order( $order_id );            // got something to work with?            if ( $order ) {                // get user id                $user_id = get_post_meta( $order_id, '_customer_user', true );                // get user data                $user = get_userdata( $user_id );                // get order items                $items = $order->get_items();                // loop thru each item                foreach( $items as $order_item_id => $item ) {                    $product = new WC_Product( $item['product_id'] );                    // do extra work...                }               }           }    }}