← All changes
|
stripe/controllers/FrmStrpLiteActionsController.php
+260
-92
6.5
→
trunk
View file →
| @@ -10,8 +10,31 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | private static $customer; |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | + * @since 6.22 | |
| 15 | + * | |
| 16 | + * @param string $callback | |
| 17 | + * @param array|false|object $field | |
| 18 | + * | |
| 19 | + * @return string | |
| 20 | + */ | |
| 21 | + public static function maybe_show_card( $callback, $field = false ) { | |
| 22 | + if ( false === $field ) { | |
| 23 | + // Pro isn't up to date. | |
| 24 | + // Fallback to Stripe Lite if we do not know the form. | |
| 25 | + // This way we do not display the default credit card field | |
| 26 | + // when Pro is not up to version 6.21. | |
| 27 | + return self::class . '::show_card'; | |
| 28 | + } | |
| 29 | + | |
| 30 | + $form_id = is_object( $field ) ? $field->form_id : $field['form_id']; | |
| 31 | + $actions = self::get_actions_before_submit( $form_id ); | |
| 32 | + | |
| 33 | + return $actions ? self::class . '::show_card' : $callback; | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 14 | 37 | * Override the credit card field HTML if there is a Stripe action. |
| 15 | 38 | * |
| 16 | 39 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 17 | 40 | * |
| @@ -17,8 +40,9 @@ | ||
| 17 | 40 | * |
| 18 | 41 | * @param array $field |
| 19 | 42 | * @param string $field_name |
| 20 | 43 | * @param array $atts |
| 44 | + * | |
| 21 | 45 | * @return void |
| 22 | 46 | */ |
| 23 | 47 | public static function show_card( $field, $field_name, $atts ) { |
| 24 | 48 | $actions = self::get_actions_before_submit( $field['form_id'] ); |
| @@ -37,20 +61,24 @@ | ||
| 37 | 61 | * Get all published payment actions with the Stripe gateway that have an amount set. |
| 38 | 62 | * |
| 39 | 63 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 40 | 64 | * |
| 41 | - * @param string|int $form_id | |
| 65 | + * @param int|string $form_id | |
| 66 | + * | |
| 42 | 67 | * @return array |
| 43 | 68 | */ |
| 44 | 69 | public static function get_actions_before_submit( $form_id ) { |
| 45 | 70 | $payment_actions = self::get_actions_for_form( $form_id ); |
| 71 | + | |
| 46 | 72 | foreach ( $payment_actions as $k => $payment_action ) { |
| 47 | 73 | $gateway = $payment_action->post_content['gateway']; |
| 48 | 74 | $is_stripe = $gateway === 'stripe' || ( is_array( $gateway ) && in_array( 'stripe', $gateway, true ) ); |
| 75 | + | |
| 49 | 76 | if ( ! $is_stripe || empty( $payment_action->post_content['amount'] ) ) { |
| 50 | 77 | unset( $payment_actions[ $k ] ); |
| 51 | 78 | } |
| 52 | 79 | } |
| 80 | + | |
| 53 | 81 | return $payment_actions; |
| 54 | 82 | } |
| 55 | 83 | |
| 56 | 84 | /** |
| @@ -59,10 +87,11 @@ | ||
| 59 | 87 | * This way any Stripe action will fall back to Stripe link when the Stripe add on is unavailable. |
| 60 | 88 | * |
| 61 | 89 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 62 | 90 | * |
| 63 | - * @param string|int $form_id | |
| 64 | - * @return WP_Post|false | |
| 91 | + * @param int|string $form_id | |
| 92 | + * | |
| 93 | + * @return false|WP_Post | |
| 65 | 94 | */ |
| 66 | 95 | public static function get_stripe_link_action( $form_id ) { |
| 67 | 96 | $actions = self::get_actions_before_submit( $form_id ); |
| 68 | 97 | return reset( $actions ); |
| @@ -74,9 +103,10 @@ | ||
| 74 | 103 | * It is also called when Stripe link is active but the Stripe payment is triggered instead with JavaScript. |
| 75 | 104 | * |
| 76 | 105 | * @param WP_Post $action |
| 77 | 106 | * @param stdClass $entry |
| 78 | - * @param stdClass $form | |
| 107 | + * @param mixed $form | |
| 108 | + * | |
| 79 | 109 | * @return array |
| 80 | 110 | */ |
| 81 | 111 | public static function trigger_gateway( $action, $entry, $form ) { |
| 82 | 112 | $response = array( |
| @@ -84,21 +114,23 @@ | ||
| 84 | 114 | 'run_triggers' => false, |
| 85 | 115 | 'show_errors' => true, |
| 86 | 116 | ); |
| 87 | 117 | $atts = compact( 'action', 'entry', 'form' ); |
| 118 | + $amount = self::prepare_amount( $action->post_content['amount'], $atts ); | |
| 88 | 119 | |
| 89 | - $amount = self::prepare_amount( $action->post_content['amount'], $atts ); | |
| 90 | - if ( empty( $amount ) || $amount == 000 ) { | |
| 120 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 121 | + if ( ! $amount || $amount == 000 ) { | |
| 91 | 122 | $response['error'] = __( 'Please specify an amount for the payment', 'formidable' ); |
| 92 | 123 | return $response; |
| 93 | 124 | } |
| 94 | 125 | |
| 95 | 126 | if ( ! self::stripe_is_configured() ) { |
| 96 | - $response['error'] = __( 'There was a problem communicating with Stripe. Please try again.', 'formidable' ); | |
| 127 | + $response['error'] = __( 'Stripe still needs to be configured.', 'formidable' ); | |
| 97 | 128 | return $response; |
| 98 | 129 | } |
| 99 | 130 | |
| 100 | - $customer = self::set_customer_with_token( $atts ); | |
| 131 | + $customer = self::set_customer_with_token( $atts, self::get_customer_id_from_posted_setup_intents( $form->id ) ); | |
| 132 | + | |
| 101 | 133 | if ( ! is_object( $customer ) ) { |
| 102 | 134 | $response['error'] = $customer; |
| 103 | 135 | return $response; |
| 104 | 136 | } |
| @@ -104,9 +136,13 @@ | ||
| 104 | 136 | } |
| 105 | 137 | |
| 106 | 138 | $one_time_payment_args = compact( 'customer', 'form', 'entry', 'action', 'amount' ); |
| 107 | 139 | |
| 108 | - FrmStrpLiteLinkController::create_pending_stripe_link_payment( $one_time_payment_args ); | |
| 140 | + if ( ! FrmStrpLiteLinkController::create_pending_stripe_link_payment( $one_time_payment_args ) ) { | |
| 141 | + $response['error'] = __( 'There was something wrong with the payment data.', 'formidable' ); | |
| 142 | + return $response; | |
| 143 | + } | |
| 144 | + | |
| 109 | 145 | $response['show_errors'] = false; |
| 110 | 146 | return $response; |
| 111 | 147 | } |
| 112 | 148 | |
| @@ -119,14 +155,56 @@ | ||
| 119 | 155 | return FrmStrpLiteAppHelper::call_stripe_helper_class( 'initialize_api' ); |
| 120 | 156 | } |
| 121 | 157 | |
| 122 | 158 | /** |
| 159 | + * Get the customer id from a setup intent that was created for this submission. | |
| 160 | + * A Stripe Link subscription builds its subscription from the setup intent's customer, | |
| 161 | + * so reusing that customer here avoids creating a duplicate customer for guests. | |
| 162 | + * | |
| 163 | + * @since 6.35 | |
| 164 | + * | |
| 165 | + * @param int|string $form_id | |
| 166 | + * | |
| 167 | + * @return false|string The Stripe customer id, or false if it can't be found. | |
| 168 | + */ | |
| 169 | + public static function get_customer_id_from_posted_setup_intents( $form_id ) { | |
| 170 | + $posted_intents = FrmStrpLiteAuth::get_payment_intents( 'frmintent' . $form_id ); | |
| 171 | + | |
| 172 | + if ( ! is_array( $posted_intents ) || ! $posted_intents ) { | |
| 173 | + return false; | |
| 174 | + } | |
| 175 | + | |
| 176 | + $setup_intent_ids = array_filter( | |
| 177 | + $posted_intents, | |
| 178 | + function ( $intent_id ) { | |
| 179 | + return str_starts_with( $intent_id, 'seti_' ); | |
| 180 | + } | |
| 181 | + ); | |
| 182 | + | |
| 183 | + if ( ! $setup_intent_ids ) { | |
| 184 | + return false; | |
| 185 | + } | |
| 186 | + | |
| 187 | + $first_setup_intent_id = reset( $setup_intent_ids ); | |
| 188 | + $first_setup_intent_id = explode( '_secret_', $first_setup_intent_id )[0]; | |
| 189 | + $setup_intent = FrmStrpLiteAppHelper::call_stripe_helper_class( 'get_setup_intent', $first_setup_intent_id ); | |
| 190 | + | |
| 191 | + if ( ! is_object( $setup_intent ) || empty( $setup_intent->customer ) ) { | |
| 192 | + return false; | |
| 193 | + } | |
| 194 | + | |
| 195 | + return $setup_intent->customer; | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 123 | 199 | * Set a customer object to $_POST['customer'] to use later. |
| 124 | 200 | * |
| 125 | - * @param array $atts | |
| 201 | + * @param array $atts The action, entry, and form for the payment. | |
| 202 | + * @param false|string $customer_id The Stripe customer id when it is already known. | |
| 203 | + * | |
| 126 | 204 | * @return object|string |
| 127 | 205 | */ |
| 128 | - private static function set_customer_with_token( $atts ) { | |
| 206 | + private static function set_customer_with_token( $atts, $customer_id = false ) { | |
| 129 | 207 | if ( isset( self::$customer ) ) { |
| 130 | 208 | // It's an object if this isn't the first Stripe action running. |
| 131 | 209 | return self::$customer; |
| 132 | 210 | } |
| @@ -134,8 +212,12 @@ | ||
| 134 | 212 | $payment_info = array( |
| 135 | 213 | 'user_id' => FrmTransLiteAppHelper::get_user_id_for_current_payment(), |
| 136 | 214 | ); |
| 137 | 215 | |
| 216 | + if ( $customer_id ) { | |
| 217 | + $payment_info['customer_id'] = $customer_id; | |
| 218 | + } | |
| 219 | + | |
| 138 | 220 | if ( ! empty( $atts['action']->post_content['email'] ) ) { |
| 139 | 221 | $payment_info['email'] = apply_filters( 'frm_content', $atts['action']->post_content['email'], $atts['form'], $atts['entry'] ); |
| 140 | 222 | $payment_info['email'] = self::replace_email_shortcode( $payment_info['email'] ); |
| 141 | 223 | } |
| @@ -141,10 +223,11 @@ | ||
| 141 | 223 | } |
| 142 | 224 | |
| 143 | 225 | self::add_customer_name( $atts, $payment_info ); |
| 144 | 226 | |
| 145 | - $customer = FrmStrpLiteAppHelper::call_stripe_helper_class( 'get_customer', $payment_info ); | |
| 146 | - self::$customer = $customer; // Set for later use. | |
| 227 | + $customer = FrmStrpLiteAppHelper::call_stripe_helper_class( 'get_customer', $payment_info ); | |
| 228 | + // Set for later use. | |
| 229 | + self::$customer = $customer; | |
| 147 | 230 | |
| 148 | 231 | return $customer; |
| 149 | 232 | } |
| 150 | 233 | |
| @@ -151,12 +234,13 @@ | ||
| 151 | 234 | /** |
| 152 | 235 | * Replace an [email] shortcode with the current user email. |
| 153 | 236 | * |
| 154 | 237 | * @param string $email |
| 238 | + * | |
| 155 | 239 | * @return string |
| 156 | 240 | */ |
| 157 | 241 | private static function replace_email_shortcode( $email ) { |
| 158 | - if ( false === strpos( $email, '[email]' ) ) { | |
| 242 | + if ( ! str_contains( $email, '[email]' ) ) { | |
| 159 | 243 | return $email; |
| 160 | 244 | } |
| 161 | 245 | |
| 162 | 246 | global $current_user; |
| @@ -173,8 +257,9 @@ | ||
| 173 | 257 | * @since 6.5, introduced in v2.02 of the Stripe add on. |
| 174 | 258 | * |
| 175 | 259 | * @param array $atts |
| 176 | 260 | * @param array $payment_info |
| 261 | + * | |
| 177 | 262 | * @return void |
| 178 | 263 | */ |
| 179 | 264 | private static function add_customer_name( $atts, &$payment_info ) { |
| 180 | 265 | if ( empty( $atts['action']->post_content['billing_first_name'] ) ) { |
| @@ -181,8 +266,9 @@ | ||
| 181 | 266 | return; |
| 182 | 267 | } |
| 183 | 268 | |
| 184 | 269 | $name = '[' . $atts['action']->post_content['billing_first_name'] . ' show="first"]'; |
| 270 | + | |
| 185 | 271 | if ( ! empty( $atts['action']->post_content['billing_last_name'] ) ) { |
| 186 | 272 | $name .= ' [' . $atts['action']->post_content['billing_last_name'] . ' show="last"]'; |
| 187 | 273 | } |
| 188 | 274 | |
| @@ -194,17 +280,20 @@ | ||
| 194 | 280 | * |
| 195 | 281 | * @since 6.5, introduced in v1.16 of the Stripe add on. |
| 196 | 282 | * |
| 197 | 283 | * @param array $atts Includes 'customer', 'entry', 'action', 'amount'. |
| 284 | + * | |
| 198 | 285 | * @return int The timestamp when the trial should end. 0 for no trial |
| 199 | 286 | */ |
| 200 | 287 | public static function get_trial_end_time( $atts ) { |
| 201 | 288 | $settings = $atts['action']->post_content; |
| 289 | + | |
| 202 | 290 | if ( empty( $settings['trial_interval_count'] ) ) { |
| 203 | 291 | return 0; |
| 204 | 292 | } |
| 205 | 293 | |
| 206 | 294 | $trial = $settings['trial_interval_count']; |
| 295 | + | |
| 207 | 296 | if ( ! is_numeric( $trial ) ) { |
| 208 | 297 | $trial = FrmTransLiteAppHelper::process_shortcodes( |
| 209 | 298 | array( |
| 210 | 299 | 'value' => $trial, |
| @@ -224,8 +313,9 @@ | ||
| 224 | 313 | * Convert the amount from 10.00 to 1000. |
| 225 | 314 | * |
| 226 | 315 | * @param mixed $amount |
| 227 | 316 | * @param array $atts |
| 317 | + * | |
| 228 | 318 | * @return string |
| 229 | 319 | */ |
| 230 | 320 | public static function prepare_amount( $amount, $atts = array() ) { |
| 231 | 321 | $amount = parent::prepare_amount( $amount, $atts ); |
| @@ -236,14 +326,21 @@ | ||
| 236 | 326 | /** |
| 237 | 327 | * Add defaults for additional Stripe action options. |
| 238 | 328 | * |
| 239 | 329 | * @param array $defaults |
| 330 | + * | |
| 240 | 331 | * @return array |
| 241 | 332 | */ |
| 242 | 333 | public static function add_action_defaults( $defaults ) { |
| 334 | + // Stripe action options. | |
| 243 | 335 | $defaults['plan_id'] = ''; |
| 244 | 336 | $defaults['capture'] = ''; |
| 245 | 337 | $defaults['stripe_link'] = ''; |
| 338 | + | |
| 339 | + // PayPal action options. | |
| 340 | + $defaults['product_name'] = ''; | |
| 341 | + $defaults['pay_later'] = ''; | |
| 342 | + | |
| 246 | 343 | return $defaults; |
| 247 | 344 | } |
| 248 | 345 | |
| 249 | 346 | /** |
| @@ -249,8 +346,9 @@ | ||
| 249 | 346 | /** |
| 250 | 347 | * Print additional options for Stripe action settings. |
| 251 | 348 | * |
| 252 | 349 | * @param array $atts |
| 350 | + * | |
| 253 | 351 | * @return void |
| 254 | 352 | */ |
| 255 | 353 | public static function add_action_options( $atts ) { |
| 256 | 354 | $form_action = $atts['form_action']; |
| @@ -262,84 +360,35 @@ | ||
| 262 | 360 | * Filter Stripe action on save. |
| 263 | 361 | * |
| 264 | 362 | * @param array $settings |
| 265 | 363 | * @param array $action |
| 364 | + * | |
| 266 | 365 | * @return array |
| 267 | 366 | */ |
| 268 | 367 | public static function before_save_settings( $settings, $action ) { |
| 269 | - $settings['currency'] = strtolower( $settings['currency'] ); | |
| 270 | - $settings['stripe_link'] = 1; // In Lite Stripe link is always used. | |
| 271 | - $settings = self::create_plans( $settings ); | |
| 272 | - $form_id = absint( $action['menu_order'] ); | |
| 368 | + $settings['currency'] = strtolower( $settings['currency'] ); | |
| 273 | 369 | |
| 274 | - if ( empty( $settings['credit_card'] ) ) { | |
| 275 | - $credit_card_field_id = FrmDb::get_var( | |
| 276 | - 'frm_fields', | |
| 277 | - array( | |
| 278 | - 'type' => 'credit_card', | |
| 279 | - 'form_id' => $form_id, | |
| 280 | - ) | |
| 281 | - ); | |
| 282 | - if ( ! $credit_card_field_id ) { | |
| 283 | - $credit_card_field_id = self::add_a_credit_card_field( $form_id ); | |
| 284 | - } | |
| 285 | - if ( $credit_card_field_id ) { | |
| 286 | - $settings['credit_card'] = $credit_card_field_id; | |
| 287 | - } | |
| 288 | - } | |
| 370 | + // Gateway is a radio button but it should always be an array in the database for | |
| 371 | + // compatibility with the payments submodule where it is a checkbox (when Authorize.Net is active). | |
| 372 | + $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' ); | |
| 289 | 373 | |
| 290 | - $gateway_field_id = FrmDb::get_var( | |
| 291 | - 'frm_fields', | |
| 292 | - array( | |
| 293 | - 'type' => 'gateway', | |
| 294 | - 'form_id' => $form_id, | |
| 295 | - ) | |
| 296 | - ); | |
| 297 | - if ( ! $gateway_field_id ) { | |
| 298 | - self::add_a_gateway_field( $form_id ); | |
| 374 | + $is_stripe = in_array( 'stripe', $settings['gateway'], true ); | |
| 375 | + | |
| 376 | + if ( ! $is_stripe ) { | |
| 377 | + return $settings; | |
| 299 | 378 | } |
| 300 | 379 | |
| 301 | - return $settings; | |
| 302 | - } | |
| 380 | + // In Lite Stripe link is always used. | |
| 381 | + $settings['stripe_link'] = 1; | |
| 303 | 382 | |
| 304 | - /** | |
| 305 | - * @param int $form_id | |
| 306 | - * @param string $field_type | |
| 307 | - * @param string $field_name | |
| 308 | - * @return int|false | |
| 309 | - */ | |
| 310 | - private static function add_a_field( $form_id, $field_type, $field_name ) { | |
| 311 | - $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); | |
| 312 | - $new_values['name'] = $field_name; | |
| 313 | - $field_id = FrmField::create( $new_values ); | |
| 314 | - return $field_id; | |
| 383 | + return self::create_plans( $settings ); | |
| 315 | 384 | } |
| 316 | 385 | |
| 317 | 386 | /** |
| 318 | - * A credit card field is added automatically if missing before a Stripe action is updated. | |
| 319 | - * | |
| 320 | - * @param int $form_id | |
| 321 | - * @return int|false | |
| 322 | - */ | |
| 323 | - private static function add_a_credit_card_field( $form_id ) { | |
| 324 | - return self::add_a_field( $form_id, 'credit_card', __( 'Payment', 'formidable' ) ); | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | - * A gateway field is added automatically for compatibility with the Stripe add on. | |
| 329 | - * The gateway field is not important for the Stripe Lite implementation. | |
| 330 | - * | |
| 331 | - * @param int $form_id | |
| 332 | - * @return int|false | |
| 333 | - */ | |
| 334 | - private static function add_a_gateway_field( $form_id ) { | |
| 335 | - return self::add_a_field( $form_id, 'gateway', __( 'Payment Method', 'formidable' ) ); | |
| 336 | - } | |
| 337 | - | |
| 338 | - /** | |
| 339 | 387 | * Create any required Stripe plans, used for subscriptions. |
| 340 | 388 | * |
| 341 | 389 | * @param array $settings |
| 390 | + * | |
| 342 | 391 | * @return array |
| 343 | 392 | */ |
| 344 | 393 | public static function create_plans( $settings ) { |
| 345 | 394 | if ( $settings['type'] !== 'recurring' || strpos( $settings['amount'], ']' ) ) { |
| @@ -348,8 +397,10 @@ | ||
| 348 | 397 | return $settings; |
| 349 | 398 | } |
| 350 | 399 | |
| 351 | 400 | $plan_opts = FrmStrpLiteSubscriptionHelper::prepare_plan_options( $settings ); |
| 401 | + | |
| 402 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 352 | 403 | if ( $plan_opts['id'] != $settings['plan_id'] ) { |
| 353 | 404 | $settings['plan_id'] = FrmStrpLiteSubscriptionHelper::maybe_create_plan( $plan_opts ); |
| 354 | 405 | } |
| 355 | 406 | |
| @@ -359,14 +410,26 @@ | ||
| 359 | 410 | /** |
| 360 | 411 | * Include settings in the plan description in order to make sure the correct plan is used. |
| 361 | 412 | * |
| 362 | 413 | * @param array $settings |
| 414 | + * | |
| 363 | 415 | * @return string |
| 364 | 416 | */ |
| 365 | 417 | public static function create_plan_id( $settings ) { |
| 366 | 418 | $amount = self::prepare_amount( $settings['amount'], $settings ); |
| 367 | - $id = sanitize_title_with_dashes( $settings['description'] ) . '_' . $amount . '_' . $settings['interval_count'] . $settings['interval'] . '_' . $settings['currency']; | |
| 368 | - return $id; | |
| 419 | + $parts = array( | |
| 420 | + sanitize_title_with_dashes( $settings['description'] ), | |
| 421 | + $amount, | |
| 422 | + $settings['interval_count'] . $settings['interval'], | |
| 423 | + $settings['currency'], | |
| 424 | + ); | |
| 425 | + | |
| 426 | + if ( isset( $settings['trial_interval_count'] ) && '' !== $settings['trial_interval_count'] ) { | |
| 427 | + // Include the trial so two actions that differ only by trial length don't share a plan. | |
| 428 | + $parts[] = $settings['trial_interval_count']; | |
| 429 | + } | |
| 430 | + | |
| 431 | + return implode( '_', $parts ); | |
| 369 | 432 | } |
| 370 | 433 | |
| 371 | 434 | /** |
| 372 | 435 | * If this form submits with ajax, load the scripts on the first page. |
| @@ -371,11 +434,13 @@ | ||
| 371 | 434 | /** |
| 372 | 435 | * If this form submits with ajax, load the scripts on the first page. |
| 373 | 436 | * |
| 374 | 437 | * @param array $params |
| 438 | + * | |
| 375 | 439 | * @return void |
| 376 | 440 | */ |
| 377 | 441 | public static function maybe_load_scripts( $params ) { |
| 442 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 378 | 443 | if ( $params['form_id'] == $params['posted_form_id'] ) { |
| 379 | 444 | // This form has already been posted, so we aren't on the first page. |
| 380 | 445 | return; |
| 381 | 446 | } |
| @@ -380,8 +445,9 @@ | ||
| 380 | 445 | return; |
| 381 | 446 | } |
| 382 | 447 | |
| 383 | 448 | $form = FrmForm::getOne( $params['form_id'] ); |
| 449 | + | |
| 384 | 450 | if ( ! $form ) { |
| 385 | 451 | return; |
| 386 | 452 | } |
| 387 | 453 | |
| @@ -390,8 +456,9 @@ | ||
| 390 | 456 | 'fi.form_id' => $form->id, |
| 391 | 457 | 'type' => 'credit_card', |
| 392 | 458 | ) |
| 393 | 459 | ); |
| 460 | + | |
| 394 | 461 | if ( ! $credit_card_field ) { |
| 395 | 462 | return; |
| 396 | 463 | } |
| 397 | 464 | |
| @@ -401,8 +468,9 @@ | ||
| 401 | 468 | /** |
| 402 | 469 | * Load front end JavaScript for a Stripe form. |
| 403 | 470 | * |
| 404 | 471 | * @param int $form_id |
| 472 | + * | |
| 405 | 473 | * @return void |
| 406 | 474 | */ |
| 407 | 475 | public static function load_scripts( $form_id ) { |
| 408 | 476 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) { |
| @@ -413,14 +481,15 @@ | ||
| 413 | 481 | return; |
| 414 | 482 | } |
| 415 | 483 | |
| 416 | 484 | $stripe_connect_is_setup = FrmStrpLiteConnectHelper::stripe_connect_is_setup(); |
| 485 | + | |
| 417 | 486 | if ( ! $stripe_connect_is_setup ) { |
| 418 | 487 | return; |
| 419 | 488 | } |
| 420 | 489 | |
| 421 | 490 | if ( ! $form_id || ! is_int( $form_id ) ) { |
| 422 | - _doing_it_wrong( __METHOD__, '$form_id parameter must be a non-zero integer', 'x.x' ); | |
| 491 | + _doing_it_wrong( __METHOD__, '$form_id parameter must be a non-zero integer', '6.5' ); | |
| 423 | 492 | return; |
| 424 | 493 | } |
| 425 | 494 | |
| 426 | 495 | $settings = FrmStrpLiteAppHelper::get_settings(); |
| @@ -444,15 +513,36 @@ | ||
| 444 | 513 | if ( ! $suffix && ! is_readable( FrmStrpLiteAppHelper::plugin_path() . 'js/frmstrp.js' ) ) { |
| 445 | 514 | // The unminified file is not included in releases so force the minified script. |
| 446 | 515 | $suffix = '.min'; |
| 447 | 516 | } |
| 517 | + | |
| 448 | 518 | $script_url = FrmStrpLiteAppHelper::plugin_url() . 'js/frmstrp' . $suffix . '.js'; |
| 449 | 519 | } |
| 450 | 520 | |
| 451 | - if ( class_exists( 'FrmProStrpLiteController' ) && ( ! $suffix || ! FrmFormsController::has_combo_js_file() ) ) { | |
| 521 | + if ( class_exists( 'FrmProStrpLiteController' ) && ( ! $suffix || ! FrmProAppController::has_combo_js_file() ) ) { | |
| 452 | 522 | $dependencies[] = 'formidablepro'; |
| 453 | 523 | } |
| 454 | 524 | |
| 525 | + $action_settings = self::prepare_settings_for_js( $form_id ); | |
| 526 | + $found_gateway = false; | |
| 527 | + | |
| 528 | + foreach ( $action_settings as &$action ) { | |
| 529 | + $gateways = $action['gateways']; | |
| 530 | + | |
| 531 | + if ( ! $gateways || in_array( 'stripe', (array) $gateways, true ) ) { | |
| 532 | + $found_gateway = true; | |
| 533 | + } | |
| 534 | + | |
| 535 | + if ( ! empty( $action['layout'] ) && ! in_array( $action['layout'], array( 'accordion', 'tabs' ), true ) ) { | |
| 536 | + $action['layout'] = ''; | |
| 537 | + } | |
| 538 | + } | |
| 539 | + unset( $action ); | |
| 540 | + | |
| 541 | + if ( ! $found_gateway ) { | |
| 542 | + return; | |
| 543 | + } | |
| 544 | + | |
| 455 | 545 | wp_enqueue_script( |
| 456 | 546 | 'formidable-stripe', |
| 457 | 547 | $script_url, |
| 458 | 548 | $dependencies, |
| @@ -459,11 +549,10 @@ | ||
| 459 | 549 | FrmAppHelper::plugin_version(), |
| 460 | 550 | false |
| 461 | 551 | ); |
| 462 | 552 | |
| 463 | - $action_settings = self::prepare_settings_for_js( $form_id ); | |
| 464 | - $style_settings = self::get_style_settings_for_form( $form_id ); | |
| 465 | - $stripe_vars = array( | |
| 553 | + $style_settings = self::get_style_settings_for_form( $form_id ); | |
| 554 | + $stripe_vars = array( | |
| 466 | 555 | 'publishable_key' => $publishable, |
| 467 | 556 | 'form_id' => $form_id, |
| 468 | 557 | 'nonce' => wp_create_nonce( 'frm_strp_ajax' ), |
| 469 | 558 | 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ), |
| @@ -482,8 +571,9 @@ | ||
| 482 | 571 | * |
| 483 | 572 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 484 | 573 | * |
| 485 | 574 | * @param mixed $form_id |
| 575 | + * | |
| 486 | 576 | * @return array |
| 487 | 577 | */ |
| 488 | 578 | private static function get_style_settings_for_form( $form_id ) { |
| 489 | 579 | if ( ! $form_id ) { |
| @@ -490,8 +580,9 @@ | ||
| 490 | 580 | return array(); |
| 491 | 581 | } |
| 492 | 582 | |
| 493 | 583 | $style = FrmStylesController::get_form_style( $form_id ); |
| 584 | + | |
| 494 | 585 | if ( ! $style ) { |
| 495 | 586 | return array(); |
| 496 | 587 | } |
| 497 | 588 | |
| @@ -496,8 +587,9 @@ | ||
| 496 | 587 | } |
| 497 | 588 | |
| 498 | 589 | $settings = FrmStylesHelper::get_settings_for_output( $style ); |
| 499 | 590 | $disallowed = array( ';', ':', '!important' ); |
| 591 | + | |
| 500 | 592 | foreach ( $settings as $k => $s ) { |
| 501 | 593 | if ( is_string( $s ) ) { |
| 502 | 594 | $settings[ $k ] = str_replace( $disallowed, '', $s ); |
| 503 | 595 | } |
| @@ -514,42 +606,97 @@ | ||
| 514 | 606 | * |
| 515 | 607 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 516 | 608 | * |
| 517 | 609 | * @param array $settings |
| 610 | + * | |
| 518 | 611 | * @return array |
| 519 | 612 | */ |
| 520 | 613 | private static function get_appearance_rules( $settings ) { |
| 521 | - return array( | |
| 522 | - '.Input' => array( | |
| 614 | + $rules = array( | |
| 615 | + '.Input' => array( | |
| 616 | + 'fontFamily' => $settings['font'], | |
| 523 | 617 | 'color' => $settings['text_color'], |
| 524 | 618 | 'backgroundColor' => $settings['bg_color'], |
| 525 | 619 | 'padding' => $settings['field_pad'], |
| 526 | 620 | 'lineHeight' => 1.3, |
| 527 | 621 | 'borderColor' => $settings['border_color'], |
| 528 | - 'borderWidth' => $settings['field_border_width'], | |
| 622 | + 'borderWidth' => self::get_border_width( $settings ), | |
| 529 | 623 | 'borderStyle' => $settings['field_border_style'], |
| 624 | + 'borderRadius' => self::get_border_radius( $settings ), | |
| 625 | + 'fontWeight' => $settings['field_weight'], | |
| 530 | 626 | ), |
| 531 | 627 | '.Input::placeholder' => array( |
| 532 | 628 | 'color' => $settings['text_color_disabled'], |
| 533 | 629 | ), |
| 534 | - '.Input:focus' => array( | |
| 630 | + '.Input:focus' => array( | |
| 535 | 631 | 'backgroundColor' => $settings['bg_color_active'], |
| 536 | 632 | ), |
| 537 | - '.Label' => array( | |
| 538 | - 'color' => $settings['label_color'], | |
| 539 | - 'fontSize' => $settings['font_size'], | |
| 540 | - 'fontWeight' => $settings['weight'], | |
| 633 | + '.Label' => array( | |
| 634 | + 'color' => $settings['label_color'], | |
| 635 | + 'fontSize' => $settings['font_size'], | |
| 636 | + 'fontWeight' => $settings['weight'], | |
| 637 | + 'padding' => $settings['label_padding'], | |
| 638 | + 'marginBottom' => 0, | |
| 541 | 639 | ), |
| 542 | - '.Error' => array( | |
| 640 | + '.Error' => array( | |
| 543 | 641 | 'color' => $settings['border_color_error'], |
| 544 | 642 | ), |
| 545 | 643 | ); |
| 644 | + | |
| 645 | + /* | |
| 646 | + * Filters the appearance rules for Stripe elements. | |
| 647 | + * | |
| 648 | + * @since 6.21 | |
| 649 | + * | |
| 650 | + * @param array $rules | |
| 651 | + * @param array $settings | |
| 652 | + */ | |
| 653 | + return apply_filters( 'frm_stripe_appearance_rules', $rules, $settings ); | |
| 546 | 654 | } |
| 547 | 655 | |
| 548 | 656 | /** |
| 657 | + * Get the border width for Stripe elements. | |
| 658 | + * | |
| 659 | + * @since 6.21 | |
| 660 | + * | |
| 661 | + * @param array $settings | |
| 662 | + * | |
| 663 | + * @return string | |
| 664 | + */ | |
| 665 | + private static function get_border_width( $settings ) { | |
| 666 | + if ( ! empty( $settings['field_shape_type'] ) && 'underline' === $settings['field_shape_type'] ) { | |
| 667 | + return '0 0 ' . $settings['field_border_width'] . ' 0'; | |
| 668 | + } | |
| 669 | + return $settings['field_border_width']; | |
| 670 | + } | |
| 671 | + | |
| 672 | + /** | |
| 673 | + * Get the border radius for Stripe elements. | |
| 674 | + * | |
| 675 | + * @since 6.21 | |
| 676 | + * | |
| 677 | + * @param array $settings | |
| 678 | + * | |
| 679 | + * @return string | |
| 680 | + */ | |
| 681 | + private static function get_border_radius( $settings ) { | |
| 682 | + if ( ! empty( $settings['field_shape_type'] ) ) { | |
| 683 | + switch ( $settings['field_shape_type'] ) { | |
| 684 | + case 'underline': | |
| 685 | + case 'regular': | |
| 686 | + return '0px'; | |
| 687 | + case 'circle': | |
| 688 | + return '30px'; | |
| 689 | + } | |
| 690 | + } | |
| 691 | + return $settings['border_radius']; | |
| 692 | + } | |
| 693 | + | |
| 694 | + /** | |
| 549 | 695 | * Get the language to use for Stripe elements. |
| 550 | 696 | * |
| 551 | 697 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 698 | + * | |
| 552 | 699 | * @return string |
| 553 | 700 | */ |
| 554 | 701 | private static function get_locale() { |
| 555 | 702 | $allowed = array( 'ar', 'da', 'de', 'en', 'es', 'fi', 'fr', 'he', 'it', 'ja', 'nl', 'no', 'pl', 'ru', 'sv', 'zh' ); |
| @@ -558,5 +705,26 @@ | ||
| 558 | 705 | $part = strtolower( $parts[0] ); |
| 559 | 706 | return in_array( $part, $allowed, true ) ? $part : 'auto'; |
| 560 | 707 | } |
| 561 | 708 | |
| 709 | + /** | |
| 710 | + * If the names are being used on the CC fields, | |
| 711 | + * make sure it doesn't prevent the submission if Stripe has approved. | |
| 712 | + * | |
| 713 | + * @since 6.7.1 | |
| 714 | + * | |
| 715 | + * @param array $errors | |
| 716 | + * @param stdClass $field | |
| 717 | + * @param array $values | |
| 718 | + * | |
| 719 | + * @return array | |
| 720 | + */ | |
| 721 | + public static function remove_cc_validation( $errors, $field, $values ) { | |
| 722 | + $has_processed = isset( $_POST[ 'frmintent' . $field->form_id ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 723 | + | |
| 724 | + if ( ! $has_processed ) { | |
| 725 | + return $errors; | |
| 726 | + } | |
| 727 | + | |
| 728 | + return FrmTransLiteActionsController::remove_cc_errors( $errors, $field ); | |
| 729 | + } | |
| 562 | 730 | } |