(async() => { const { actions, utils, EventManager } = ShopbySkin; const redirectLoginPage = () => { const nextUrl = `${location.origin}/pages/sign-in/sign-in.html`; location.href = `${nextUrl}?from=${encodeURIComponent(location.href)}`; }; async function downloadCoupon ( promotionCode ) { if ( true === utils.isSignedIn() ) { const url = `https://shop-api.e-ncp.com/coupons/register-code/${promotionCode}`; const commonData = await actions.executeCommonAction('PC'); const vertion = '1.0'; const { clientId, platform } = commonData; const accesstoken = `Bearer ${utils.memberAuth.get()}`; const myHeaders = new Headers(); myHeaders.append("version" , "1.0" ); myHeaders.append("clientid" , clientId ); myHeaders.append("platform" , platform ); myHeaders.append("shop-by-authorization", accesstoken ); const requestOptions = { method : "POST" , headers : myHeaders , credentials : "omit" , mode : "cors" , }; fetch(url, requestOptions) .then ( async ( res ) => { const data = await res.json(); console.log(data); if ( 200 == res.status ) { const couponName = data.couponName.split('(')[0]; alert(`${couponName} 쿠폰을 받았습니다.`); } else { alert(data.message); } }) .catch( ( err ) => { alert(err.message); }) } else { alert('로그인이 필요합니다.'); redirectLoginPage(); } } EventManager.on('QUERY_BANNER_BY_ID', (res)=>{ const checkBannerId = res.bannerId.replace('pc-', '').replace('mo-', ''); if ( ['benefit-01', 'benefit-02', 'benefit-03'].includes(checkBannerId) ) { const interval = setInterval(()=>{ if ( document.querySelector(`[shopby-banner-id='${res.bannerId}'] .btn_coupon`) ) { document.querySelector(`[shopby-banner-id='${res.bannerId}'] .btn_coupon`).addEventListener('click', async (e)=>{ const bannerId = e.target.closest('skin-banner').getAttribute('shopby-banner-id'); const promotionCode = res?.data?.banners?.[0].bannerTitle; await downloadCoupon(promotionCode); }); clearInterval(interval); } }, 100); } }); })();