(function() { //DDP const BASE_URL = "https://pr.aqur.com"; const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json" } }; const chatform = localStorage.getItem("chatform"); var take = async function() { function getUuIdFromAnchor() { const queryString = window.location.search; const params = new URLSearchParams(queryString); return params.get("uuId"); } if(/\/payment\/entry1_test.ph/.test(location.pathname)) { let uuId = getUuIdFromAnchor(); if (uuId) { const fetchUrl = `${BASE_URL}/pdi/${uuId}`; fetch(fetchUrl) .then(response => response.json()) .then(async data => { console.log(data) let d = data.data let chatform = { pid: d.pid, sid: d.sid, uuId: d.uuId, customId: "conversion" } if (d.pid === 'lfa') { localStorage.setItem('chatform', JSON.stringify(chatform)); bindFormLfa(d.data, chatform) } }) .catch(error => { console.error('Error fetching the URL:', error); }); } } if (chatform) { if (/cv_upsell/.test(window.location.pathname)) { async function track(trackingData) { try { await fetch(`${BASE_URL}/track`, { ...fetchOptions, body: trackingData }); localStorage.removeItem("chatform"); const orderId = new URLSearchParams(window.location.search).get('order_id'); if (orderId) { let chatformData = JSON.parse(chatform); const response2 = await fetch(`${BASE_URL}/pdi/${chatformData.uuId}`); const data2 = await response2.json(); data2.data.data.order_id = orderId; data2.data.Completed = true; data2.data.status = "Completed"; await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(data2.data) }); } } catch (error) { console.error('Error:', error); } } track(chatform); } if(/\/lp/.test(window.location.pathname) || /\/lp\/new/.test(window.location.pathname)) { await new Promise(resolve => setTimeout(resolve, 5000)) async function checkErrors(){ console.log("checkerror") let errors= "" const alert_danger = document.querySelector('#alert-box'); if (alert_danger) { errors = alert_danger.textContent // alert_danger.remove() } return errors } let error = await checkErrors() if(error) { let chatformData = JSON.parse(chatform); const response2 = await fetch(`${BASE_URL}/pdi/${chatformData.uuId}`); const data2 = await response2.json(); data2.data.Completed = false; if(/カード情報を確認してください/.test(error)) { data2.data.status = "creditcardCheckNG"; } else if(/与信審査が通りませんでした/.test(error)) { data2.data.status = "postpaymentCheckNG"; } else { data2.data.message = error } await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(data2.data) }); } } } } document.addEventListener("DOMContentLoaded", () => { setTimeout(take, 0); }) const fillInput = (selector, value) => { return new Promise((resolve, reject) => { let inputEvent = new InputEvent('input', { bubbles: true, cancelable: false }); let inputEvent2 = new InputEvent('change', { bubbles: true, cancelable: false }); const inputElement = document.querySelector(selector); if (inputElement) { inputElement.value = value; inputElement.dispatchEvent(inputEvent); inputElement.dispatchEvent(inputEvent2); resolve(`Input with name "${selector}" filled and event dispatched`); } else { reject(`Element with name "${selector}" not found.`); } }); }; const chooseSelectBox = (selectBoxSelector, value, options = { useOptionVal: true, useInnerText: false }) => { return new Promise((resolve, reject) => { let option = ""; if (options.useInnerText) { option = Array.from(document.querySelectorAll(`${selectBoxSelector} option`)).find(opt => opt.innerText.trim() === value.trim()); } else if (options.useOptionVal) { option = document.querySelector(`${selectBoxSelector} option[value='${value}']`); } else { option = document.querySelector(`${selectBoxSelector} option[label='${value}']`); } const selectElement = document.querySelector(`${selectBoxSelector}`); if (option && selectElement) { option.selected = true; const event = new Event('change', { bubbles: true, composed: true }); selectElement.dispatchEvent(event); resolve(`Select box with name '${selectBoxSelector}' set to option with ${options.useInnerText ? 'text' : 'value'} '${value}' and event dispatched.`); } else { reject(new Error(`Select box with name '${selectBoxSelector}' and option with ${options.useInnerText ? 'text' : 'value'} '${value}' not found.`)); } }); }; async function track(trackingData) { try { await fetch(`${BASE_URL}/track`, { ...fetchOptions, body: trackingData }); localStorage.removeItem("chatform"); let chatformData = JSON.parse(trackingData); const response2 = await fetch(`${BASE_URL}/pdi/${chatformData.uuId}`); const data2 = await response2.json(); data2.data.Completed = true; data2.data.status = "Completed"; await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(data2.data) }); } catch (error) { console.error('Error:', error); } } async function bindFormLfa(data, chatform) { console.log("bindFormLFA", data) try { if(data.Optin == "その他") { await chooseSelectBox('[name="iid"]', data.Optin2, { useInnerText: true }) } else { await chooseSelectBox('[name="iid"]', data.Optin, { useInnerText: true }) } await fillInput('[name="name01"]', data.LastName) await fillInput('[name="name02"]', data.FirstName) await fillInput('[name="ruby01"]', data.LastNameKana) await fillInput('[name="ruby02"]', data.FirstNameKana) await fillInput('[name="em"]', data.Email) await fillInput('[name="em2"]', data.Email) await fillInput('[name="zip01"]', data.Zip.replace("-", "")) await chooseSelectBox('select[name="pref01"]', data.Prefecture, { useInnerText: true }) await fillInput('input[name="addr01"]', data.City) await fillInput('input[name="addr02"]', data.Street1 + "" + data.Street2) await fillInput('input[name="pn"]', data.Tel.replace(/-/g, "")) if (data.Year && data.Month && data.Day) { await chooseSelectBox('[name="year"]', data.Year) await chooseSelectBox('[name="month"]', Number(data.Month)) await chooseSelectBox('[name="day"]', Number(data.Day)) } // Scroll to selector credit form const input = document.querySelector('[name="cn"]'); input.closest('div').scrollIntoView({ behavior: 'smooth' }); setTimeout(() => { input.focus(); }, 300); // Adjust time if needed track(JSON.stringify(chatform)) } catch (error) { console.log(error) } } }())