← Back
Editing: functions.php
<?php /** * Payment Form: Functions * * @package SimplePay\Core\Payment_Form * @copyright Copyright (c) 2022, Sandhills Development, LLC * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 4.1.0 */ if ( ! defined( 'ABSPATH' ) ) { exit; } use SimplePay\Core\PaymentForm\PriceOptions; /** * Returns a list of prices associated with a Payment Form's Payment Mode. * * @since 4.1.0 * * @param \SimplePay\Core\Abstracts\Form $form Payment Form. * @return \SimplePay\Core\PaymentForm\PriceOption[] */ function simpay_get_payment_form_prices( $form ) { if ( false === $form ) { return array(); } $price_options = ( new PriceOptions( $form ) )->get_prices(); /** * Filters the price options for a specific Payment Form. * * @since 4.1.1 * * @param \SimplePay\Core\PaymentForm\PriceOption[] $price_options Price option. * @param \SimplePay\Core\Abstracts\Form $form Payment Form. */ $price_options = apply_filters( 'simpay_get_payment_form_price_options', $price_options, $form ); return $price_options; } /** * Retrieves a specific price option. * * @since 4.1.0 * * @param \SimplePay\Core\Abstracts\Form $form Payment Form. * @param string $price_id Price option ID. * @return false|\SimplePay\Core\PaymentForm\PriceOption Price option. False if * not found. */ function simpay_payment_form_prices_get_price_by_id( $form, $price_id ) { $prices = simpay_get_payment_form_prices( $form ); $prices = array_filter( $prices, function ( $price ) use ( $price_id ) { return $price->id === $price_id; } ); if ( empty( $prices ) ) { return false; } return current( $prices ); } /** * Retrieves a specific price option by instance ID. * * @since 4.11.0 * * @param \SimplePay\Core\Abstracts\Form $form Payment Form. * @param string $instance_id Price option Instance ID. * @return false|\SimplePay\Core\PaymentForm\PriceOption Price option. False if * not found. */ function simpay_payment_form_prices_get_price_by_instance_id( $form, $instance_id ) { $prices = simpay_get_payment_form_prices( $form ); $prices = array_filter( $prices, function ( $price ) use ( $instance_id ) { return $price->instance_id === $instance_id; } ); if ( empty( $prices ) ) { return false; } return current( $prices ); } /** * Determines if the stored price has been defined in Stripe. * * @since 4.1.0 * * @param string $price_id Price option ID. * @return bool */ function simpay_payment_form_prices_is_defined_price( $price_id ) { if ( null === $price_id ) { return false; } return strpos( $price_id, 'simpay_' ) !== 0; } /** * Determines if a list of Prices includes a recurring Price. * * @since 4.1.0 * * @param array array<\SimplePay\Core\PaymentForm\PriceOption> $prices Price options. * @return bool */ function simpay_payment_form_prices_has_subscription_price( $prices ) { $has_subscription = false; foreach ( $prices as $price ) { if ( null !== $price->recurring ) { $has_subscription = true; break; } } return $has_subscription; } /** * Retrieves the first recurring price option. * * This is only used to fill legacy Payment Form properties and should not be * extended. * * @since 4.1.0 * @access private * * @param \SimplePay\Core\PaymentForm\PriceOption[] $prices Price options. * @return \SimplePay\Core\PaymentForm\PriceOption[]|array */ function __unstable_simpay_get_payment_form_prices_subscription_price( $prices ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound $has_subscription = simpay_payment_form_prices_has_subscription_price( $prices ); if ( false === $has_subscription ) { return array(); } foreach ( $prices as $price ) { if ( null !== $price->recurring ) { return $price; break; } } return array(); } /** * Returns the default price option for a Payment FOrm. * * @since 4.1.0 * * @param \SimplePay\Core\PaymentForm\PriceOption[] $prices Prices list. * @return false|\SimplePay\Core\PaymentForm\PriceOption Default price, or first price. * False if no prices are found. */ function simpay_get_payment_form_default_price( $prices ) { $price = current( array_filter( $prices, function ( $price ) { return null !== $price->default && true === $price->default; } ) ); // Fallback to the first price. if ( false === $price ) { $price = current( $prices ); } return $price; } /** * Determines if a list of prices includes the ability to set a custom amount. * * @since 4.1.0 * * @param \SimplePay\Core\PaymentForm\PriceOption[] $prices Price options. * @return bool */ function simpay_payment_form_prices_has_custom_price( $prices ) { $prices = array_filter( $prices, function ( $price ) { return false === $price->is_defined_amount(); } ); return ! empty( $prices ); } /** * Retrieves the first custom price option. * * This is only used to fill legacy Payment Form properties and should not be * extended. * * @since 4.1.0 * * @param \SimplePay\Core\PaymentForm\PriceOption[] $prices Price options. * @return \SimplePay\Core\PaymentForm\PriceOption[]|false Price option or false if no custom * amounts are allowed. */ function __unstable_simpay_get_payment_form_custom_price( $prices ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound $has_custom_price = simpay_payment_form_prices_has_custom_price( $prices ); if ( false === $has_custom_price ) { return false; } $prices = array_filter( $prices, function ( $price ) { return false === $price->is_defined_amount(); } ); if ( empty( $prices ) ) { return false; } return current( $prices ); } /** * Determines if a list of prices includes the ability to optionally recur. * * @since 4.1.0 * * @param \SimplePay\Core\PaymentForm\PriceOption[] $prices Price options. * @return bool */ function simpay_payment_form_prices_has_recurring_price( $prices ) { $prices = array_filter( $prices, function ( $price ) { return true === $price->can_recur; } ); return ! empty( $prices ); } /** * Determines whether a Stripe Elements payment method is enabled on a form. * * @since 4.17.2 * * @param array $payment_methods Payment methods meta from `_payment_methods`. * @param string $method_id Stripe Element payment method id (e.g. `klarna`). * @return bool */ function simpay_payment_form_has_stripe_element_payment_method( $payment_methods, $method_id ) { return isset( $payment_methods['stripe-elements'][ $method_id ]['id'] ); } /** * Determines if a form's payment methods force the Address field to collect a * shipping address. * * Afterpay/Clearpay requires a shipping address, so the Address Type is locked * to Shipping in the form builder and force-set on save while it is enabled. * * "Enabled" matches how the builder renders the Afterpay/Clearpay toggle: the * saved entry only counts while the payment method is registered, available * and supported in the Stripe account's country. A stale entry (for example * after the account country changed) renders the toggle unchecked, so it must * not lock the Address Type or force Shipping either. * * @since 4.17.5 * * @param mixed $payment_methods Payment methods meta (`_payment_methods`). * @return bool */ function simpay_payment_form_requires_shipping_address( $payment_methods ) { if ( ! is_array( $payment_methods ) ) { return false; } if ( ! simpay_payment_form_has_stripe_element_payment_method( $payment_methods, 'afterpay-clearpay' ) ) { return false; } if ( ! function_exists( '\SimplePay\Core\PaymentMethods\get_payment_method' ) ) { return false; } $payment_method = \SimplePay\Core\PaymentMethods\get_payment_method( 'afterpay-clearpay' ); if ( false === $payment_method ) { return false; } return $payment_method->is_available() && $payment_method->is_country_supported(); } /** * Determines if a form's Address field collects a shipping address. * * @since 4.17.5 * * @param mixed $fields Custom fields (`_custom_fields`). * @param bool $fallback Returned when there are no fields or no Address field. * @return bool */ function simpay_payment_form_address_collects_shipping( $fields, $fallback = false ) { if ( ! is_array( $fields ) || empty( $fields['address'] ) || ! is_array( $fields['address'] ) ) { return $fallback; } $address_field = current( $fields['address'] ); return is_array( $address_field ) && isset( $address_field['collect-shipping'] ) && 'yes' === $address_field['collect-shipping']; } /** * Ensures the Address and Amount Breakdown fields are present and configured * correctly when required by enabled payment methods or location-based taxes. * * Shared between the `overlay` and `embedded` form display types. The Address * field is required for Klarna, Afterpay/Clearpay, and automatic taxes; * Afterpay additionally requires shipping collection. The Amount Breakdown * (`total_amount`) is required when automatic taxes are enabled. * * @since 4.17.2 * * @param array $fields Current custom fields. * @param int $form_id Form ID. * @param array $payment_methods Payment methods meta. * @param string $tax_status Tax status meta. * @param int $count Field count, mutated when fields are added. * @param array $changes Notice strings, mutated when fields are added. * @param mixed $saved_fields Optional. Custom fields saved before the current * request, used to tell when the Address Type * actually changes. Default null (compare against * `$fields` only). * @return array Updated fields. */ function simpay_payment_form_ensure_address_and_total_fields( $fields, $form_id, $payment_methods, $tax_status, &$count, &$changes, $saved_fields = null ) { $has_klarna = simpay_payment_form_has_stripe_element_payment_method( $payment_methods, 'klarna' ); $has_afterpay_entry = simpay_payment_form_has_stripe_element_payment_method( $payment_methods, 'afterpay-clearpay' ); $is_auto_tax = 'automatic' === $tax_status; // Shipping is only forced while Afterpay/Clearpay can actually be used, // matching the builder's lock. A saved entry still requires the Address // field, as it did before the lock was introduced. $has_afterpay = simpay_payment_form_requires_shipping_address( $payment_methods ); $needs_required_address = ( $has_klarna || $has_afterpay_entry || $is_auto_tax ); $shipping_notice = __( 'The Address field has been set to collect a Shipping Address, which is required by Afterpay/Clearpay.', 'stripe' ); if ( $needs_required_address ) { if ( ! isset( $fields['address'] ) ) { $fields['address'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_address', 'collect-shipping' => $has_afterpay ? 'yes' : 'no', 'required' => 'yes', ); $changes[] = __( 'Address field is required, and has been added to the payment form.', 'stripe' ); // Explain why the new field is a Shipping Address rather than // leaving the merchant to discover it in the builder. if ( $has_afterpay ) { $changes[] = $shipping_notice; } ++$count; } else { $current_address_field = current( $fields['address'] ); $args = array( 'required' => 'yes', ); if ( $has_afterpay ) { $already_shipping = simpay_payment_form_address_collects_shipping( $fields ) && simpay_payment_form_address_collects_shipping( $saved_fields, true ); // Only notify when the value actually changes, so the notice // does not repeat on every save once shipping is enabled. The // builder locks the control to Shipping before it submits, so // the submitted value alone cannot show a Billing -> Shipping // switch; the value saved before this request can. if ( ! $already_shipping ) { $changes[] = $shipping_notice; } $args['collect-shipping'] = 'yes'; } $fields['address'] = array( array_merge( $current_address_field, $args ), ); } } if ( $is_auto_tax && ! isset( $fields['total_amount'] ) ) { $fields['total_amount'][] = array(); ++$count; } return $fields; } /** * Returns the next free field ID for a field added to a payment form. * * Fields are keyed by type, so the number of field types is not the number * of fields, and a field added with that count could reuse an existing * field's ID (e.g. the Checkout Button's). Fields with the same ID render * elements with the same HTML `id`, which breaks their labels and scripts. * * @since 4.17.7 * * @param mixed $fields Payment form custom fields, keyed by field type. * @return int Next free field ID. */ function simpay_payment_form_get_next_field_uid( $fields ) { if ( ! is_array( $fields ) ) { return 0; } $next = count( $fields ); foreach ( $fields as $type_fields ) { if ( ! is_array( $type_fields ) ) { continue; } foreach ( $type_fields as $field ) { if ( ! is_array( $field ) || ! isset( $field['uid'] ) ) { continue; } if ( ! is_numeric( $field['uid'] ) ) { continue; } $next = max( $next, (int) $field['uid'] + 1 ); } } return $next; } /** * Ensures Payment Forms have required fields and remove unnecessary fields. * * @since 4.1.0 * * @param array $fields Payment Form custom fields. * @param int $form_id Payment Form ID. * @param string $form_display_type Payment Form display type. * @param mixed $saved_fields Optional. Custom fields saved before the current * request. Since 4.17.5. Default null. * @return array{fields: array<string, mixed>, changes: array<string>} */ function simpay_payment_form_add_missing_custom_fields( $fields, $form_id, $form_display_type = 'embedded', $saved_fields = null ) { $form = simpay_get_form( $form_id ); if ( false === $form ) { return $fields; } $prices = simpay_get_payment_form_prices( $form ); $count = simpay_payment_form_get_next_field_uid( $fields ); $payment_methods = get_post_meta( $form_id, '_payment_methods', true ); $tax_status = get_post_meta( $form_id, '_tax_status', true ); $allow_line_items = get_post_meta( $form_id, '_allow_purchasing_multiple_line_items', true ); $changes = array(); // Form display type-specific. switch ( $form_display_type ) { case 'overlay': if ( ! isset( $fields['payment_button'] ) ) { $fields['payment_button'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_payment_button', ); $changes[] = __( 'Payment Button field is required, and has been added to the payment form.', 'stripe' ); ++$count; } // Ensure "Customer Email" exists. if ( ! isset( $fields['email'] ) ) { $fields['email'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_email', 'label' => 'Email Address', ); $changes[] = __( 'Customer Email field is required, and has been added to the payment form.', 'stripe' ); ++$count; } $fields = simpay_payment_form_ensure_address_and_total_fields( $fields, $form_id, is_array( $payment_methods ) ? $payment_methods : array(), $tax_status, $count, $changes, $saved_fields ); break; case 'stripe_checkout': if ( isset( $fields['coupon'] ) ) { unset( $fields['coupon'] ); $changes[] = __( 'The coupon field has been removed since you selected Off-site Stripe Checkout. ', 'stripe' ); } break; case 'embedded': $pm_methods = is_array( $payment_methods ) ? $payment_methods : array(); $customer_name_methods = array( 'bancontact', 'p24', 'sepa-debit', 'klarna', 'afterpay-clearpay', 'ach-debit', ); $needs_customer_name = false; foreach ( $customer_name_methods as $_pm ) { if ( simpay_payment_form_has_stripe_element_payment_method( $pm_methods, $_pm ) ) { $needs_customer_name = true; break; } } // Ensure "Customer Name" exists if using a payment method that requires it. if ( ! isset( $fields['customer_name'] ) && $needs_customer_name ) { $fields['customer_name'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_customer_name', 'label' => 'Full Name', 'required' => 'yes', ); $changes[] = __( 'Customer Name field is required for one or more payment methods, and has been added to the payment form.', 'stripe' ); ++$count; } // Ensure "Customer Email" exists. if ( ! isset( $fields['email'] ) ) { $fields['email'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_email', 'label' => 'Email Address', ); $changes[] = __( 'Customer Email field is required, and has been added to the payment form.', 'stripe' ); ++$count; } $fields = simpay_payment_form_ensure_address_and_total_fields( $fields, $form_id, is_array( $payment_methods ) ? $payment_methods : array(), $tax_status, $count, $changes, $saved_fields ); // Set "Phone" to optional if using Payment Request Button. if ( isset( $fields['payment_request_button'] ) && isset( $fields['telephone'] ) ) { $fields['telephone'] = array( array_merge( current( $fields['telephone'] ), array( 'required' => 'no', ) ), ); } // Ensure "Payment Methods" exist. if ( ! isset( $fields['card'] ) ) { $fields['card'][] = array( 'order' => 9998, 'uid' => $count, 'id' => 'simpay_' . $form_id . '_card', 'label' => '', ); $changes[] = __( 'Payment Methods is required, and has been added to the payment form.', 'stripe' ); ++$count; } // Ensure "Checkout" button exists. if ( ! isset( $fields['checkout_button'] ) ) { $fields['checkout_button'][] = array( 'order' => 9999, 'uid' => $count, 'id' => 'simpay_' . $form_id . '_checkout_button', ); $changes[] = __( 'Checkout Button field is required, and has been added to the payment form.', 'stripe' ); ++$count; } // Ensure "Payment Button" exists. if ( 'overlay' === $form_display_type ) { if ( ! isset( $fields['payment_button'] ) ) { $fields['payment_button'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_payment_button', ); $changes[] = __( 'Payment Button field is required, and has been added to the payment form.', 'stripe' ); ++$count; } // Remove "Payment Button". } elseif ( isset( $fields['payment_button'] ) ) { unset( $fields['payment_button'] ); $changes[] = __( 'Payment Button has been removed from the payment form.', 'stripe' ); } break; default: // Remove "Address" if using automatic taxes. if ( 'automatic' === $tax_status ) { unset( $fields['customer_name'] ); unset( $fields['email'] ); unset( $fields['telephone'] ); unset( $fields['address'] ); unset( $fields['tax_id'] ); } // Ensure "Payment Button" exists. if ( ! isset( $fields['payment_button'] ) ) { $fields['payment_button'][] = array( 'order' => 9999, 'uid' => $count, 'id' => 'simpay_' . $form_id . '_payment_button', ); $changes[] = __( 'Payment Button field is required, and has been added to the payment form.', 'stripe' ); ++$count; } // Remove unnecessary fields. unset( $fields['card'] ); unset( $fields['checkout_button'] ); unset( $fields['payment_request_button'] ); unset( $fields['fee_recovery_toggle'] ); } // Ensure "Price Selector" is always present. if ( ! isset( $fields['plan_select'] ) ) { $fields['plan_select'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_plan_select_' . $count, 'label' => 'Choose an amount', ); $changes[] = __( 'Price Selector is required, and has been added to the payment form. ', 'stripe' ); ++$count; } if ( 'yes' === $allow_line_items ) { if ( isset( $fields['custom_amount'] ) ) { unset( $fields['custom_amount'] ); $changes[] = __( 'Custom Amount Input has been removed from the payment form. This field is automatically displayed with multiple line items.', 'stripe' ); } if ( isset( $fields['recurring_amount_toggle'] ) ) { unset( $fields['recurring_amount_toggle'] ); $changes[] = __( 'Recurring Amount Toggle has been removed from the payment form. This field is automatically displayed with multiple line items.', 'stripe' ); } if ( isset( $fields['number'] ) ) { foreach ( $fields['number'] as $number_field_index => $number_field ) { if ( isset( $number_field['quantity'] ) && 'yes' === $number_field['quantity'] ) { unset( $fields['number'][ $number_field_index ] ); $changes[] = __( 'Quantity Input has been removed from the payment form. This field is automatically displayed with multiple line items.', 'stripe' ); } } } if ( isset( $fields['radio'] ) || isset( $fields['dropdown'] ) ) { foreach ( array( 'radio', 'dropdown' ) as $field_type ) { if ( isset( $fields[ $field_type ] ) ) { foreach ( $fields[ $field_type ] as $field_index => $field ) { if ( isset( $field['amount_quantity'] ) && 'yes' === $field['amount_quantity'] ) { unset( $fields[ $field_type ][ $field_index ] ); $changes[] = __( 'Quantity Input has been removed from the payment form. This field is automatically displayed with multiple line items.', 'stripe' ); } } } } } } else { // Ensure "Custom Amount" field exists if using a custom amount, and no Subscription. $has_custom_amount = simpay_payment_form_prices_has_custom_price( $prices ); if ( true === $has_custom_amount && ! isset( $fields['custom_amount'] ) ) { $fields['custom_amount'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_custom_amount_' . $count, 'label' => 'Custom Amount', ); $changes[] = __( 'The Custom Amount Input field is required because you have set a custom amount on your price list, and has been added to the payment form.', 'stripe' ); ++$count; } elseif ( false === $has_custom_amount ) { if ( isset( $fields['custom_amount'] ) ) { unset( $fields['custom_amount'] ); $changes[] = __( 'Custom Amount Input has been removed from the payment form. A price option that allows user-defined amounts is required. ', 'stripe' ); } } } // Ensure "Recurring Amount Toggle" field exists if using an optional recurring price. $can_recur = simpay_payment_form_prices_has_recurring_price( $prices ); if ( 'yes' !== $allow_line_items && true === $can_recur && ! isset( $fields['recurring_amount_toggle'] ) ) { $fields['recurring_amount_toggle'][] = array( 'uid' => $count, 'id' => 'simpay_' . $form_id . '_recurring_amount_toggle_' . $count, 'label' => 'Make this a recurring amount', ); $changes[] = __( 'Recurring Amount Toggle is required, and has been added to the payment form.', 'stripe' ); ++$count; } elseif ( 'yes' !== $allow_line_items && false === $can_recur ) { if ( isset( $fields['recurring_amount_toggle'] ) ) { unset( $fields['recurring_amount_toggle'] ); $changes[] = __( 'Recurring Amount Toggle has been removed from the payment form. An optionally recurring price option is required.', 'stripe' ); } } // Remove "Coupon" field if using Fee Recovery. if ( isset( $fields['coupon'] ) ) { // Fee recovery toggle exists, remove Coupon. if ( isset( $fields['fee_recovery_toggle'] ) ) { unset( $fields['coupon'] ); $changes[] = __( 'Coupon field has been removed from the payment form because it is incompatible with Fee Recovery.', 'stripe' ); } // Fee recovery exists in Payment Method configuration. if ( isset( $payment_methods['stripe-elements'] ) ) { foreach ( $payment_methods['stripe-elements'] as $payment_method ) { if ( isset( $payment_method['fee_recovery'], $payment_method['fee_recovery']['enabled'] ) && 'yes' === $payment_method['fee_recovery']['enabled'] ) { unset( $fields['coupon'] ); $changes[] = __( 'Coupon field has been removed from the payment form because it is incompatible with Fee Recovery.', 'stripe' ); break; } } } } // Remove "1-Click Payment Methods" (Payment Request Button). unset( $fields['payment_request_button'] ); // General sorting template for auto-added fields. $fields = array_merge( array_flip( array( 'customer_name', 'email', 'address', 'plan_select', 'custom_amount', 'recurring_amount_toggle', 'total_amount', 'card', 'checkout_button', ) ), $fields ); // "Checkout Button" and "Payment Button" should always be last. if ( isset( $fields['payment_button'] ) ) { $payment_button = $fields['payment_button']; unset( $fields['payment_button'] ); $fields['payment_button'] = $payment_button; } if ( isset( $fields['checkout_button'] ) ) { $checkout_button = $fields['checkout_button']; unset( $fields['checkout_button'] ); $fields['checkout_button'] = $checkout_button; } // Remove empty/invalid fields after sorting. $fields = array_filter( $fields, function ( $field ) { return true === is_array( $field ); } ); return array( 'fields' => $fields, 'changes' => $changes, ); }
Save File
Cancel