WC 2.2 payment gateways: Adding refund support and transaction IDs

Payment Gateways in WC 2.2 can be improved with the introduction of two new features; being able to store a transaction ID in a standardised format, and being able to programatically handle refunds.

Storing transaction IDs

Storing transaction IDs is useful because these will appear in the order interface:

2014-08-05 at 16.41

If your gateway makes use of the $order->payment_complete() method, storing your transaction ID is a simple process. Simply pass the transaction ID to the method:

$order->payment_complete( $transaction_id );

WooCommerce will then store the ID as post meta.

If you need to do this manually for any reason, you can set it directly using add_post_meta:

add_post_meta( $order->id, '_transaction_id', $transaction_id, true );

Turning the transaction ID into a link

You’ll notice in the screenshot above, the transaction ID is a link – in this case linking the the transaction on the PayPal site. If you want to do this for your gateway, in your constructor set a “view_transaction_url” property, e.g:

$this->view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';

WooCommerce will substitute the %s for the transaction ID during output.

Supporting refunds

To support refunds you need to create a process_refund() method in your gateway class. Without showing the refund code itself (which youโ€™ll be writing) your method should look something like:

public function process_refund( $order_id, $amount = null ) {
  // Do your refund here. Refund $amount for the order with ID $order_id
  return true;
}

This method should return a bool (true or false, if the refund was successfull or unsuccessfull) or a WP_Error object if something went wrong.

As well as created this method, you need to define support for refunds. To do this, set the โ€œsupportsโ€ property in your constructor, e.g.:

$this->supports = array(
  'products',
  'refunds'
);

This will let WooCommerce know that your gateway can handle refunds.


Keep yourself in the loop!

This field is hidden when viewing the form
This field is hidden when viewing the form
This field is hidden when viewing the form


6 responses to “WC 2.2 payment gateways: Adding refund support and transaction IDs”

  1. Emyr Thomas Avatar
    Emyr Thomas

    This is fantastic – excellent work! I assume the bundled PayPal gateway will be one of the first to support refunds. Will your official Stripe gateway support refunds by the time WC 2.2 is released?

    1. PayPal has it yes – it does require some extra API details, but once setup it works well.

      I’ll be looking at adding this to Stripe and a few others over the next few weeks.

  2. Lee Willis Avatar
    Lee Willis

    Hi Mike,

    I’m just adding support for refunds / transaction IDs to the Paymill gateway – can you confirm what an amount of “null” – means – is it “no refund”, or (As the bundled PayPal gateway code implies) that a “full” refund should be attempted. If so – would it be better if WC core sent the amount needing to be refunded, rather than having the gateways try and work it out?

    1. Unsure on best handling for this one – the gateways I’ve done so far have all supported doing a ‘full’ refund without defining an amount.

  3. Awesome! I can’t wait until Authorize.net gets some support for this! Is this something Woo is working on or is this up to the payment gateway and other developers?

  4. Mike Schwarz Avatar
    Mike Schwarz

    @mikejolley — Noticing that refunds for PayPal are working good, but they aren’t yet working for PayPal Pro (Version 4.1.0)? Seems like the refunds API interface would be similar for PayPal and PayPal Pro. Any idea when there will be refund support for PayPal Pro?

    Good work on the support for PayPal though.

Leave a Reply

Your email address will not be published. Required fields are marked *