1jQuery(function ($) { 2 3 var paypal_sdk_checkout = { 4 "client-id" : pms_paypal.paypal_client_id, 5 "merchant-id" : pms_paypal.paypal_merchant_id, 6 "components" : "buttons", 7 "disable-funding" : "card,credit,paylater,bancontact,blik,eps,giropay,ideal,mercadopago,mybank,p24,sepa,sofort,venmo", 8 "currency" : pms_paypal.paypal_currency, 9 "intent" : 'capture', 10 "commit" : true, 11 dataPartnerAttributionId: pms_paypal.paypal_partner_attribution_id, 12 } 13 14 var paypal_sdk_setup_intents = { 15 "client-id" : pms_paypal.paypal_client_id, 16 "merchant-id" : pms_paypal.paypal_merchant_id, 17 "components" : "buttons", 18 "disable-funding" : "card,credit,paylater,bancontact,blik,eps,giropay,ideal,mercadopago,mybank,p24,sepa,sofort,venmo", 19 dataPartnerAttributionId: pms_paypal.paypal_partner_attribution_id, 20 } 21 22 var subscription_plan_selector = 'input[name=subscription_plans]' 23 var paygate_selector = 'input.pms_pay_gate' 24 var paypal_initialized_type = false 25 26 $(document).ready( async function () { 27 28 // Don't initialize the SDK if the necessary data is missing 29 if( typeof pms_paypal === 'undefined' || typeof window.paypalLoadScript === 'undefined' ){ 30 console.log( 'PPCP: Necessary data is missing.' ); 31 return 32 } 33 34 if ( pms_paypal.paypal_client_id == '' || pms_paypal.paypal_merchant_id == '' ){ 35 console.log( 'PPCP: Necessary data is missing.' ); 36 return 37 } 38 39 // Move the PayPal Connect button placeholder just before the submit button of the form 40 await pms_ppcp_move_paypal_connect_button() 41 42 // Add Customer ID to the PayPal SDK configuration if user is logged in 43 if( jQuery('body').hasClass( 'logged-in') ){ 44 45 let customer_id = await pms_ppcp_generate_client_token() 46 47 if( customer_id ){ 48 paypal_sdk_checkout['dataUserIdToken'] = customer_id 49 } 50 51 } 52 53 // Initialize the SDK with the correct configuration based on the current checkout 54 if ( $.pms_checkout_is_setup_intents() || $( '#pms-paypal-update-payment-method-nonce' ).length > 0 ) 55 await pms_ppcp_initialize_sdk( paypal_sdk_setup_intents, 'setup_intents' ) 56 else 57 await pms_ppcp_initialize_sdk( paypal_sdk_checkout, 'checkout' ) 58 59 // Show PayPal Gateway extra fields on the update payment method form 60 if ( $( 'input[name="pms_update_payment_method"]' ).length > 0 && $( '.pms-paygate-extra-fields-paypal_connect' ).length > 0 ){ 61 $( '.pms-paygate-extra-fields-paypal_connect' ).show() 62 $( '#pms-update-payment-method-form input[type="submit"]:not([name="pms_redirect_back"]):not([id="pms-apply-discount"]), #pms-update-payment-method-form button[type="submit"], .wppb-edit-user input[name="pms_update_payment_method"]' ).last().hide() 63 } 64 65 // Show PayPal subscribe button on page load when PayPal is the selected payment gateway 66 pms_ppcp_maybe_show_paypal_subscribe_button() 67 68 // Switch the form submit button with the PayPal Connect subscribe button 69 $(document).on('click', paygate_selector, function () { 70 71 let form = $(this).closest( '.pms-form, .wppb-register-user' ) 72 73 if ( $(this).is(':checked') && $(this).val() == 'paypal_connect' ) 74 $('input[type="submit"]:not([name="pms_redirect_back"]):not([id="pms-apply-discount"]), button[type="submit"]', form).last().hide() 75 else 76 $('input[type="submit"], button[type="submit"]', form).show() 77 78 }) 79 80 // Re-initialize the SDK when the subscription plan is changed 81 $(document).on('click', subscription_plan_selector + '[type=radio], ' + subscription_plan_selector + '[type="hidden"]', async function () { 82 83 pms_ppcp_maybe_show_paypal_subscribe_button() 84 85 pms_ppcp_reinitialize_sdk() 86 87 }) 88 89 $( document ).on( 'click', paygate_selector, async function() { 90 91 if ( pms_paypal.pms_ppcp_mc_addon_active ){ 92 if( ($('input[type=hidden][name=pay_gate]').val() == 'paypal_connect' || $('input[type=radio][name=pay_gate]:checked').val() == 'paypal_connect') 93 && !$('input[type=hidden][name=pay_gate]').is(':disabled') && !$('input[type=radio][name=pay_gate]:checked').is(':disabled') ) { 94 await pms_ppcp_validate_sdk_checkout_currency( $(this) ); 95 } 96 } 97 98 }) 99 100 // Compatibility with "Automatically renew subscription" option 101 $(document).on('change', '.pms-subscription-plan-auto-renew input[name="pms_recurring"]', pms_ppcp_maybe_show_paypal_subscribe_button ) 102 103 // Compatibility with Multi-Step Forms 104 $(document).on('wppb_msf_next_step', pms_ppcp_maybe_show_paypal_subscribe_button ) 105 $(document).on('wppb_msf_previous_step', pms_ppcp_maybe_show_paypal_subscribe_button ) 106 107 // Re-initialize the SDK when a discount is applied 108 $(document).on( 'pms_discount_success', pms_ppcp_reinitialize_sdk ) 109 $(document).on( 'pms_discount_error', pms_ppcp_reinitialize_sdk ) 110 111 // Re-initialize when an addon signals a checkout state change (e.g. Order Bumps toggling line items) 112 $(document).on( 'pms_recompute_payment_mode', pms_ppcp_reinitialize_sdk ) 113 114 }) 115 116 /** 117 * Initialize the PayPal SDK with the given configuration. If the SDK is already loaded, it will be re-rendered with the new configuration. 118 * 119 * @param {Object} data PayPal SDK configuration
120 * @param {String} type The type of PayPal SDK to initialize 121 * @returns {Promise} Resolves with PayPal object when initialization is complete 122 */ 123 async function pms_ppcp_initialize_sdk( data, type ) { 124 125 pms_ppcp_show_spinner() 126 127 // Return if already initialized and the currency was not changed for paypal_sdk_checkout 128 if( type === paypal_initialized_type && ( type !== 'checkout' || data.currency === pms_paypal.paypal_currency ) ){ 129 pms_ppcp_hide_spinner() 130 return 131 } 132 133 // If the currency changes remove existing button 134 if( data.currency !== pms_paypal.paypal_currency ){ 135 $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).html('') 136 pms_paypal.paypal_currency = data.currency 137 } 138 139 // Load SDK with new arguments 140 const paypal = await window.paypalLoadScript( data ) 141 142 // Re-render buttons if placeholder exists 143 if ( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).length > 0 ) { 144 145 if ( $.pms_checkout_is_setup_intents() ) { 146 147 paypal_initialized_type = 'setup_intents' 148 149 paypal.Buttons({ 150 style : pms_paypal.paypal_button_styles, 151 createVaultSetupToken: pms_ppcp_create_vault_setup_token, 152 onApprove : pms_ppcp_process_checkout, 153 onCancel : function (data, actions) { 154 155 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 156 157 }, 158 onError: function () { 159 160 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 161 162 } 163 }).render('#pms-paygate-extra-fields-paypal_connect__placeholder') 164 165 } else if ( $( '#pms-paypal-update-payment-method-nonce' ).length > 0 ) { 166 167 paypal_initialized_type = 'update_payment_method' 168 169 // On the Update payment method form, leave the label of the field as just "PayPal" 170 if ( $( '#pms-update-payment-method-form #pms-paypal-connect' ).length > 0 ) { 171 pms_paypal.paypal_button_styles.label = 'paypal' 172 } 173 174 paypal.Buttons({ 175 style : pms_paypal.paypal_button_styles, 176 createVaultSetupToken: pms_ppcp_update_payment_method_create_vault_setup_token, 177 onApprove : pms_ppcp_process_checkout, 178 onCancel : function (data, actions) { 179 180 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 181 182 }, 183 onError: function () { 184 185 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 186 187 } 188 }).render('#pms-paygate-extra-fields-paypal_connect__placeholder') 189 190 } else { 191 192 paypal_initialized_type = 'checkout' 193 194 paypal.Buttons({ 195 style : pms_paypal.paypal_button_styles, 196 createOrder: pms_ppcp_create_order, 197 onApprove : pms_ppcp_process_checkout, 198 onCancel : function (data, actions) { 199 200 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 201 202 }, 203 onError: function () { 204 205 $.pms_form_reset_submit_button( $( '#pms-paygate-extra-fields-paypal_connect__placeholder' ).closest( '.pms-form, .wppb-register-user' ).find( 'input[type="submit"], button[type="submit"]' ).last() ) 206 207 } 208 }
208).render('#pms-paygate-extra-fields-paypal_connect__placeholder') 209 210 } 211 212 } 213 214 pms_ppcp_hide_spinner() 215 216 return paypal 217 218 } 219 220 async function pms_ppcp_reinitialize_sdk(){ 221 222 if( !$.pms_checkout_is_setup_intents() ) { 223 224 if ( pms_paypal.pms_ppcp_mc_addon_active ) 225 await pms_ppcp_validate_sdk_checkout_currency( $pms_checked_subscription ); 226 227 pms_ppcp_initialize_sdk( paypal_sdk_checkout, 'checkout' ) 228 } 229 else 230 pms_ppcp_initialize_sdk( paypal_sdk_setup_intents, 'setup_intents' ) 231 232 } 233 234 /** 235 * Create an order 236 * 237 * @param {Object} data Data object 238 * @param {Object} actions PayPal actions object 239 */ 240 async function pms_ppcp_create_order( data, actions ) { 241 242 if ( $('input[type=hidden][name=pay_gate]').val() != 'paypal_connect' && $('input[type=radio][name=pay_gate]:checked').val() != 'paypal_connect' ) 243 return false 244 245 if ( $('input[type=hidden][name=pay_gate]').is(':disabled') || $('input[type=radio][name=pay_gate]:checked').is(':disabled') ) 246 return false 247 248 $.pms_form_remove_errors() 249 250 // Find the form that contains the active PayPal Connect payment gateway 251 let paygate_input = $('input[type=hidden][name=pay_gate][value="paypal_connect"]').length > 0 ? $('input[type=hidden][name=pay_gate][value="paypal_connect"]') : $('input[type=radio][name=pay_gate][value="paypal_connect"]:checked') 252 let target_form = paygate_input.closest('.pms-form, .wppb-register-user') 253 let current_button = target_form.find('.pms-form-submit, input[type="submit"], button[type="submit"]').not('#pms-apply-discount').not('input[name="pms_redirect_back"]') 254 255 if ( !( current_button.length > 0 ) ) 256 return false 257 258 // Disable the button 259 current_button.attr( 'disabled', true ) 260 261 let pms_checked_subscription = jQuery(subscription_plan_selector + '[type=radio]').length > 0 ? jQuery(subscription_plan_selector + '[type=radio]:checked') : jQuery(subscription_plan_selector + '[type=hidden]') 262 263 let extra_data = { 264 'pay_gate' : $('input[type=hidden][name=pay_gate]').length > 0 ? $('input[type=hidden][name=pay_gate]').val(): $('input[type=radio][name=pay_gate]:checked').val(), 265 'subscription_plans' : pms_checked_subscription.val(), 266 'paypal_action_nonce': pms_paypal.pms_ppcp_create_order_nonce 267 } 268 269 return pms_ppcp_maybe_validate_recaptcha( current_button ).then( function( recaptcha_response ){ 270 271 return pms_ppcp_validate_checkout( current_button, extra_data ).then( function (response) { 272 273 if( response ) { 274 if( response.success == true && response.order_id ) { 275 276 return response.order_id 277 278 } 279 } 280 281 return false 282 283 }) 284 285 }) 286 287 } 288 289 /** 290 * Create a vault setup token 291 * 292 * @param {Object} data Data object 293 * @param {Object} actions PayPal actions object 294 */ 295 function pms_ppcp_create_vault_setup_token( data, actions ) { 296 297 if ( $('input[type=hidden][name=pay_gate]').val() != 'paypal_connect' && $('input[type=radio][name=pay_gate]:checked').val() != 'paypal_connect' ) 298 return false 299 300 if ( $('input[type=hidden][name=pay_gate]').is(':disabled') || $('input[type=radio][name=pay_gate]:checked').is(':disabled') ) 301 return false 302 303 $.pms_form_remove_errors() 304 305 // Find the form that contains the active PayPal Connect payment gateway 306 let paygate_input = $('input[type=hidden][name=pay_gate][value="paypal_connect"]').length > 0 ? $('input[type=hidden][name=pay_gate][value="paypal_connect"]') : $('input[type=radio][name=pay_gate][value="paypal_connect"]:checked') 307 let target_form = paygate_input.closest('.pms-form, .wppb-register-user') 308 let current_button = target_form.find('.pms-form-submit, input[type="submit"], button[type="submit"]').not('#pms-apply-discount').not('input[name="pms_redirect_back"]') 309 310 if ( !( current_button.length > 0 ) ) 311 return false 312 313 // Disable the button 314 current_button.attr('disabled', true) 315 316 let pms_checked_subscription = jQuery(subscription_plan_selector + '[type=radio]').length > 0 ? jQuery(subscription_plan_selector + '[type=radio]:checked') : jQuery(subscription_plan_selector + '[type=hidden]') 317 318 let extra_data = { 319 'pay_gate' : $('input[type=hidden][name=pay_gate]').length > 0 ? $('input[type=hidden][name=pay_gate]').val(): $('input[type=radio][name=pay_gate]:checked').val(), 320 'subscription_plans' : pms_checked_subscription.val(), 321 'paypal_action_nonce': pms_paypal.pms_ppcp_create_setup_token_nonce 322 } 323 324 return pms_ppcp_validate_checkout( current_button, extra_data ).then(function (response) { 325 326 if( response ) { 327 if( response.success == true && response.setup_token_id ) { 328 329 return response.setup_token_id 330 331 } 332 } 333 334 return false 335 336 }) 337 338 } 339 340 /** 341 * Create a vault setup token 342 * 343 * @param {Object} data Data object 344 * @param {Object} actions PayPal actions object 345 * 346 * @returns {Promise} The vault setup token 347 */ 348 function pms_ppcp_update_payment_method_create_vault_setup_token( data, actions ) { 349 350 // prepare data 351 var form_data = new FormData() 352 353 form_data.append( 'action', 'pms_ppcp_create_setup_token' ) 354 form_data.append( 'nonce', pms_paypal.pms_ppcp_create_setup_token_nonce ) 355 356 return fetch( pms_paypal.ajax_url, { 357 method : 'post', 358 credentials: 'same-origin', 359 body : form_data 360 }).then(function (res) { 361 return res.json() 362 }).then(function (response) { 363 364 if ( response && response.success && response.setup_token_id ) { 365 return response.setup_token_id 366 } 367 368 return false 369 370 }).catch(error => { 371 console.error('Something went wrong:', error) 372 throw error 373 }) 374 375 } 376 377 /** 378 * Process the checkout 379 * 380 * @param {Object} response Response from PayPal 381 * @param {Object} actions PayPal actions object 382 */ 383 async function pms_ppcp_process_checkout( response, actions ) { 384 385 if ( !response ) 386 return 387 388 // Find the form that contains the active PayPal Connect payment gateway 389 let paygate_input = $('input[type=hidden][name=pay_gate][value="paypal_connect"]').length > 0 ? $('input[type=hidden][name=pay_gate][value="paypal_connect"]') : $('input[type=radio][name=pay_gate][value="paypal_connect"]:checked') 390 let target_form = paygate_input.closest('.pms-form, .wppb-register-user') 391 let current_button = target_form.find('.pms-form-submit, input[type="submit"], button[type="submit"]').not('#pms-apply-discount').not('input[name="pms_redirect_back"]') 392 393 if ( !( current_button.length > 0 ) ) 394 return; 395 396 // grab all data from the form 397 var data = await $.pms_form_get_data( current_button, false ) 398 399 if ( data == false ) 400 return 401 402 if( response.vaultSetupToken ){ 403 data.paypal_vault_setup_token = response.vaultSetupToken 404 } else { 405 data.paypal_order_id = response.orderID 406 } 407 408 // expand array values into separate appends so multi-value fields reach the wire as repeated entries 409 // - FormData.append stringifies array values via comma-join, collapsing them into a single scalar 410 var form_data = new FormData(); 411 412 for (var key in data) { 413 if ( Array.isArray( data[key] ) ) {
414 data[key].forEach( function ( value ) { 415 form_data.append( key, value ) 416 } ) 417 } else { 418 form_data.append( key, data[key] ) 419 } 420 } 421 422 return fetch( pms_paypal.ajax_url, { 423 method: 'post', 424 credentials: 'same-origin', // Required for WordPress cookie authentication 425 body: form_data 426 }).then(function (res) { 427 return res.json() 428 }).then(function (response) { 429 430 if (typeof response.redirect_url != 'undefined' && response.redirect_url) 431 window.location.replace(response.redirect_url) 432 433 }).catch(error => { 434 console.error('Something went wrong:', error); 435 throw error; 436 }); 437 438 } 439 440 /** 441 * Validate custom currency 442 * 443 * @param subscriptionPlan 444 * @returns {Promise<*>} 445 */ 446 async function pms_ppcp_validate_sdk_checkout_currency( subscriptionPlan ) { 447 448 pms_ppcp_show_spinner() 449 450 return $.ajax({ 451 url: pms_paypal.ajax_url, 452 type: 'POST', 453 data: { 454 // Send the current subscription context so prorated upgrade and downgrade flows can resolve the correct SDK currency 455 action : 'pms_validate_sdk_currency', 456 pms_nonce : pms_paypal.pms_validate_currency_nonce, 457 subscription_plan_id : subscriptionPlan.val(), 458 pms_mc_currency : subscriptionPlan.data('mc_currency'), 459 pms_current_subscription: $('input[name="pms_current_subscription"]').val(), 460 subscription_id : $('input[name="subscription_id"]').val(), 461 pay_gate : 'paypal_connect' 462 }, 463 dataType: 'json', 464 }).done( function ( response ) { 465 466 pms_ppcp_hide_spinner() 467 468 if ( response && response.success ) { 469 paypal_sdk_checkout.currency = response.currency; 470 } 471 }).fail( function () { 472 473 pms_ppcp_hide_spinner() 474 475 }); 476 } 477 478 /** 479 * Validate the checkout 480 * 481 * @param {Object} current_button The submit button element 482 * @param {Object} extra_data Additional data to send with the validation request 483 * @returns {Boolean} True if the checkout is valid, false otherwise 484 */ 485 async function pms_ppcp_validate_checkout( current_button, extra_data = {} ) { 486 487 if (current_button.length == 0) 488 return false 489 490 var data = await $.pms_form_get_data( current_button ) 491 492 // Same data as a Process Checkout request, we only switch the action 493 data.action = 'pms_validate_checkout' 494 495 // Merge extra data with the data object 496 if ( extra_data ) { 497 for ( var key in extra_data ) { 498 data[key] = extra_data[key] 499 } 500 } 501 502 // expand array values into separate appends so multi-value fields reach the wire as repeated entries 503 // - FormData.append stringifies array values via comma-join, collapsing them into a single scalar 504 var form_data = new FormData() 505 506 for (var key in data) { 507 if ( Array.isArray( data[key] ) ) { 508 data[key].forEach( function ( value ) { 509 form_data.append( key, value ) 510 } ) 511 } else { 512 form_data.append( key, data[key] ) 513 } 514 } 515 516 return fetch( pms_paypal.ajax_url, { 517 method: 'post', 518 credentials: 'same-origin', // Required for WordPress cookie authentication 519 body: form_data 520 }).then(function (res) { 521 return res.json() 522 }).then(function (response) { 523 524 if ( response ) { 525 526 if ( response.success == false ) { 527 528 pms_ppcp_handle_validation_errors( response, current_button ) 529 530 return false 531 532 } else if( response.success == true ){ 533 534 return response 535 536 } 537 538 } 539 540 return false 541 542 }).catch(error => { 543 console.error('Something went wrong:', error) 544 throw error 545 }) 546 547 } 548 549 async function pms_ppcp_maybe_validate_recaptcha( current_button ){ 550 551 if( typeof pms_initialize_recaptcha_v3 == 'function' ){ 552 553 let form = current_button.closest('form') 554 555 var recaptcha_field = jQuery('.pms-recaptcha', form ) 556 557 if( recaptcha_field.length > 0 ){ 558 559 return await pms_initialize_recaptcha_v3( null, form ) 560 561 } 562 563 } 564 565 let wppb_form = current_button.closest('.wppb-register-user') 566 567 if( wppb_form.length > 0 && wppb_form[0].length > 0 && typeof wppbInitializeRecaptchaV3 == 'function' ){ 568 569 let wppb_recaptcha_field = jQuery('.wppb-recaptcha .wppb-recaptcha-element', wppb_form ) 570 571 if( wppb_recaptcha_field.length > 0 ){ 572 return await wppbInitializeRecaptchaV3( null, wppb_form ) 573 } 574 575 } 576 577 return true 578 579 } 580 581 /** 582 * Handle validation errors 583 * 584 * @param {Object} response Validation response 585 * @param {Object} current_button Current button 586 */
587 function pms_ppcp_handle_validation_errors(response, current_button) { 588 589 var form_type = $('.wppb-register-user .wppb-subscription-plans').length > 0 ? 'wppb' : $('.pms-ec-register-form').length > 0 ? 'pms_email_confirmation' : 'pms' 590 591 // Paid Member Subscription forms 592 if (response.data && (form_type == 'pms' || form_type == 'pms_email_confirmation')) { 593 $.pms_form_add_validation_errors(response.data, current_button) 594 // Profile Builder form 595 } else { 596 597 // Add PMS related errors (Billing Fields) 598 // These are added first because the form will scroll to the error and these 599 // are always placed at the end of the WPPB form 600 if (response.pms_errors && response.pms_errors.length > 0) 601 $.pms_form_add_validation_errors(response.pms_errors, current_button) 602 603 // Add WPPB related errors 604 if (typeof response.wppb_errors == 'object') 605 $.pms_form_add_wppb_validation_errors(response.wppb_errors, current_button) 606 607 jQuery(document).trigger('pms_checkout_validation_error', response, current_button) 608 609 } 610 611 } 612 613 /** 614 * Move the PayPal Connect button to the correct position 615 */ 616 async function pms_ppcp_move_paypal_connect_button() { 617 618 if( $( '#pms-paypal-connect' ).length == 0 ) 619 return 620 621 // Logged out PMS register form 622 if ( $( '.pms-form .pms-form-submit' ).length > 0 ) { 623 624 $( '.pms-form #pms-paypal-connect' ).insertBefore( '.pms-form .pms-form-submit' ) 625 626 // Logged in PMS payment form 627 } else if( $( '.pms-form' ).length > 0 ){ 628 629 // Some themes switch the submit button with a button element 630 if( $( '.pms-form button[type="submit"]' ).length > 0 ){ 631 $( '.pms-form #pms-paypal-connect' ).insertBefore( '.pms-form button[type="submit"]' ) 632 } else { 633 $( '.pms-form #pms-paypal-connect' ).insertBefore( 634 $( '.pms-form input[type="submit"]:not([name="pms_redirect_back"]):not([id="pms-apply-discount"])' ) 635 ) 636 } 637 638 // Profile Builder register form 639 } else if ( $( '.wppb-register-user' ).length > 0 ) { 640 641 $( '.wppb-register-user #pms-paypal-connect' ).insertBefore( '.wppb-register-user .form-submit' ) 642 643 } 644 645 } 646 647 /** 648 * Generate a client token 649 * 650 * @returns {Promise} The client token 651 */ 652 async function pms_ppcp_generate_client_token(){ 653 654 // prepare data 655 var form_data = new FormData() 656 657 form_data.append( 'action', 'pms_ppcp_generate_client_token' ) 658 form_data.append( 'nonce', pms_paypal.pms_ppcp_generate_client_token_nonce ) 659 660 return fetch( pms_paypal.ajax_url, { 661 method : 'post', 662 credentials: 'same-origin', 663 body : form_data 664 }).then(function (res) { 665 return res.json() 666 }).then(function (response) { 667 668 if ( response && response.success && response.client_token ) { 669 return response.client_token 670 } 671 672 return false 673 674 }).catch(error => { 675 console.error('Something went wrong:', error) 676 throw error 677 }) 678 679 } 680 681 function pms_ppcp_show_spinner(){ 682 jQuery( '#pms-paypal-connect .pms-spinner__holder' ).show() 683 jQuery( '#pms-paypal-connect #pms-paygate-extra-fields-paypal_connect__placeholder' ).hide() 684 } 685 686 function pms_ppcp_hide_spinner(){ 687 688 setTimeout( function(){ 689 690 jQuery( '#pms-paypal-connect .pms-spinner__holder' ).hide() 691 jQuery( '#pms-paypal-connect #pms-paygate-extra-fields-paypal_connect__placeholder' ).show() 692 693 }, 100 ) 694 695 } 696 697 function pms_ppcp_maybe_show_paypal_subscribe_button(){ 698 699 if ( ( $( 'input[type=radio][name=pay_gate]:checked' ).val() == 'paypal_connect' || $('input[type=hidden][name=pay_gate]').val() == 'paypal_connect' ) && 700 ( !$( 'input[type=radio][name=pay_gate]:checked' ).is(':disabled') || !$('input[type=hidden][name=pay_gate]').is(':disabled') ) 701 ){ 702 703 // Also verify that the selected plan is not free 704 if( $pms_checked_subscription.data('price') > 0 ){ 705 706 let show_button = true 707 708 if( jQuery('.wppb-register-user .form-submit input[type="submit"], .wppb-register-user.form-submit button[type="submit"]').length > 0 ){ 709 710 let wppb_form = jQuery('.wppb-register-user .form-submit input[type="submit"], .wppb-register-user.form-submit button[type="submit"]').closest('.wppb-register-user') 711 712 // If Multi-Step Forms is enabled, we only need to show the button on the last step 713 if( jQuery( '.wppb-msf-step', wppb_form ).length > 0 ){ 714 715 let step_count = jQuery( '.wppb-msf-step', wppb_form ).length 716 step_count = step_count - 1 717 718 if( !jQuery( '#wppb-msf-step-' + step_count ).is(':visible') ){ 719 show_button = false 720 721 $( '.pms-paygate-extra-fields-paypal_connect' ).hide() 722 } 723 724 } 725 726 } 727 728 if( show_button ) { 729 $( '.pms-paygate-extra-fields-paypal_connect' ).show() 730 $( '.pms-form input[type="submit"]:not([name="pms_redirect_back"]):not([id="pms-apply-discount"]), .pms-form button[type="submit"], .wppb-register-user .form-submit input[type="submit"], .wppb-register-user.form-submit button[type="submit"]' ).last().hide() 731 } 732 733 } else { 734 735 $( '.pms-paygate-extra-fields-paypal_connect' ).hide() 736 $( '.pms-form input[type="submit"]:not([name="pms_redirect_back"]):not([id="pms-apply-discount"]), .pms-form button[type="submit"], .wppb-register-user .form-submit input[type="submit"], .wppb-register-user.form-submit button[type="submit"]' ).last().show() 737 738 } 739 } 740 741 } 742 743})
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.