1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
<?php
defined( 'ABSPATH' ) || exit();
if ( ! class_exists( 'WC_Braintree_Advanced_Settings_API' ) ) {
return;
}
class WC_Braintree_Fee_Settings extends WC_Braintree_Advanced_Settings_API {
public function __construct() {
$this->id = 'braintree_fee';
$this->tab_title = __( 'Fee Settings', 'woo-payment-gateway' );
$this->braintree_documentation_id = 'fee-settings';
parent::__construct();
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_cart_fees' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
}
public function init_form_fields() {
$this->form_fields = apply_filters(
'wc_braintree_fee_form_fields',
array(
'title' => array(
'title' => __( 'Fees', 'woo-payment-gateway' ),
'type' => 'title',
'description' => __(
'You can add a fee to the customer\'s order such as a convenience fee for accepting credit cards.
<a target="_blank" href="https://docs.paymentplugins.com/wc-braintree/config/#/braintree_advanced?id=fee-settings">Fee Guide & Examples</a>',
'woo-payment-gateway'
),
),
'enabled' => array(
'type' => 'checkbox',
'title' => __( 'Enabled', 'woo-payment-gateway' ),
'default' => 'no',
'value' => 'yes',
'desc_tip' => true,
'description' => __( 'If enabled, you can charge fees on the checkout page.', 'woo-payment-gateway' ),
),
'fees' => array(
'type' => 'fee',
'title' => '',
),
)
);
}
public function enqueue_admin_scripts() {
wp_enqueue_script(
'wc-braintree-fee-settings',
braintree()->assets_path() . 'js/admin/fee-settings.js',
array(
'wc-braintree-admin-settings',
'wc-enhanced-select',
'underscore',
'backbone',
),
braintree()->version,
true
);
}
public function generate_fee_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$data = wp_parse_args(
$data,
array(
'title' => '',
'class' => '',
'desc_tip' => true,
)
);
$gateways = wc_braintree_get_fee_gateways();
ob_start();
include braintree()->plugin_path() . 'includes/admin/views/fee-template.php';
return ob_get_clean();
}
public function validate_fee_field( $key, $value ) {
$value = is_array( $value ) ? $value : array();
return $value;
}
public function enqueue_frontend_scripts() {
if ( $this->is_active( 'enabled' ) && is_checkout() && $this->has_fees() ) {
$scripts = braintree()->scripts();
$scripts->enqueue_script( 'checkout-fees', $scripts->assets_url( 'js/frontend/checkout-fees.js' ), array( 'jquery' ) );
}
}
public function after_calculate_totals( $cart ) {
$this->add_cart_fees( $cart );
}
public function add_cart_fees( $cart ) {
if ( $this->is_active( 'enabled' ) && $this->has_fees() ) {
if ( isset( $cart->recurring_cart_key ) || isset( $cart->is_recurring_cart ) ) {
return;
}
$this->calculate_fees( $cart );
}
}
public function calculate_fees( $cart ) {
$fees = $this->get_option( 'fees' );
$gateway_id = WC()->session->get( 'chosen_payment_method', false );
if ( ! empty( $fees ) && $gateway_id ) {
foreach ( $fees as $fee ) {
$fee = apply_filters( 'wc_braintree_fee_attributes', $fee );
if ( in_array( $gateway_id, $fee['gateways'] ) ) {
$taxable = $fee['tax_status'] === 'taxable';
$cart->add_fee( $fee['name'], $this->calculate_amount( $fee, $cart ), $taxable );
}
}
}
}
private function calculate_amount( $fee, $cart ) {
if ( ! class_exists( 'WC_Eval_Math' ) ) {
include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
}
$args = $this->get_args( $cart );
$calculation = str_replace( array_keys( $args ), $args, $fee['calculation'] );
return WC_Eval_Math::evaluate( $calculation );
}
private function get_args( $cart ) {
return apply_filters(
'wc_braintree_fee_args',
array(
'[qty]' => $cart->get_cart_contents_count(),
'[cost]' => $cart->cart_contents_total
)
);
}
private function has_fees() {
$fees = $this->get_option( 'fees' );
return ! empty( $fees );
}
}