Skip to content

Commit a35038f

Browse files
committedFeb 27, 2025·
feat: Add refund support to Cash payment gateway and refactor initialization
1 parent babdb26 commit a35038f

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
 

‎includes/Gateways/Cash.php

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace WeDevs\WePOS\Gateways;
33

4+
use Automattic\WooCommerce\Enums\OrderInternalStatus;
5+
46
/**
57
* Cash gateway payment for POS
68
*/
@@ -36,6 +38,9 @@ protected function setup_properties() {
3638
$this->method_title = __( 'Cash', 'wepos' );
3739
$this->method_description = __( 'Have your customers pay with cash', 'wepos' );
3840
$this->has_fields = false;
41+
$this->supports = array(
42+
'refunds'
43+
);
3944
}
4045

4146
/**
@@ -118,4 +123,32 @@ public function process_payment( $order_id ) {
118123
);
119124
}
120125

126+
/**
127+
* Process refund.
128+
*
129+
* If the gateway declares 'refunds' support, this will allow it to refund.
130+
* a passed in amount.
131+
*
132+
* @param int $order_id Order ID.
133+
* @param float|null $amount Refund amount.
134+
* @param string $reason Refund reason.
135+
* @return bool|\WP_Error True or false based on success, or a WP_Error object.
136+
*/
137+
public function process_refund( $order_id, $amount = null, $reason = '' ) {
138+
$order = wc_get_order( $order_id );
139+
140+
if ( ! $this->can_refund_order( $order ) ) {
141+
return new \WP_Error( 'error', __( 'Refund failed.', 'woocommerce' ) );
142+
}
143+
144+
$order->add_order_note(
145+
/* translators: 1: Refund amount, 2: Refund reason */
146+
sprintf( __( 'Refunded %1$s - Reason: %2$s', 'woocommerce' ), $amount, $reason ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
147+
);
148+
149+
$order->update_status(OrderInternalStatus::REFUNDED );
150+
$order->save();
151+
152+
return true;
153+
}
121154
}

‎includes/Gateways/Manager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Manager {
1414
*/
1515
public function __construct() {
1616
add_action( 'plugins_loaded', [ $this, 'init_gateways' ], 11, 1 );
17-
add_action( 'woocommerce_payment_gateways', [ $this, 'payment_gateways' ] );
17+
add_filter( 'woocommerce_payment_gateways', [ $this, 'payment_gateways' ] );
1818
}
1919

2020
/**

‎wepos.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public function includes() {
313313
public function init_hooks() {
314314
add_action( 'init', [ $this, 'init_classes' ] );
315315
add_action( 'init', [ $this, 'localization_setup' ] );
316+
add_action( 'wepos_loaded', [ $this, 'load_payment_gateways' ] );
316317
}
317318

318319
/**
@@ -340,9 +341,6 @@ public function init_classes() {
340341
$this->container['common'] = new WeDevs\WePOS\Common();
341342
$this->container['rest'] = new WeDevs\WePOS\REST\Manager();
342343
$this->container['assets'] = new WeDevs\WePOS\Assets();
343-
344-
// Payment gateway manager
345-
$this->container['gateways'] = new \WeDevs\WePOS\Gateways\Manager();
346344
}
347345

348346
/**
@@ -354,6 +352,18 @@ public function localization_setup() {
354352
load_plugin_textdomain( 'wepos', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
355353
}
356354

355+
/**
356+
* Load the payment gateways.
357+
*
358+
* @since WEPOS_SINCE
359+
*
360+
* @return void
361+
*/
362+
public function load_payment_gateways() {
363+
// Payment gateway manager
364+
$this->container['gateways'] = new \WeDevs\WePOS\Gateways\Manager();
365+
}
366+
357367
/**
358368
* On WC init, include cart required files in REST request
359369
*

0 commit comments

Comments
 (0)
Please sign in to comment.