Support

Home Forums Event Espresso Premium Add Fee To Credit Card Payments Only

Add Fee To Credit Card Payments Only

Posted: June 17, 2024 at 6:16 pm

Viewing 3 reply threads


cata-log

June 17, 2024 at 6:16 pm

We want to add a fee of 3% for all payments made with credit card. We use Authorize.net, so the fee would apply only if the payment id is ‘aim’.

Here is our current code, but it is applying to all payment methods:

add_filter(‘FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge’, ‘__return_true’);
add_filter(‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘ee_cc_surcharge_details’);

function ee_cc_surcharge_details() {
return array(
‘name’ => ‘Credit Card Surcharge’,
‘code’ => ‘credit-card-surcharge’,
‘description’ => ‘3% surcharge for credit card payments’,
‘percent’ => 3,
‘taxable’ => false,
);
}

/**
* DO NOT EDIT ANYTHING EXCEPT DEFAULT SURCHARGE DETAILS
*
* bc_ee_apply_transaction_surcharge
*
* @param \EE_Checkout $checkout
* @return \EE_Checkout
*/
function bc_ee_apply_transaction_surcharge(EE_Checkout $checkout) {
// DEFAULT SURCHARGE DETAILS – EDIT THIS
$surcharge_details = apply_filters(
‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’,
array(
// name for surcharge that will be displayed
‘name’ => ‘Credit Card Surcharge’,
// unique code used to identify surcharge in the db
‘code’ => ‘credit-card-surcharge’,
// description for line item
‘description’ => ‘3% surcharge for credit card payments’,
// percentage amount
‘percent’ => 3,
// whether or not tax is applied on top of the surcharge
‘taxable’ => false,
)
);

// STOP EDITING
// verify checkout
if (!$checkout instanceof EE_Checkout) {
return $checkout;
}

// verify cart
$cart = $checkout->cart;
if (!$cart instanceof EE_Cart) {
return $checkout;
}

// verify grand total line item
$grand_total = $cart->get_grand_total();
if (!$grand_total instanceof EE_Line_Item) {
return $checkout;
}

// check for existing surcharge
$existing_surcharge = $grand_total->get_child_line_item($surcharge_details[‘code’]);

// remove the surcharge if payment method is not ‘aim’
if ($existing_surcharge instanceof EE_Line_Item && $checkout->selected_method_of_payment !== ‘aim’) {
$grand_total->remove_item($existing_surcharge->ID());
$grand_total->recalculate_total_including_taxes();
$cart->set_grand_total($grand_total); // Ensure the cart is updated
return $checkout;
}

// apply new surcharge if payment method is ‘aim’ and surcharge does not already exist
if ($checkout->selected_method_of_payment === ‘aim’ && !$existing_surcharge) {
EE_Registry::instance()->load_helper(‘Line_Item’);
$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal($grand_total);
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance(array(
‘LIN_name’ => $surcharge_details[‘name’],
‘LIN_desc’ => $surcharge_details[‘description’],
‘LIN_unit_price’ => 0,
‘LIN_percent’ => $surcharge_details[‘percent’],
‘LIN_quantity’ => NULL,
‘LIN_is_taxable’ => $surcharge_details[‘taxable’],
‘LIN_order’ => 0,
‘LIN_total’ => (float)($surcharge_details[‘percent’] * ($pre_tax_subtotal->total() / 100)),
‘LIN_type’ => EEM_Line_Item::type_line_item,
‘LIN_code’ => $surcharge_details[‘code’],
))
);
$grand_total->recalculate_total_including_taxes();
$cart->set_grand_total($grand_total); // Ensure the cart is updated
}

return $checkout;
}
add_filter(‘FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout’, ‘bc_ee_apply_transaction_surcharge’);


Tony

  • Support Staff

June 18, 2024 at 7:08 am

Hi there,

Currently, we don’t have a method to add surcharges based on the selected payment method.

The code you are using now adds a surcharge before the payment method is selected, which is too early.

We have another example of setting a surcharge if Invoice is selected, but that happens AFTER the selection and finalize button is clicked:

https://github.com/eventespresso/ee-code-snippet-library/blob/master/checkout/bc_add_cart_modifier.php

For what you are currently looking for you’ll need custom development to hook into when the payment method are selected to add/remove the surcharge and refresh/update the page to display the relevant line items for the fees.


cata-log

June 18, 2024 at 8:48 am

Thanks, Tony. We have used the script you provided and edited it to our needs. We are adding a surcharge fee if the payment method is a credit card. However, it is creating 2 new problems now.

1) When the credit card is selected, the fee will appear on the receipt but the payment will be made having only the event price with no fee. Generating an owned amount as seen in the screenshot. https://share.zight.com/jkupWdxX . This owned amount can not be paid by clicking on make a payment.
2) The second problem is that if the person selects the invoice PDF method, he can pay later. When he clicks on make a payment there will be a credit card option and the surcharge won’t apply in this case.

Here is the code we edited for our needs:

add_action( ‘AHEE__Single_Page_Checkout__before_payment_options__process_reg_step’, ‘bc_add_cart_modifier’, 10, 1 );
function bc_add_cart_modifier( EE_SPCO_Reg_Step $payment_options_reg_step ) {
// CHANGE THESE TO YOUR LIKING
$cart_modifier_name = ‘credit card surcharge’;
$cart_modifier_amount = 3.00;
$cart_modifier_description = ‘3% surcharge for choosing credit card payment method’;
$cart_modifier_taxable = false;
$payment_methods_with_surcharges = array( ‘aim’ );
// get what the user selected for payment method
$selected_method_of_payment = $payment_options_reg_step->checkout->selected_method_of_payment;
if ( ! in_array( $selected_method_of_payment, $payment_methods_with_surcharges, true ) ) {
// does not require surcharge
return;
}
$cart = $payment_options_reg_step->checkout->cart;
if ( ! $cart instanceof EE_Cart ) {
// ERROR
return;
}
$total_line_item = $cart->get_grand_total();
if ( ! $total_line_item instanceof EE_Line_Item && ! $total_line_item->is_total() ) {
// ERROR
return;
}
$transaction = $payment_options_reg_step->checkout->transaction;
if ( ! $transaction instanceof EE_Transaction) {
// ERROR
return;
}
//delete existing in case page is refreshed or something
EEM_Line_Item::instance()->delete(
array(
array(
‘TXN_ID’ => $transaction->ID(),
‘LIN_name’ => $cart_modifier_name,
)
)
);
$amount_owing_before = $payment_options_reg_step->checkout->amount_owing;
$surcharge_line_item_id = EEH_Line_Item::add_percentage_based_item(
$total_line_item,
$cart_modifier_name,
$cart_modifier_amount,
$cart_modifier_description,
$cart_modifier_taxable
);
if ($surcharge_line_item_id) {
$total_line_item->recalculate_total_including_taxes();
$payment_options_reg_step->checkout->amount_owing += ($total_line_item->total() – $amount_owing_before);
$transaction->set_total($total_line_item->total());
$success = $transaction->save();
if ($success) {
/** @type EE_Registration_Processor $registration_processor */
$registration_processor = EE_Registry::instance()->load_class(‘Registration_Processor’);
$registration_processor->update_registration_final_prices($transaction);
}
}
}


Tony

  • Support Staff

June 18, 2024 at 9:01 am

You need custom development for what you are trying to do, currently, Event Espresso does not have this functionality.

1) When the credit card is selected, the fee will appear on the receipt but the payment will be made having only the event price with no fee. Generating an owned amount as seen in the screenshot. https://share.zight.com/jkupWdxX .

Yeah, as mentioned that snippet applies AFTER the registrations has been finalized (after payment in this case).

2) The second problem is that if the person selects the invoice PDF method, he can pay later. When he clicks on make a payment there will be a credit card option and the surcharge won’t apply in this case.

That is essentially the same issue as above, the code you are now using hooks in at the end of the registration and adds the surcharge based on the select Payment method, in this example the surcharge is skipped because card was chosen during checkout revisit … before the snippet can run again.

Viewing 3 reply threads

You must be logged in to reply to this support post. Sign In or Register for an Account

Event Espresso