Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce payment complete hook

After a long search, I found this post:

WooCommerce hook for "after payment complete" actions

which talks about creating web hooks in WooCommerce to notify a script to do...something...doesn't matter too much what.

I've also read everything I can find in WooCommerce docs.

but I need some kind of documentation or guidance on actually writing the handler on the other end.

My goal is to receive a payment complete notification and then move the user to a different list (a customer list rather than a prospects list) after purchase - I use PHPlist in house as my list manager. Pretty sure I can deal with that part, if I can just get the listener going...

But..I don't know what the web hook sends, how to get it to send data that I want, and what to do with the listener.

I did also find this:

https://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-order-id-for-payment-notification-to-external-webservice?replies=4

which - MIGHT be helpful? i'm still not sure where to begin with the listener, or if this post is valid still, given that it's a couple of years old...

like image 537
Aaron Trumm Avatar asked Nov 19 '15 23:11

Aaron Trumm


People also ask

Does WooCommerce have its own payment gateway?

WooCommerce.com offers our own WooCommerce Payments! WooCommerce Payments is free to install, with no setup fees or monthly fees. Pay-as-you-go fees start from just 2.9% + $0.30 per transaction for U.S.-issued cards, read more about transaction fees.

How does WooCommerce integrate payment method?

Step 1: Go to WooCommerce >> Payments and enable Check payments. Step 2: Click on Set up and configure the payment method. You will get the check payment settings to be set. Here, enable the cheque payments and then enter the instructions and description for your customers.


3 Answers

The woocommerce_payment_complete hook is fired when the payment is completed. The only variable passed is the order id, though from that you can get the order object, and ultimately the user.

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){
    $order = wc_get_order( $order_id );
    $user = $order->get_user();
    if( $user ){
        // do something with the user
    }
}
like image 70
helgatheviking Avatar answered Oct 16 '22 14:10

helgatheviking


with the help from @helgatheviking and @Scriptonomy I settled on this code, with NO webhook enabled in woocommerce->settings->api->webhooks:

add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete( $order_id ){  
  $order = wc_get_order( $order_id );
  $billingEmail = $order->billing_email;
  $products = $order->get_items();

foreach($products as $prod){
  $items[$prod['product_id']] = $prod['name'];
}

$url = 'http://requestb.in/15gbo981';
// post to the request somehow
wp_remote_post( $url, array(
 'method' => 'POST',
 'timeout' => 45,
 'redirection' => 5,
 'httpversion' => '1.0',
 'blocking' => true,
 'headers' => array(),
 'body' => array( 'billingemail' => $billingEmail, 'items' => $items ),
 'cookies' => array()
 )
);

Now I just have to write the listener :) This is the body of the request that gets sent (which I can see at requestb.in):

billingemail=%22aaron-buyer%40thirdoptionmusic.com%22&items%5B78%5D=Cult+Of+Nice&items%5B126%5D=Still&items%5B125%5D=The+Monkey+Set
like image 21
Aaron Trumm Avatar answered Oct 16 '22 14:10

Aaron Trumm


If you so desire to inspect the web hook request makeup, I suggest you head over to requestb.in and setup a bin. Thus allowing you to inspect the request and formulate an action handler.

Hint: the webhook request sends relative information in the body of the request as JSON formatted data. Once you decode the body, it's easy to traverse it and extract the needed information.

On a different leg of the answer, I point you to @helgatheviking answer and use the woocommerce_payment_complete hook. Once inside the hook, fire off a curl POST request and insert in the body any request handler dependencies. You will extract those dependencies from the $order_id.

like image 44
Scriptonomy Avatar answered Oct 16 '22 15:10

Scriptonomy