"use strict";
jQuery(function ($){
var getpaid_square_cache={}
var payments;
var payment_methods=[ 'square', 'square_ach', 'square_gift_card' ];
function initializeCard(form){
return payments
.card()
.then(function(card){
getpaid_square_cache[ form.data('key') ].square=card;
card.attach('#' + form.find('.getpaid-square-card-container').attr('id') );
}
);
}
function initializeGiftCard(form){
return payments
.giftCard()
.then(function(giftCard){
getpaid_square_cache[ form.data('key') ].square_gift_card=giftCard;
giftCard.attach('#' + form.find('.getpaid-square-gift-card-container').attr('id') );
}
);
}
function initializeACH(form){
return payments
.ach()
.then(function(ach){
getpaid_square_cache[ form.data('key') ].square_ach=ach;
}
);
}
function handlePaymentMethodSubmission(form_data, selected_gateway){
var paymentMethod=getpaid_square_cache[ form_data.key ][ selected_gateway ];
var errorDiv=form_data.form.find('.getpaid-' + selected_gateway + '-errors');
errorDiv.text('').removeClass('alert alert-danger mt-2');
function showError(error){
errorDiv.addClass('alert alert-danger mt-2').html(error);
form_data.submit=false;
$('body').trigger('getpaid_payment_form_delayed_submit' + form_data.key);
}
if(selected_gateway==='square_ach'&&! form_data.form.find('.getpaid-square-ach-account-holder-name').val()){
showError(square_params.ach_account_holder_name_required);
return;
}
function verifyBuyer(token, form){
var verificationDetails={
intent: 'CHARGE',
amount: form.data('initial_amt'),
currencyCode: form.data('currency'),
billingContact: {
givenName: 'NA',
}};
if(form.find('.getpaid-billing-address-wrapper .wpinv_first_name').length){
verificationDetails.billingContact.givenName=form.find('.getpaid-billing-address-wrapper .wpinv_first_name').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_last_name').length){
verificationDetails.billingContact.familyName=form.find('.getpaid-billing-address-wrapper .wpinv_last_name').val()
}
if(form.find('.wpinv_billing_email').length){
verificationDetails.billingContact.email=form.find('.wpinv_billing_email').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_country').length){
verificationDetails.billingContact.country=form.find('.getpaid-billing-address-wrapper .wpinv_country').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_country').length){
verificationDetails.billingContact.country=form.find('.getpaid-billing-address-wrapper .wpinv_country').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_city').length){
verificationDetails.billingContact.city=form.find('.getpaid-billing-address-wrapper .wpinv_city').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_zip').length){
verificationDetails.billingContact.postalCode=form.find('.getpaid-billing-address-wrapper .wpinv_zip').val()
}
if(form.find('.getpaid-billing-address-wrapper .wpinv_phone').length){
verificationDetails.billingContact.phone=form.find('.getpaid-billing-address-wrapper .wpinv_phone').val()
}
payments.verifyBuyer(token, verificationDetails).then(function(result){
if(result.errors){
showError(result.errors.join('<br>') );
}else{
form_data.data=form_data.data + '&square_payment_nonce=' + token + '&square_verification_token=' + result.token;
$('body').trigger('getpaid_payment_form_delayed_submit' + form_data.key);
}
console.log(result);
});
}
try {
var options;
if(selected_gateway==='square_ach'){
options={
accountHolderName: form_data.form.find('.getpaid-square-ach-account-holder-name').val(),
}}
paymentMethod
.tokenize(options)
.then(function(token){
if(token.status==='OK'){
if('square'===selected_gateway){
verifyBuyer(token.token, form_data.form);
}else{
form_data.data=form_data.data + '&square_payment_nonce=' + token.token;
$('body').trigger('getpaid_payment_form_delayed_submit' + form_data.key);
}}else{
var errorMessage='';
token.errors.forEach(function(error){
errorMessage +='<p>' + error.message + '</p>' ;
});
showError(errorMessage);
}})
.catch(function(error){
showError(error);
console.trace(error);
})
} catch (e){
showError(e.message);
console.trace(e);
}}
$('body').on('getpaid_setup_payment_form', function(e, form){
try {
if(! payments){
payments=window.Square.payments(square_params.application_id, square_params.location_id);
}
getpaid_square_cache[ form.data('key') ]={}
if(form.find('.getpaid-square-ach-form') ){
initializeACH(form);
}
if(form.find('.getpaid-square-cc-form') ){
initializeCard(form);
}
if(form.find('.getpaid-square-gift-card-form') ){
initializeGiftCard(form);
}} catch (e){
form.find('.getpaid-payment-form-errors').text(square_params.error_message).removeClass('d-none');
console.error('Initializing square failed', e);
}})
$('body').on('getpaid_payment_form_before_submit', function(e, data){
var selected_gateway=data.form.find('input[name="wpi-gateway"]:checked').val();
if(payment_methods.indexOf(selected_gateway)===-1){
return;
}
if(! getpaid_square_cache[ data.key ]){
console.error('Square payment form not initialized');
return;
}
if(! getpaid_square_cache[ data.key ][ selected_gateway ]){
console.error(selected_gateway + ' not initialized');
return;
}
data.delay=true;
handlePaymentMethodSubmission(data, selected_gateway);
})
});