Tweaking WooCommerce payment workflows

I’m playing part-time webmaster for the choir I sing in, and as such, am getting up close and personal with WooCommerce. Quite a nifty shopping cart, but it does require a lot of tweaks to really make it work to your liking – unless you’re willing to shell out a lot of cash.

The latest modification was changing the workflow of the payment gateways – more specifically, the BACS gateway (Bank Account Clearing System – or as we mortals call it, wire transfer).

The default flow for WooCommerce (for this gateway) is:

  1. Order is put in by customer
  2. Order is automatically flagged as on-hold, and a mail is sent out to the customer with the bank info
  3. Customer (supposedly) pays
  4. Store manager sees the payment, and flags order as processing – another mail is sent out with the notification that it’s being processed
  5. Store manager (hopefully) ships the product, flags the order as completed and another mail is sent out with ‘order complete’ status.

Now, for our uses, the on-hold status is a bit superfluous (and we’ve had people getting confused by it).
We’d rather have it go straight to processing, and have that mail contain the bank information (only for BACS payments, ofcourse).

After some testing, I came up with two solutions: One very hacky, and not maintainable, the other better. Both solutions need to be inserted in your theme’s functions.php file.

/* override gateway for BACS */
function my_core_gateways($methods) {
  foreach ($methods as &$method) {
    if($method == 'WC_Gateway_BACS') {
      $method = 'WC_Gateway_BACS_custom';
    }
  }
  return $methods;
}

/* custom gateway processor for BACS */
class WC_Gateway_BACS_custom extends WC_Gateway_BACS {
  public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {

    if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
      if ( $this->instructions ) {
        echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
      }
 
     /* dirty hack to get access to bank_details */
     $reflector = new ReflectionObject($this);
     $method = $reflector->getMethod('bank_details');
     $method->setAccessible(true);
 
     $result = $method->invoke($this, $order->id);
    }
  }

  public function process_payment( $order_id ) {
    $order = wc_get_order( $order_id );

    // Mark as processing (we're awaiting the payment)
    $order->update_status( 'processing', __( 'Awaiting BACS payment', 'woocommerce' ) );

    // Reduce stock levels
    $order->reduce_order_stock();

    // Remove cart
    WC()->cart->empty_cart();

    // Return thankyou redirect
    return array(
      'result' => 'success',
      'redirect' => $this->get_return_url( $order )
    );
  }
}

I have several reservations with the code above: it’s basically shamelessly copying and overloading the two functions of the parent class, and calling a private function which is internal to the parent class – both of which might cause trouble if there are big changes in WooCommerce. It works, but well, it’s .. ugly. So, I looked for a better way to tackle this.

sadfasdfasdf
add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );
add_action( 'woocommerce_thankyou', 'bacs_order_payment_processing_order_status', 10, 1 );

function bacs_order_payment_processing_order_status( $order_id ) {
  if ( ! $order_id ) {
    return;
  }

  $order = new WC_Order( $order_id );
 
  if ('bacs' === $order->payment_method && ('on-hold' == $order->status || 'pending' == $order->status)) {
    $order->update_status('processing');
  } else {
    return;
  }
}

function add_order_email_instructions( $order, $sent_to_admin ) {
  if ( ! $sent_to_admin && 'bacs' === $order->payment_method && $order->has_status( 'processing' ) ) {
    $gw = new WC_Gateway_BACS();
 
    $reflector = new ReflectionObject($gw);
    $method = $reflector->getMethod('bank_details');
    $method->setAccessible(true);
 
    $result = $method->invoke($gw, $order->id);
  }
}

Still not as clean as I’d like, as we’re still invoking an internal function, but atleast we’re using the proper hooks to tweak WooCommerce. I’ll update if I ever find a better way to get to the bank details.

Using a Yubikey for account security

I got a Yubikey 4 half a year ago (during Red Hat Summit 2016), but until now I didn’t do much with it. Time to change that ;)

If you have any more ideas on how to use the Yubikey, feel free to comment!

Also, If you’re not using 2  factor authentication yet, I urge you to start using it. It gives you a nice additional layer of account security, with limited hassle. It doesn’t even have to cost you any money, if you’re using a software solution. Checkout twofactorauth.org for a (non-comprehensive) list of sites that support it!

 

Replacing Crashplan

I’ve been a longtime user of Crashplan, an easy-to-use cloud backup solution. It works well, and it used to work also on nearly any platform that had a java run-time and some add-on opensource libraries. I’ve used it for some time on my raspberry pi to automatically backup my data to the cloud. (Crashplan on ARM (the architecture of the raspberry pi) is an unsupported configuration though).

Used to work, past tense.

Code42 (the company behind Crashplan) decided to incorporate a new library (libc42archive.so) in the latest update of their client, version 4.8, which has no ARM counterpart. Only x86 (and amd_64) architectures are supported, removing a lot of devices which were able to run crashplan from the list. No source code is available, so this is basically a call to stop using Crashplan on anything other than Intel-compatible architectures. Bleh.
(I opened a support ticket to ask them to restore compatibility, but I’m not holding my breath for it)

I was able to keep it alive for some time by downgrading back to version 4.7 and making the upgrade directory immutable, but it seems that this trick has run it’s course. The client needs to be version 4.8 or you aren’t allowed to connect to the Crashplan back-end.

So, I needed a new solution. One with the requirements of being open source (I don’t want to run in that issue again), offering client-side encryption and incremental forever style backups. Being able to be stored in the cloud was a no-brainer. After some testing of various tools, I ended up with the following combination:

While Crashplan offered immediate push to the cloud, the workflow is now somewhat different: every day a script is triggered (via cron), which executes borgbackup against a USB-connected harddisk for my local (and optionally NFS-shared) data. This allows for fast backups, fast deduplication, and encryption. No data leaves my network at this point.
When all backups are done, the encrypted repository is synced (using rclone) to Backblaze B2, bringing my offsite backup in sync with the local repository.

Using an intermediate USB harddisk is not ideal, but it gives me yet another copy of my data – which is convenient when I’ve just deleted a file that I really did want to keep.

To give you an idea about the compression and deduplication statistics:

                       Original size      Compressed size    Deduplicated size
All archives:                1.10 TB              1.07 TB            446.63 GB

1.10TB is compressed to 1.07TB, and this results in an archive if 446GB. Less than half ;)

To be able to find a file that has been deleted at some point, you can use borgbackup mount :: /<mountpoint> – this will mount the entire repository (using FUSE) on that directory, making it available for browsing. Don’t forget to unmount it using fusermount -u /<mountpoint> when you’re finished.

I’ve uploaded the script to my scripts repository on GitHub.

A new home, a new look

I finally decided to retire my custom-written CMS (wrote it back in 2003, when I had more free time), which – while obviously fantastic, super advanced, and capable of making coffee for you – had some drawbacks. Mostly being that it was a pain to use and update content with.

I also maintained a blog, on another domain, which was being updated more frequently than this site.

So, to make matters easier on myself, I’ve merged the (old) articles with the blog and moved it to my main domain – kcore.org.

I also threw in a new look (which – If you’re reading this on RSS – you won’t notice), activated SSL (thanks to Let’s Encrypt), and installed a boatload of redirects to keep the spice flowing!

Welcome! :)

Running crashplan (headless) on a Raspberry pi 2

In my grand scheme of “abuse all the low-power computing things!”, I’ve moved my crashplan backups over to the Raspberry Pi 2 (rpi2 for short). Installation is relatively painless: download the installer from the crashplan site, and unpack and execute. I installed mine under /opt/crashplan.

Afterwards, there are some things to fix, though, as by default Crashplan is only supported on the Intel architecture:

Install a working JRE (& dependencies for the GUI app should you want to launch it through X forwarding):
apt-get install oracle-java8-jdk libswt-gtk-3-jni libswt-cairo-gtk-3-jni
rm /opt/crashplan/jre; ln -s /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/ /opt/crashplan/jre
rm /opt/crashplan/lib/swt.jar; ln -s /usr/share/java/swt.jar /opt/crashplan/lib/swt.jar

Replace some libraries by their recompiled variants – you can compile them yourself (thanks to Jon Rogers for the instructions) or download them straight from his site if you’re lazy.
wget http://www.jonrogers.co.uk/wp-content/uploads/2012/05/libmd5.so -O /opt/crashplan/libmd5.so
wget http://www.jonrogers.co.uk/wp-content/uploads/2012/05/libjtux.so -O /opt/crashplan/libjtux.so

Add a library to the CrashplanEngine startup classpath:
sed -i 's|FULL_CP="|FULL_CP="/usr/share/java/jna.jar:|' /opt/crashplan/bin/CrashPlanEngine
And now you should be able to start your engine(s)!
/opt/crashplan/bin/CrashPlanEngine start
And the desktop app (which you can forward to your local Linux pc via ssh -X user@rpi2)
/opt/crashplan/bin/CrashPlanDesktop
this does take forever to start. But it works. Or you can use these instructions (from Crashplan Support) to administer it remotely.

ASUS UX305UA and Linux

The ASUS UX305UA is an ultrabook with the Skylake microarchitecture – the (as of writing) latest iteration in Intel processors. Unfortunately, Skylake support on Linux wasn’t really a granted thing the time the device got released. Fortunately it’s gotten a lot better of late.

After searching and reporting some bugs to the Debian Bugtracker, nearly everything works out of the box on Debian Sid (unstable), and probably soon on Stretch (current testing). So if you’re installing a new one now, I’d really suggest you go for Sid instead.

After installing the base system via a netinstall image, you’ll probably end up with a Stretch (testing) installation with a 4.3 kernel. This will not really work when rebooting, giving you a black screen. To solve that, boot with

i915.preliminary_hw_support=1 i915.modeset=0

on the kernel command line.

After this, I’d recommend adding a line for unstable and experimental to your apt sources:

# echo "deb http://httpredir.debian.org/debian/ unstable main contrib non-free" > /etc/apt/sources.list.d/unstable.list
# echo "deb http://httpredir.debian.org/debian/ experimental main contrib non-free" > /etc/apt/sources.list.d/experimental.list

and then upgrading your system to the latest unstable:
# apt-get update && apt-get dist-upgrade
This will result in you getting a linux-4.5 kernel and a boatload of updated drivers (eg. Xorg)

Next, upgrade even further: scary experimental mode on! This you’ll need to do manually (experimental never auto-upgrades, because of the possible breakage that might be caused):

First, find out the latest kernel

# apt-cache search linux-image-4 | grep amd64
linux-headers-4.5.0-1-amd64 - Header files for Linux 4.5.0-1-amd64
linux-image-4.5.0-1-amd64 - Linux 4.5 for 64-bit PCs
linux-image-4.5.0-1-amd64-dbg - Debugging symbols for Linux 4.5.0-1-amd64
linux-headers-4.6.0-rc3-amd64 - Header files for Linux 4.6.0-rc3-amd64
linux-image-4.6.0-rc3-amd64 - Linux 4.6-rc3 for 64-bit PCs
linux-image-4.6.0-rc3-amd64-dbg - Debugging symbols for Linux 4.6.0-rc3-amd64
linux-image-4.5.0-1-amd64-signed - Signatures for Linux 4.5.0-1-amd64 kernel and modules
linux-headers-4.4.0-1-grsec-amd64 - Header files for Linux 4.4.0-1-grsec-amd64
linux-image-4.4.0-1-grsec-amd64 - Linux 4.4 for 64-bit PCs, Grsecurity protection

As you can see above, 4.6.0-rc3 is available, but since it’s a prerelease kernel it’s not automatically installed. We want it, and with it, a bunch of firmware packages (to make sure we have the latest)
# apt-get install -t experimental linux-image-4.6.0-rc3-amd64 firmware-linux firmware-iwlwifi firmware-misc-nonfree intel-microcode
For good measure, you can even throw the latest iwlwifi firmware (not packaged yet in Debian) in the mix (found on GitHub):
# wget https://github.com/OpenELEC/iwlwifi-firmware/raw/master/firmware/iwlwifi-7265D-21.ucode -O /lib/firmware/iwlwifi-7265D-21.ucode
Next, reboot, and things should look a lot better already. Right now everything will work, except..

  • screen brightness buttons (Fn-F5 Fn-F6 Fn-F7). This requires (for now) this patch from kernel bugreport 98931. (Debian bugreport: 818494)
  • Screen auto brightness/ambient light (Fn-A): you can use the driver from GitHub
  • Disable-touchpad button (Fn-F7): you can use any old script, really. Just call synclient TouchpadOff=1 and it’s off. And =0 for on)

Using adb with a OnePlus X on Linux…

… is not really that hard. Just annoying. Since OnePlus’ USB ID is not in the default adb list, you need to add it yourself:
echo "0x2a70" >> ~/.android/adb_usb.ini
where 0x2a70 is the identifier for OnePlus. (you can find this with lsusb)

To add automatic permissions to the device node when it’s created, add this udev rule to /etc/udev/rules.d/51-android.rules (all on one line):

SUBSYSTEM=="usb", ATTR{idVendor}=="2a70", MODE="0666", GROUP="plugdev" ATTR{idVendor}=="2a70", ATTR{idProduct}=="9011|f003", SYMLINK+="libmtp-%k", MODE="660", GROUP="audio", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1"

Replacing OS X with Linux on my Mac Mini 2,1

I still had an old Mac Mini (model 2,1) – which I bought during a period of experimentation with different operating systems –  connected to the TV, running Mac OS X Lion. Not Apple’s finest installment of OS X, truth be told.

The reasons I wanted to get rid of it:

  • Apple stopped providing updates for it. Not fantastic from a security point of view.
  • They also managed to actually break VNC for anything except the OS X client
  • TeamViewer takes up a ridiculous amount of CPU power on OS X
  • You can’t turn off the Mac Mini using the power button, it goes to sleep, and it can’t be reprogrammed.
  • It’s just .. sooo… slooooooooow

The only thing the device is used for is

  • iTunes to manage an iPod classic, and to auto-rip newly bought CD’s
  • Using Videostream to cast movies to our Chromecast
  • Playing music from the audio library to the connected amplifier

Not much, really. So, in the end, being tired of the general slowness of the device, I bit the bullet, exchanged the old 80GB hard disk with a newer and bigger model, and went on the journey to install Debian on it.

So, the road to success was:

  1. download the multiarch network install CD image, burn it to a CD. 1
    Why multi-arch, you might ask? Why not use the x86_64 (64-bit) install image, as the Intel Core2Duo is capable of handling this? Because Apple, in all their wisdom, decided to include a 32-bit EFI with a CPU capable of handling 64-bit code. So you get a bit of a schizophrenic situation. The multiarch CD image supports both 32-bit and 64-bit (U)EFI, and hence, it works for this device.
  2. boot from said CD (press and hold the ALT button as soon as the grey screen appears on your Mac)
  3. profit!

I installed:

All in all it works rather nicely. The only problems I ran into was with respect to the iPod management, which was solved by resetting the iPod with iTunes for windows, which formatted the device as VFAThttps://en.wikipedia.org/wiki/File_Allocation_Table#FAT32, instead of Mac OS’ HFS+.

  1. note that this link points to the daily built CD images, which might or might not be broken at any given day