get_eligible_gateways() ) ) { return; } $cart = function_exists( 'WC' ) ? WC()->cart : null; if ( ! $cart instanceof \WC_Cart || $cart->is_empty() ) { return; } if ( ! SDFW_Calculator::is_deposit_eligible( $cart ) ) { SDFW_Calculator::set_payment_mode( 'full' ); printf( '

%s

', esc_html( sprintf( /* translators: %s: formatted minimum order amount required for the deposit + COD split to apply */ __( 'Your order total is under %s, so the full amount is payable online now — nothing will be due on delivery.', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( sdfw_get_global_amount() ) ) ) ); return; } $total = (float) $cart->get_total( 'edit' ); $deposit = SDFW_Calculator::get_raw_deposit_for_cart( $cart ); $fee = sdfw_get_processing_fee_amount( $deposit ); $cod_remainder = SDFW_Calculator::get_cod_remainder( $total, $deposit ); $cod_due = $cod_remainder + $fee; // fee is added to the delivery amount, not charged online $mode = SDFW_Calculator::get_payment_mode(); // The deposit itself is exactly what gets charged online (see // maybe_override_total_for_active_order()) — the fee only affects // the on-delivery figure, so it's called out there instead of next // to the online amount. $deposit_option_label = $fee > 0.0 ? sprintf( /* translators: 1: formatted deposit amount, 2: formatted total due on delivery (remainder + fee), 3: formatted processing fee included in that delivery amount */ __( 'Pay %1$s deposit now + %2$s on delivery (includes %3$s processing fee)', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $deposit ), sdfw_format_price( $cod_due ), sdfw_format_price( $fee ) ) : sprintf( /* translators: 1: formatted deposit amount, 2: formatted COD remainder */ __( 'Pay %1$s deposit now + %2$s on delivery', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $deposit ), sdfw_format_price( $cod_due ) ); printf( '
' . '%1$s' . '' . '' . '
', esc_html__( 'Choose how to pay', 'aawahid-simple-deposits-for-woocommerce' ), checked( $mode, 'deposit', false ), esc_html( $deposit_option_label ), checked( $mode, 'full', false ), esc_html( sprintf( /* translators: %s: formatted full order total */ __( 'Pay full %s online now', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $total ) ) ) ); } /** * Persists the customer's payment-mode radio choice into the WC * session. Parses the checkout form's serialized fields the same * way this hook has always documented passing them. * * @param string $post_data Serialized checkout form fields. */ public function maybe_update_payment_mode_from_checkout( $post_data ): void { $fields = []; parse_str( (string) $post_data, $fields ); $mode = isset( $fields['sdfw_payment_mode'] ) ? sanitize_text_field( wp_unslash( $fields['sdfw_payment_mode'] ) ) : 'deposit'; SDFW_Calculator::set_payment_mode( $mode ); } /** * Resolves and saves the advance/COD split onto the order — but * only if the gateway the customer actually chose (bKash, Nagad, * etc.) is one the merchant has marked eligible to collect the * advance. Any other payment method is left completely untouched: * no meta, no total override, normal full payment as if this * plugin weren't involved. * * When the resolved advance doesn't produce a genuine partial split * (mode was 'full', cart was below the minimum, or the resolved * amount happens to already cover the whole order), the entire * order total is treated as the "advance" and COD due is 0 — same * "pay everything online now" fallback as before. * * @param int $order_id * @param array $posted_data Unused — required by the hook signature. * @param \WC_Order|WP_Error $order */ public function maybe_apply_deposit_to_order( $order_id, $posted_data, $order ): void { if ( ! $order instanceof \WC_Order ) { return; } if ( ! sdfw_is_enabled() ) { return; } if ( ! isset( $this->get_eligible_gateways()[ $order->get_payment_method() ] ) ) { return; } $resolved = $this->resolve_advance_for_order( $order ); $order_total = (float) $order->get_total(); $advance = $resolved['advance']; if ( $advance > 0.0 && $advance < $order_total ) { // Genuine partial split: some of the total is paid now, // the rest stays due on delivery. The processing fee is // deposit-only (based on the deposit amount) but is charged // as part of the on-delivery balance, not added to what's // paid online — see maybe_override_total_for_active_order(), // which charges $advance alone. $source_product_id = $resolved['source_product_id']; $fee = sdfw_get_processing_fee_amount( $advance ); $cod_due = SDFW_Calculator::get_cod_remainder( $order_total, $advance ) + $fee; } else { // 'full' mode, below-minimum, or the resolved advance // already covers the whole order — the entire total is // paid online now, nothing is due on delivery, and no fee // applies. $advance = $order_total; $cod_due = 0.0; $source_product_id = 0; $fee = 0.0; } $order->update_meta_data( self::META_ADVANCE_AMOUNT, $advance ); $order->update_meta_data( self::META_COD_DUE, $cod_due ); $order->update_meta_data( self::META_PROCESSING_FEE, $fee ); if ( $source_product_id > 0 ) { $order->update_meta_data( self::META_ADVANCE_SOURCE_PRODUCT_ID, $source_product_id ); } $order->save(); // Only worth flagging as "active" when there's an actual total // substitution to perform — a full-payment order already has // advance === order total, so overriding it would be a no-op. if ( $cod_due > 0.0 && function_exists( 'WC' ) && WC()->session ) { WC()->session->set( self::SESSION_ACTIVE_ORDER_KEY, $order->get_id() ); } } /** * Substitutes the resolved advance amount for the order's total — * ONLY for the one order flagged active in the WC session by * maybe_apply_deposit_to_order(), for exactly as long as it takes * the chosen gateway's own process_payment() to read it. Every * other order total lookup (admin lists, emails, other requests) * is unaffected, and the order's real total in the database is * never touched. * * @param float $total Original total. * @param \WC_Order $order Order being queried. */ public function maybe_override_total_for_active_order( $total, $order ) { if ( ! $order instanceof \WC_Order ) { return $total; } if ( '' === (string) $order->get_meta( self::META_ADVANCE_AMOUNT, true ) ) { return $total; } $active_order_id = ( function_exists( 'WC' ) && WC()->session ) ? WC()->session->get( self::SESSION_ACTIVE_ORDER_KEY ) : null; if ( (int) $active_order_id !== $order->get_id() ) { return $total; } $advance = (float) $order->get_meta( self::META_ADVANCE_AMOUNT, true ); return $advance > 0 ? $advance : $total; } /** * Fires on woocommerce_payment_complete regardless of which * gateway actually processed the money. No-ops for orders this * plugin never touched (no advance meta). Records the transaction * ID and adds a summary order note — does NOT set the order * status; that's owned entirely by whichever gateway (bKash, * Nagad, etc.) actually processed the payment. */ public function on_payment_complete( $order_id ): void { $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order || '' === (string) $order->get_meta( self::META_ADVANCE_AMOUNT, true ) ) { return; } $txn_id = $order->get_transaction_id(); if ( $txn_id ) { $order->update_meta_data( self::META_TXN_ID, $txn_id ); } $advance = (float) $order->get_meta( self::META_ADVANCE_AMOUNT, true ); $cod_due = (float) $order->get_meta( self::META_COD_DUE, true ); $fee = (float) $order->get_meta( self::META_PROCESSING_FEE, true ); $gateway_label = $order->get_payment_method_title(); $gateway_label = '' !== $gateway_label ? $gateway_label : __( 'the selected gateway', 'aawahid-simple-deposits-for-woocommerce' ); if ( $cod_due > 0.0 && $fee > 0.0 ) { $note = sprintf( /* translators: 1: formatted advance amount, 2: gateway used, 3: formatted COD remainder (already includes the processing fee), 4: formatted processing fee included in that remainder */ __( 'Deposit of %1$s received via %2$s. %3$s remains due on delivery (includes %4$s processing fee).', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $advance ), $gateway_label, sdfw_format_price( $cod_due ), sdfw_format_price( $fee ) ); } elseif ( $cod_due > 0.0 ) { $note = sprintf( /* translators: 1: formatted advance amount, 2: gateway used, 3: formatted COD remainder */ __( 'Deposit of %1$s received via %2$s. %3$s remains due on delivery.', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $advance ), $gateway_label, sdfw_format_price( $cod_due ) ); } else { $note = sprintf( /* translators: 1: formatted full amount, 2: gateway used */ __( 'Full payment of %1$s received via %2$s. Nothing is due on delivery.', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $advance ), $gateway_label ); } $order->add_order_note( $note ); $order->save(); if ( function_exists( 'WC' ) && WC()->session ) { WC()->session->__unset( self::SESSION_ACTIVE_ORDER_KEY ); } } /** * Gateways the merchant has explicitly allowed to collect the * advance (sdfw_allowed_gateways, set on the Deposits settings * tab), filtered down to ones that are actually currently active. * Cash on Delivery is always excluded defensively, even though * SDFW_Settings already prevents it from ever being saved into * this list. * * @return \WC_Payment_Gateway[] Keyed by gateway ID. */ private function get_eligible_gateways(): array { $allowed = (array) get_option( 'sdfw_allowed_gateways', [] ); if ( empty( $allowed ) || ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) { return []; } $available = WC()->payment_gateways()->payment_gateways(); $eligible = []; foreach ( $available as $gateway ) { if ( 'cod' === $gateway->id ) { continue; } if ( in_array( $gateway->id, $allowed, true ) && 'yes' === $gateway->enabled ) { $eligible[ $gateway->id ] = $gateway; } } return $eligible; } /** * Resolves the advance amount (and the product that drove it) for * an order that has just been placed. Prefers the live cart * session (matches SDFW_Calculator's own logic exactly); falls * back to recomputing straight from the order's line items — using * the same "highest wins, quantity ignored" rule — if the cart * session is ever unavailable at this point in the request. * * @return array{advance: float, source_product_id: int} */ private function resolve_advance_for_order( \WC_Order $order ): array { $cart = function_exists( 'WC' ) ? WC()->cart : null; if ( $cart instanceof \WC_Cart && ! $cart->is_empty() ) { $advance = SDFW_Calculator::get_advance_for_cart( $cart ); $source = SDFW_Calculator::get_advance_source_product( $cart ); return [ 'advance' => $advance, 'source_product_id' => $source instanceof \WC_Product ? $source->get_id() : 0, ]; } // Same basis as SDFW_Calculator: items after discounts, ex shipping/tax. $basis = max( 0.0, (float) $order->get_subtotal() - (float) $order->get_total_discount() ); $highest = 0.0; $source_id = 0; foreach ( $order->get_items() as $item ) { if ( ! $item instanceof \WC_Order_Item_Product ) { continue; } $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(); if ( ! $product_id ) { continue; } $amount = sdfw_get_product_amount( $product_id, $basis ); if ( $amount > $highest ) { $highest = $amount; $source_id = $item->get_product_id(); } } $highest = min( $highest, (float) $order->get_total() ); // Same minimum-threshold rule as SDFW_Calculator::get_advance_for_cart() // — can't call is_deposit_eligible() here since it needs a live // WC_Cart (which is exactly what's unavailable in this fallback // branch), so the order's own total is compared directly instead. if ( (float) $order->get_total() < sdfw_get_global_amount() ) { $highest = 0.0; } return [ 'advance' => round( $highest, function_exists( 'wc_get_price_decimals' ) ? wc_get_price_decimals() : 2 ), 'source_product_id' => $source_id, ]; } }