sprinque-js-sdk-wip
Version:
UI kit to implement Pay by Invoice with Sprinque
71 lines (70 loc) • 3.16 kB
JavaScript
import { getElemValueBySelector } from '../helpers';
import { USER_EMAIL, COMPANY_NAME, USER_PHONE, BILLING_EMAIL } from '../selectors';
import { getAddressFromInputs } from '../step-4/helpers';
import { SprinqueModal } from '../inject-web-component';
import { itiPhoneInput } from '../step-2/form-listeners';
export const collectBuyerPayload = (shadowRoot) => {
var _a, _b;
const { creditBureauId = '', ipv4 = '', continentCode = '', continentName = '', countryCode = '', countryName = '', city = '', } = shadowRoot.getElementById(COMPANY_NAME.replace('#', '')).dataset;
const buyer = {
business_name: getElemValueBySelector(COMPANY_NAME, shadowRoot),
legal_form: 'Sole Proprietorship',
address: getAddressFromInputs(shadowRoot),
//website: 'http://wisozk-schiller.com',
merchant_buyer_id: String(new Date().getTime()),
//vat_id: '8273JK',
registration_number: getElemValueBySelector('#sp-reg-number', shadowRoot),
buyer_users: [
{
first_name: getElemValueBySelector('#sp-user-name', shadowRoot),
last_name: getElemValueBySelector('#sp-user-surname', shadowRoot),
phone: (itiPhoneInput === null || itiPhoneInput === void 0 ? void 0 : itiPhoneInput.getNumber()) || getElemValueBySelector(USER_PHONE, shadowRoot),
email: getElemValueBySelector(USER_EMAIL, shadowRoot),
//language: 'English',
},
],
metadata: {},
};
// billing email
const billingEmail = getElemValueBySelector(BILLING_EMAIL, shadowRoot);
if (billingEmail)
buyer.email = billingEmail;
// initial_shipping_address
if ((_a = SprinqueModal.order) === null || _a === void 0 ? void 0 : _a.shipping_address) {
buyer['initial_shipping_address'] = (_b = SprinqueModal.order) === null || _b === void 0 ? void 0 : _b.shipping_address;
}
if (creditBureauId)
buyer['credit_bureau_id'] = creditBureauId;
if (ipv4)
buyer.metadata.IPv4 = ipv4;
if (continentCode)
buyer.metadata.continent_code = continentCode;
if (continentName)
buyer.metadata.continent_name = continentName;
if (countryCode)
buyer.metadata.country_code = countryCode;
if (countryName)
buyer.metadata.country_name = countryName;
if (city)
buyer.metadata.city = city;
return buyer;
};
export const enableOtpButton = (shadowRoot) => {
const INTERVAL = 30;
const otpRootSpan = shadowRoot.getElementById('resend-code');
const otpSecondsValue = shadowRoot.getElementById('resend-code-sec');
if (!otpRootSpan.classList.contains('disabled')) {
otpRootSpan.classList.add('disabled');
}
let currentSec = 0;
const interval = setInterval(() => {
currentSec += 1;
otpSecondsValue.innerHTML = String(+otpSecondsValue.innerHTML - 1);
// interval is ended
if (currentSec === INTERVAL) {
otpSecondsValue.innerHTML = String(INTERVAL);
clearInterval(interval);
otpRootSpan.classList.remove('disabled');
}
}, 1000);
};