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:
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.
Leave a Reply