window.GFStripe=null,function(c){GFStripe=function(e){for(var t in e)e.hasOwnProperty(t)&&(this[t]=e[t]);this.form=null,this.activeFeed=null,this.GFCCField=null,this.stripeResponse=null,this.hasPaymentIntent=!1,this.init=function(){var i,a,n,d,o,_,g,p,s;(this.isCreditCardOnPage()||"stripe.js"!==this.stripe_payment&&("elements"!==this.stripe_payment||c("#gf_stripe_response").length))&&(a=null,d=n=!1,o=(i=this).apiKey,this.form=c("#gform_"+this.formId),this.GFCCField=c("#input_"+this.formId+"_"+this.ccFieldId+"_1"),gform.addAction("gform_frontend_feeds_evaluated",function(e,t){if(t===i.formId){a=null,d=n=!1;for(var r=0;r<Object.keys(e).length;r++)if("gravityformsstripe"===e[r].addonSlug&&e[r].isActivated){n=!0;for(var s=0;s<Object.keys(i.feeds).length;s++)if(i.feeds[s].feedId===e[r].feedId){a=i.feeds[s];break}switch(o=(a.hasOwnProperty("apiKey")?a:i).apiKey,i.activeFeed=a,i.stripe_payment){case"elements":_=Stripe(o),g=_.elements(),d=""!==a.address_zip,null!=p&&p.hasOwnProperty("_destroyed")&&!1===p._destroyed&&p.destroy(),i.GFCCField.next(".validation_message").length&&i.GFCCField.next(".validation_message").remove(),p=g.create("card",{classes:i.cardClasses,style:i.cardStyle,hidePostalCode:d}),c(".gform_stripe_requires_action").length?(2===c(".ginput_container_creditcard > div").length?(c(".ginput_container_creditcard > div:last").hide(),c(".ginput_container_creditcard > div:first").html("<p><strong>"+gforms_stripe_frontend_strings.requires_action+"</strong></p>")):c(".ginput_container_creditcard").html("<p><strong>"+gforms_stripe_frontend_strings.requires_action+"</strong></p>"),i.scaActionHandler(_,t)):(p.mount("#"+i.GFCCField.attr("id")),p.on("change",function(e){i.displayStripeCardError(e)}));break;case"stripe.js":Stripe.setPublishableKey(o)}break}n||("elements"===i.stripe_payment&&(null!=g&&p===g.getElement("card")&&p.destroy(),i.GFCCField.next(".validation_message").length||i.GFCCField.after('<div class="gfield_description validation_message">'+gforms_stripe_frontend_strings.no_active_frontend_feed+"</div>"),wp.a11y.speak(gforms_stripe_frontend_strings.no_active_frontend_feed)),i.resetStripeStatus(i.form,t,i.isLastPage()),o=i.apiKey,i.activeFeed=null)}}),"elements"===this.stripe_payment&&(p=g=_=null,s=!1,c("#gf_stripe_response").length&&(this.stripeResponse=JSON.parse(c("#gf_stripe_response").val()),this.stripeResponse.hasOwnProperty("client_secret")&&(this.hasPaymentIntent=!0))),c("#gform_"+this.formId).on("submit",function(e){if(!(!n||c(this).data("gfstripesubmitting")||1==c("#gform_save_"+i.formId).val()||!i.isLastPage()&&"elements"!==i.stripe_payment||gformIsHidden(i.GFCCField)||i.maybeHitRateLimits()||i.invisibleCaptchaPending()))switch(e.preventDefault(),c(this).data("gfstripesubmitting",!0),i.maybeAddSpinner(),i.stripe_payment){case"elements":i.form=c(this),"form_total"===a.paymentAmount&&(gform.addFilter("gform_product_total",function(e,t){return window["gform_stripe_amount_"+t]=e},51),gformCalculateTotalPrice(i.formId)),i.updatePaymentAmount();var t=parseInt(c("#gform_source_page_number_"+i.formId).val(),10),r=parseInt(c("#gform_target_page_number_"+i.formId).val(),10);if((r<t&&0!==r||0===window["gform_stripe_amount_"+i.formId])&&(s=!0),i.isLastPage()&&!i.isCreditCardOnPage()||gformIsHidden(i.GFCCField)||s)return void c(this).submit();"product"===a.type?i.createPaymentMethod(_,p):i.createToken(_,p);break;case"stripe.js":t=c(this),r="input_"+i.formId+"_"+i.ccFieldId+"_",r={number:t.find("#"+r+"1").val(),exp_month:t.find("#"+r+"2_month").val(),exp_year:t.find("#"+r+"2_year").val(),cvc:t.find("#"+r+"3").val(),name:t.find("#"+r+"5").val()};i.form=t,Stripe.card.createToken(r,function(e,t){i.responseHandler(e,t)})}}))},this.getBillingAddressMergeTag=function(e){return""===e?"":"{:"+e+":value}"},this.responseHandler=function(e,t){for(var r=this.form,s="input_"+this.formId+"_"+this.ccFieldId+"_",i=["1","2_month","2_year","3","5"],a=0;a<i.length;a++){var n,d=r.find("#"+s+i[a]);"1"==i[a]&&(n=c.trim(d.val()),d=gformFindCardType(n),void 0!==this.cardLabels[d]&&(d=this.cardLabels[d]),r.append(c('<input type="hidden" name="stripe_credit_card_last_four" />').val(n.slice(-4))),r.append(c('<input type="hidden" name="stripe_credit_card_type" />').val(d)))}r.append(c('<input type="hidden" name="stripe_response" />').val(c.toJSON(t))),r.submit()},this.elementsResponseHandler=function(e){var t,r=this.form,s=this,i=this.activeFeed,a=gform.applyFilters("gform_stripe_currency",this.currency,this.formId),n=0===gf_global.gf_currency_config.decimals?window["gform_stripe_amount_"+this.formId]:gformRoundPrice(100*window["gform_stripe_amount_"+this.formId]);if(e.error)return this.displayStripeCardError(e),void this.resetStripeStatus(r,this.formId,this.isLastPage());this.hasPaymentIntent?"product"===i.type?e.hasOwnProperty("paymentMethod")?(c("#gf_stripe_credit_card_last_four").val(e.paymentMethod.card.last4),c("#stripe_credit_card_type").val(e.paymentMethod.card.brand),c.ajax({async:!1,url:gforms_stripe_frontend_strings.ajaxurl,dataType:"json",method:"POST",data:{action:"gfstripe_update_payment_intent",nonce:gforms_stripe_frontend_strings.create_payment_intent_nonce,payment_intent:e.id,payment_method:e.paymentMethod,currency:a,amount:n,feed_id:i.feedId},success:function(e){e.success?(c("#gf_stripe_response").val(c.toJSON(e.data)),r.submit()):(e.error=e.data,delete e.data,s.displayStripeCardError(e),s.resetStripeStatus(r,s.formId,s.isLastPage()))}})):e.hasOwnProperty("amount")&&r.submit():((t=JSON.parse(c("#gf_stripe_response").val())).updatedToken=e.token.id,c("#gf_stripe_response").val(c.toJSON(t)),r.append(c('<input type="hidden" name="stripe_credit_card_last_four" id="gf_stripe_credit_card_last_four" />').val(e.token.card.last4)),r.append(c('<input type="hidden" name="stripe_credit_card_type" id="stripe_credit_card_type" />').val(e.token.card.brand)),r.submit()):(c("#gf_stripe_response").length?c("#gf_stripe_response").val(c.toJSON(e)):r.append(c('<input type="hidden" name="stripe_response" id="gf_stripe_response" />').val(c.toJSON(e))),"product"===i.type?(r.append(c('<input type="hidden" name="stripe_credit_card_last_four" id="gf_stripe_credit_card_last_four" />').val(e.paymentMethod.card.last4)),r.append(c('<input type="hidden" name="stripe_credit_card_type" id="stripe_credit_card_type" />').val(e.paymentMethod.card.brand)),c.ajax({async:!1,url:gforms_stripe_frontend_strings.ajaxurl,dataType:"json",method:"POST",data:{action:"gfstripe_create_payment_intent",nonce:gforms_stripe_frontend_strings.create_payment_intent_nonce,payment_method:e.paymentMethod,currency:a,amount:n,feed_id:i.feedId},success:function(e){e.success?(c("#gf_stripe_response").length?c("#gf_stripe_response").val(c.toJSON(e.data)):r.append(c('<input type="hidden" name="stripe_response" id="gf_stripe_response" />').val(c.toJSON(e.data))),r.submit()):(e.error=e.data,delete e.data,s.displayStripeCardError(e),s.resetStripeStatus(r,s.formId,s.isLastPage()))}})):(r.append(c('<input type="hidden" name="stripe_credit_card_last_four" id="gf_stripe_credit_card_last_four" />').val(e.token.card.last4)),r.append(c('<input type="hidden" name="stripe_credit_card_type" id="stripe_credit_card_type" />').val(e.token.card.brand)),r.submit()))},this.scaActionHandler=function(t,r){var s,i;c("#gform_"+r).data("gfstripescaauth")||(c("#gform_"+r).data("gfstripescaauth",!0),s=this,i=JSON.parse(c("#gf_stripe_response").val()),"product"===this.activeFeed.type?t.retrievePaymentIntent(i.client_secret).then(function(e){"requires_action"===e.paymentIntent.status&&t.handleCardAction(i.client_secret).then(function(e){var t=JSON.parse(c("#gf_stripe_response").val());t.scaSuccess=!0,c("#gf_stripe_response").val(c.toJSON(t)),s.maybeAddSpinner(),c("#gform_"+r).data("gfstripescaauth",!1),c("#gform_"+r).data("gfstripesubmitting",!0).submit()})}):t.retrievePaymentIntent(i.client_secret).then(function(e){"requires_action"===e.paymentIntent.status&&t.handleCardPayment(i.client_secret).then(function(e){s.maybeAddSpinner(),c("#gform_"+r).data("gfstripescaauth",!1),c("#gform_"+r).data("gfstripesubmitting",!0).submit()})}))},this.isLastPage=function(){var e=c("#gform_target_page_number_"+this.formId);return!(0<e.length)||0==e.val()},this.isCreditCardOnPage=function(){var e=this.getCurrentPageNumber();return!this.ccPage||!e||this.ccPage==e},this.getCurrentPageNumber=function(){var e=c("#gform_source_page_number_"+this.formId);return 0<e.length&&e.val()},this.maybeAddSpinner=function(){var e,t;this.isAjax||("function"==typeof gformAddSpinner?gformAddSpinner(this.formId):(e=this.formId,0==jQuery("#gform_ajax_spinner_"+e).length&&(t=gform.applyFilters("gform_spinner_url",gf_global.spinnerUrl,e),gform.applyFilters("gform_spinner_target_elem",jQuery("#gform_submit_button_"+e+", #gform_wrapper_"+e+" .gform_next_button, #gform_send_resume_link_button_"+e),e).after('<img id="gform_ajax_spinner_'+e+'"  class="gform_ajax_spinner" src="'+t+'" alt="" />'))))},this.resetStripeStatus=function(e,t,r){c("#gf_stripe_response, #gf_stripe_credit_card_last_four, #stripe_credit_card_type").remove(),e.data("gfstripesubmitting",!1),c("#gform_ajax_spinner_"+t).remove(),r&&(window["gf_submitting_"+t]=!1)},this.displayStripeCardError=function(e){e.error&&!this.GFCCField.next(".validation_message").length&&this.GFCCField.after('<div class="gfield_description validation_message"></div>');var t=this.GFCCField.next(".validation_message");e.error?(t.html(e.error.message),wp.a11y.speak(e.error.message,"assertive"),0<c("#gform_ajax_spinner_"+this.formId).length&&c("#gform_ajax_spinner_"+this.formId).remove()):t.remove()},this.updatePaymentAmount=function(){var e,t,r=this.formId,s=this.activeFeed;"form_total"!==s.paymentAmount&&(e=GFMergeTag.getMergeTagValue(r,s.paymentAmount,":price"),t=GFMergeTag.getMergeTagValue(r,s.paymentAmount,":qty"),"string"==typeof e&&(e=GFMergeTag.getMergeTagValue(r,s.paymentAmount+".2",":price"),t=GFMergeTag.getMergeTagValue(r,s.paymentAmount+".3",":qty")),window["gform_stripe_amount_"+r]=e*t),s.hasOwnProperty("setupFee")&&(e=GFMergeTag.getMergeTagValue(r,s.setupFee,":price"),t=GFMergeTag.getMergeTagValue(r,s.setupFee,":qty"),"string"==typeof e&&(e=GFMergeTag.getMergeTagValue(r,s.setupFee+".2",":price"),t=GFMergeTag.getMergeTagValue(r,s.setupFee+".3",":qty")),window["gform_stripe_amount_"+r]+=e*t)},this.createToken=function(e,t){var r=this,s=this.activeFeed;cardholderName=c("#input_"+this.formId+"_"+this.ccFieldId+"_5").val(),tokenData={name:cardholderName,address_line1:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_line1)),address_line2:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_line2)),address_city:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_city)),address_state:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_state)),address_zip:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_zip)),address_country:GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(s.address_country)),currency:gform.applyFilters("gform_stripe_currency",this.currency,this.formId)},e.createToken(t,tokenData).then(function(e){r.elementsResponseHandler(e)})},this.createPaymentMethod=function(t,r,e){var s,i,a,n,d,o,_,g=this,p=this.activeFeed,l="";""===(l=""!==p.address_country?GFMergeTag.replaceMergeTags(g.formId,g.getBillingAddressMergeTag(p.address_country)):l)||void 0!==e&&""!==e?(s=c("#input_"+this.formId+"_"+this.ccFieldId+"_5").val(),i=GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(p.address_line1)),a=GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(p.address_line2)),n=GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(p.address_city)),d=GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(p.address_state)),o=GFMergeTag.replaceMergeTags(this.formId,this.getBillingAddressMergeTag(p.address_zip)),_={billing_details:{name:null,address:{}}},""!==s&&(_.billing_details.name=s),""!==i&&(_.billing_details.address.line1=i),""!==a&&(_.billing_details.address.line2=a),""!==n&&(_.billing_details.address.city=n),""!==d&&(_.billing_details.address.state=d),""!==o&&(_.billing_details.address.postal_code=o),""!==e&&(_.billing_details.address.country=e),null===_.billing_details.name&&delete _.billing_details.name,_.billing_details.address==={}&&delete _.billing_details.address,t.createPaymentMethod("card",r,_).then(function(e){null!==g.stripeResponse&&(e.id=g.stripeResponse.id,e.client_secret=g.stripeResponse.client_secret),g.elementsResponseHandler(e)})):c.ajax({async:!1,url:gforms_stripe_frontend_strings.ajaxurl,dataType:"json",method:"POST",data:{action:"gfstripe_get_country_code",nonce:gforms_stripe_frontend_strings.create_payment_intent_nonce,country:l,feed_id:p.feedId},success:function(e){e.success&&g.createPaymentMethod(t,r,e.data.code)}})},this.maybeHitRateLimits=function(){return!!(this.hasOwnProperty("cardErrorCount")&&5<=this.cardErrorCount)},this.invisibleCaptchaPending=function(){var e=this.form.find(".ginput_recaptcha");if(!e.length||"invisible"!==e.data("size"))return!1;e=e.find(".g-recaptcha-response");return!(e.length&&e.val())},this.init()}}(jQuery);