Hello,
Im getting the error in the function initializeCard attaching the div to the card:
Initializing Card failed TypeError: card.attach is not a function
appId = null;
locationId = null;
form = null;
function initializeCard(payments) {
const card = payments.card();
card.attach('#card-container');
return card;
}
function fillPaymentForm(anAppId, aLocationId) {
this.appId = anAppId;
this.locationId = aLocationId;
this.form = document.getElementById('payment-form');
if (!window.Square) {
throw new Error('Square.js failed to load properly');
}
const payments = window.Square.payments(this.appId, this.locationId);
let card;
try {
card = initializeCard(payments);
} catch (e) {
console.error('Initializing Card failed', e);
return;
}
const cardButton = form.getElementById('card-button');
cardButton.addEventListener('click', function (event) {
handlePaymentMethodSubmission(event, card);
});
}
function handlePaymentMethodSubmission(event, paymentMethod) {
event.preventDefault();
try {
// disable the submit button as we await tokenization and make a
// payment request.
cardButton.disabled = true;
const token = tokenize(paymentMethod);
const paymentResults = createPayment(token);
displayPaymentResults('SUCCESS');
console.debug('Payment Success', paymentResults);
} catch (e) {
cardButton.disabled = false;
displayPaymentResults('FAILURE');
console.error(e.message);
}
}
// Call this function to send a payment token, buyer name, and other details
// to the project server code so that a payment can be created with
// Payments API
function createPayment(token) {
const body = JSON.stringify({
locationId,
sourceId: token,
idempotencyKey
});
console.log(body);
const paymentResponse = fetch('/payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
});
if (paymentResponse.ok) {
return paymentResponse.json();
}
const errorBody = paymentResponse.text();
throw new Error(errorBody);
}
// This function tokenizes a payment method.
// The ‘error’ thrown from this async function denotes a failed tokenization,
// which is due to buyer error (such as an expired card). It's up to the
// developer to handle the error and provide the buyer the chance to fix
// their mistakes.
function tokenize(paymentMethod) {
const tokenResult = paymentMethod.tokenize();
if (tokenResult.status === 'OK') {
return tokenResult.token;
} else {
let errorMessage = `Tokenization failed-status: ${tokenResult.status}`;
if (tokenResult.errors) {
errorMessage += ` and errors: ${JSON.stringify(
tokenResult.errors
)}`;
}
throw new Error(errorMessage);
}
}
// Helper method for displaying the Payment Status on the screen.
// status is either SUCCESS or FAILURE;
function displayPaymentResults(aStatus) {
const statusContainer = this.form.getElementById(
'payment-status-container'
);
if (aStatus === 'SUCCESS') {
statusContainer.classList.remove('is-failure');
statusContainer.classList.add('is-success');
} else {
statusContainer.classList.remove('is-success');
statusContainer.classList.add('is-failure');
}
statusContainer.style.visibility = 'visible';
}