1"use strict"; 2 3jQuery( function($) { 4 5 if( 'undefined' === typeof( Stripe ) ) { 6 return; 7 } 8 9 // Create a Stripe client. 10 var stripe = Stripe( GetPaid_Stripe.stripePublishableKey ); 11 12 // Set-up forms. 13 $( 'body' ).on( 'getpaid_setup_payment_form', function( e, form ) { 14 15 form.on( 'getpaid_payment_form_changed_state', function() { 16 17 // Prepare args. 18 var stripe_id = form.find( '.getpaid-stripe-elements').attr( 'id' ); 19 var displayError = $( '#' + stripe_id + ' .getpaid-stripe-card-errors' ); 20 var form_state = form.data( 'getpaid_js_data' ); 21 22 // Abort if we have an error 23 if ( form_state.stripe_error ) { 24 displayError.addClass( 'alert alert-danger' ).text( form_state.stripe_error ); 25 return; 26 } 27 28 // Abort if already set-up. 29 if ( form.data( 'getpaid_stripe_elements' ) ) { 30 form.data( 'getpaid_stripe_element' ).destroy(); 31 } 32 33 displayError.removeClass( 'alert alert-danger' ).text( '' ); 34 35 // Set-up the payment intent and secret. 36 form.find( '.getpaid-stripe-payment-intent' ).val( form_state.stripe_payment_intent ); 37 form.find( '.getpaid-stripe-payment-intent-secret' ).val( form_state.stripe_payment_intent_secret ); 38 39 // Init the payment element. 40 try { 41 42 // Create an instance of Elements. 43 var elements = stripe.elements({ clientSecret: form_state ? form_state.stripe_payment_intent_secret : '' }); 44 45 // Create a payment element. 46 var element = elements.create( 'payment' ); 47 48 // Mount the element. 49 element.mount( '#' + stripe_id + ' .getpaid-stripe-elements-wrapper' ); 50 51 // Add the element to the cache. 52 form.data( 'getpaid_stripe_elements', elements ); 53 form.data( 'getpaid_stripe_element', element ); 54 55 // Handle real-time validation errors from the card Element. 56 element.addEventListener( 'change', function( event ) { 57 58 if ( event.error ) { 59 displayError.addClass( 'alert alert-danger mt-2' ).text( event.error.message ) 60 } else { 61 displayError.removeClass( 'alert alert-danger mt-2' ).text( '' ) 62 } 63 64 }); 65 66 } catch ( e ) { 67 displayError.addClass( 'alert alert-danger mt-2' ).text( e.message ) 68 } 69 70 }) 71 }) 72 73 // Handle form submission. 74 $( 'body' ).on( 'getpaid_process_stripe_payment', function( e, data, form ) { 75 76 var displayError = form.find( '.getpaid-stripe-card-errors' ); 77 78 // Abort if not set-up. 79 if ( ! form.data( 'getpaid_stripe_elements' ) ) { 80 displayError.addClass( 'alert alert-danger mt-2' ).text( GetPaid_Stripe.unknownError ); 81 return; 82 } 83 84 // Confirm the payment. 85 wpinvBlock( form ); 86 87 var elements = form.data( 'getpaid_stripe_elements' ); 88 89 elements 90 .fetchUpdates() 91 .then(function(result) { 92 93 // Handle result.error 94 if ( result.error ) { 95 displayError.addClass( 'alert alert-danger mt-2' ).text( result.error.message ); 96 wpinvUnblock( form ); 97 return; 98 } 99 100 // Process the payment. 101 var confirmation; 102 if ( data.is_setup ) { 103 confirmation = stripe.confirmSetup({ 104 elements: elements, 105 confirmParams: { 106 return_url: data.redirect, 107 }, 108 }) 109 } else { 110 confirmation = stripe.confirmPayment({ 111 elements: elements, 112 confirmParams: { 113 return_url: data.redirect, 114 }, 115 }) 116 } 117 118 // Check for errors. 119 confirmation.then(function( result ) { 120 if ( ! result.error ) { 121 return; 122 } 123 124 wpinvUnblock( form ); 125 126 // This point will only be reached if there is an immediate error when 127 // confirming the payment. Otherwise, your customer will be redirected to 128 // your `return_url`. For some payment methods like iDEAL, your customer will 129 // be redirected to an intermediate site first to authorize the payment, then 130 // redirected to the `return_url`. 131 if ( result.error.message ) { 132 displayError.addClass( 'alert alert-danger mt-2' ).text( result.error.message ); 133 } else { 134 displayError.addClass( 'alert alert-danger mt-2' ).text( GetPaid_Stripe.unknownError ); 135 } 136 137 console.log( result ); 138 }); 139 140 }); 141 }); 142 143 // Update payment methods. 144 if ( $( '#getpaid-stripe-update-payment-modal' ).length ) { 145 146 var updateModalElements; 147 var updateModalElement; 148 var successURL; 149 150 // Show update form. 151 $( 'body' ).on( 'click', '.getpaid-stripe-update-payment-method-button', function( e ) { 152
153 // Do not submit the form. 154 e.preventDefault(); 155 156 successURL = $( this ).data( 'redirect' ); 157 158 // Display the modal. 159 if ( window.bootstrap && window.bootstrap.Modal ) { 160 var paymentModal = new window.bootstrap.Modal(document.getElementById('getpaid-stripe-update-payment-modal') ); 161 paymentModal.show(); 162 } else { 163 $('#getpaid-stripe-update-payment-modal').modal() 164 } 165 166 // Handle errors. 167 var displayError = $( '.getpaid-stripe-update-payment-method-errors' ).removeClass( 'alert alert-danger mt-2' ).html( '' ); 168 169 if ( $( this ).data( 'error' ) ) { 170 displayError.addClass( 'alert alert-danger mt-2' ).html( $( this ).data( 'error' ) ); 171 return; 172 } 173 174 // Init the payment element. 175 try { 176 177 if ( ! updateModalElements ) { 178 179 // Create an instance of Elements. 180 updateModalElements = stripe.elements({ clientSecret: $( this ).data( 'intent' ) }); 181 182 // Create a payment element. 183 updateModalElement = updateModalElements.create( 'payment' ); 184 185 // Mount the element. 186 updateModalElement.mount( '.getpaid-stripe-update-payment-method' ); 187 188 // Handle real-time validation errors from the card Element. 189 updateModalElement.addEventListener( 'change', function( event ) { 190 191 if ( event.error ) { 192 displayError.addClass( 'alert alert-danger mt-2' ).text( event.error.message ) 193 } else { 194 displayError.removeClass( 'alert alert-danger mt-2' ).text( '' ) 195 } 196 197 }); 198 } 199 200 } catch ( e ) { 201 displayError.addClass( 'alert alert-danger mt-2' ).text( e.message ) 202 } 203 204 }); 205 206 // Handle form submission. 207 $( 'body' ).on( 'click', '.getpaid-process-updated-stripe-payment-method', function( e ) { 208
209 // Do not submit the form. 210 e.preventDefault(); 211 212 // Handle errors. 213 var displayError = $( '.getpaid-stripe-update-payment-method-errors' ).removeClass( 'alert alert-danger mt-2' ).html( '' ); 214 215 // Abort if not set-up. 216 if ( ! updateModalElements ) { 217 displayError.addClass( 'alert alert-danger mt-2' ).text( GetPaid_Stripe.unknownError ); 218 return; 219 } 220 221 // Confirm the payment. 222 wpinvBlock( '#getpaid-stripe-update-payment-modal .modal-body' ); 223 224 stripe 225 .confirmSetup({ 226 elements: updateModalElements, 227 confirmParams: { 228 return_url: successURL, 229 }, 230 }) 231 .then(function( result ) { 232 if ( ! result.error ) { 233 return; 234 } 235 236 wpinvUnblock( '#getpaid-stripe-update-payment-modal .modal-body' ); 237 238 // This point will only be reached if there is an immediate error when 239 // confirming the payment. Otherwise, your customer will be redirected to 240 // your `return_url`. For some payment methods like iDEAL, your customer will 241 // be redirected to an intermediate site first to authorize the payment, then 242 // redirected to the `return_url`. 243 if ( result.error.message ) { 244 displayError.addClass( 'alert alert-danger mt-2' ).text( result.error.message ); 245 } else { 246 displayError.addClass( 'alert alert-danger mt-2' ).text( GetPaid_Stripe.unknownError ); 247 } 248 249 console.log( result ); 250 }); 251 }); 252 } 253 254});
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.