Skip to content

Commit

Permalink
fix: let "Cover fees" checkbox work with any payment method (#3491)
Browse files Browse the repository at this point in the history
* fix: let "Cover fees" checkbox work with any payment method

* fix: only enable for Stripe payment gateway, for now

* fix: support transaction fees for WooPayments too

* chore: update method names + inline docs to exclude Stripe mentions

* fix: remove WooPayments as a supported gateway for transaction fees, for now

* fix: make default for newspack_donations_allow_covering_fees always true
  • Loading branch information
dkoo authored Oct 28, 2024
1 parent cc0c713 commit 74ea6cc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Add ability to cover transaction fees for an order.
* Add ability to cover transaction fees for donations completed via supported payment gateways.
*
* @package Newspack
*/
Expand All @@ -13,7 +13,8 @@
* WooCommerce Order UTM class.
*/
class WooCommerce_Cover_Fees {
const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees';
const CUSTOM_FIELD_NAME = 'newspack-wc-pay-fees';
const SUPPORTED_GATEWAYS = [ 'stripe' ];

/**
* Initialize hooks.
Expand All @@ -23,7 +24,7 @@ public static function init() {
\add_action( 'woocommerce_checkout_update_order_review', [ __CLASS__, 'persist_fee_selection' ] );
\add_action( 'woocommerce_cart_calculate_fees', [ __CLASS__, 'add_transaction_fee' ] );
\add_action( 'woocommerce_checkout_order_processed', [ __CLASS__, 'add_order_note' ], 1, 3 );
\add_action( 'wc_stripe_payment_fields_stripe', [ __CLASS__, 'render_stripe_input' ] );
\add_action( 'newspack_blocks_after_payment_fields', [ __CLASS__, 'render_transaction_fee_input' ] );
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'print_checkout_helper_script' ] );
}

Expand Down Expand Up @@ -94,7 +95,7 @@ public static function add_transaction_fee( $cart ) {
*/
public static function add_order_note( $order_id, $posted_data, $order ) {
if ( \WC()->session->get( self::CUSTOM_FIELD_NAME ) ) {
$order->add_order_note( __( 'The donor opted to cover Stripe\'s transaction fee.', 'newspack-plugin' ) );
$order->add_order_note( __( 'The donor opted to cover transaction fees.', 'newspack-plugin' ) );
}
}

Expand All @@ -117,7 +118,7 @@ private static function should_allow_covering_fees() {
// but at this point handling coupons + covering fees is an edge case.
return false;
}
if ( true !== boolval( get_option( 'newspack_donations_allow_covering_fees' ) ) ) {
if ( true !== boolval( get_option( 'newspack_donations_allow_covering_fees', true ) ) ) {
return false;
}
return true;
Expand All @@ -134,7 +135,7 @@ private static function should_apply_fee( $data ) {
if ( ! self::should_allow_covering_fees() ) {
return false;
}
if ( ! isset( $data['payment_method'] ) || 'stripe' !== $data['payment_method'] ) {
if ( ! isset( $data['payment_method'] ) || ! in_array( $data['payment_method'], self::SUPPORTED_GATEWAYS, true ) ) {
return false;
}
if ( ! isset( $data[ self::CUSTOM_FIELD_NAME ] ) || '1' !== $data[ self::CUSTOM_FIELD_NAME ] ) {
Expand All @@ -144,17 +145,19 @@ private static function should_apply_fee( $data ) {
}

/**
* Render the "cover fees" input for Stripe.
* Render the "cover transaction fees" input for supported payment gateways.
*
* @param string $payment_gateway The slug for the payment gateway rendering these payment fields.
*/
public static function render_stripe_input() {
if ( ! self::should_allow_covering_fees() ) {
public static function render_transaction_fee_input( $payment_gateway ) {
if ( ! self::should_allow_covering_fees() || ! in_array( $payment_gateway, self::SUPPORTED_GATEWAYS, true ) ) {
return;
}
?>
<fieldset>
<p class="form-row newspack-cover-fees" style="display: flex;">
<input
id="<?php echo esc_attr( self::CUSTOM_FIELD_NAME ); ?>"
id="<?php echo esc_attr( self::CUSTOM_FIELD_NAME . '_' . $payment_gateway ); ?>"
name="<?php echo esc_attr( self::CUSTOM_FIELD_NAME ); ?>"
type="checkbox"
style="margin-right: 8px;"
Expand All @@ -163,7 +166,7 @@ public static function render_stripe_input() {
checked
<?php endif; ?>
/>
<label for=<?php echo esc_attr( self::CUSTOM_FIELD_NAME ); ?> style="display:inline;">
<label for=<?php echo esc_attr( self::CUSTOM_FIELD_NAME . '_' . $payment_gateway ); ?> style="display:inline;">
<?php
$custom_message = get_option( 'newspack_donations_allow_covering_fees_label', '' );
if ( ! empty( $custom_message ) ) {
Expand Down Expand Up @@ -211,26 +214,19 @@ public static function print_checkout_helper_script() {
NEWSPACK_PLUGIN_VERSION,
[ 'in_footer' => true ]
);
wp_localize_script(
$handler,
'newspack_wc_cover_fees',
[
'custom_field_name' => self::CUSTOM_FIELD_NAME,
]
);
}

/**
* Get the fee multiplier value.
*/
private static function get_stripe_fee_multiplier_value() {
private static function get_fee_multiplier_value() {
return floatval( get_option( 'newspack_blocks_donate_fee_multiplier', '2.9' ) );
}

/**
* Get the fee static portion value.
*/
private static function get_stripe_fee_static_value() {
private static function get_fee_static_value() {
return floatval( get_option( 'newspack_blocks_donate_fee_static', '0.3' ) );
}

Expand All @@ -255,8 +251,8 @@ public static function get_fee_display_value( $subtotal ) {
* @param float $subtotal The subtotal to calculate the fee for.
*/
public static function get_fee_value( $subtotal ) {
$fee_multiplier = self::get_stripe_fee_multiplier_value();
$fee_static = self::get_stripe_fee_static_value();
$fee_multiplier = self::get_fee_multiplier_value();
$fee_static = self::get_fee_static_value();
$fee = ( ( ( $subtotal + $fee_static ) / ( 100 - $fee_multiplier ) ) * 100 - $subtotal );
return $fee;
}
Expand Down
30 changes: 23 additions & 7 deletions src/other-scripts/wc-cover-fees/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
/* globals jQuery, newspack_wc_cover_fees */
/* globals jQuery */
( function ( $ ) {
if ( ! $ ) {
return;
}
const $body = $( document.body );
const getInputs = function () {
return Array.from( document.querySelectorAll( '.newspack-cover-fees input' ) );
};
$body.on( 'init_checkout', function () {
const form = document.querySelector( 'form.checkout' );
if ( ! form ) {
return;
}
let checked = document.getElementById( newspack_wc_cover_fees.custom_field_name )?.checked;
let checked = document.querySelector( '.newspack-cover-fees input' )?.checked;
form.addEventListener( 'change', function () {
// Get element on every change because the DOM is replaced by AJAX.
const input = document.getElementById( newspack_wc_cover_fees.custom_field_name );
if ( ! input ) {
// Get elements on every change because the DOM is replaced by AJAX.
const inputs = getInputs();
if ( ! inputs.length ) {
return;
}
if ( checked !== input.checked ) {
checked = input.checked;

// Check if any checkbox has changed.
const update = inputs.find( function ( input ) {
if ( input.checked !== checked ) {
return true;
}
return false;
} );

// Update checked state of all checkboxes.
if ( update ) {
checked = update.checked;
inputs.forEach( function ( input ) {
input.checked = checked;
} );
$body.trigger( 'update_checkout', { update_shipping_method: false } );
}
} );
Expand Down

0 comments on commit 74ea6cc

Please sign in to comment.