Reputation: 3777
I am creating a custom system for a client. He already had Wordpress with WooCommerce setup for selling his products. I know that it actually works though. His sales, shipping, tracking, all of it works fine.
What I am trying to do, is capture every valid, successful sale and pass the data to a 3rd party API (mine created on Yii2 framework). My system needs to be "told" of every completed sale. I don't care about pending, only completed.
After finding other SO threads regarding this, I used the woocommerce_payment_complete
action hook. Looks legit :)
However, when I use it, it only works on free checkouts (when paypal is bypassed due to 100% off coupon). It sends the data to my 3rd party API just fine. I then tested with a PayPal Sandbox transaction. Both with a full charge, and a 99% off coupon (to not blow through sandbox funds during testing). When PayPal is being used, it all looks good from the user end (return to merchant, successful receipt page), but no data is passed to my 3rd party API. Weird, because on free purchases (100% off coupon bypassing paypal) it works perfect.
Maybe this action hook doesn't work right. Maybe WooCommerce has a new one now, and I am using an old one? Which action hook should I use?
Maybe this action hook doesn't work right when WooCommerce is using PayPal sandbox? I haven't tested a real transaction with real money yet, that is the point of sandbox, to test with lol! For all I know, it could work during a live test.
Maybe this action hook doesn't work with PayPal?
Hopefully someone with more Wordpress and WooCommerce experience can help me narrow this down.
Here are the guts of my custom plugin to capture the action hook. My custom plugin just has a simple 1 option page to enter the API url so it can be easily changed from the WP admin dashboard (truncated for SO).
<?php
if ( ! defined( 'ABSPATH' ) ) {
die('Access Denied!');
}
define('WCO_ROOT_DIR', __DIR__);
function init()
{
add_action( 'woocommerce_payment_complete', 'wco_payment_complete' );
}
function wco_payment_complete( $order_id )
{
$order = wc_get_order( $order_id );
$billingEmail = $order->billing_email;
$products = $order->get_items();
$items = [];
foreach($products as $prod){
$items[$prod['product_id']] = $prod['name'];
}
// gets the API url from database
$url = get_option('wco_api_url');
$response = 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()
)
);
if ( is_wp_error( $response ) )
{
$error_message = $response->get_error_message();
file_put_contents(ABSPATH . '/wco-orders.log', '[WooCompletedOrders] Error: ' . $error_message, FILE_APPEND | LOCK_EX);
} else {
$logData = '--------------------' . PHP_EOL;
$logData .= '[WooCompletedOrders] Success: Posted data to API: ' . PHP_EOL;
$logData .= print_r($order, true);
$logData .= '--------------------' . PHP_EOL;
file_put_contents(ABSPATH . '/wco-orders.log', $logData, FILE_APPEND | LOCK_EX);
}
}
init();
Upvotes: 0
Views: 3726
Reputation: 3777
I got it!
The issue was that I didn't update the Advanced options: Receiver email
address under WooCommerce -> Settings -> Checkout (tab) -> PayPal (sub-link)
. This should also match right above it, for your PayPal email address. Remember, this is sandbox, so it should have -facilitator
at the end of your email, before the @
. I actually created -seller
and -buyer
to make things easier to remember. Also, ensure you have the sandbox options checked. Before making a purchase, ensure the paypal url is sandbox.paypal.com
, else you will spend real money lol.
If you click the order number (under the orders list) and look at the notes on the right hand side, it was saying the email address didn't match the one for the IPN. So once I did the above, and both emails matched (my PP sandbox seller emails), boom, they worked :) It actually came in under yellow "processing", then refreshed the page, and it was green "completed". So it takes a few seconds for PP to send the IPN. Give it 30 seconds, refresh your orders list, and it should show completed.
After that was ironed out, the woocommerce_payment_complete
hook was triggered, and my 3rd party API logged the data!
How to getting WooCommerce PayPal Sandbox to work.
I had issues using Google Chrome with the Developers and Sandbox sections of PayPal. I remember dealing with them a long time ago, and had the same problems! So the dev and sandbox sections of PayPal, flat out suck. I switched to FireFox though, and things worked a LOT better!
So use FireFox, it will save you some frustrating issues. I was trying to create sandbox accounts, and they were all failing to create. Switched to FF and they worked instantly.
Create new sandbox accounts (from within your developer.paypal.com account). It will use your real email, only append -something
to it. So I created [email protected]
and [email protected]
. Then delete, or just don't use, the pre-created ones, like facilitator
. The -seller
and -buyer
just make more sense and are easier to remember and type...
When creating a buyer account, give yourself a lot of money, like 99999. You can't edit the funds later! So it will save you from having to create a new buyer test account.
Ensure WooCommerce is in Sandbox mode.
Ensure paypal emails are using the sandbox email ([email protected]), whichever your using as your seller sandbox account, it must be the same on the PayPal settings page. It will ensure the IPN matches the seller accounts email, and fail if not. When woo thinks it's a fake/fraud attempt, it marks it "on hold".
So if your orders are "On Hold", then double check your Woo PayPal settings, specifically the email addresses your using.
IPN URL: Login at sandbox.paypal.com
to your sellers sandbox account ([email protected]). Go to Profile > My Selling Tools
, Then click the link for Instant payment notifications
.
Enter this URL, but replace the placeholder with your own domain and path:
http://[YOURSITE.COM]?wc-api=WC_Gateway_Paypal
So if your wordpress is joesblog.com
it would be http://joesblog.com?wc-api=WC_Gateway_Paypal
In this instance, I have wordpress on a dev sub-domain AND within a blog folder. So if that is you, then here is an example for that IPN URL:
http://dev.mysite.com/blog/?wc-api=WC_Gateway_Paypal
I hope this helps someone :) Good Luck!
Upvotes: 2