<template>
<BaseDialog v-model:open="isError">
<template #main>
<slot name="failure-message">
<p>
{{ $t('payment.refused') }}
</p>
<template v-if="type === WSResources.BID">
<p>{{ $t('payment.bidNotPlaced') }}</p>
<p>{{ $t('payment.tryAgain') }}</p>
</template>
<template v-if="type === WSResources.OFFER">
<p>{{ $t('payment.offerNotPlaced') }}</p>
<p>{{ $t('payment.tryAgain') }}</p>
</template>
<p class="mt-4">
{{ $t('payment.contactSupport') }}
</p>
<ButtonAskQuestion
class="mt-4"
:label="$t('common.cta.contactMe')"
/>
</slot>
</template>
</BaseDialog>
<BaseDialog v-if="!hideSuccessMessage" v-model:open="isSuccess">
<template #main>
<slot name="success-message">
<p>{{ $t('bidDialog.title.offer') }}</p>
</slot>
<BaseButton
v-if="props.type === WSResources.ORDER"
class="mt-7 w-full"
:label="$t('checkout.payment.seeSummary')"
i18n-key="checkout.payment.seeSummary"
@click="seeSummary"
/>
</template>
</BaseDialog>
<div v-if="isVisible" ref="dropin" />
</template>
<script setup lang="ts">
import { useApi } from '@/composables/useApi'
import type { PaymentMethod, CheckoutSession } from '@adyen/adyen-web/dist/types/types'
import { WSResources } from '~/types/WebSockets'
import AdyenCheckout from '@adyen/adyen-web'
import '@adyen/adyen-web/dist/adyen.css'
import { AydenResponses } from '~/types/Payment'
import { theme } from '@/tailwind.config'
import type { Price } from '~/types/Price'
type Props = {
id?: string
type: WSResources.BID | WSResources.OFFER | WSResources.ORDER
amount?: Price
onPaymentCompleted: (status: string) => void
setPaymentLoading?: (isLoading: boolean) => void
hideSuccessMessage?: boolean
location?: string
}
const props = withDefaults(defineProps<Props>(), {
location: 'Adyen Checkout'
})
const emit = defineEmits([ 'click:seeSummary', 'success' ])
const dropin = ref<HTMLElement | null>(null)
const isError = ref<boolean>(false)
const isSuccess = ref<boolean>(false)
const isVisible = ref<boolean>(true)
const config = useAppConfig()
const { public: env } = useRuntimeConfig()
const { $post } = useApi()
const { selectedLanguage, localeCodeForAdyen } = useLanguageSelector()
const { fontFamily: { helvetica }, colors: { charcoal, error } } = theme.extend
const { pushCTAClickEvent } = useGtm()
const { t } = useI18n()
const getEndpointUrl = (id: string, type: string): [string, string] => {
switch (type) {
case WSResources.BID:
return [
`${config.api.endpoints.auction.myBids}/${id}/pre-authorize`,
`${config.api.endpoints.auction.myBids}/${id}/pre-authorize/available-payment-methods`
]
case WSResources.OFFER:
return [
`${config.api.endpoints.auction.myOffers}/${id}/pre-authorize`,
`${config.api.endpoints.auction.myOffers}/${id}/pre-authorize/available-payment-methods`
]
case WSResources.ORDER:
return [
`${config.api.endpoints.ecommerce.orders}/${id}/payments`,
`${config.api.endpoints.ecommerce.orders}/available-payment-methods`
]
default:
throw new Error('Invalid type')
}
}
const initiateCheckout = async () => {
const endpoints = getEndpointUrl(props.id, props.type)
const [ session ] = await $post(endpoints[ 0 ], {
returnUrl: env.siteUrl,
...props.type === WSResources.ORDER && { amount: props.amount!.centAmount, isWireTransfer: false }
}) as [CheckoutSession, any]
const [ paymentMethodsResponse ] = await $post(endpoints[ 1 ], {
shopperLocale: selectedLanguage.value.code,
...props.type === WSResources.ORDER && { amount: props.amount!.centAmount, currency: props.amount!.currencyCode }
}) as [{ paymentMethods: PaymentMethod[] }, any]
const styleObject = {
base: {
fontSize: '14px',
fontFamily: `${helvetica[ 0 ]}, ${helvetica[ 1 ]}`,
fontWeight: '400',
color: charcoal.DEFAULT,
},
error: {
color: error.DEFAULT
},
placeholder: {
color: charcoal[ 50 ]
},
validated: {
color: charcoal.DEFAULT
}
}
const checkout = await AdyenCheckout({
environment: env.adyenEnv,
clientKey: env.adyenClientKey,
setStatusAutomatically: true,
paymentMethodsResponse,
locale: localeCodeForAdyen.value,
session: {
id: session.id,
sessionData: session.sessionData,
},
paymentMethodsConfiguration: {
card: {
styles: styleObject,
},
storedCard: {
styles: styleObject,
}
},
beforeSubmit: (data, _, actions) => {
actions.resolve(data)
return props.setPaymentLoading && props.setPaymentLoading(true)
},
onActionHandled: (_) => {
return props.setPaymentLoading && props.setPaymentLoading(false)
},
onPaymentCompleted: (data) => {
if (data.resultCode === AydenResponses.AUTHORISED) {
props.onPaymentCompleted(data.resultCode)
isVisible.value = false
if ([ WSResources.OFFER, WSResources.ORDER ].includes(props.type)) {
isSuccess.value = true
emit('success')
}
pushCTAClickEvent(
null,
{
label: t('common.payment.pay'),
location: props.location
}
)
return
}
isError.value = true
props.onPaymentCompleted(data.resultCode)
checkout.remove('dropin')
initiateCheckout()
},
})
if (dropin.value) {
checkout.create('dropin').mount(dropin.value)
}
}
const seeSummary = () => {
isSuccess.value = false
emit('click:seeSummary')
}
onMounted(initiateCheckout)
</script>
<style lang="scss" scoped>
:deep(.adyen-checkout__dropin) {
@apply bg-beige mb-8 rounded-lg;
.adyen-checkout__payment-methods-list {
@apply py-2.5;
}
.adyen-checkout__payment-method,
.adyen-checkout__payment-method--selected {
@apply bg-transparent border-0;
&:not(.adyen-checkout__payment-method--selected) {
.adyen-checkout__payment-method__radio {
@apply shadow-none;
}
}
}
.adyen-checkout__payment-method__header {
> .adyen-checkout__payment-method__brands {
@apply hidden;
}
.adyen-checkout__payment-method__radio {
@apply w-4 h-4 min-w-4 rounded-full transition-all duration-150 bg-beige bg-center bg-no-repeat border border-charcoal-30;
&:after {
@apply left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-full m-0 w-0 h-0;
}
&.adyen-checkout__payment-method__radio--selected {
@apply border border-solid border-charcoal;
&:hover {
@apply shadow-none;
}
&:after {
@apply bg-charcoal w-2 h-2;
}
}
}
.adyen-checkout__payment-method__image__wrapper {
@apply w-6 h-6 mr-3;
&:after {
@apply bg-no-repeat border-0 w-6 h-6;
background-image: url('~/assets/img/icons/payment.svg');
}
img {
@apply hidden;
}
}
.adyen-checkout__payment-method__header__title {
.adyen-checkout__payment-method__name,
.adyen-checkout__payment-method__name--selected {
@apply text-xl font-normal text-charcoal;
}
}
}
.adyen-checkout__payment-method__details {
.adyen-checkout-form-instruction {
@apply text-charcoal-60 text-xs mt-1 mb-5;
}
.adyen-checkout__label {
&.adyen-checkout__label--focused {
.adyen-checkout__label__text {
@apply text-charcoal;
}
}
.adyen-checkout__label__text {
@apply text-base pb-3;
}
.adyen-checkout__label__text--error {
@apply text-charcoal;
}
}
.adyen-checkout__input-wrapper {
.adyen-checkout__field__exp-date_hint_wrapper,
.adyen-checkout__card__cvc__hint__wrapper {
@apply hidden;
}
.adyen-checkout__input {
@apply rounded-[44px] border border-charcoal-30 w-full bg-transparent outline-none text-sm py-0 pl-4 pr-11 shadow-none;
&.adyen-checkout__input--error {
@apply border-error;
}
&:after {
@apply bg-no-repeat bg-cover border-0 w-4 h-4 inline-block absolute right-4 top-1/2 -translate-y-1/2;
content: '';
background-image: url('~/assets/img/icons/payment.svg');
}
img {
@apply hidden;
}
&.adyen-checkout__input--valid {
&:after {
@apply hidden;
}
img {
@apply block;
}
}
}
.adyen-checkout-input__inline-validation {
@apply hidden;
}
}
.adyen-checkout__card__brands {
@apply h-auto gap-3 mt-0;
.adyen-checkout__card__brands__brand-wrapper {
@apply h-8 bg-beige;
width: 52px;
img {
@apply w-full h-full rounded;
}
}
}
}
.adyen-checkout__checkbox {
.adyen-checkout__checkbox__input {
+ .adyen-checkout__checkbox__label {
&:after {
@apply bg-beige border border-charcoal rounded-sm shadow-none;
}
&:before {
@apply top-0 left-0 rotate-0 border-0 rounded-sm w-4 h-4 bg-charcoal bg-no-repeat bg-center bg-[length:12px] shadow-none;
background-image: url('~/assets/img/icons/check.svg');
}
}
}
.adyen-checkout__checkbox__label {
@apply text-base pl-8;
}
}
.adyen-checkout__button--pay {
@apply h-10 py-2 px-4 bg-sunshine rounded-full border border-sunshine-600 shadow-none
hover:bg-opacity-50 hover:border-opacity-0
focus:bg-sunshine-600 focus:border-sunshine-600
active:bg-sunshine-600 active:border-sunshine-600;
.adyen-checkout__button__icon {
@apply hidden;
}
.adyen-checkout__button__text {
@apply text-charcoal text-sm font-normal;
}
}
.adyen-checkout__field--error {
.adyen-checkout__error-text {
@apply text-error mt-2;
&:before {
@apply w-3 h-3 mr-1 bg-no-repeat bg-center bg-[length:12px];
content: '';
background-image: url('~/assets/img/icons/warning-circle.svg');
}
}
}
}
:deep(.adyen-checkout__status--success) {
@apply bg-beige border-0 rounded-lg;
.adyen-checkout__status__text {
@apply text-charcoal text-base;
}
}
</style>