How to add refund support and transaction id while WooCommerce payment gateway development?

Photo of author
Written By geekerhub

Lorem ipsum dolor sit amet consectetur pulvinar ligula augue quis venenatis. 

While developing a new payment gateway we can easily implement the payment method to collect the payment using the example from the google. But when further process come to store transaction ids and allow refunds , most of initial developer get confused.

To allow refund support, you need to add support in your payment gateway.
 "$this->supports = array( 'refunds' );"

So i will add some basic code snippet and the function which are related to the refund and transaction id.

List of functions associated with basic payment gateway for collecting payment and refunds are:

PROCESS PAYMENT FUNCTIONS to collect payment

public function process_payment( $order_id ) {
	global $woocommerce;
    $customer_order = new WC_Order( $order_id );
    $customer_order->get_currency();
    $customer_order->order_total;

	//you need to do your logic for collecting payment over here
	/*
		YOUR LOGIC ON PAYMENT SUCCESS
	*/
	if ( PAYMENT_RESPONSE_SUCCESS) {
		// Payment successful
		$transactionId= TRANSACTION ID FROM PAYMENT RESPONSE;
		$customer_order->add_order_note( esc_attr__( 'Payment Success', 'text-domain' ) );
		$customer_order->payment_complete($transactionId);
		$woocommerce->cart->empty_cart();
		return array(
			'result'   => 'success',
			'redirect' => $this->get_return_url( $customer_order ),
			);
	}

}
PROCESS REFUND FUNCTIONS to refund payment.
public function process_refund( $order_id, $amount = null, $reason = '' ) {

	global $woocommerce;
	$customer_order = new WC_Order( $order_id );
	$customer_order->get_currency();
	$refundamount = $amount;
	$transactionId = get_post_meta( $order_id, '_transaction_id', true );

	//you need to do your logic for refund functionality over here

	/*
		RETURN TRUE IF YOUR LOGIC ON REFUND IS SUCCESS ELSE RETURN FALSE
	*/
}

Leave a Comment