1jQuery( async function( $ ) { 2 3 if( !( $('#stripe-pk').length > 0 ) ) 4 return false 5 6 var stripe_pk = $( '#stripe-pk' ).val() 7 8 //compatibility with PB conditional logic. if there are multiple subscription plans fields and the first one is hidden then it won't have a value attribute because of conditional logic 9 if( typeof stripe_pk == 'undefined' || stripe_pk == '' ) 10 stripe_pk = $('#stripe-pk').attr('conditional-value') 11 12 if( typeof stripe_pk == 'undefined' ) 13 return false 14 15 if ( typeof pms.stripe_connected_account == 'undefined' || pms.stripe_connected_account == '' ){ 16 console.log( 'Before you can accept payments, you need to connect your Stripe Account by going to Dashboard -> Paid Member Subscriptions -> Settings -> Payments.' ) 17 return false 18 } 19 20 var elements = false 21 var elements_setup_intent = false 22 var update_elements_setup_intent = false 23 var stripe = false 24 25 var $payment_element = '' 26 var $elements_instance_slug = '' 27 28 var cardIsEmpty = true 29 var recreating_payment_element = false 30 var email_change_debounce_timer = null 31 32 var subscription_plan_selector = 'input[name=subscription_plans]' 33 var paygate_selector = 'input.pms_pay_gate' 34 35 var stripe_appearance = {} 36 37 if( pms.pms_elements_appearance_api ) 38 stripe_appearance = pms.pms_elements_appearance_api 39 40 var StripeData = { 41 stripeAccount: pms.stripe_connected_account 42 } 43 44 if( pms.stripe_locale ) 45 StripeData.locale = pms.stripe_locale 46 47 stripe = Stripe( stripe_pk, StripeData ) 48 49 var stripe_payment_intent_options = { 50 mode : 'payment', 51 currency : pms.currency, 52 amount : 1099, 53 paymentMethodCreation: 'manual', 54 appearance : stripe_appearance, 55 } 56 57 var stripe_setup_intent_options = { 58 mode : 'setup', 59 currency : pms.currency, 60 paymentMethodCreation: 'manual', 61 appearance : stripe_appearance, 62 } 63 64 if( pms.off_session_payments && pms.off_session_payments == 1 ){ 65 stripe_payment_intent_options.setupFutureUsage = 'off_session' 66 stripe_setup_intent_options.setupFutureUsage = 'off_session' 67 } 68 69 if( pms.pms_customer_session ){ 70 stripe_payment_intent_options.customerSessionClientSecret = pms.pms_customer_session 71 stripe_setup_intent_options.customerSessionClientSecret = pms.pms_customer_session 72 } 73 74 elements = stripe.elements( stripe_payment_intent_options ) 75 elements_setup_intent = stripe.elements( stripe_setup_intent_options ) 76 77 stripeConnectInit() 78 79 // Validate currency for currently selected subscription plan if MC add-on
79is active 80 if ( pms.pms_mc_addon_active ) 81 await pms_stripe_validate_sdk_checkout_currency( $( $pms_checked_subscription ) ); 82 83 // Declare reCaptcha callback as already executed 84 if( typeof pms_initialize_recaptcha_v3 == 'function' ){ 85 window.pmsRecaptchaCallbackExecuted = true 86 87 jQuery('.pms-form').off('submit', pms_initialize_recaptcha_v3 ); 88 } 89 90 if( typeof wppbInitializeRecaptchaV3 == 'function' ){ 91 window.wppbRecaptchaCallbackExecuted = true 92 } 93 94 // Update Stripe Payment Intent on subscription plan change 95 $(document).on('click', subscription_plan_selector, async function ( event ) { 96 97 if ( pms.pms_mc_addon_active ){ 98 await pms_stripe_validate_sdk_checkout_currency( $(this) ); 99 } 100 101 stripeConnectInit() 102 103 }) 104 105 $(document).on('click', '.pms-subscription-plan-auto-renew input[name="pms_recurring"]', function ( event ) { 106 107 if( $(this).prop('checked') ){ 108 elements.update( { setupFutureUsage: 'off_session' } ); 109 } else { 110 elements.update( { setupFutureUsage: null } ); 111 } 112 113 }) 114 115 // This was added for Link support. 116 // After the user email is entered, the payment element is destroyed and then recreated using the new parameters. 117 $(document).on('change', '.pms-form .pms-user-email-field input[name="user_email"], .wppb-register-user input[name="email"], .pms-form .pms-billing-details input[name="pms_billing_email"]', function ( event ) { 118 119 // Skip if already recreating 120 if( recreating_payment_element ){ 121 return 122 } 123 124 let element = $(this) 125 let email = $(this).val() 126 127 // Clear any pending debounce timer 128 if( email_change_debounce_timer ){ 129 clearTimeout( email_change_debounce_timer ) 130 } 131 132 if( email.length > 0 ){ 133 134 // Disable the input to prevent multiple submissions 135 element.attr('disabled', true) 136 137 pms_stripe_show_spinner() 138 139 email_change_debounce_timer = setTimeout( function(){ 140 141 recreating_payment_element = true 142 143 // Only destroy if element exists 144 if( $payment_element && $payment_element != '' ){ 145 $payment_element.destroy() 146 $payment_element = '' 147 } 148 149 setTimeout( function(){ 150 stripeConnectInit() 151 element.attr('disabled', false) 152 recreating_payment_element = false 153 }, 300 ) 154 155 }, 250 ) 156 } 157 158 }) 159 160 // Discount applied 161 $(document).on('pms_discount_success', stripeConnectInit ) 162 $(document).on('pms_discount_error', stripeConnectInit ) 163 164 // Update elements price when taxes are applied or removed 165 $(document).on('pms_tax_applied', function ( event, data ) { 166 167 if( data.total > 0 ){ 168 elements.update( { amount: pms_stripe_convert_amount_to_cents( data.total ) } ); 169 } 170 171 }) 172 173 $(document).on('pms_tax_removed', function ( event ) { 174 175 if( typeof $pms_checked_subscription != 'undefined' ){ 176 177 let price = $pms_checked_subscription.data( 'price' ) 178 179 if( price > 0 ){ 180 elements.update( { amount: pms_stripe_convert_amount_to_cents( price ) } ); 181 } 182 } 183 184 }) 185 186 // Show credit card details on the update payment method form 187 if ( $( 'input[name="pms_update_payment_method"]' ).length > 0 && $( '.pms-paygate-extra-fields-stripe_connect' ).length > 0 ){ 188 $('.pms-paygate-extra-fields-stripe_connect').show() 189 } 190 191 // Paid Member Subscription submit buttons 192 var payment_buttons = 'input[name=pms_register], '; 193 payment_buttons += 'input[name=pms_new_subscription], '; 194 payment_buttons += 'input[name=pms_change_subscription], '; 195 payment_buttons += 'input[name=pms_upgrade_subscription], '; 196 payment_buttons += 'input[name=pms_renew_subscription], '; 197 payment_buttons += 'input[name=pms_confirm_retry_payment_subscription], '; 198 199 // Profile Builder submit buttons 200 payment_buttons += '.wppb-register-user input[name=register]'; 201 202 // WPPB Recaptcha 203 $(document).on( 'wppb_invisible_recaptcha_success', stripeConnectPaymentGatewayHandler ) 204 205 $(document).on('submit', '.pms-form', async function (e) { 206 207 if( e.target && ( jQuery( e.target ).attr('id') == 'pms_recover_password_form' || jQuery( e.target ).attr('id') == 'pms_new_password_form' || jQuery( e.target ).attr('id') == 'pms_login' ) ) 208 return 209 210 var target_button = $('input[type="submit"], button[type="submit"]', $(this)).not('#pms-apply-discount').not('input[name="pms_redirect_back"]') 211 212 // Email Confirmation using PB form 213 var form = $(this).closest( 'form' ) 214 215 if( typeof form != 'undefined' && form && form.length > 0 && form.hasClass( 'pms-ec-register-form' ) ){ 216
217 stripeConnectPaymentGatewayHandler(e, target_button) 218 219 // Skip if the Go Back button was pressed 220 } else if ( !e.originalEvent || !e.originalEvent.submitter || $(e.originalEvent.submitter).attr('name') != 'pms_redirect_back' ) { 221 222 if ( $(e.originalEvent.submitter).attr('name') == 'pms_update_payment_method' ) 223 stripeConnectUpdatePaymentMethod(e, target_button) 224 else 225 stripeConnectPaymentGatewayHandler(e, target_button) 226 227 } 228 229 }) 230 231 $(document).on('submit', '.wppb-register-user', function (e) { 232 233 if ( ! ( $( '.wppb-recaptcha .wppb-recaptcha-element', $(e.currentTarget) ).hasClass( 'wppb-invisible-recaptcha' ) ) ) { 234 235 var target_button = $('input[type="submit"], button[type="submit"]', $(this)).not('#pms-apply-discount').not('input[name="pms_redirect_back"]') 236 237 stripeConnectPaymentGatewayHandler(e, target_button) 238 239 } 240 241 }) 242 243 async function stripeConnectPaymentGatewayHandler( e, target_button = false ){ 244 245 if( $('input[type=hidden][name=pay_gate]').val() != 'stripe_connect' && $('input[type=radio][name=pay_gate]:checked').val() != 'stripe_connect' ) 246 return 247 248 if( $('input[type=hidden][name=pay_gate]').is(':disabled') || $('input[type=radio][name=pay_gate]:checked').is(':disabled') ) 249 return 250 251 e.preventDefault() 252 253 $.pms_form_remove_errors() 254 255 var current_button = $(this) 256 257 // Current submit button can't be determined from `this` context in case of the Invisible reCaptcha handler 258 if( e.type == 'wppb_invisible_recaptcha_success' || e.type == 'wppb_v3_recaptcha_success' || e.type == 'pms_v3_recaptcha_success' ){ 259 260 // target_button is supplied to the handler starting with version 3.5.0 of Profile Builder, we use this for backwards compatibility 261 current_button = target_button == false ? $( 'input[type="submit"]', $( '.wppb-recaptcha-element' ).closest( 'form' ) ) : $( target_button ) 262 263 } else if ( e.type == 'submit' ){ 264 265 if( target_button != false ) 266 current_button = $( target_button ) 267 268 } 269 270 // Disable the button 271 current_button.attr( 'disabled', true ) 272 273 return pms_stripe_maybe_validate_recaptcha( current_button, e ).then( async function( recaptcha_response ){ 274 275 let target_elements = elements 276 277 if( $.pms_checkout_is_setup_intents() ) 278 target_elements = elements_setup_intent 279 280 const {error: submitError} = await target_elements.submit() 281 282 if (submitError) { 283 let message = '' 284 285 if( submitError.message && submitError.message != '' ){ 286 message = submitError.message 287 } else { 288 message = 'An error occurred while processing your payment. Please try again.' 289 } 290 291 $.pms_stripe_add_credit_card_error( message ) 292 $.pms_form_scrollTo( '#pms-paygates-wrapper', current_button ) 293 return false 294 } 295 296 // Create the ConfirmationToken using the details collected by the Payment Element 297 const {error, confirmationToken} = await stripe.createConfirmationToken({ 298 elements : target_elements, 299 }); 300 301 if (error) { 302 303 let message = '' 304 305 if( error.message && error.message != '' ){ 306 message = error.message 307 } else { 308 message = 'An error occurred while processing your payment. Please try again.' 309 } 310 311 $.pms_stripe_add_credit_card_error( message ) 312 $.pms_form_scrollTo( '#pms-paygates-wrapper', current_button ) 313 return false 314 } 315 316 return pms_stripe_process_checkout( current_button, confirmationToken.id ).then( async function( response ){ 317 318 // Handle validation errors 319 if( response.success == false && ( typeof response.data != 'undefined' || typeof response.wppb_errors != 'undefined' || typeof response.pms_errors != 'undefined' ) ){
320 pms_stripe_handle_validation_errors( response, current_button ) 321 322 return false 323 } else if( response.success == false && typeof response.type != 'undefined' && response.type == 'use_stripe_sdk' ){ 324 325 let intent 326 327 if( $.pms_checkout_is_setup_intents() ){ 328 var { error, setupIntent } = await stripe.handleNextAction({ 329 clientSecret: response.client_secret 330 }); 331 332 intent = setupIntent 333 334 } else { 335 var { error, paymentIntent } = await stripe.handleNextAction({ 336 clientSecret: response.client_secret 337 }); 338 339 intent = paymentIntent 340 } 341 342 if( error && error.payment_intent ){ 343 intent = error.payment_intent 344 } else if ( error && error.setup_intent ){ 345 intent = error.setup_intent 346 } 347 348 // Process the payment on the server 349 const server_response = await pms_stripe_process_payment( intent, response, current_button ) 350 351 if ( typeof server_response.redirect_url != 'undefined' && server_response.redirect_url ){ 352 window.location.replace( server_response.redirect_url ) 353 354 return true 355 } 356 357 return false 358 359 } 360 361 362 // Redirect to the URL if the response contains a redirect URL 363 if ( typeof response.redirect_url != 'undefined' && response.redirect_url ){ 364 window.location.replace( response.redirect_url ) 365 return true 366 } 367 368 console.log( '[PMS Stripe] Something unexpected happened. Response: ' + response ) 369 370 return false; 371 372 }) 373 374 }) 375 } 376 377 async function pms_stripe_process_checkout( current_button, confirmationToken ){ 378 379 // grab all data from the form 380 var data = await $.pms_form_get_data( current_button, true ) 381 382 if( confirmationToken ){ 383 data['stripe_confirmation_token'] = confirmationToken 384 } 385 386 if( data == false ) 387 return 388 389 // prepare data 390 var form_data = new FormData() 391 392 for (var key in data) { 393 form_data.append(key, data[key]) 394 } 395 396 return fetch( pms.ajax_url, { 397 method : 'post', 398 credentials: 'same-origin', // Required for WordPress cookie authentication 399 body : form_data 400 }).then(function (res) { 401 return res.json() 402 }).catch(error => { 403 console.error('Something went wrong:', error) 404 throw error 405 }) 406 407 } 408 409 async function pms_stripe_process_payment( payment_intent, user_data, target_button ){ 410 411 // update nonce 412 nonce_data = {} 413 nonce_data.action = 'pms_update_nonce' 414 415 // Update nonce 416 const nonce = await fetch(pms.ajax_url, { 417 method: 'POST', 418 credentials: 'same-origin', 419 headers: { 420 'Content-Type': 'application/x-www-form-urlencoded', 421 }, 422 body: new URLSearchParams({ 423 action: 'pms_update_nonce' 424 }) 425 }).then(function (res) { 426 return res.json() 427 }).catch(error => { 428 console.error('Something went wrong:', error) 429 throw error 430 }) 431 432 // get form data 433 var form_data = await $.pms_form_get_data( target_button ) 434 435 form_data.pmstkn_original = form_data.pmstkn 436 form_data.pmstkn = '' 437 438 const data = { 439 ...form_data, 440 action : 'pms_process_payment', 441 user_id : user_data.user_id, 442 payment_id : user_data.payment_id, 443 subscription_id : user_data.subscription_id, 444 subscription_plan_id: user_data.subscription_plan_id, 445 payment_intent : payment_intent.id, 446 current_page : window.location.href, 447 pms_nonce : nonce 448 }; 449 450 // Add subscription price if plans exist 451 if ( data.subscription_plans ) { 452 const priceKey = `subscription_price_${data.subscription_plans}`; 453 454 if ( form_data[priceKey] ) { 455 data[priceKey] = form_data[priceKey]; 456 } 457 } 458 459 // prepare data 460 var request_data = new FormData() 461 462 for (var key in data) { 463 request_data.append(key, data[key]) 464 } 465 466 // Process payment 467 return await fetch(pms.ajax_url, { 468 method : 'POST', 469 credentials: 'same-origin', 470 body : request_data 471 }).then(function (res) { 472 return res.json() 473 }).catch(error => { 474 console.error('Something went wrong:', error) 475 throw error 476 }) 477 478 } 479 480 // Update Payment Method 481 function stripeConnectInitUpdatePaymentMethod(){ 482 483 var $client_secret_setup_intent = $('.pms-form input[name="pms_stripe_connect_setup_intent"]').val() 484 485 if( $client_secret_setup_intent && $client_secret_setup_intent.length > 0 ){ 486 487 update_elements_setup_intent = stripe.elements({ clientSecret: $client_secret_setup_intent, appearance: stripe_appearance }) 488 489 $update_element = update_elements_setup_intent.create( "payment" ) 490 $update_element.mount("#pms-stripe-payment-elements") 491 492 // Show credit card form error messages to the user as they happpen 493 $update_element.addEventListener('change', creditCardErrorsHandler ) 494 495 pms_stripe_hide_spinner() 496 } 497 498 } 499 500 function stripeConnectUpdatePaymentMethod( e, target_button = false ){ 501 502 e.preventDefault() 503 504 $.pms_form_remove_errors() 505 506 var current_button = $(this) 507 508 if ( target_button != false ) 509 current_button = $( target_button ) 510 511 //Disable the button 512 current_button.attr('disabled', true) 513 514 // Add error if credit card was not completed 515 if (cardIsEmpty === true) { 516 $.pms_form_add_validation_errors([{ target: 'credit_card', message: pms.invalid_card_details_error }], current_button) 517 return 518 } 519
520 stripe.confirmSetup({ 521 elements: update_elements_setup_intent, 522 confirmParams: { 523 return_url: pms.stripe_return_url, 524 payment_method_data: { billing_details: pms_stripe_get_billing_details() } 525 }, 526 redirect: 'if_required', 527 }).then(function (result) { 528 529 let token 530 531 if (result.error && result.error.decline_code && result.error.decline_code == 'live_mode_test_card') { 532 let errors = [{ target: 'credit_card', message: result.error.message }] 533 534 $.pms_form_add_validation_errors(errors, current_button) 535 } else if (result.error && result.error.type && result.error.type == 'validation_error') 536 $.pms_form_reset_submit_button(current_button) 537 else { 538 if (result.error && result.error.setup_intent) 539 token = { id: result.error.setup_intent.id } 540 else if (result.setupIntent) 541 token = { id: result.setupIntent.payment_method } 542 else 543 token = '' 544 545 stripeTokenHandler(token, $(current_button).closest('form')) 546 } 547 548 }) 549 550 } 551 552 function stripeConnectInit(){ 553 554 var target_elements_instance = false 555 var target_elements_instance_slug = '' 556 557 // Update Payment Method SetupIntent 558 if ( $('#pms-update-payment-method-form #pms-stripe-payment-elements').length > 0 ){ 559 stripeConnectInitUpdatePaymentMethod(); 560 return false; 561 // SetupIntent 562 } else if ( $.pms_checkout_is_setup_intents() ) { 563 target_elements_instance = elements_setup_intent 564 target_elements_instance_slug = 'setup_intents' 565 // PaymentIntents 566 } else { 567 target_elements_instance = elements 568 target_elements_instance_slug = 'payment_intents' 569 } 570 571 let selected_subscription = jQuery( subscription_plan_selector + '[type=radio]' ).length > 0 ? jQuery( subscription_plan_selector + '[type=radio]:checked' ) : jQuery( subscription_plan_selector + '[type=hidden]' ) 572 573 // Handle Setup Future Usage parameter 574 if( selected_subscription.data('recurring') == 0 || selected_subscription.data('recurring') == 1 ){ 575 576 let default_recurring = $('input[type="hidden"][name="pms_default_recurring"]').val() 577 578 if( default_recurring == 2 ){ 579 elements.update( { setupFutureUsage: 'off_session' } ); 580 } else if ( default_recurring == 3 ){ 581 elements.update( { setupFutureUsage: null } ); 582 } else { 583 // Verify renew checkbox status and update the payment element accordingly 584 if( $('.pms-subscription-plan-auto-renew input[name="pms_recurring"]').prop('checked') ){ 585 elements.update( { setupFutureUsage: 'off_session' } ); 586 } else { 587 elements.update( { setupFutureUsage: null } ); 588 } 589 } 590 591 } else if( selected_subscription.data('recurring') == 2 ){ 592 elements.update( { setupFutureUsage: 'off_session' } ); 593 } else if( selected_subscription.data('recurring') == 3 ){ 594 elements.update( { setupFutureUsage: null } ); 595 } 596 597 if( target_elements_instance != false ){ 598 599 if( $payment_element == '' ){ 600 601 if( typeof selected_subscription != 'undefined' && target_elements_instance_slug == 'payment_intents' ){ 602 603 // Use default price if custom currency price is unavailable 604 let price = (selected_subscription.data('mc_price') !== undefined && selected_subscription.data('mc_price') !== null) ? selected_subscription.data('mc_price') : selected_subscription.data('price'); 605 606 // Take into account sign-up fees as well 607 if( selected_subscription.data('sign_up_fee') !== undefined && selected_subscription.data('sign_up_fee') !== null && selected_subscription.data('sign_up_fee') !== '' && selected_subscription.data('sign_up_fee') !== '0' ){ 608 price = price + selected_subscription.data('sign_up_fee'); 609 } 610 611 if( price > 0 ){ 612 target_elements_instance.update( { amount: pms_stripe_convert_amount_to_cents( price ) } ); 613 } 614 } 615 616 let default_values = pms_stripe_get_default_values() 617 618 $payment_element = target_elements_instance.create( "payment", default_values ) 619 $payment_element.mount("#pms-stripe-payment-elements") 620 621 // Show credit card form error messages to the user as they happpen 622 $payment_element.addEventListener('change', creditCardErrorsHandler ) 623 624 } else { 625 626 // Update the amount of the payment element 627 if( typeof selected_subscription != 'undefined' && target_elements_instance_slug == 'payment_intents' ){ 628 629 // Use default price if custom currency price is unavailable 630 let price = (selected_subscription.data('mc_price') !== undefined && selected_subscription.data('mc_price') !== null) ? selected_subscription.data('mc_price') : selected_subscription.data('price'); 631 632 // Take into account sign-up fees as well 633 if( selected_subscription.data('sign_up_fee') !== undefined && selected_subscription.data('sign_up_fee') !== null && selected_subscription.data('sign_up_fee') !== '' && selected_subscription.data('sign_up_fee') !== '0' ){ 634 price = price + selected_subscription.data('sign_up_fee'); 635 } 636 637 if( price > 0 ){ 638 target_elements_instance.update( { amount: pms_stripe_convert_amount_to_cents( price ) } ); 639 } 640 } 641 642 let default_values = pms_stripe_get_default_values() 643 644 if( $elements_instance_slug != target_elements_instance_slug ){ 645 646 $payment_element.destroy() 647 648 $payment_element = target_elements_instance.create( "payment", default_values ) 649 $payment_element.mount("#pms-stripe-payment-elements") 650 651 // Show credit card form error messages to the user as they happpen 652 $payment_element.addEventListener('change', creditCardErrorsHandler ) 653 654 } 655 656 } 657 658 $elements_instance_slug = target_elements_instance_slug 659 660 pms_stripe_hide_spinner() 661 662 if( typeof paymentSidebarPosition == 'function' ){ 663 setTimeout( paymentSidebarPosition, 300 ) 664 } 665 666 } 667 668 } 669 670 async function stripeConnectUpdatePaymentIntent(){ 671 672 if( !$client_secret || !( $client_secret.length > 0 ) ) 673 return 674 675 // Don't make this call when a Free Trial subscription is selected since we use the prepared SetupIntent 676 if ( $.pms_checkout_is_setup_intents() || $( '#pms-update-payment-method-form' ).length > 0 ) 677 return 678 679 if( updating_payment_intent ) 680 return 681 682 updating_payment_intent = true 683
684 var submitButton = $('.pms-form .pms-form-submit, .pms-form input[type="submit"], .pms-form button[type="submit"], .wppb-register-user input[type="submit"], .wppb-register-user button[type="submit"]').not('#pms-apply-discount, .login-submit #wp-submit') 685 686 var data = $.pms_form_get_data( submitButton ) 687 688 data.action = 'pms_update_payment_intent_connect' 689 data.pms_nonce = $('#pms-stripe-ajax-update-payment-intent-nonce').val() 690 data.intent_secret = $client_secret 691 692 data.pmstkn_original = data.form_type == 'pms' ? $('.pms-form #pmstkn').val() : 'wppb_register' 693 data.pmstkn = '' 694 695 return await $.post(pms.ajax_url, data, function (response) { 696 697 if( typeof response == 'undefined' || response == '' ){ 698 updating_payment_intent = false 699 return false; 700 } 701 702 response = JSON.parse( response ) 703 704 if ( response.status == 'requires_payment_method' ) { 705 updating_payment_intent = false 706 707 elements.fetchUpdates().then( function(elements_response){ 708 if( typeof paymentSidebarPosition == 'function' ){ 709 setTimeout( paymentSidebarPosition, 300 ) 710 } 711 712 return true; 713 }) 714 } 715 716 updating_payment_intent = false 717 718 return false; 719 720 }) 721 722 } 723 724 function stripeConnectProcessPayment( result, user_data, form_data, target_button ){ 725 726 // update nonce 727 nonce_data = {} 728 nonce_data.action = 'pms_update_nonce' 729 730 $.post(pms.ajax_url, nonce_data, function (response) { 731 732 response = JSON.parse(response) 733 734 data = {} 735 data.action = 'pms_process_payment' 736 data.user_id = user_data.user_id 737 data.payment_id = user_data.payment_id 738 data.subscription_id = user_data.subscription_id 739 data.subscription_plan_id = user_data.subscription_plan_id 740 data.pms_current_subscription = form_data.pms_current_subscription 741 data.current_page = window.location.href 742 data.pms_nonce = response 743 data.form_type = form_data.form_type ? form_data.form_type : '' 744 data.pmstkn_original = form_data.pmstkn ? form_data.pmstkn : '' 745 data.setup_intent = form_data.setup_intent ? form_data.setup_intent : '' 746 data.user_consent_logged_in = form_data.user_consent_logged_in ? form_data.user_consent_logged_in : '' 747 748 if( data.setup_intent == '' ) 749 data.payment_intent = $client_secret_id 750 else 751 data.payment_intent = $client_secret_setup_id 752 753 // to determine actual location for change subscription 754 data.form_action = form_data.form_action ? form_data.form_action : '' 755 756 // for member data 757 data.pay_gate = form_data.pay_gate ? form_data.pay_gate : '' 758 data.subscription_plans = form_data.subscription_plans ? form_data.subscription_plans : '' 759 760 if( data.subscription_plans ) 761 data['subscription_price_' + data.subscription_plans] = form_data['subscription_price_' + data.subscription_plans] 762 763 // custom profile builder form name 764 data.form_name = form_data.form_name ? form_data.form_name : '' 765 766 if( form_data.pms_default_recurring ) 767 data.pms_default_recurring = form_data.pms_default_recurring 768 769 if ( form_data.pms_recurring ) 770 data.pms_recurring = form_data.pms_recurring 771 772 if ( form_data.discount_code ) 773 data.discount_code = form_data.discount_code 774 775 if ( form_data.group_name ) 776 data.group_name = form_data.group_name 777 778 if ( form_data.group_description ) 779 data.group_description = form_data.group_description 780 781 // add billing details 782 if ( form_data.pms_billing_address ) 783 data.pms_billing_address = form_data.pms_billing_address 784 785 if ( form_data.pms_billing_city ) 786 data.pms_billing_city = form_data.pms_billing_city 787 788 if ( form_data.pms_billing_country ) 789 data.pms_billing_country = form_data.pms_billing_country 790 791 if ( form_data.pms_billing_state ) 792 data.pms_billing_state = form_data.pms_billing_state 793 794 if ( form_data.pms_billing_zip ) 795 data.pms_billing_zip = form_data.pms_billing_zip 796 797 if ( form_data.pms_vat_number ) 798 data.pms_vat_number = form_data.pms_vat_number 799 800 if ( form_data.wppb_referer_url ) 801 data.wppb_referer_url = form_data.wppb_referer_url 802 803 $.post(pms.ajax_url, data, function (response) { 804 805 response = JSON.parse(response) 806 807 if( typeof response.redirect_url != 'undefined' && response.redirect_url ) 808 window.location.replace( response.redirect_url ) 809 810 }) 811 812 }) 813 814 } 815 816 /* 817 * Stripe response handler 818 * 819 */ 820 function stripeTokenHandler( token, $form = null ) { 821 822 if( $form === null ) 823 $form = $(payment_buttons).closest('form') 824 825 $form.append( $('<input type="hidden" name="stripe_token" />').val( token.id ) ) 826 827 // We have to append a hidden input to the form to simulate that the submit 828 // button has been clicked to have it to the $_POST 829 var button_name = $form.find('input[type="submit"], button[type="submit"]').not('#pms-apply-discount').not('input[name="pms_redirect_back"]').attr('name') 830 var button_value = $form.find('input[type="submit"], button[type="submit"]').not('#pms-apply-discount').not('input[name="pms_redirect_back"]').val() 831 832 $form.append( $('<input type="hidden" />').val( button_value ).attr('name', button_name ) ) 833 834 $form.get(0).submit() 835 836 } 837 838 function pms_stripe_get_billing_details() { 839 840 var data = {} 841 842 var email = $( '.pms-form input[name="user_email"], .wppb-user-forms input[name="email"]' ).val() 843 844 if( typeof email == 'undefined' || email == '' ) 845 data.email = $( '.pms-form input[name="pms_billing_email"]' ).val() 846 847 if( typeof email != 'undefined' && email != '' ) 848 data.email = email.replace(/\s+/g, '') // remove any whitespace that might be present in the email 849 850 var name = '' 851 852 if( $( '.pms-billing-details input[name="pms_billing_first_name"]' ).length > 0 ) 853 name = name + $( '.pms-billing-details input[name="pms_billing_first_name"]' ).val() + ' ' 854 else if( $( '.pms-form input[name="first_name"], .wppb-user-forms input[name="first_name"]' ).length > 0 ) 855 name = name + $( '.pms-form input[name="first_name"], .wppb-user-forms input[name="first_name"]' ).val() + ' ' 856 857 if( $( '.pms-billing-details input[name="pms_billing_last_name"]' ).length > 0 ) 858 name = name + $( '.pms-billing-details input[name="pms_billing_last_name"]' ).val() 859 else if( $( '.pms-form input[name="last_name"], .wppb-user-forms input[name="last_name"]' ).length > 0 ) 860 name = name + $( '.pms-form input[name="last_name"], .wppb-user-forms input[name="last_name"]' ).val() 861 862 if( name.length > 1 ) 863 data.name = name 864 865 if( $( '.pms-billing-details ').length > 0 ){ 866 867 data.address = { 868 city : $( '.pms-billing-details input[name="pms_billing_city"]' ).val(), 869 country : $( '.pms-billing-details input[name="pms_billing_country"]' ).val(), 870 line1 : $( '.pms-billing-details input[name="pms_billing_address"]' ).val(), 871 postal_code : $( '.pms-billing-details input[name="pms_billing_zip"]' ).val(), 872 state : $( '.pms-billing-details input[name="pms_billing_state"]' ).val() 873 } 874 875 } 876 877 return data 878 879 } 880
881 function creditCardErrorsHandler( event ){ 882 883 if( event.complete == true ) 884 cardIsEmpty = false 885 else 886 cardIsEmpty = true 887 888 if( typeof paymentSidebarPosition == 'function' ){ 889 setTimeout( paymentSidebarPosition, 300 ) 890 } 891 892 } 893 894 function pms_stripe_handle_validation_errors( response, current_button ){ 895 896 var form_type = $('.wppb-register-user .wppb-subscription-plans').length > 0 ? 'wppb' : $('.pms-ec-register-form').length > 0 ? 'pms_email_confirmation' : 'pms' 897 898 // Paid Member Subscription forms 899 if (response.data && ( form_type == 'pms' || form_type == 'pms_email_confirmation' ) ){ 900 $.pms_form_add_validation_errors( response.data, current_button ) 901 // Profile Builder form 902 } else { 903 904 // Add PMS related errors (Billing Fields) 905 // These are added first because the form will scroll to the error and these 906 // are always placed at the end of the WPPB form 907 if( response.pms_errors && response.pms_errors.length > 0 ) 908 $.pms_form_add_validation_errors( response.pms_errors, current_button ) 909 910 // Add WPPB related errors 911 if( typeof response.wppb_errors == 'object' ) 912 $.pms_form_add_wppb_validation_errors( response.wppb_errors, current_button ) 913 914 } 915 916 jQuery(document).trigger( 'pms_checkout_validation_error', response, current_button ) 917 918 } 919 920 async function pms_stripe_maybe_validate_recaptcha( current_button, event = null ){ 921 922 if( typeof pms_initialize_recaptcha_v3 == 'function' ){ 923 924 let form = current_button.closest('form') 925 926 var recaptcha_field = jQuery('.pms-recaptcha', form ) 927 928 if( recaptcha_field.length > 0 ){ 929 930 return await pms_initialize_recaptcha_v3( event, form ) 931 932 } 933 934 } 935 936 let wppb_form = current_button.closest('.wppb-register-user') 937 938 if( wppb_form.length > 0 && wppb_form[0].length > 0 && typeof wppbInitializeRecaptchaV3 == 'function' ){ 939 940 let wppb_recaptcha_field = jQuery('.wppb-recaptcha .wppb-recaptcha-element', wppb_form ) 941 942 if( wppb_recaptcha_field.length > 0 ){ 943 return await wppbInitializeRecaptchaV3( event, wppb_form ) 944 } 945 946 } 947 948 return true 949 950 } 951 952 function pms_stripe_convert_amount_to_cents( amount ){ 953 954 let currency = pms.currency 955 956 // List of zero-decimal currencies 957 const zero_decimal_currencies = [ 958 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 959 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF' 960 ]; 961 962 // If currency is not in zero-decimal list, multiply by 100 963 if ( !zero_decimal_currencies.includes( currency.toUpperCase() ) ) { 964 amount = amount * 100; 965 } 966 967 // Round to ensure we have a whole number 968 return Math.round( amount ); 969 } 970 971 /** 972 * Validate custom currency 973 * 974 * @param subscriptionPlan 975 * @returns {Promise<*>} 976 */ 977 async function pms_stripe_validate_sdk_checkout_currency( subscriptionPlan ) { 978 979 pms_stripe_show_spinner() 980 981 return $.ajax({ 982 url: pms.ajax_url, 983 type: 'POST', 984 data: { 985 action : 'pms_validate_sdk_currency', 986 pms_nonce : pms.pms_validate_currency_nonce, 987 subscription_plan_id: subscriptionPlan.val(), 988 pms_mc_currency : subscriptionPlan.data('mc_currency'), 989 pay_gate : 'stripe_connect' 990 }, 991 dataType: 'json', 992 }).done( function ( response ) { 993 994 if ( response && response.success && response.currency ) 995 elements.update( { currency: response.currency.toLowerCase() } ) 996 997 pms_stripe_hide_spinner() 998 999 return; 1000 1001 }); 1002 } 1003 1004 function pms_stripe_hide_spinner(){ 1005 1006 jQuery('#pms-stripe-payment-elements').show() 1007 jQuery( '#pms-stripe-connect .pms-spinner__holder' ).hide() 1008 1009 } 1010 1011 function pms_stripe_show_spinner(){ 1012 1013 jQuery('#pms-stripe-payment-elements').hide() 1014 jQuery( '#pms-stripe-connect .pms-spinner__holder' ).show() 1015 1016 } 1017 1018 function pms_stripe_get_default_values(){ 1019 1020 if( jQuery('body').hasClass( 'logged-in' ) && pms.pms_customer_email ){ 1021 var user_email = pms.pms_customer_email 1022 } else { 1023 var user_email = $( '.pms-form input[name="user_email"], .wppb-register-user input[name="email"]' ).val() 1024 } 1025 1026 let name = '' 1027 1028 if( jQuery('body').hasClass( 'logged-in' ) && pms.pms_customer_name ){ 1029 name = pms.pms_customer_name 1030 } else { 1031 if( $( '.pms-billing-details input[name="pms_billing_first_name"]' ).length > 0 ) 1032 name = name + $( '.pms-billing-details input[name="pms_billing_first_name"]' ).val() + ' ' 1033 else if( $( '.pms-form input[name="first_name"], .wppb-user-forms input[name="first_name"]' ).length > 0 ) 1034 name = name + $( '.pms-form input[name="first_name"], .wppb-user-forms input[name="first_name"]' ).val() + ' ' 1035 1036 if( $( '.pms-billing-details input[name="pms_billing_last_name"]' ).length > 0 ) 1037 name = name + $( '.pms-billing-details input[name="pms_billing_last_name"]' ).val() 1038 else if( $( '.pms-form input[name="last_name"], .wppb-user-forms input[name="last_name"]' ).length > 0 ) 1039 name = name + $( '.pms-form input[name="last_name"], .wppb-user-forms input[name="last_name"]' ).val() 1040 } 1041 1042 if( typeof user_email != 'undefined' && user_email.length > 0 ){ 1043 var default_values = { 1044 defaultValues: { 1045 billingDetails: { 1046 email: user_email, 1047 name : name.length > 0 ? name : null 1048 } 1049 } 1050 } 1051 } else { 1052 var default_values = {} 1053 } 1054 1055 return default_values 1056 1057 } 1058 1059});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.