From 42c724fa984f33139e572ecc8ff59fdaa10c7529 Mon Sep 17 00:00:00 2001 From: kadetXx Date: Sun, 17 Dec 2023 05:59:43 +0100 Subject: [PATCH] feat: allow paystack options as argument in initialize function --- dist/index.es.js | 112 +++++++++++++----------- dist/index.es.js.map | 2 +- dist/index.js | 112 +++++++++++++----------- dist/index.js.map | 2 +- dist/paystack-button.d.ts | 2 +- dist/paystack-consumer.d.ts | 4 +- dist/paystack-context.d.ts | 5 +- dist/paystack-provider.d.ts | 3 +- dist/types.d.ts | 7 +- dist/use-paystack.d.ts | 4 +- example/src/App.js | 74 +++++++++------- example/src/dist/index.es.js | 112 +++++++++++++----------- example/src/dist/index.es.js.map | 2 +- example/src/dist/index.js | 112 +++++++++++++----------- example/src/dist/index.js.map | 2 +- example/src/dist/paystack-button.d.ts | 2 +- example/src/dist/paystack-consumer.d.ts | 4 +- example/src/dist/paystack-context.d.ts | 5 +- example/src/dist/paystack-provider.d.ts | 3 +- example/src/dist/types.d.ts | 7 +- example/src/dist/use-paystack.d.ts | 4 +- libs/paystack-button.tsx | 10 ++- libs/paystack-consumer.tsx | 9 +- libs/paystack-context.ts | 7 +- libs/paystack-provider.tsx | 7 +- libs/test/use-paystack.test.tsx | 10 +-- libs/types.ts | 11 ++- libs/use-paystack.ts | 55 ++++++------ 28 files changed, 386 insertions(+), 303 deletions(-) diff --git a/dist/index.es.js b/dist/index.es.js index 08429c5..b4441a4 100644 --- a/dist/index.es.js +++ b/dist/index.es.js @@ -1,5 +1,50 @@ import React, { useState, useEffect, createContext, forwardRef, useContext } from 'react'; +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + + +var __assign = function() { + __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + var cachedScripts = []; function usePaystackScript() { var src = 'https://js.paystack.co/v1/inline.js'; @@ -55,16 +100,18 @@ var callPaystackPop = function (paystackArgs) { handler && handler.openIframe(); }; -function usePaystackPayment(options) { +function usePaystackPayment(hookConfig) { var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; - var publicKey = options.publicKey, firstname = options.firstname, lastname = options.lastname, phone = options.phone, email = options.email, amount = options.amount, reference = options.reference, _b = options.metadata, metadata = _b === void 0 ? {} : _b, _c = options.currency, currency = _c === void 0 ? 'NGN' : _c, channels = options.channels, _d = options.label, label = _d === void 0 ? '' : _d, _e = options.plan, plan = _e === void 0 ? '' : _e, _f = options.quantity, quantity = _f === void 0 ? '' : _f, _g = options.subaccount, subaccount = _g === void 0 ? '' : _g, _h = options.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = options.bearer, bearer = _j === void 0 ? 'account' : _j, split = options.split, split_code = options.split_code; - function initializePayment(callback, onClose) { + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; if (scriptError) { throw new Error('Unable to load paystack inline script'); } + var args = __assign(__assign({}, hookConfig), config); + var publicKey = args.publicKey, firstname = args.firstname, lastname = args.lastname, phone = args.phone, email = args.email, amount = args.amount, reference = args.reference, _b = args.metadata, metadata = _b === void 0 ? {} : _b, _c = args.currency, currency = _c === void 0 ? 'NGN' : _c, channels = args.channels, _d = args.label, label = _d === void 0 ? '' : _d, _e = args.plan, plan = _e === void 0 ? '' : _e, _f = args.quantity, quantity = _f === void 0 ? '' : _f, _g = args.subaccount, subaccount = _g === void 0 ? '' : _g, _h = args.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = args.bearer, bearer = _j === void 0 ? 'account' : _j, split = args.split, split_code = args.split_code; if (scriptLoaded) { var paystackArgs = { - callback: callback ? callback : function () { return null; }, + callback: onSuccess ? onSuccess : function () { return null; }, onClose: onClose ? onClose : function () { return null; }, key: publicKey, ref: reference, @@ -76,7 +123,6 @@ function usePaystackPayment(options) { currency: currency, plan: plan, quantity: quantity, - 'data-custom-button': options['data-custom-button'] || '', channels: channels, subaccount: subaccount, transaction_charge: transaction_charge, @@ -85,6 +131,7 @@ function usePaystackPayment(options) { metadata: metadata, split: split, split_code: split_code, + 'data-custom-button': args['data-custom-button'] || '', }; callPaystackPop(paystackArgs); } @@ -97,66 +144,29 @@ function usePaystackPayment(options) { return initializePayment; } -/****************************************************************************** -Copyright (c) Microsoft Corporation. - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -***************************************************************************** */ - -var __assign = function() { - __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; - -function __rest(s, e) { - var t = {}; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) - t[p] = s[p]; - if (s != null && typeof Object.getOwnPropertySymbols === "function") - for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { - if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) - t[p[i]] = s[p[i]]; - } - return t; -} - var PaystackButton = function (_a) { - var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement("button", { className: className, onClick: function () { return initializePayment(onSuccess, onClose); } }, text || children)); + var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement("button", { className: className, onClick: function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); } }, text || children)); }; var PaystackContext = createContext({ + config: {}, initializePayment: function () { return null; }, onSuccess: function () { return null; }, onClose: function () { return null; }, }); var PaystackProvider = function (_a) { - var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement(PaystackContext.Provider, { value: { initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); + var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement(PaystackContext.Provider, { value: { config: config, initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); }; var PaystackConsumerChild = function (_a) { var children = _a.children, ref = _a.ref; - var _b = useContext(PaystackContext), initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; - var completeInitializePayment = function () { return initializePayment(onSuccess, onClose); }; + var _b = useContext(PaystackContext), config = _b.config, initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; + var completeInitializePayment = function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); }; return children({ initializePayment: completeInitializePayment, ref: ref }); }; // eslint-disable-next-line react/display-name diff --git a/dist/index.es.js.map b/dist/index.es.js.map index c0a615f..2920845 100644 --- a/dist/index.es.js.map +++ b/dist/index.es.js.map @@ -1 +1 @@ -{"version":3,"file":"index.es.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {callback, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(\n options: PaystackProps,\n): (callback?: () => void, onClose?: () => void) => void {\n const [scriptLoaded, scriptError] = usePaystackScript();\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = options;\n\n function initializePayment(callback?: callback, onClose?: callback): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: callback ? callback : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n 'data-custom-button': options['data-custom-button'] || '',\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {callback} from './types';\ntype IPaystackContext = {\n initializePayment: (arg0: callback, arg1: callback) => void;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystacConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n const completeInitializePayment = (): void => initializePayment(onSuccess, onClose);\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":[],"mappings":";;AAEA,IAAM,aAAa,GAAa,EAAE,CAAC;AAMrB,SAAU,iBAAiB,GAAA;IACvC,IAAM,GAAG,GAAG,qCAAqC,CAAC;IAE5C,IAAA,EAAA,GAAoB,QAAQ,CAAgB;AAChD,QAAA,MAAM,EAAE,KAAK;AACb,QAAA,KAAK,EAAE,KAAK;AACb,KAAA,CAAC,EAHK,KAAK,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,QAAQ,QAGpB,CAAC;AAEH,IAAA,SAAS,CAAC,YAAA;AACR,QAAA,IAAI,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AAC/B,YAAA,QAAQ,CAAC;AACP,gBAAA,MAAM,EAAE,IAAI;AACZ,gBAAA,KAAK,EAAE,KAAK;AACb,aAAA,CAAC,CAAC;AACJ,SAAA;AAAM,aAAA;AACL,YAAA,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAExB,IAAM,QAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAChD,YAAA,QAAM,CAAC,GAAG,GAAG,GAAG,CAAC;AACjB,YAAA,QAAM,CAAC,KAAK,GAAG,IAAI,CAAC;AAEpB,YAAA,IAAM,cAAY,GAAG,YAAA;AACnB,gBAAA,QAAQ,CAAC;AACP,oBAAA,MAAM,EAAE,IAAI;AACZ,oBAAA,KAAK,EAAE,KAAK;AACb,iBAAA,CAAC,CAAC;AACL,aAAC,CAAC;AAEF,YAAA,IAAM,eAAa,GAAG,YAAA;gBACpB,IAAM,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;gBACzC,IAAI,KAAK,IAAI,CAAC;AAAE,oBAA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AAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;"} \ No newline at end of file +{"version":3,"file":"index.es.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: Partial): InitializePayment {\n const [scriptLoaded, scriptError] = usePaystackScript();\n\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n const args = {...hookConfig, ...config};\n\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = args;\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: onSuccess ? onSuccess : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n 'data-custom-button': args['data-custom-button'] || '',\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n initializePayment({config, onSuccess, onClose})}\n >\n {text || children}\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\n\ntype IPaystackContext = {\n config: PaystackProps;\n initializePayment: InitializePayment;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n config: {} as PaystackProps,\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystackConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {config, initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n\n const completeInitializePayment = (): void => initializePayment({config, onSuccess, onClose});\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystackConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAM,aAAa,GAAa,EAAE,CAAC;AAMrB,SAAU,iBAAiB,GAAA;IACvC,IAAM,GAAG,GAAG,qCAAqC,CAAC;IAE5C,IAAA,EAAA,GAAoB,QAAQ,CAAgB;AAChD,QAAA,MAAM,EAAE,KAAK;AACb,QAAA,KAAK,EAAE,KAAK;AACb,KAAA,CAAC,EAHK,KAAK,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,QAAQ,QAGpB,CAAC;AAEH,IAAA,SA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 +2,51 @@ var React = require('react'); +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + + +var __assign = function() { + __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + var cachedScripts = []; function usePaystackScript() { var src = 'https://js.paystack.co/v1/inline.js'; @@ -57,16 +102,18 @@ var callPaystackPop = function (paystackArgs) { handler && handler.openIframe(); }; -function usePaystackPayment(options) { +function usePaystackPayment(hookConfig) { var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; - var publicKey = options.publicKey, firstname = options.firstname, lastname = options.lastname, phone = options.phone, email = options.email, amount = options.amount, reference = options.reference, _b = options.metadata, metadata = _b === void 0 ? {} : _b, _c = options.currency, currency = _c === void 0 ? 'NGN' : _c, channels = options.channels, _d = options.label, label = _d === void 0 ? '' : _d, _e = options.plan, plan = _e === void 0 ? '' : _e, _f = options.quantity, quantity = _f === void 0 ? '' : _f, _g = options.subaccount, subaccount = _g === void 0 ? '' : _g, _h = options.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = options.bearer, bearer = _j === void 0 ? 'account' : _j, split = options.split, split_code = options.split_code; - function initializePayment(callback, onClose) { + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; if (scriptError) { throw new Error('Unable to load paystack inline script'); } + var args = __assign(__assign({}, hookConfig), config); + var publicKey = args.publicKey, firstname = args.firstname, lastname = args.lastname, phone = args.phone, email = args.email, amount = args.amount, reference = args.reference, _b = args.metadata, metadata = _b === void 0 ? {} : _b, _c = args.currency, currency = _c === void 0 ? 'NGN' : _c, channels = args.channels, _d = args.label, label = _d === void 0 ? '' : _d, _e = args.plan, plan = _e === void 0 ? '' : _e, _f = args.quantity, quantity = _f === void 0 ? '' : _f, _g = args.subaccount, subaccount = _g === void 0 ? '' : _g, _h = args.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = args.bearer, bearer = _j === void 0 ? 'account' : _j, split = args.split, split_code = args.split_code; if (scriptLoaded) { var paystackArgs = { - callback: callback ? callback : function () { return null; }, + callback: onSuccess ? onSuccess : function () { return null; }, onClose: onClose ? onClose : function () { return null; }, key: publicKey, ref: reference, @@ -78,7 +125,6 @@ function usePaystackPayment(options) { currency: currency, plan: plan, quantity: quantity, - 'data-custom-button': options['data-custom-button'] || '', channels: channels, subaccount: subaccount, transaction_charge: transaction_charge, @@ -87,6 +133,7 @@ function usePaystackPayment(options) { metadata: metadata, split: split, split_code: split_code, + 'data-custom-button': args['data-custom-button'] || '', }; callPaystackPop(paystackArgs); } @@ -99,66 +146,29 @@ function usePaystackPayment(options) { return initializePayment; } -/****************************************************************************** -Copyright (c) Microsoft Corporation. - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -***************************************************************************** */ - -var __assign = function() { - __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; - -function __rest(s, e) { - var t = {}; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) - t[p] = s[p]; - if (s != null && typeof Object.getOwnPropertySymbols === "function") - for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { - if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) - t[p[i]] = s[p[i]]; - } - return t; -} - var PaystackButton = function (_a) { - var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement("button", { className: className, onClick: function () { return initializePayment(onSuccess, onClose); } }, text || children)); + var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement("button", { className: className, onClick: function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); } }, text || children)); }; var PaystackContext = React.createContext({ + config: {}, initializePayment: function () { return null; }, onSuccess: function () { return null; }, onClose: function () { return null; }, }); var PaystackProvider = function (_a) { - var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement(PaystackContext.Provider, { value: { initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); + var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement(PaystackContext.Provider, { value: { config: config, initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); }; var PaystackConsumerChild = function (_a) { var children = _a.children, ref = _a.ref; - var _b = React.useContext(PaystackContext), initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; - var completeInitializePayment = function () { return initializePayment(onSuccess, onClose); }; + var _b = React.useContext(PaystackContext), config = _b.config, initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; + var completeInitializePayment = function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); }; return children({ initializePayment: completeInitializePayment, ref: ref }); }; // eslint-disable-next-line react/display-name diff --git a/dist/index.js.map b/dist/index.js.map index 30db5a6..608e889 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {callback, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(\n options: PaystackProps,\n): (callback?: () => void, onClose?: () => void) => void {\n const [scriptLoaded, scriptError] = usePaystackScript();\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = options;\n\n function initializePayment(callback?: callback, onClose?: callback): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: callback ? callback : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n 'data-custom-button': options['data-custom-button'] || '',\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {callback} from './types';\ntype IPaystackContext = {\n initializePayment: (arg0: callback, arg1: callback) => void;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystacConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n const completeInitializePayment = (): void => initializePayment(onSuccess, onClose);\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":["useState","useEffect","createContext","useContext","forwardRef"],"mappings":";;;;AAEA,IAAM,aAAa,GAAa,EAAE,CAAC;AAMrB,SAAU,iBAAiB,GAAA;IACvC,IAAM,GAAG,GAAG,qCAAqC,CAAC;IAE5C,IAAA,EAAA,GAAoBA,cAAQ,CAAgB;AAChD,QAAA,MAAM,EAAE,KAAK;AACb,QAAA,KAAK,EAAE,KAAK;AACb,KAAA,CAAC,EAHK,KAAK,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,QAAQ,QAGpB,CAAC;AAEH,IAAAC,eAAS,CAAC,YAAA;AACR,QAAA,IAAI,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AAC/B,YAAA,QAAQ,CAAC;AACP,gBAAA,MAAM,EAAE,IAAI;AACZ,gBAAA,KAAK,EAAE,KAAK;AACb,aAAA,CAAC,CAAC;AACJ,SAAA;AAAM,aAAA;AACL,YAAA,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAExB,IAAM,QAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAChD,YAAA,QAAM,CAAC,GAAG,GAAG,GAAG,CAAC;AACjB,YAAA,QAAM,CAAC,KAAK,GAAG,IAAI,CAAC;AAEp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AAS,GAAG,WAAW,GAAG,WAAW,GAAG,YAAW,EAAA,OAAA,IAAI,CAAA,EAAA,CAAC;AAC9D,IAAA,IAAM,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,YAAW,EAAA,OAAA,IAAI,CAAA,EAAA,CAAC;AACxD,IAAA,QACE,KAAA,CAAA,aAAA,CAAC,gBAAgB,EAAA,QAAA,CAAA,EAAA,EAAK,MAAM,EAAA,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: Partial): InitializePayment {\n const [scriptLoaded, scriptError] = usePaystackScript();\n\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n const args = {...hookConfig, ...config};\n\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = args;\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: onSuccess ? onSuccess : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n 'data-custom-button': args['data-custom-button'] || '',\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n initializePayment({config, onSuccess, onClose})}\n >\n {text || children}\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\n\ntype IPaystackContext = {\n config: PaystackProps;\n initializePayment: InitializePayment;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n config: {} as PaystackProps,\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystackConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {config, initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n\n const completeInitializePayment = (): void => initializePayment({config, onSuccess, onClose});\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystackConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":["useState","useEffect","createContext","useContext","forwardRef"],"mappings":";;;;;;;;;;;;;;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D,IAAA,QACE,KAAA,CAAA,aAAA,CAAC,gBAAgB,EAAA,QAAA,CAAA,EAAA,EAAK,MAAM,EAAA,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;;;"} \ No newline at end of file diff --git a/dist/paystack-button.d.ts b/dist/paystack-button.d.ts index a2d3b2a..ce4458f 100644 --- a/dist/paystack-button.d.ts +++ b/dist/paystack-button.d.ts @@ -7,5 +7,5 @@ interface PaystackButtonProps extends PaystackProps { onSuccess?: callback; onClose?: callback; } -declare const PaystackButton: ({ text, className, children, onSuccess, onClose, ...others }: PaystackButtonProps) => JSX.Element; +declare const PaystackButton: ({ text, className, children, onSuccess, onClose, ...config }: PaystackButtonProps) => JSX.Element; export default PaystackButton; diff --git a/dist/paystack-consumer.d.ts b/dist/paystack-consumer.d.ts index e80f377..b465966 100644 --- a/dist/paystack-consumer.d.ts +++ b/dist/paystack-consumer.d.ts @@ -1,9 +1,9 @@ import React from 'react'; import { PaystackProps } from './types'; -interface PaystacConsumerProps extends PaystackProps { +interface PaystackConsumerProps extends PaystackProps { children: (arg: Record) => any; onSuccess?: () => void; onClose?: () => void; } -declare const PaystackConsumer: React.ForwardRefExoticComponent>; +declare const PaystackConsumer: React.ForwardRefExoticComponent>; export default PaystackConsumer; diff --git a/dist/paystack-context.d.ts b/dist/paystack-context.d.ts index 7e92095..ddb3bc7 100644 --- a/dist/paystack-context.d.ts +++ b/dist/paystack-context.d.ts @@ -1,7 +1,8 @@ /// -import { callback } from './types'; +import { InitializePayment, PaystackProps } from './types'; type IPaystackContext = { - initializePayment: (arg0: callback, arg1: callback) => void; + config: PaystackProps; + initializePayment: InitializePayment; onSuccess: () => void; onClose: () => void; }; diff --git a/dist/paystack-provider.d.ts b/dist/paystack-provider.d.ts index a3a6ca7..f5ec1e8 100644 --- a/dist/paystack-provider.d.ts +++ b/dist/paystack-provider.d.ts @@ -1,8 +1,9 @@ +/// import { callback, PaystackProps } from './types'; interface PaystackProviderProps extends PaystackProps { children: JSX.Element; onSuccess: callback; onClose: callback; } -declare const PaystackProvider: ({ children, onSuccess, onClose, ...others }: PaystackProviderProps) => JSX.Element; +declare const PaystackProvider: ({ children, onSuccess, onClose, ...config }: PaystackProviderProps) => JSX.Element; export default PaystackProvider; diff --git a/dist/types.d.ts b/dist/types.d.ts index 2a33db1..700debf 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -17,10 +17,10 @@ export type callback = (response?: any) => void; export interface PaystackProps { publicKey: string; email: string; + amount: number; firstname?: string; lastname?: string; phone?: phone; - amount: number; reference?: string; metadata?: PaystackMetadata; currency?: Currency; @@ -35,4 +35,9 @@ export interface PaystackProps { split_code?: string; split?: Record; } +export type InitializePayment = (options: { + onSuccess?: callback; + onClose?: callback; + config?: PaystackProps; +}) => void; export {}; diff --git a/dist/use-paystack.d.ts b/dist/use-paystack.d.ts index 212173b..51688f4 100644 --- a/dist/use-paystack.d.ts +++ b/dist/use-paystack.d.ts @@ -1,2 +1,2 @@ -import { PaystackProps } from './types'; -export default function usePaystackPayment(options: PaystackProps): (callback?: () => void, onClose?: () => void) => void; +import { InitializePayment, PaystackProps } from './types'; +export default function usePaystackPayment(hookConfig: Partial): InitializePayment; diff --git a/example/src/App.js b/example/src/App.js index ea31859..4eb9887 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -1,16 +1,16 @@ import React from 'react'; import logo from './logo.svg'; -import { usePaystackPayment, PaystackButton, PaystackConsumer } from './dist/index.es'; +import {usePaystackPayment, PaystackButton, PaystackConsumer} from './dist/index.es'; import './App.css'; const config = { - reference: (new Date()).getTime().toString(), - email: "user@example.com", - amount: 20000, - publicKey: 'pk_test_a137d402b5975716e89952a898aad2832c961d69', - firstname: 'cool', - lastname: 'story', - /*split: { //if you want to use transaction split + reference: new Date().getTime().toString(), + email: 'user@example.com', + amount: 20000, + publicKey: 'pk_test_a137d402b5975716e89952a898aad2832c961d69', + firstname: 'cool', + lastname: 'story', + /*split: { //if you want to use transaction split "type": "percentage", "bearer_type": "all", "subaccounts": [ @@ -27,33 +27,37 @@ const config = { }; const onSuccess = (reference) => { - // Implementation for whatever you want to do with reference and after success call. - console.log('reference',reference); + // Implementation for whatever you want to do with reference and after success call. + console.log('reference', reference); }; const onClose = () => { - // implementation for whatever you want to do when the Paystack dialog closed. - console.log('closed') -} + // implementation for whatever you want to do when the Paystack dialog closed. + console.log('closed'); +}; const PaystackHookExample = () => { - const initializePayment = usePaystackPayment(config); - return ( -
- -
- ); + const initializePayment = usePaystackPayment(config); + return ( +
+ +
+ ); }; function App() { - const componentProps = { - ...config, - text: 'Paystack Button Implementation', - onSuccess, - onClose - }; + const componentProps = { + ...config, + text: 'Paystack Button Implementation', + onSuccess, + onClose, + }; return (
@@ -71,11 +75,17 @@ function App() { Learn React - - - - {({initializePayment}) => } - + + + + {({initializePayment}) => { + return ( + + ); + }} +
); } diff --git a/example/src/dist/index.es.js b/example/src/dist/index.es.js index 08429c5..b4441a4 100644 --- a/example/src/dist/index.es.js +++ b/example/src/dist/index.es.js @@ -1,5 +1,50 @@ import React, { useState, useEffect, createContext, forwardRef, useContext } from 'react'; +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + + +var __assign = function() { + __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + var cachedScripts = []; function usePaystackScript() { var src = 'https://js.paystack.co/v1/inline.js'; @@ -55,16 +100,18 @@ var callPaystackPop = function (paystackArgs) { handler && handler.openIframe(); }; -function usePaystackPayment(options) { +function usePaystackPayment(hookConfig) { var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; - var publicKey = options.publicKey, firstname = options.firstname, lastname = options.lastname, phone = options.phone, email = options.email, amount = options.amount, reference = options.reference, _b = options.metadata, metadata = _b === void 0 ? {} : _b, _c = options.currency, currency = _c === void 0 ? 'NGN' : _c, channels = options.channels, _d = options.label, label = _d === void 0 ? '' : _d, _e = options.plan, plan = _e === void 0 ? '' : _e, _f = options.quantity, quantity = _f === void 0 ? '' : _f, _g = options.subaccount, subaccount = _g === void 0 ? '' : _g, _h = options.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = options.bearer, bearer = _j === void 0 ? 'account' : _j, split = options.split, split_code = options.split_code; - function initializePayment(callback, onClose) { + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; if (scriptError) { throw new Error('Unable to load paystack inline script'); } + var args = __assign(__assign({}, hookConfig), config); + var publicKey = args.publicKey, firstname = args.firstname, lastname = args.lastname, phone = args.phone, email = args.email, amount = args.amount, reference = args.reference, _b = args.metadata, metadata = _b === void 0 ? {} : _b, _c = args.currency, currency = _c === void 0 ? 'NGN' : _c, channels = args.channels, _d = args.label, label = _d === void 0 ? '' : _d, _e = args.plan, plan = _e === void 0 ? '' : _e, _f = args.quantity, quantity = _f === void 0 ? '' : _f, _g = args.subaccount, subaccount = _g === void 0 ? '' : _g, _h = args.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = args.bearer, bearer = _j === void 0 ? 'account' : _j, split = args.split, split_code = args.split_code; if (scriptLoaded) { var paystackArgs = { - callback: callback ? callback : function () { return null; }, + callback: onSuccess ? onSuccess : function () { return null; }, onClose: onClose ? onClose : function () { return null; }, key: publicKey, ref: reference, @@ -76,7 +123,6 @@ function usePaystackPayment(options) { currency: currency, plan: plan, quantity: quantity, - 'data-custom-button': options['data-custom-button'] || '', channels: channels, subaccount: subaccount, transaction_charge: transaction_charge, @@ -85,6 +131,7 @@ function usePaystackPayment(options) { metadata: metadata, split: split, split_code: split_code, + 'data-custom-button': args['data-custom-button'] || '', }; callPaystackPop(paystackArgs); } @@ -97,66 +144,29 @@ function usePaystackPayment(options) { return initializePayment; } -/****************************************************************************** -Copyright (c) Microsoft Corporation. - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -***************************************************************************** */ - -var __assign = function() { - __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; - -function __rest(s, e) { - var t = {}; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) - t[p] = s[p]; - if (s != null && typeof Object.getOwnPropertySymbols === "function") - for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { - if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) - t[p[i]] = s[p[i]]; - } - return t; -} - var PaystackButton = function (_a) { - var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement("button", { className: className, onClick: function () { return initializePayment(onSuccess, onClose); } }, text || children)); + var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement("button", { className: className, onClick: function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); } }, text || children)); }; var PaystackContext = createContext({ + config: {}, initializePayment: function () { return null; }, onSuccess: function () { return null; }, onClose: function () { return null; }, }); var PaystackProvider = function (_a) { - var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement(PaystackContext.Provider, { value: { initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); + var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement(PaystackContext.Provider, { value: { config: config, initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); }; var PaystackConsumerChild = function (_a) { var children = _a.children, ref = _a.ref; - var _b = useContext(PaystackContext), initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; - var completeInitializePayment = function () { return initializePayment(onSuccess, onClose); }; + var _b = useContext(PaystackContext), config = _b.config, initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; + var completeInitializePayment = function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); }; return children({ initializePayment: completeInitializePayment, ref: ref }); }; // eslint-disable-next-line react/display-name diff --git a/example/src/dist/index.es.js.map b/example/src/dist/index.es.js.map index 4ae1ffa..2920845 100644 --- a/example/src/dist/index.es.js.map +++ b/example/src/dist/index.es.js.map @@ -1 +1 @@ -{"version":3,"file":"index.es.js","sources":["../../../libs/paystack-script.ts","../../../libs/paystack-actions.ts","../../../libs/use-paystack.ts","../../../libs/paystack-button.tsx","../../../libs/paystack-context.ts","../../../libs/paystack-provider.tsx","../../../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {callback, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(\n options: PaystackProps,\n): (callback?: () => void, onClose?: () => void) => void {\n const [scriptLoaded, scriptError] = usePaystackScript();\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = options;\n\n function initializePayment(callback?: callback, onClose?: callback): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: callback ? callback : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n 'data-custom-button': options['data-custom-button'] || '',\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {callback} from './types';\ntype IPaystackContext = {\n initializePayment: (arg0: callback, arg1: callback) => void;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystacConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n const completeInitializePayment = (): void => initializePayment(onSuccess, onClose);\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose :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CAAC;;;;"} \ No newline at end of file +{"version":3,"file":"index.es.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: Partial): InitializePayment {\n const [scriptLoaded, scriptError] = usePaystackScript();\n\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n const args = {...hookConfig, ...config};\n\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = args;\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: onSuccess ? onSuccess : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n 'data-custom-button': args['data-custom-button'] || '',\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n initializePayment({config, onSuccess, onClose})}\n >\n {text || children}\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\n\ntype IPaystackContext = {\n config: PaystackProps;\n initializePayment: InitializePayment;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n config: {} as PaystackProps,\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystackConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {config, initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n\n const completeInitializePayment = (): void => initializePayment({config, onSuccess, onClose});\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystackConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAM,aAAa,GAAa,EAAE,CAAC;AAMrB,SAAU,iBAAiB,GAAA;IACvC,IAAM,GAAG,GAAG,qCAAqC,CAAC;IAE5C,IAAA,EAAA,GAAoB,QAAQ,CAAgB;AAChD,QAAA,MAAM,EAAE,KAAK;AACb,QAAA,KAAK,EAAE,KAAK;AACb,KAAA,CAAC,EAHK,KAAK,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,QAAQ,QAGpB,CAAC;AAEH,IAAA,SAAS,CAAC,YAAA;AACR,QAAA,IAAI,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AAC/B,YAAA,QAAQ,CAAC;AACP,gBAAA,MAA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M,yBAAyB,GAAG,YAAA,EAAY,OAAA,iBAAiB,CAAC,EAAC,MAAM,EAAA,MAAA,EAAE,SAAS,EAAA,SAAA,EAAE,OAAO,EAAA,OAAA,EAAC,CAAC,CAAA,EAAA,CAAC;IAC9F,OAAO,QAAQ,CAAC,EAAC,iBAAiB,EAAE,yBAAyB,EAAE,GAAG,EAAA,GAAA,EAAC,CAAC,CAAC;AACvE,CAAC,CAAC;AAEF;AACA,IAAM,gBAAgB,GAAG,UAAU,CACjC,UACE,EAAwF,EACxF,GAAQ,EAAA;AADP,IAAA,IAAA,QAAQ,GAAA,EAAA,CAAA,QAAA,EAAa,WAAW,GAAA,EAAA,CAAA,SAAA,EAAW,SAAS,GAAA,EAAA,CAAA,OAAA,EAAK,MAAM,GAAhE,MAAA,CAAA,EAAA,EAAA,CAAA,UAAA,EAAA,WAAA,EAAA,SAAA,CAAiE,CAAD,CAAA;AAGhE,IAAA,IAAM,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,YAAW,EAAA,OAAA,IAAI,CAAA,EAAA,CAAC;AAC9D,IAAA,IAAM,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,YAAW,EAAA,OAAA,IAAI,CAAA,EAAA,CAAC;AACxD,IAAA,QACE,KAAA,CAAA,aAAA,CAAC,gBAAgB,EAAA,QAAA,CAAA,EAAA,EAAK,MAAM,EAAA,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;"} \ No newline at end of file diff --git a/example/src/dist/index.js b/example/src/dist/index.js index 3fa793e..ee51386 100644 --- a/example/src/dist/index.js +++ b/example/src/dist/index.js @@ -2,6 +2,51 @@ var React = require('react'); +/****************************************************************************** +Copyright (c) Microsoft Corporation. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +***************************************************************************** */ +/* global Reflect, Promise, SuppressedError, Symbol */ + + +var __assign = function() { + __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { + if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) + t[p[i]] = s[p[i]]; + } + return t; +} + +typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}; + var cachedScripts = []; function usePaystackScript() { var src = 'https://js.paystack.co/v1/inline.js'; @@ -57,16 +102,18 @@ var callPaystackPop = function (paystackArgs) { handler && handler.openIframe(); }; -function usePaystackPayment(options) { +function usePaystackPayment(hookConfig) { var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; - var publicKey = options.publicKey, firstname = options.firstname, lastname = options.lastname, phone = options.phone, email = options.email, amount = options.amount, reference = options.reference, _b = options.metadata, metadata = _b === void 0 ? {} : _b, _c = options.currency, currency = _c === void 0 ? 'NGN' : _c, channels = options.channels, _d = options.label, label = _d === void 0 ? '' : _d, _e = options.plan, plan = _e === void 0 ? '' : _e, _f = options.quantity, quantity = _f === void 0 ? '' : _f, _g = options.subaccount, subaccount = _g === void 0 ? '' : _g, _h = options.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = options.bearer, bearer = _j === void 0 ? 'account' : _j, split = options.split, split_code = options.split_code; - function initializePayment(callback, onClose) { + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; if (scriptError) { throw new Error('Unable to load paystack inline script'); } + var args = __assign(__assign({}, hookConfig), config); + var publicKey = args.publicKey, firstname = args.firstname, lastname = args.lastname, phone = args.phone, email = args.email, amount = args.amount, reference = args.reference, _b = args.metadata, metadata = _b === void 0 ? {} : _b, _c = args.currency, currency = _c === void 0 ? 'NGN' : _c, channels = args.channels, _d = args.label, label = _d === void 0 ? '' : _d, _e = args.plan, plan = _e === void 0 ? '' : _e, _f = args.quantity, quantity = _f === void 0 ? '' : _f, _g = args.subaccount, subaccount = _g === void 0 ? '' : _g, _h = args.transaction_charge, transaction_charge = _h === void 0 ? 0 : _h, _j = args.bearer, bearer = _j === void 0 ? 'account' : _j, split = args.split, split_code = args.split_code; if (scriptLoaded) { var paystackArgs = { - callback: callback ? callback : function () { return null; }, + callback: onSuccess ? onSuccess : function () { return null; }, onClose: onClose ? onClose : function () { return null; }, key: publicKey, ref: reference, @@ -78,7 +125,6 @@ function usePaystackPayment(options) { currency: currency, plan: plan, quantity: quantity, - 'data-custom-button': options['data-custom-button'] || '', channels: channels, subaccount: subaccount, transaction_charge: transaction_charge, @@ -87,6 +133,7 @@ function usePaystackPayment(options) { metadata: metadata, split: split, split_code: split_code, + 'data-custom-button': args['data-custom-button'] || '', }; callPaystackPop(paystackArgs); } @@ -99,66 +146,29 @@ function usePaystackPayment(options) { return initializePayment; } -/****************************************************************************** -Copyright (c) Microsoft Corporation. - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -***************************************************************************** */ - -var __assign = function() { - __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; - -function __rest(s, e) { - var t = {}; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) - t[p] = s[p]; - if (s != null && typeof Object.getOwnPropertySymbols === "function") - for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { - if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) - t[p[i]] = s[p[i]]; - } - return t; -} - var PaystackButton = function (_a) { - var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement("button", { className: className, onClick: function () { return initializePayment(onSuccess, onClose); } }, text || children)); + var text = _a.text, className = _a.className, children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["text", "className", "children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement("button", { className: className, onClick: function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); } }, text || children)); }; var PaystackContext = React.createContext({ + config: {}, initializePayment: function () { return null; }, onSuccess: function () { return null; }, onClose: function () { return null; }, }); var PaystackProvider = function (_a) { - var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); - var initializePayment = usePaystackPayment(others); - return (React.createElement(PaystackContext.Provider, { value: { initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); + var children = _a.children, onSuccess = _a.onSuccess, onClose = _a.onClose, config = __rest(_a, ["children", "onSuccess", "onClose"]); + var initializePayment = usePaystackPayment(config); + return (React.createElement(PaystackContext.Provider, { value: { config: config, initializePayment: initializePayment, onSuccess: onSuccess, onClose: onClose } }, children)); }; var PaystackConsumerChild = function (_a) { var children = _a.children, ref = _a.ref; - var _b = React.useContext(PaystackContext), initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; - var completeInitializePayment = function () { return initializePayment(onSuccess, onClose); }; + var _b = React.useContext(PaystackContext), config = _b.config, initializePayment = _b.initializePayment, onSuccess = _b.onSuccess, onClose = _b.onClose; + var completeInitializePayment = function () { return initializePayment({ config: config, onSuccess: onSuccess, onClose: onClose }); }; return children({ initializePayment: completeInitializePayment, ref: ref }); }; // eslint-disable-next-line react/display-name diff --git a/example/src/dist/index.js.map b/example/src/dist/index.js.map index 83d50af..608e889 100644 --- a/example/src/dist/index.js.map +++ b/example/src/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../../../libs/paystack-script.ts","../../../libs/paystack-actions.ts","../../../libs/use-paystack.ts","../../../libs/paystack-button.tsx","../../../libs/paystack-context.ts","../../../libs/paystack-provider.tsx","../../../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {callback, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(\n options: PaystackProps,\n): (callback?: () => void, onClose?: () => void) => void {\n const [scriptLoaded, scriptError] = usePaystackScript();\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = options;\n\n function initializePayment(callback?: callback, onClose?: callback): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: callback ? callback : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n 'data-custom-button': options['data-custom-button'] || '',\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {callback} from './types';\ntype IPaystackContext = {\n initializePayment: (arg0: callback, arg1: callback) => void;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...others\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(others);\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystacConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n const completeInitializePayment = (): void => initializePayment(onSuccess, onClose);\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":["useState","useEffect","createContext","useContext","forwardRef"],"mappings":";;;;AAEA,IAAM,aAAa,GAAa,EAAE,CAAC;AAMrB,SAAU,iBAAiB,GAAA;IACvC,IAAM,GAAG,GAAG,qCAAqC,CAAC;IAE5C,IAAA,EAAA,GAAoBA,cAAQ,CAAgB;AAChD,QAAA,MAAM,EAAE,KAAK;AACb,QAAA,KAAK,EAAE,KAAK;AACb,KAAA,CAAC,EAHK,KAAK,GAAA,EAAA,CAAA,CAAA,CAAA,EAAE,QAAQ,QAGpB,CAAC;AAEH,IAAAC,eAAS,CAAC,YAAA;AACR,QAAA,IAAI,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;AAC/B,YAAA,QAAQ,CAAC;AACP,gBAAA,MAAM,EAAE,IAAI;AACZ,gBAAA,KAAK,EAAE,KAAK;AACb,aAAA,CAAC,CAAC;AACJ,SAAA;AAAM,aAAA;AACL,YAAA,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAExB,IAAM,QAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAChD,YAAA,QAAM,CAAC,GAAG,GAAG,GAAG,CAAC;AACjB,YAAA,QAAM,CAAC,KAAK,GAAG,IAAI,CAAC;AAEpB,YAAA,IAAM,cAAY,GAAG,YAAA;AACnB,gBAAA,QAAQ,CAAC;AACP,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,EAAA,CAAC;AAC9D,IAAA,IAAM,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,YAAW,EAAA,OAAA,IAAI,CAAA,EAAA,CAAC;AACxD,IAAA,QACE,KAAA,CAAA,aAAA,CAAC,gBAAgB,EAAA,QAAA,CAAA,EAAA,EAAK,MAAM,EAAA,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../libs/paystack-script.ts","../libs/paystack-actions.ts","../libs/use-paystack.ts","../libs/paystack-button.tsx","../libs/paystack-context.ts","../libs/paystack-provider.tsx","../libs/paystack-consumer.tsx"],"sourcesContent":["import {useState, useEffect} from 'react';\n\nconst cachedScripts: string[] = [];\ninterface IScriptResult {\n loaded: boolean;\n error: boolean;\n}\n\nexport default function usePaystackScript(): boolean[] {\n const src = 'https://js.paystack.co/v1/inline.js';\n\n const [state, setState] = useState({\n loaded: false,\n error: false,\n });\n\n useEffect((): any => {\n if (cachedScripts.includes(src)) {\n setState({\n loaded: true,\n error: false,\n });\n } else {\n cachedScripts.push(src);\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n\n const onScriptLoad = (): void => {\n setState({\n loaded: true,\n error: false,\n });\n };\n\n const onScriptError = (): void => {\n const index = cachedScripts.indexOf(src);\n if (index >= 0) cachedScripts.splice(index, 1);\n script.remove();\n\n setState({\n loaded: true,\n error: true,\n });\n };\n\n script.addEventListener('load', onScriptLoad);\n script.addEventListener('complete', onScriptLoad);\n script.addEventListener('error', onScriptError);\n\n document.body.appendChild(script);\n\n return (): void => {\n script.removeEventListener('load', onScriptLoad);\n script.removeEventListener('error', onScriptError);\n };\n }\n }, [src]);\n\n return [state.loaded, state.error];\n}\n","/* eslint-disable */\nexport let callPaystackPop = (paystackArgs: Record): void => {\n // @ts-ignore\n const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs);\n handler && handler.openIframe();\n};\n","import {useEffect} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: Partial): InitializePayment {\n const [scriptLoaded, scriptError] = usePaystackScript();\n\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n\n const args = {...hookConfig, ...config};\n\n const {\n publicKey,\n firstname,\n lastname,\n phone,\n email,\n amount,\n reference,\n metadata = {},\n currency = 'NGN',\n channels,\n label = '',\n plan = '',\n quantity = '',\n subaccount = '',\n transaction_charge = 0,\n bearer = 'account',\n split,\n split_code,\n } = args;\n\n if (scriptLoaded) {\n const paystackArgs: Record = {\n callback: onSuccess ? onSuccess : () => null,\n onClose: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n phone,\n amount,\n currency,\n plan,\n quantity,\n channels,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n split,\n split_code,\n 'data-custom-button': args['data-custom-button'] || '',\n };\n callPaystackPop(paystackArgs);\n }\n }\n\n useEffect(() => {\n if (scriptError) {\n throw new Error('Unable to load paystack inline script');\n }\n }, [scriptError]);\n\n return initializePayment;\n}\n","import React, {ReactNode} from 'react';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackButtonProps extends PaystackProps {\n text?: string;\n className?: string;\n children?: ReactNode;\n onSuccess?: callback;\n onClose?: callback;\n}\n\nconst PaystackButton = ({\n text,\n className,\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackButtonProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n initializePayment({config, onSuccess, onClose})}\n >\n {text || children}\n \n );\n};\n\nexport default PaystackButton;\n","import {createContext} from 'react';\nimport {InitializePayment, PaystackProps} from './types';\n\ntype IPaystackContext = {\n config: PaystackProps;\n initializePayment: InitializePayment;\n onSuccess: () => void;\n onClose: () => void;\n};\n\nconst PaystackContext = createContext({\n config: {} as PaystackProps,\n initializePayment: () => null,\n onSuccess: () => null,\n onClose: () => null,\n});\n\nexport default PaystackContext;\n","import React from 'react';\nimport PaystackContext from './paystack-context';\nimport usePaystackPayment from './use-paystack';\nimport {callback, PaystackProps} from './types';\n\ninterface PaystackProviderProps extends PaystackProps {\n children: JSX.Element;\n onSuccess: callback;\n onClose: callback;\n}\n\nconst PaystackProvider = ({\n children,\n onSuccess,\n onClose,\n ...config\n}: PaystackProviderProps): JSX.Element => {\n const initializePayment = usePaystackPayment(config);\n\n return (\n \n {children}\n \n );\n};\n\nexport default PaystackProvider;\n","import React, {forwardRef, useContext, FunctionComponentElement} from 'react';\nimport PaystackProvider from './paystack-provider';\nimport {PaystackProps} from './types';\nimport PaystackContext from './paystack-context';\n\ninterface PaystackConsumerProps extends PaystackProps {\n children: (arg: Record) => any;\n onSuccess?: () => void;\n onClose?: () => void;\n}\n\nconst PaystackConsumerChild = ({\n children,\n ref,\n}: {\n children: any;\n ref: any;\n}): FunctionComponentElement => {\n const {config, initializePayment, onSuccess, onClose} = useContext(PaystackContext);\n\n const completeInitializePayment = (): void => initializePayment({config, onSuccess, onClose});\n return children({initializePayment: completeInitializePayment, ref});\n};\n\n// eslint-disable-next-line react/display-name\nconst PaystackConsumer = forwardRef(\n (\n {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystackConsumerProps,\n ref: any,\n ): JSX.Element => {\n const onSuccess = paraSuccess ? paraSuccess : (): any => null;\n const onClose = paraClose ? paraClose : (): any => null;\n return (\n \n {children}\n \n );\n },\n);\n\nexport default PaystackConsumer;\n"],"names":["useState","useEffect","createContext","useContext","forwardRef"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAM,aAAa,GAA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EAAA,EAAK,MAAM,EAAA,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAA,CAAA;QAClE,KAAC,CAAA,aAAA,CAAA,qBAAqB,EAAC,EAAA,GAAG,EAAE,GAAG,IAAG,QAAQ,CAAyB,CAClD,EACnB;AACJ,CAAC;;;;;;"} \ No newline at end of file diff --git a/example/src/dist/paystack-button.d.ts b/example/src/dist/paystack-button.d.ts index a2d3b2a..ce4458f 100644 --- a/example/src/dist/paystack-button.d.ts +++ b/example/src/dist/paystack-button.d.ts @@ -7,5 +7,5 @@ interface PaystackButtonProps extends PaystackProps { onSuccess?: callback; onClose?: callback; } -declare const PaystackButton: ({ text, className, children, onSuccess, onClose, ...others }: PaystackButtonProps) => JSX.Element; +declare const PaystackButton: ({ text, className, children, onSuccess, onClose, ...config }: PaystackButtonProps) => JSX.Element; export default PaystackButton; diff --git a/example/src/dist/paystack-consumer.d.ts b/example/src/dist/paystack-consumer.d.ts index e80f377..b465966 100644 --- a/example/src/dist/paystack-consumer.d.ts +++ b/example/src/dist/paystack-consumer.d.ts @@ -1,9 +1,9 @@ import React from 'react'; import { PaystackProps } from './types'; -interface PaystacConsumerProps extends PaystackProps { +interface PaystackConsumerProps extends PaystackProps { children: (arg: Record) => any; onSuccess?: () => void; onClose?: () => void; } -declare const PaystackConsumer: React.ForwardRefExoticComponent>; +declare const PaystackConsumer: React.ForwardRefExoticComponent>; export default PaystackConsumer; diff --git a/example/src/dist/paystack-context.d.ts b/example/src/dist/paystack-context.d.ts index 7e92095..ddb3bc7 100644 --- a/example/src/dist/paystack-context.d.ts +++ b/example/src/dist/paystack-context.d.ts @@ -1,7 +1,8 @@ /// -import { callback } from './types'; +import { InitializePayment, PaystackProps } from './types'; type IPaystackContext = { - initializePayment: (arg0: callback, arg1: callback) => void; + config: PaystackProps; + initializePayment: InitializePayment; onSuccess: () => void; onClose: () => void; }; diff --git a/example/src/dist/paystack-provider.d.ts b/example/src/dist/paystack-provider.d.ts index a3a6ca7..f5ec1e8 100644 --- a/example/src/dist/paystack-provider.d.ts +++ b/example/src/dist/paystack-provider.d.ts @@ -1,8 +1,9 @@ +/// import { callback, PaystackProps } from './types'; interface PaystackProviderProps extends PaystackProps { children: JSX.Element; onSuccess: callback; onClose: callback; } -declare const PaystackProvider: ({ children, onSuccess, onClose, ...others }: PaystackProviderProps) => JSX.Element; +declare const PaystackProvider: ({ children, onSuccess, onClose, ...config }: PaystackProviderProps) => JSX.Element; export default PaystackProvider; diff --git a/example/src/dist/types.d.ts b/example/src/dist/types.d.ts index 2a33db1..700debf 100644 --- a/example/src/dist/types.d.ts +++ b/example/src/dist/types.d.ts @@ -17,10 +17,10 @@ export type callback = (response?: any) => void; export interface PaystackProps { publicKey: string; email: string; + amount: number; firstname?: string; lastname?: string; phone?: phone; - amount: number; reference?: string; metadata?: PaystackMetadata; currency?: Currency; @@ -35,4 +35,9 @@ export interface PaystackProps { split_code?: string; split?: Record; } +export type InitializePayment = (options: { + onSuccess?: callback; + onClose?: callback; + config?: PaystackProps; +}) => void; export {}; diff --git a/example/src/dist/use-paystack.d.ts b/example/src/dist/use-paystack.d.ts index 212173b..51688f4 100644 --- a/example/src/dist/use-paystack.d.ts +++ b/example/src/dist/use-paystack.d.ts @@ -1,2 +1,2 @@ -import { PaystackProps } from './types'; -export default function usePaystackPayment(options: PaystackProps): (callback?: () => void, onClose?: () => void) => void; +import { InitializePayment, PaystackProps } from './types'; +export default function usePaystackPayment(hookConfig: Partial): InitializePayment; diff --git a/libs/paystack-button.tsx b/libs/paystack-button.tsx index 8d66820..1764c96 100644 --- a/libs/paystack-button.tsx +++ b/libs/paystack-button.tsx @@ -16,11 +16,15 @@ const PaystackButton = ({ children, onSuccess, onClose, - ...others + ...config }: PaystackButtonProps): JSX.Element => { - const initializePayment = usePaystackPayment(others); + const initializePayment = usePaystackPayment(config); + return ( - ); diff --git a/libs/paystack-consumer.tsx b/libs/paystack-consumer.tsx index 280e3b7..e34200f 100644 --- a/libs/paystack-consumer.tsx +++ b/libs/paystack-consumer.tsx @@ -3,7 +3,7 @@ import PaystackProvider from './paystack-provider'; import {PaystackProps} from './types'; import PaystackContext from './paystack-context'; -interface PaystacConsumerProps extends PaystackProps { +interface PaystackConsumerProps extends PaystackProps { children: (arg: Record) => any; onSuccess?: () => void; onClose?: () => void; @@ -16,15 +16,16 @@ const PaystackConsumerChild = ({ children: any; ref: any; }): FunctionComponentElement => { - const {initializePayment, onSuccess, onClose} = useContext(PaystackContext); - const completeInitializePayment = (): void => initializePayment(onSuccess, onClose); + const {config, initializePayment, onSuccess, onClose} = useContext(PaystackContext); + + const completeInitializePayment = (): void => initializePayment({config, onSuccess, onClose}); return children({initializePayment: completeInitializePayment, ref}); }; // eslint-disable-next-line react/display-name const PaystackConsumer = forwardRef( ( - {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystacConsumerProps, + {children, onSuccess: paraSuccess, onClose: paraClose, ...others}: PaystackConsumerProps, ref: any, ): JSX.Element => { const onSuccess = paraSuccess ? paraSuccess : (): any => null; diff --git a/libs/paystack-context.ts b/libs/paystack-context.ts index 4bba054..93a2e02 100644 --- a/libs/paystack-context.ts +++ b/libs/paystack-context.ts @@ -1,12 +1,15 @@ import {createContext} from 'react'; -import {callback} from './types'; +import {InitializePayment, PaystackProps} from './types'; + type IPaystackContext = { - initializePayment: (arg0: callback, arg1: callback) => void; + config: PaystackProps; + initializePayment: InitializePayment; onSuccess: () => void; onClose: () => void; }; const PaystackContext = createContext({ + config: {} as PaystackProps, initializePayment: () => null, onSuccess: () => null, onClose: () => null, diff --git a/libs/paystack-provider.tsx b/libs/paystack-provider.tsx index 8ee4488..98398cd 100644 --- a/libs/paystack-provider.tsx +++ b/libs/paystack-provider.tsx @@ -13,11 +13,12 @@ const PaystackProvider = ({ children, onSuccess, onClose, - ...others + ...config }: PaystackProviderProps): JSX.Element => { - const initializePayment = usePaystackPayment(others); + const initializePayment = usePaystackPayment(config); + return ( - + {children} ); diff --git a/libs/test/use-paystack.test.tsx b/libs/test/use-paystack.test.tsx index d46fe0f..518366d 100644 --- a/libs/test/use-paystack.test.tsx +++ b/libs/test/use-paystack.test.tsx @@ -28,7 +28,7 @@ describe('usePaystackPayment()', () => { const onSuccess = jest.fn(); const onClose = jest.fn(); act(() => { - result.current(onSuccess, onClose); + result.current({onSuccess, onClose}); }); expect(onSuccess).toHaveBeenCalledTimes(0); @@ -41,7 +41,7 @@ describe('usePaystackPayment()', () => { rerender(); act(() => { - result.current(); + result.current({}); }); expect(callPaystackPop).toHaveBeenCalledTimes(1); @@ -89,7 +89,7 @@ describe('usePaystackPayment()', () => { ); rerender(); act(() => { - result.current(); + result.current({}); }); expect(callPaystackPop).toHaveBeenCalledTimes(1); @@ -100,7 +100,7 @@ describe('usePaystackPayment()', () => { rerender(); const Btn = (): any => (
- {' '} + {' '}
); @@ -120,7 +120,7 @@ describe('usePaystackPayment()', () => { rerender(); act(() => { - result.current(); + result.current({}); }); // @ts-ignore diff --git a/libs/types.ts b/libs/types.ts index ee2162a..cb033ef 100644 --- a/libs/types.ts +++ b/libs/types.ts @@ -33,10 +33,10 @@ export type callback = (response?: any) => void; export interface PaystackProps { publicKey: string; email: string; + amount: number; firstname?: string; lastname?: string; phone?: phone; - amount: number; reference?: string; metadata?: PaystackMetadata; currency?: Currency; @@ -51,3 +51,12 @@ export interface PaystackProps { split_code?: string; split?: Record; } + +export type InitializePayment = (options: { + onSuccess?: callback; + onClose?: callback; + config?: Omit; +}) => void; + +export type HookConfig = Omit, 'publicKey'> & + Pick; diff --git a/libs/use-paystack.ts b/libs/use-paystack.ts index 7278bc1..c0961ae 100644 --- a/libs/use-paystack.ts +++ b/libs/use-paystack.ts @@ -1,41 +1,42 @@ import {useEffect} from 'react'; -import {callback, PaystackProps} from './types'; +import {HookConfig, InitializePayment} from './types'; import usePaystackScript from './paystack-script'; import {callPaystackPop} from './paystack-actions'; -export default function usePaystackPayment( - options: PaystackProps, -): (callback?: () => void, onClose?: () => void) => void { +export default function usePaystackPayment(hookConfig: HookConfig): InitializePayment { const [scriptLoaded, scriptError] = usePaystackScript(); - const { - publicKey, - firstname, - lastname, - phone, - email, - amount, - reference, - metadata = {}, - currency = 'NGN', - channels, - label = '', - plan = '', - quantity = '', - subaccount = '', - transaction_charge = 0, - bearer = 'account', - split, - split_code, - } = options; - function initializePayment(callback?: callback, onClose?: callback): void { + function initializePayment({config, onSuccess, onClose}: Parameters[0]): void { if (scriptError) { throw new Error('Unable to load paystack inline script'); } + const args = {...hookConfig, ...config}; + + const { + publicKey, + firstname, + lastname, + phone, + email, + amount, + reference, + metadata = {}, + currency = 'NGN', + channels, + label = '', + plan = '', + quantity = '', + subaccount = '', + transaction_charge = 0, + bearer = 'account', + split, + split_code, + } = args; + if (scriptLoaded) { const paystackArgs: Record = { - callback: callback ? callback : () => null, + callback: onSuccess ? onSuccess : () => null, onClose: onClose ? onClose : () => null, key: publicKey, ref: reference, @@ -47,7 +48,6 @@ export default function usePaystackPayment( currency, plan, quantity, - 'data-custom-button': options['data-custom-button'] || '', channels, subaccount, transaction_charge, @@ -56,6 +56,7 @@ export default function usePaystackPayment( metadata, split, split_code, + 'data-custom-button': args['data-custom-button'] || '', }; callPaystackPop(paystackArgs); }