Skip to content

Full customization of pay button and basic validation for missing props. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,41 @@ vue3-paystack allows you to accept payments in your vue application created with
The component is easily customisable.

### NPM

```npm
npm i vue3-paystack
```

## Usage

```javascript
<template>
<paystack
buttonClass="'button-class btn btn-primary'"
buttonText="Pay Online"
:publicKey="publicKey"
:email="email"
:amount="amount"
:reference="reference"
:onSuccess="onSuccessfulPayment"
:onCancel="onCancelledPayment">
</paystack>
</template>

<script>
import paystack from "vue3-paystack";

export default {
components: {
paystack,
},
}
</script>

```

## Usage With A custom button, you can fully customize the look of the pay button

```javascript
<template>
<paystack
Expand All @@ -19,6 +50,38 @@ npm i vue3-paystack
:reference="reference"
:onSuccess="onSuccessfulPayment"
:onCancel="onCancelledPayment">

<slot>
<button
style="
line-height: 100%;
text-decoration: none;
display: block;
max-width: 100%;
mso-padding-alt: 0px;
background-color: #f95738;
border-radius: 6px;
color: #fff;
font-size: 18px;
padding-top: 19px;
padding-bottom: 19px;
text-align: center;
width: 100%;
padding: 19px 0px 19px 0px;
"
>
<span
style="
max-width: 100%;
display: inline-block;
line-height: 120%;
mso-padding-alt: 0px;
mso-text-raise: 14.25px;
"
>Pay</span
>
</button>
</slot>
</paystack>
</template>

Expand All @@ -31,15 +94,17 @@ export default {
},
}
</script>

```

## Documentation

A more detailed guide can be found [here](https://vue3paystack.netlify.app/)

## Want to say thanks?
If this has been helpful, kindly share the link to other and a star on the github repo would be helpful too

If this has been helpful, kindly share the link to other and a star on the github repo would be helpful too

## License
Licensed under MIT License. [Click](https://github.com/somteacodes/vue3-paystack/blob/master/LICENSE.md) for details.

Licensed under MIT License. [Click](https://github.com/somteacodes/vue3-paystack/blob/master/LICENSE.md) for details.
49 changes: 42 additions & 7 deletions dev/serve.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script>
import { defineComponent } from 'vue';
import Vue3Paystack from '@/vue3-paystack.vue';
import { defineComponent } from "vue";
import Vue3Paystack from "@/vue3-paystack.vue";

export default defineComponent({
name: 'ServeDev',
name: "ServeDev",
components: {
Vue3Paystack
}
Vue3Paystack,
},
});
</script>

Expand All @@ -15,12 +15,47 @@ export default defineComponent({
<vue3-paystack buttonText="Hello Paynow">
<slot>
<img
style="width: 40px;"
style="width: 40px"
src="https://images.unsplash.com/photo-1575936123452-b67c3203c357?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8aW1hZ2V8ZW58MHx8MHx8fDA%3D"
/>
VuePaystack with slot
</slot>
</vue3-paystack><br /><br />
</vue3-paystack>
<br />
<vue3-paystack>
<slot>
<button
style="
line-height: 100%;
text-decoration: none;
display: block;
max-width: 100%;
mso-padding-alt: 0px;
background-color: #3eaf7c;
border-radius: 6px;
color: #fff;
font-size: 18px;
padding-top: 19px;
padding-bottom: 19px;
text-align: center;
width: 100%;
padding: 19px 0px 19px 0px;
"
target="_blank"
>
<span
style="
max-width: 100%;
display: inline-block;
line-height: 120%;
mso-padding-alt: 0px;
mso-text-raise: 14.25px;
"
>Pay</span
>
</button>
</slot> </vue3-paystack
><br /><br />
<vue3-paystack buttonText="Vue Paystack without slot" /><br /><br />
<vue3-paystack />
</div>
Expand Down
95 changes: 61 additions & 34 deletions src/vue3-paystack.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script>
import { defineComponent } from 'vue';
import { defineComponent } from "vue";

export default /*#__PURE__*/defineComponent({
name: 'Vue3Paystack', // vue component name
props: {
export default /*#__PURE__*/ defineComponent({
name: "Vue3Paystack", // vue component name
props: {
// styles
buttonClass: {
type: String,
Expand All @@ -23,7 +23,7 @@ export default /*#__PURE__*/defineComponent({
type: String,
required: true,
}, //required
firstname: {
firstname: {
type: String,
default: "",
},
Expand All @@ -45,59 +45,71 @@ export default /*#__PURE__*/defineComponent({
},
onSuccess: {
type: Function,
default: function(response) {
default: function (response) {
console.log(response);
},
},
onCancel: {
type: Function,
default: function() {
default: function () {
console.log("payment closed");
},
},
channels: {
type: Array,
default: function() {
default: function () {
return ["card", "bank", "ussd", "qr", "mobile_money"];
},
},
metadata: {
type: Object,
default: function() {
default: function () {
return {};
},
},
label: {
type: String,
default: "",
},

},
async created() {
this.mountScript()
.then(()=>{
this.hasScriptLoaded = true
})
this.mountScript().then(() => {
this.hasScriptLoaded = true;
});
},
data() {
return {
hasScriptLoaded:false
}
hasScriptLoaded: false,
errorMessage: "",
};
},
methods: {
async mountScript( ) {
return new Promise((res,rej)=>{
async mountScript() {
return new Promise((res, rej) => {
const script = document.createElement("script");
// add script source
script.setAttribute("src", "https://js.paystack.co/v2/inline.js");
script.setAttribute("type", "text/javascript");
document.head.appendChild(script);
// check if script is ready
script.onload=()=>res()
script.onerror=()=>rej()
})
// add script source
script.setAttribute("src", "https://js.paystack.co/v2/inline.js");
script.setAttribute("type", "text/javascript");
document.head.appendChild(script);
// check if script is ready
script.onload = () => res();
script.onerror = () => rej();
});
},
validateProps() {
// Check for missing required props
if (!this.publicKey) return "Paystack Public key prop is missing.";
if (!this.email) return "Paying customer email prop is missing.";
if (!this.amount) return "Amount prop is missing.";
if (!this.reference) return "Reference prop is missing.";
return ""; // No errors
},
payWithPaystack() {
this.errorMessage = this.validateProps();
if (this.errorMessage) {
// Prevent payment if there's an error
return;
}
// options
const paymentOptions = {
// general options
Expand All @@ -109,7 +121,7 @@ export default /*#__PURE__*/defineComponent({
currency: this.currency,
channels: this.channels,
metadata: this.metadata,
label: this.label,
label: this.label,
firstname: this.firstname,
lastname: this.lastname,
onSuccess: (response) => {
Expand Down Expand Up @@ -140,14 +152,29 @@ export default /*#__PURE__*/defineComponent({
</script>

<template>
<button
:disabled="!hasScriptLoaded"
:class="buttonClass" @click="payWithPaystack">
<slot>
{{ buttonText }}
</slot>
</button>
<div>
<div @click="payWithPaystack" v-bind="$attrs">
<slot>
<!-- Fallback button if no slot is provided -->
<button :disabled="!hasScriptLoaded" :class="buttonClass">
{{ buttonText }}
</button>
</slot>
</div>
<p v-if="errorMessage" class="error-text">{{ errorMessage }}</p>
</div>
</template>


<style scoped>
.paystack-container {
position: relative;
display: inline-block;
}

.error-text {
color: red;
font-size: 0.875rem;
margin-top: 0.5rem;
}
</style>