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 all 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
46 changes: 43 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,43 @@ 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 = 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, true );
}

if ( is_string( $currency_code ) && 'ZAR' === $currency_code ) {
return 'ZAR';
}

return $currency;
}
}
Loading