Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/167: Add WooPayments compatibility #201

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions includes/class-wc-gateway-payfast.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public function __construct() {

// Change Payment Method actions.
add_action( 'woocommerce_subscription_payment_method_updated_from_' . $this->id, array( $this, 'maybe_cancel_subscription_token' ), 10, 2 );

// Add support for WooPayments multi-currency.
add_filter( 'woocommerce_currency', array( $this, 'filter_currency' ) );
}

/**
Expand Down Expand Up @@ -786,7 +789,6 @@ public function handle_itn_request( $data ) {
. PHP_EOL . 'End ITN call'
. PHP_EOL . '----------'
);

}

/**
Expand Down Expand Up @@ -1174,7 +1176,6 @@ public function scheduled_subscription_payment( $amount_to_charge, $renewal_orde
}
// Payment will be completion will be capture only when the ITN callback is sent to $this->handle_itn_request().
$renewal_order->add_order_note( esc_html__( 'Payfast Subscription renewal transaction submitted.', 'woocommerce-gateway-payfast' ) );

}

/**
Expand Down Expand Up @@ -1727,7 +1728,7 @@ public function admin_notices() {
. wp_kses_post(
array_reduce(
$errors_to_show,
function( $errors_list, $error_item ) {
function ( $errors_list, $error_item ) {
$errors_list = $errors_list . PHP_EOL . ( '<li>' . $this->get_error_message( $error_item ) . '</li>' );
return $errors_list;
},
Expand Down Expand Up @@ -1796,4 +1797,45 @@ public function display_order_net( $order_id ) {

<?php
}

/**
* Filters the currency to 'ZAR' if set via WooPayments multi-currency feature.
*
* @param string $currency The currency code.
* @return string
*/
public function filter_currency( $currency ) {
// Do nothing if WooPayments is not activated.
if ( ! class_exists( '\WCPay\MultiCurrency\MultiCurrency' ) ) {
return $currency;
}

// Do nothing if the page is admin screen.
if ( is_admin() ) {
return $currency;
}

$user_id = get_current_user_id();

// Check if the currency is set in the URL.
if ( isset( $_GET['currency'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$currency_code = array(
sanitize_text_field(
wp_unslash( $_GET['currency'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
),
);
// Check if the currency is set in the session (for logged-out users).
} elseif ( 0 === $user_id && WC()->session ) {
$currency_code = WC()->session->get( \WCPay\MultiCurrency\MultiCurrency::CURRENCY_SESSION_KEY );
// Check if the currency is set in the user meta (for logged-in users).
} elseif ( $user_id ) {
$currency_code = get_user_meta( $user_id, \WCPay\MultiCurrency\MultiCurrency::CURRENCY_META_KEY );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with how multi-currency works so this feedback may not make sense. But wondering if there's a scenario where the user session or user meta hasn't been set and thus the currency code ends up being empty here?

I guess wondering if we should separate this code into two if statements. So we check for the currency value from the URL first and store that in the $currency_code variable. And then we overwrite that value from session or user meta, if that value exists in either. Something like:

$currency_code = '';
if ( isset( $_GET['currency'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$currency_code = array(
		sanitize_text_field(
			wp_unslash( $_GET['currency'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		),
	);
}

// Check if the currency is set in the session (for logged-out users).
if ( 0 === $user_id && WC()->session ) {
	$stored_code = WC()->session->get( \WCPay\MultiCurrency\MultiCurrency::CURRENCY_SESSION_KEY );

	// Not sure the best check on this value? Should it always be an array and we can check for that? Can it sometimes be a string?
	if ( $stored_code ) {
		$currency_code = $stored_code;
	}
// Check if the currency is set in the user meta (for logged-in users).
} elseif ( $user_id ) {
	$stored_code = get_user_meta( $user_id, \WCPay\MultiCurrency\MultiCurrency::CURRENCY_META_KEY );

	// Not sure the best check on this value? Should it always be an array and we can check for that? Can it sometimes be a string?
	if ( $stored_code ) {
		$currency_code = $stored_code;
	}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkotter I've fixed the confusion between the return value types array vs string. There was a small bug, thanks for pointing that out.

Regarding separating if statements into 2, the problem is that in Block Checkout, the user meta is saved after the block is rendered. So the block checkout page requires an additional reload for Payfast to appear in the list of gateways. We give precedence to the GET parameter, if that is set, then we use it, because ultimately that gets saved to the session and user meta.


if ( is_array( $currency_code ) && in_array( 'ZAR', $currency_code, true ) ) {
return 'ZAR';
}

return $currency;
}
}
Loading