Skip to content

@pesepay/vue

@pesepay/vue npm

Vue components for Pesepay Elements. Vue 3.5 or later.

npm install @pesepay/js @pesepay/vue

The components create, mount and destroy the elements from @pesepay/js for you, including under server rendering.

Your server creates the transaction and passes its reference number to the component:

src/Checkout.vue
<script setup lang="ts">
import type { CheckoutOutcome } from "@pesepay/js";
import { CheckoutElement, Elements } from "@pesepay/vue";
const props = defineProps<{ referenceNumber: string }>();
// The payment has ended and the element shows the result. Fulfil the order on
// your server, which Pesepay notifies too.
const emit = defineEmits<{ finished: [outcome: CheckoutOutcome] }>();
// The methods could not load: Pesepay's hosted checkout takes the payment.
function leave(fallbackUrl: string | undefined): void {
if (fallbackUrl) window.location.assign(fallbackUrl);
}
</script>
<template>
<Elements>
<CheckoutElement
:options="{ referenceNumber: props.referenceNumber }"
@finished="(event) => emit('finished', event.outcome)"
@load-error="(event) => leave(event.fallbackUrl)"
/>
</Elements>
</template>

<CheckoutElement> lists the methods and pays with its own button. It runs every step itself, including the result when the customer comes back from 3-D Secure, and emits finished once with outcome: succeeded, failed, reversed or unknown. For your own Pay button, see Custom payment flow.

PropTypeNotes
pesepayPesepay | null | Promise<Pesepay | null>Optional. What loadPesepay() returns. Fixed on first render.
optionsElementsOptionstheme, fonts, locale for every element inside. Changes are applied.

Left out, pesepay defaults to loading Pesepay with the default options in the browser. To set options, such as apiBaseUrl, call loadPesepay({ apiBaseUrl }) once, outside the component, and pass the promise.

<Elements> creates one elements group once it is mounted and Pesepay has loaded, and destroys it, and every element in it, when it unmounts. Group-wide options that change, including a reactive object changed in place, are passed to elements.update().

Its default slot receives { pesepay, elements }, so the component that renders <Elements> can pay. In a child component, usePesepay() and useElements() return the same two as shallow refs. All of them are null while Pesepay loads and during server rendering. The composables throw outside <Elements>.

ComponentElementoptions
<CheckoutElement>checkoutrequired: { referenceNumber }
<PaymentElement>paymentrequired: { referenceNumber }
<CardElement>cardoptional
<MobileElement>mobileoptional: { provider }, …
<ScanElement>scanoptional: { provider }
<HostedCardElement>hostedCardrequired: { cardCheckout }

Each renders a <div>, with your id and class, and mounts its element into it. Events are typed for the element: @ready, @change, @focus, @blur, @escape and @load-error, plus @status on the mobile and payment elements and @finished on the checkout element.

Changed options are passed to the element’s update(). A payment or checkout element given a new referenceNumber is replaced, since one element pays one transaction.

Outside the checkout element, to pay, read the element off the group with elements.getElement(type) and pass it to confirmPayment(). One <Elements> holds one element of each type; for two card fields, use two <Elements>.

<Elements> loads nothing before it is mounted, and loadPesepay() resolves null on the server. On the server, and in Nuxt, each element component renders an empty <div>. See Server-side rendering.