| 1 |
<?php |
| 2 |
/** |
| 3 |
* Loads the POS Payment Gateways. |
| 4 |
* |
| 5 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 6 |
* |
| 7 |
* @see https://wcpos.com |
| 8 |
* @package WCPOS\WooCommercePOS |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS; |
| 12 |
|
| 13 |
/** |
| 14 |
* Gateways class. |
| 15 |
*/ |
| 16 |
class Gateways { |
| 17 |
/** |
| 18 |
* Constructor. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_filter( 'woocommerce_payment_gateways', array( $this, 'payment_gateways' ) ); |
| 22 |
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'available_payment_gateways' ), 99 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add POS gateways |
| 27 |
* BEWARE: some gateways/themes/plugins call this very early on every page!! |
| 28 |
* We cannot guarantee that $wp is set, so we cannot use woocommerce_pos_request. |
| 29 |
* |
| 30 |
* @param array $gateways The registered payment gateways. |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function payment_gateways( array $gateways ) { |
| 35 |
global $plugin_page; |
| 36 |
|
| 37 |
// Early exit for WooCommerce settings, ie: don't show POS gateways. |
| 38 |
if ( is_admin() && 'wc-settings' == $plugin_page ) { |
| 39 |
return $gateways; |
| 40 |
} |
| 41 |
|
| 42 |
// All other cases, the default POS gateways are added. |
| 43 |
return array_merge( |
| 44 |
$gateways, |
| 45 |
array( |
| 46 |
'WCPOS\WooCommercePOS\Gateways\Cash', |
| 47 |
'WCPOS\WooCommercePOS\Gateways\Card', |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get available payment POS gateways, |
| 54 |
* - Order and set default order |
| 55 |
* - Also going to remove icons from the gateways. |
| 56 |
* |
| 57 |
* - NOTE: lots of plugins/themes call this filter and I can't guarantee that $gateways is an array |
| 58 |
* |
| 59 |
* @param null|array $gateways The available payment gateways. |
| 60 |
* |
| 61 |
* @return null|array The available payment gateways. |
| 62 |
*/ |
| 63 |
public function available_payment_gateways( ?array $gateways ): ?array { |
| 64 |
// early exit. |
| 65 |
if ( ! woocommerce_pos_request() ) { |
| 66 |
return $gateways; |
| 67 |
} |
| 68 |
|
| 69 |
// use POS settings. |
| 70 |
$settings = woocommerce_pos_get_settings( 'payment_gateways' ); |
| 71 |
|
| 72 |
// Get all payment gateways. |
| 73 |
$all_gateways = WC()->payment_gateways->payment_gateways; |
| 74 |
|
| 75 |
$_available_gateways = array(); |
| 76 |
|
| 77 |
foreach ( $all_gateways as $gateway ) { |
| 78 |
if ( isset( $settings['gateways'][ $gateway->id ] ) && $settings['gateways'][ $gateway->id ]['enabled'] ) { |
| 79 |
if ( isset( $settings['gateways'][ $gateway->id ]['title'] ) ) { |
| 80 |
$gateway->title = $settings['gateways'][ $gateway->id ]['title']; |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* There is an issue over-writing the description field because some gateways use this for info, |
| 85 |
* eg: Account Funds uses it to show the current balance. |
| 86 |
*/ |
| 87 |
// if ( isset( $settings['gateways'][ $gateway->id ]['description'] ) ) { |
| 88 |
// $gateway->description = $settings['gateways'][ $gateway->id ]['description']; |
| 89 |
// }. |
| 90 |
|
| 91 |
$gateway->icon = ''; |
| 92 |
$gateway->enabled = 'yes'; |
| 93 |
$gateway->chosen = $gateway->id === $settings['default_gateway']; |
| 94 |
|
| 95 |
$_available_gateways[ $gateway->id ] = $gateway; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Order the available gateways according to the settings. |
| 100 |
uksort( |
| 101 |
$_available_gateways, |
| 102 |
function ( $a, $b ) use ( $settings ) { |
| 103 |
return $settings['gateways'][ $a ]['order'] <=> $settings['gateways'][ $b ]['order']; |
| 104 |
} |
| 105 |
); |
| 106 |
|
| 107 |
return $_available_gateways; |
| 108 |
} |
| 109 |
} |
| 110 |
|