Vue Paystack Component
vue-paystack is a Vue 2.x component designed to integrate the Paystack payment gateway into web applications. It provides a `<paystack>` component that wraps the Paystack inline and popup payment experiences, allowing developers to configure payment details such as amount, email, and public key via props, and handle callbacks for success and closure events. The package is currently at version 2.0.4 and appears to have a sporadic release cadence, primarily focusing on maintaining compatibility with Vue 2.x. A key differentiator is its straightforward, declarative integration for Vue 2 projects, abstracting the direct Paystack API calls into a component-based structure. It is specifically designed for Vue 2 applications, with no explicit support for Vue 3.
Common errors
-
[Vue warn]: Unknown custom element: <paystack> - did you register the component correctly?
cause The `paystack` component was not registered in the `components` option of the parent Vue component or globally.fixEnsure `import paystack from 'vue-paystack';` is present and the component is listed in the `components` option: `components: { paystack }`. -
Uncaught ReferenceError: VuePaystack is not defined
cause When using the CDN version, the `<script>` tag for `vue-paystack` was either not included, or included after the script attempting to use `VuePaystack`.fixEnsure the `<script src="https://unpkg.com/vue-paystack/dist/paystack.min.js"></script>` tag is correctly placed in your HTML, preferably after the Vue library script and before your application script. -
Paystack: Public key not supplied
cause The `paystackkey` prop passed to the `<paystack>` component is either missing, empty, or invalid.fixProvide a valid Paystack public key to the `paystackkey` prop, e.g., `:paystackkey="'pk_test_xxxxxxxxxxxxxxxxxxxxxxx'"`.
Warnings
- breaking Version 2.0.1 and 2.0.2 included a 'major upgrade implemented' which could introduce breaking changes not explicitly detailed in the commit messages beyond general dependency updates and refactoring. Users upgrading from pre-2.0.1 versions should thoroughly test their implementations.
- gotcha This package is specifically designed for Vue 2.x applications. It does not officially support Vue 3. Attempting to use it in a Vue 3 project will likely lead to compatibility issues and errors.
- gotcha The `paystackkey` prop must be your Paystack public key. For production deployments, ensure you are using your live public key and not a test key. Exposing sensitive keys directly in client-side code is generally discouraged; consider fetching it from a secure API endpoint.
- gotcha The `amount` prop expects the value in kobo (for NGN) or the smallest currency unit for other supported currencies (e.g., cents for USD). Incorrect unit conversion is a common mistake leading to wrong transaction amounts.
Install
-
npm install vue-paystack -
yarn add vue-paystack -
pnpm add vue-paystack
Imports
- paystack
const paystack = require('vue-paystack');import paystack from 'vue-paystack';
- VuePaystack
import VuePaystack from 'https://unpkg.com/vue-paystack/dist/paystack.min.js'
<script src="https://unpkg.com/vue-paystack/dist/paystack.min.js"></script> // Then use VuePaystack.default
- Paystack Component Registration
Vue.component('paystack', require('vue-paystack'));components: { paystack } // In a Vue component options object
Quickstart
<template>
<paystack
:amount="amount"
:email="email"
:paystackkey="paystackkey"
:reference="reference"
:callback="callback"
:close="close"
:embed="false"
>
<button>Make Payment</button>
</paystack>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import paystack from 'vue-paystack';
export default defineComponent({
components: {
paystack
},
data() {
return {
paystackkey: process.env.VUE_APP_PAYSTACK_PUBLIC_KEY || 'pk_test_xxxxxxxxxxxxxxxxxxxxxxx', // Replace with your actual public key
email: 'customer@example.com', // Customer email
amount: 1000000, // Amount in kobo (10,000 NGN)
};
},
computed: {
reference(): string {
// Generate a unique reference for each transaction
return `PAY-${Date.now()}-${Math.floor(Math.random() * 1000000)}`;
}
},
methods: {
callback(response: any): void {
console.log('Payment successful:', response);
alert(`Payment successful! Reference: ${response.reference}`);
// Verify payment on your backend using response.reference
},
close(): void {
console.log('Payment window closed');
alert('Payment was closed without completion.');
}
}
});
</script>