webapp/src/components/Discours/Donate.tsx

199 lines
7.5 KiB
TypeScript
Raw Normal View History

import { clsx } from 'clsx'
2022-09-09 11:53:35 +00:00
import { createSignal, onMount } from 'solid-js'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import { useSnackbar } from '../../context/snackbar'
import { showModal } from '../../stores/ui'
2022-09-09 11:53:35 +00:00
import styles from './Donate.module.scss'
2024-02-04 09:17:02 +00:00
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type DWindow = Window & { cp: any }
2022-09-09 11:53:35 +00:00
export const Donate = () => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2022-09-09 11:53:35 +00:00
const once = ''
const monthly = 'Monthly'
const cpOptions = {
publicId: 'pk_0a37bab30ffc6b77b2f93d65f2aed',
2022-10-07 19:35:53 +00:00
description: t('Help discours to grow'),
currency: 'RUB',
2022-09-09 11:53:35 +00:00
}
let amountSwitchElement: HTMLDivElement | undefined
let customAmountElement: HTMLInputElement | undefined
2022-10-07 19:35:53 +00:00
const [widget, setWidget] = createSignal()
const [customerReciept, setCustomerReciept] = createSignal({})
2022-09-09 11:53:35 +00:00
const [showingPayment, setShowingPayment] = createSignal<boolean>()
const [period, setPeriod] = createSignal(monthly)
const [amount, setAmount] = createSignal(0)
2024-02-04 17:40:15 +00:00
const { showSnackbar } = useSnackbar()
2022-09-09 11:53:35 +00:00
2022-11-10 17:04:39 +00:00
const initiated = () => {
2024-02-04 09:17:02 +00:00
try {
const { cp: CloudPayments } = window as unknown as DWindow
setWidget(new CloudPayments())
console.log('[donate] payments initiated')
setCustomerReciept({
Items: [
//товарные позиции
{
label: cpOptions.description, //наименование товара
price: amount() || 0, //цена
quantity: 1, //количество
amount: amount() || 0, //сумма
vat: 20, //ставка НДС
method: 0, // тег-1214 признак способа расчета - признак способа расчета
object: 0, // тег-1212 признак предмета расчета - признак предмета товара, работы, услуги, платежа, выплаты, иного предмета расчета
},
],
// taxationSystem: 0, //система налогообложения; необязательный, если у вас одна система налогообложения
// email: 'user@example.com', //e-mail покупателя, если нужно отправить письмо с чеком
// phone: '', //телефон покупателя в любом формате, если нужно отправить сообщение со ссылкой на чек
isBso: false, //чек является бланком строгой отчетности
amounts: {
electronic: amount(), // Сумма оплаты электронными деньгами
advancePayment: 0, // Сумма из предоплаты (зачетом аванса) (2 знака после запятой)
credit: 0, // Сумма постоплатой(в кредит) (2 знака после запятой)
provision: 0, // Сумма оплаты встречным предоставлением (сертификаты, др. мат.ценности) (2 знака после запятой)
},
2024-02-04 09:17:02 +00:00
})
} catch (error) {
console.error(error)
}
2022-11-10 17:04:39 +00:00
}
onMount(() => {
const script = document.createElement('script')
script.type = 'text/javascript'
script.src = 'https://widget.cloudpayments.ru/bundles/cloudpayments.js'
script.async = true
2022-11-13 06:41:04 +00:00
script.addEventListener('load', initiated)
2024-01-23 16:44:58 +00:00
document.head.append(script)
2022-09-09 11:53:35 +00:00
})
const show = () => {
// $openModal = 'donate'
setShowingPayment(true)
console.log('[donate] clicked')
const choice: HTMLInputElement | undefined | null =
amountSwitchElement?.querySelector('input[type=radio]:checked')
setAmount(Number.parseInt(customAmountElement?.value || choice?.value || '0'))
2024-02-04 09:03:15 +00:00
console.log(`[donate] input amount ${amount}`)
// biome-ignore lint/suspicious/noExplicitAny: it's a widget!
2022-10-07 19:35:53 +00:00
;(widget() as any).charge(
2022-09-09 11:53:35 +00:00
{
// options
...cpOptions,
amount: amount(),
skin: 'classic',
requireEmail: true,
retryPayment: true,
// invoiceId: '1234567', //номер заказа (необязательно)
// accountId: 'user@example.com', //идентификатор плательщика (обязательно для создания подписки)
data: {
CloudPayments: {
2022-10-07 19:35:53 +00:00
CustomerReciept: customerReciept(),
2022-09-09 11:53:35 +00:00
recurrent: {
interval: period(), // local solid's signal
period: 1, // internal widget's
CustomerReciept: customerReciept(), // чек для регулярных платежей
},
},
},
2022-09-09 11:53:35 +00:00
},
2022-10-08 16:40:58 +00:00
(opts) => {
2022-09-09 11:53:35 +00:00
// success
// действие при успешной оплате
console.debug('[donate] options', opts)
showModal('thank')
},
2024-02-04 09:03:15 +00:00
(reason: string, options) => {
2022-09-09 11:53:35 +00:00
// fail
// действие при неуспешной оплате
console.debug('[donate] options', options)
showSnackbar({
type: 'error',
body: reason,
2022-09-09 11:53:35 +00:00
})
},
2022-09-09 11:53:35 +00:00
)
}
return (
<form class={styles.donateForm} action="" method="post">
2022-11-10 17:04:39 +00:00
<input type="hidden" name="shopId" value="156465" />
<input value="148805" name="scid" type="hidden" />
<input value="0" name="customerNumber" type="hidden" />
2022-09-09 11:53:35 +00:00
<div class={styles.formGroup}>
<div class={styles.donateButtonsContainer} ref={amountSwitchElement}>
2022-11-10 17:04:39 +00:00
<input type="radio" name="amount" id="fix250" value="250" />
<label for="fix250" class={styles.btn}>
2022-11-10 17:04:39 +00:00
250&thinsp;
</label>
<input type="radio" name="amount" id="fix500" value="500" checked />
<label for="fix500" class={styles.btn}>
2022-11-10 17:04:39 +00:00
500&thinsp;
</label>
<input type="radio" name="amount" id="fix1000" value="1000" />
<label for="fix1000" class={styles.btn}>
2022-11-10 17:04:39 +00:00
1000&thinsp;
</label>
<input
class={styles.donateInput}
2022-11-10 17:04:39 +00:00
required
ref={customAmountElement}
type="number"
name="sum"
placeholder={t('Another amount')}
/>
2022-09-09 11:53:35 +00:00
</div>
2022-11-10 17:04:39 +00:00
</div>
2022-09-09 11:53:35 +00:00
<div class={styles.formGroup} id="payment-type" classList={{ showing: showingPayment() }}>
<div class={clsx(styles.btnGroup, styles.paymentChoose)} data-toggle="buttons">
2022-11-10 17:04:39 +00:00
<input
type="radio"
autocomplete="off"
id="once"
name="once"
onClick={() => setPeriod(once)}
checked={period() === once}
/>
<label
for="once"
class={clsx(styles.btn, styles.paymentType)}
classList={{ active: period() === once }}
>
2022-11-10 17:04:39 +00:00
{t('One time')}
</label>
<input
type="radio"
autocomplete="off"
id="monthly"
name="monthly"
onClick={() => setPeriod(monthly)}
checked={period() === monthly}
/>
<label
for="monthly"
class={clsx(styles.btn, styles.paymentType)}
classList={{ active: period() === monthly }}
>
2022-11-10 17:04:39 +00:00
{t('Every month')}
</label>
2022-09-09 11:53:35 +00:00
</div>
2022-11-10 17:04:39 +00:00
</div>
2022-09-09 11:53:35 +00:00
<div class={styles.formGroup}>
<button type="button" class={clsx(styles.btn, styles.sendBtn)} onClick={show}>
2022-11-10 17:04:39 +00:00
{t('Help discours to grow')}
</button>
2022-11-10 17:04:39 +00:00
</div>
</form>
2022-09-09 11:53:35 +00:00
)
}