Skip to content

Feat: Implement paystack composable #50

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 4 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
Binary file added .DS_Store
Binary file not shown.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ npm install vue vue-paystack --save

#### Via NPM

###### my-compnent.vue sample
This library can be implemented in 2 different ways:
1. By using the component provided by the library
2. By using the composable provided by the library (For vue versions earlier than 2.7 you would need to install the composition api plugin)

##### Using the Component
###### my-component.vue sample

```vue
<template>
Expand Down Expand Up @@ -84,6 +89,54 @@ export default {

[Usage Example via NPM or Yarn](examples/commonjs/App.vue)

##### Using the Composable
###### my-component-two.vue sample

```vue
<template>
<div class="App">
<p class="App-intro">
<button @click="handlePayWithPaystack">Pay with paystack</button>
</p>
</div>
</template>

<script type="text/javascript">
import usePaystackPayment from "../../src";
export default {
setup() {
const paystackArgs = {
paystackkey: "pk_test_a137d402b5975716e89952a898aad2832c961d69",
email: "foobar@example.com",
amount: 1000000,
reference: "PayXYZ344234",
};

const payWithPaystack = usePaystackPayment(paystackArgs);

const callback = (response) => {
console.log(response);
};

const close = () => {
console.log("Payment closed");
};

const handlePayWithPaystack = () => {
payWithPaystack(callback, close);
};

return {
handlePayWithPaystack,
};
},
};
</script>
```

[Usage Example via NPM or Yarn](examples/commonjs/AppTwo.vue)


#### via CDN

```javascript
Expand Down
2 changes: 1 addition & 1 deletion dist/paystack.umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions examples/commonjs/AppTwo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<div class="App">
<p class="App-intro">
<button @click="handlePayWithPaystack">Pay with paystack</button>
</p>
</div>
</template>

<script type="text/javascript">
import usePaystackPayment from "../../src";

export default {
setup() {
const paystackArgs = {
paystackkey: "pk_test_a137d402b5975716e89952a898aad2832c961d69",
email: "foobar@example.com",
amount: 1000000,
reference: "PayXYZ344234",
};

const payWithPaystack = usePaystackPayment(paystackArgs);

const callback = (response) => {
console.log(response);
};

const close = () => {
console.log("Payment closed");
};

const handlePayWithPaystack = () => {
payWithPaystack(callback, close);
};

return {
handlePayWithPaystack,
};
},
};
</script>
<style>
.App {
text-align: center;
}

.App-intro {
font-size: large;
}
.payButton {
color: #61dafb;
width: 250px;
height: 50px;
font-weight: 800;
}
</style>
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<body>
<div id="app">
<pay-stack :amount="amount" :email="email" :paystackkey="paystackkey" :reference="reference" :callback="callback"
<pay-stack :amount="amount" :email="email" :paystackKey="paystackkey" :reference="reference" :callback="callback"
:close="close" :embed="false" />
<i class="fas fa-money-bill-alt"></i>
Make Payment
Expand Down
1 change: 1 addition & 0 deletions lib/index.js → lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Paystack from "./paystack.vue";
export { default as usePaystackPayment } from "./use-paystack";

export default Paystack;
63 changes: 63 additions & 0 deletions lib/paystack-script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ref, onUnmounted, reactive, onBeforeMount } from "vue";

const cachedScripts: string[] = [];
interface IScriptResult {
scriptLoaded: boolean;
scriptError: boolean;
}

export default function usePaystackScript(): IScriptResult {
const src = "https://js.paystack.co/v1/inline.js";

const scriptState = reactive<IScriptResult>({
scriptLoaded: false,
scriptError: false,
});

const script = ref<HTMLScriptElement>();

const onScriptLoad = (): void => {
scriptState.scriptLoaded = true;
};

const onScriptError = (): void => {
const index = cachedScripts.indexOf(src);
if (index >= 0) cachedScripts.splice(index, 1);
script.value && script.value.remove();

scriptState.scriptError = true;
};

const handleLoadScript = () => {
if (cachedScripts.includes(src)) {
scriptState.scriptLoaded = true;
} else {
cachedScripts.push(src);

const scriptElement = document.createElement("script");
script.value = scriptElement;

script.value.src = src;
script.value.async = true;

script.value.addEventListener("load", onScriptLoad);
script.value.addEventListener("complete", onScriptLoad);
script.value.addEventListener("error", onScriptError);

document.body.appendChild(script.value);
}
};

onBeforeMount(() => {
handleLoadScript();
});

onUnmounted(() => {
if (script.value) {
script.value.removeEventListener("load", onScriptLoad);
script.value.removeEventListener("error", onScriptError);
}
});

return scriptState;
}
Loading