adjust payment step

pull/349/head
Pete Matsyburka 1 year ago
parent 66bc8cfb5f
commit cf6b378ccb

@ -388,6 +388,7 @@
v-model="values[currentField.uuid]" v-model="values[currentField.uuid]"
:field="currentField" :field="currentField"
:submitter-slug="submitterSlug" :submitter-slug="submitterSlug"
:values="values"
@attached="attachments.push($event)" @attached="attachments.push($event)"
@focus="scrollIntoField(currentField)" @focus="scrollIntoField(currentField)"
@submit="submitStep" @submit="submitStep"
@ -808,7 +809,7 @@ export default {
}, []) }, [])
}, },
formulaFields () { formulaFields () {
return this.fields.filter((f) => f.preferences?.formula) return this.fields.filter((f) => f.preferences?.formula && f.type !== 'payment')
}, },
attachmentsIndex () { attachmentsIndex () {
return this.attachments.reduce((acc, a) => { return this.attachments.reduce((acc, a) => {

@ -93,6 +93,10 @@ export default {
type: Object, type: Object,
required: true required: true
}, },
values: {
type: Object,
required: true
},
submitterSlug: { submitterSlug: {
type: String, type: String,
required: true required: true
@ -101,7 +105,8 @@ export default {
emits: ['focus', 'submit', 'update:model-value', 'attached'], emits: ['focus', 'submit', 'update:model-value', 'attached'],
data () { data () {
return { return {
isCreatingCheckout: false isCreatingCheckout: false,
isMathLoaded: false
} }
}, },
computed: { computed: {
@ -114,15 +119,23 @@ export default {
defaultName () { defaultName () {
const { price, currency } = this.field.preferences || {} const { price, currency } = this.field.preferences || {}
const formattedPrice = new Intl.NumberFormat([], { const formatter = new Intl.NumberFormat([], {
style: 'currency', style: 'currency',
currency currency
}).format(price) })
return this.t('pay') + ' ' + formattedPrice if (this.field.preferences?.formula) {
if (this.isMathLoaded) {
return this.t('pay') + ' ' + formatter.format(this.calculateFormula())
} else {
return ''
}
} else {
return this.t('pay') + ' ' + formatter.format(price)
}
} }
}, },
mounted () { async mounted () {
if (this.sessionId) { if (this.sessionId) {
this.$emit('submit') this.$emit('submit')
} }
@ -130,8 +143,48 @@ export default {
if (!this.sessionId) { if (!this.sessionId) {
this.postCheckout({ checkStatus: true }) this.postCheckout({ checkStatus: true })
} }
if (this.field.preferences?.formula) {
const {
create,
evaluateDependencies,
addDependencies,
subtractDependencies,
divideDependencies,
multiplyDependencies,
powDependencies,
roundDependencies,
absDependencies,
sinDependencies,
tanDependencies,
cosDependencies
} = await import('mathjs')
this.math = create({
evaluateDependencies,
addDependencies,
subtractDependencies,
divideDependencies,
multiplyDependencies,
powDependencies,
roundDependencies,
absDependencies,
sinDependencies,
tanDependencies,
cosDependencies
})
this.isMathLoaded = true
}
}, },
methods: { methods: {
calculateFormula () {
const transformedFormula = this.field.preferences.formula.replace(/{{(.*?)}}/g, (match, uuid) => {
return this.values[uuid] || 0.0
})
return this.math.evaluate(transformedFormula.toLowerCase())
},
async submit () { async submit () {
if (this.sessionId) { if (this.sessionId) {
return fetch(this.baseUrl + '/api/stripe_payments/' + this.sessionId, { return fetch(this.baseUrl + '/api/stripe_payments/' + this.sessionId, {

@ -94,6 +94,7 @@
:field="field" :field="field"
@click-condition="isShowConditionsModal = true" @click-condition="isShowConditionsModal = true"
@click-description="isShowDescriptionModal = true" @click-description="isShowDescriptionModal = true"
@click-formula="isShowFormulaModal = true"
/> />
<span <span
v-else-if="field.type !== 'heading'" v-else-if="field.type !== 'heading'"
@ -345,7 +346,7 @@ export default {
}, },
methods: { methods: {
buildDefaultName (field, fields) { buildDefaultName (field, fields) {
if (field.type === 'payment' && field.preferences?.price) { if (field.type === 'payment' && field.preferences?.price && !field.preferences?.formula) {
const { price, currency } = field.preferences || {} const { price, currency } = field.preferences || {}
const formattedPrice = new Intl.NumberFormat([], { const formattedPrice = new Intl.NumberFormat([], {

@ -204,7 +204,10 @@ export default {
alert('Some fields are missing in the formula.') alert('Some fields are missing in the formula.')
} else { } else {
this.field.preferences.formula = normalizedFormula this.field.preferences.formula = normalizedFormula
this.field.readonly = !!normalizedFormula
if (this.field.type !== 'payment') {
this.field.readonly = !!normalizedFormula
}
this.save() this.save()

@ -17,6 +17,7 @@ const en = {
display_title: 'Display title', display_title: 'Display title',
with_logo: 'With logo', with_logo: 'With logo',
unchecked: 'Unchecked', unchecked: 'Unchecked',
price: 'Price',
type_value: 'Type value', type_value: 'Type value',
equal: 'Equal', equal: 'Equal',
not_equal: 'Not equal', not_equal: 'Not equal',

@ -1,7 +1,7 @@
<template> <template>
<span <span
class="dropdown dropdown-end" class="dropdown dropdown-end"
:class="{ 'dropdown-open': (!field.preferences?.price || !isConnected) && !isLoading }" :class="{ 'dropdown-open': ((!field.preferences?.price && !field.preferences?.formula) || !isConnected) && !isLoading }"
> >
<label <label
tabindex="0" tabindex="0"
@ -26,7 +26,7 @@
> >
<select <select
v-model="field.preferences.currency" v-model="field.preferences.currency"
placeholder="Price" :placeholder="t('price')"
class="select select-bordered select-xs font-normal w-full max-w-xs !h-7 !outline-0" class="select select-bordered select-xs font-normal w-full max-w-xs !h-7 !outline-0"
@change="save" @change="save"
> >
@ -51,14 +51,23 @@
@click.stop @click.stop
> >
<input <input
v-if="field.preferences.formula"
type="number"
:placeholder="t('price')"
disabled="true"
class="input input-bordered input-xs w-full max-w-xs h-7 !outline-0"
@blur="save"
>
<input
v-else
v-model="field.preferences.price" v-model="field.preferences.price"
type="number" type="number"
placeholder="Price" :placeholder="t('price')"
class="input input-bordered input-xs w-full max-w-xs h-7 !outline-0" class="input input-bordered input-xs w-full max-w-xs h-7 !outline-0"
@blur="save" @blur="save"
> >
<label <label
v-if="field.preferences.price" v-if="field.preferences.price && !field.preferences.formula"
:style="{ backgroundColor: backgroundColor }" :style="{ backgroundColor: backgroundColor }"
class="absolute -top-1 left-2.5 px-1 h-4" class="absolute -top-1 left-2.5 px-1 h-4"
style="font-size: 8px" style="font-size: 8px"
@ -149,8 +158,21 @@
data-turbo="false" data-turbo="false"
>Learn more</a> >Learn more</a>
</div> </div>
<li class="mb-1">
<label
class="label-text cursor-pointer text-center w-full flex items-center"
@click="$emit('click-formula')"
>
<IconMathFunction
width="18"
/>
<span class="text-sm">
{{ t('formula') }}
</span>
</label>
</li>
<hr> <hr>
<li class="mt-1"> <li>
<label <label
class="label-text cursor-pointer text-center w-full flex items-center" class="label-text cursor-pointer text-center w-full flex items-center"
@click="$emit('click-description')" @click="$emit('click-description')"
@ -181,7 +203,7 @@
</template> </template>
<script> <script>
import { IconSettings, IconCircleCheck, IconInfoCircle, IconBrandStripe, IconInnerShadowTop, IconRouteAltLeft } from '@tabler/icons-vue' import { IconMathFunction, IconSettings, IconCircleCheck, IconInfoCircle, IconBrandStripe, IconInnerShadowTop, IconRouteAltLeft } from '@tabler/icons-vue'
import { ref } from 'vue' import { ref } from 'vue'
const isConnected = ref(false) const isConnected = ref(false)
@ -193,6 +215,7 @@ export default {
IconCircleCheck, IconCircleCheck,
IconRouteAltLeft, IconRouteAltLeft,
IconInfoCircle, IconInfoCircle,
IconMathFunction,
IconInnerShadowTop, IconInnerShadowTop,
IconBrandStripe IconBrandStripe
}, },
@ -203,7 +226,7 @@ export default {
required: true required: true
} }
}, },
emits: ['click-condition', 'click-description'], emits: ['click-condition', 'click-description', 'click-formula'],
data () { data () {
return { return {
isLoading: false isLoading: false

@ -108,6 +108,7 @@ module Submitters
def merge_formula_values(submitter) def merge_formula_values(submitter)
computed_values = submitter.submission.template_fields.each_with_object({}) do |field, acc| computed_values = submitter.submission.template_fields.each_with_object({}) do |field, acc|
next if field['submitter_uuid'] != submitter.uuid next if field['submitter_uuid'] != submitter.uuid
next if field['type'] == 'payment'
formula = field.dig('preferences', 'formula') formula = field.dig('preferences', 'formula')

Loading…
Cancel
Save