Skip to content

Commit

Permalink
handle card gateways including WooPayments & Stripe plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Timur Karimov committed Apr 8, 2024
1 parent 480adaa commit d0854b0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
38 changes: 25 additions & 13 deletions includes/admin/class-wc-rest-payments-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,29 @@ public function validate_business_support_address( array $value, WP_REST_Request
return true;
}

/**
* Map plugin-specific card gateway IDs for Stripe and WooPayments plugins to 'card',
* so that the duplicates detector can identify them.
*
* @param array $gateways Gateways to prepare.
* @return array Mapped gateways.
*/
private function prepare_card_gateways( $gateways ) {
foreach ( $gateways as $gateway ) {
if ( 'yes' === $gateway->enabled ) {
if ( 'stripe' === $gateway->id ) {
$gateway->id = 'card';
}

if ( 'woocommerce_payments' === $gateway->id ) {
$gateway->id = 'card';
}
}
}

return $gateways;
}

/**
* Find duplicates.
*
Expand Down Expand Up @@ -481,8 +504,9 @@ private function find_duplicates() {
];

$gateways_qualified_by_duplicates_detector = [];
$gateways = $this->prepare_card_gateways( WC()->payment_gateways()->payment_gateways );

foreach ( WC()->payment_gateways()->payment_gateways as $gateway ) {
foreach ( $gateways as $gateway ) {
foreach ( $keywords as $keyword => $stripe_id ) {
if ( 'yes' === $gateway->enabled && strpos( $gateway->id, $keyword ) !== false ) {
$gateways_qualified_by_duplicates_detector[ $stripe_id ][] = $gateway->id;
Expand All @@ -497,18 +521,6 @@ private function find_duplicates() {
}
}

$gateways_to_skip = array_map(
function( $gateway ) {
return $gateway->id;
},
WC_Payments::get_payment_gateway_map()
);

foreach ( $gateways_qualified_by_duplicates_detector as $gateway_id => $gateway_ids ) {
$gateway_ids = array_diff( $gateway_ids, $gateways_to_skip );
$gateways_qualified_by_duplicates_detector[ $gateway_id ] = $gateway_ids;
}

return $gateways_qualified_by_duplicates_detector;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/duplicates/class-duplicates-finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function find_duplicates( $gateways ) {
if ( strpos( $gateway->id, $keyword ) !== false ) {
// Increment counter or initialize if not exists.
if ( isset( $counter[ $keyword ] ) ) {
$counter[ $keyword ]++;
++$counter[ $keyword ];
} else {
$counter[ $keyword ] = 1;
}
Expand Down

0 comments on commit d0854b0

Please sign in to comment.