From 93c1740f5a1356ff7ec2b1b2a167f8a686d49da2 Mon Sep 17 00:00:00 2001 From: Olusegun Ayeni Date: Sun, 5 May 2024 19:57:10 -0700 Subject: [PATCH] fix(paystack-connect): upgrade inline to v2, improve on inline solution and fix connect solution --- dist/index.es.js | 134 +++---- dist/index.es.js.map | 2 +- dist/index.js | 132 +++---- dist/index.js.map | 2 +- dist/paystack-actions.d.ts | 2 +- example/src/dist/index.es.js | 371 ++++++------------ example/src/dist/index.es.js.map | 2 +- example/src/dist/index.js | 367 ++++++----------- example/src/dist/index.js.map | 2 +- example/src/dist/paystack-actions.d.ts | 2 +- example/src/dist/types.d.ts | 17 +- example/src/dist/use-paystack.d.ts | 4 +- libs/paystack-actions.ts | 10 +- libs/paystack-script.ts | 62 --- libs/src/types/@paystack/inline-js/index.d.ts | 2 + libs/test/paystack-button.test.tsx | 2 - libs/test/paystack-provider.test.tsx | 5 +- libs/test/paystack-script.test.ts | 28 -- libs/test/use-paystack.test.tsx | 3 - libs/use-paystack.ts | 88 +++-- package.json | 6 +- 21 files changed, 397 insertions(+), 846 deletions(-) delete mode 100644 libs/paystack-script.ts create mode 100644 libs/src/types/@paystack/inline-js/index.d.ts delete mode 100644 libs/test/paystack-script.test.ts diff --git a/dist/index.es.js b/dist/index.es.js index efa2bfa..1c9ce66 100644 --- a/dist/index.es.js +++ b/dist/index.es.js @@ -1,4 +1,5 @@ -import React, { useState, useEffect, createContext, forwardRef, useContext } from 'react'; +import PaystackPop from '@paystack/inline-js'; +import React, { createContext, forwardRef, useContext } from 'react'; /****************************************************************************** Copyright (c) Microsoft Corporation. @@ -45,104 +46,59 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; -var cachedScripts = []; -function usePaystackScript() { - var src = 'https://js.paystack.co/v1/inline.js'; - var _a = useState({ - loaded: false, - error: false, - }), state = _a[0], setState = _a[1]; - useEffect(function () { - if (cachedScripts.includes(src)) { - setState({ - loaded: true, - error: false, - }); - } - else { - cachedScripts.push(src); - var script_1 = document.createElement('script'); - script_1.src = src; - script_1.async = true; - var onScriptLoad_1 = function () { - setState({ - loaded: true, - error: false, - }); - }; - var onScriptError_1 = function () { - var index = cachedScripts.indexOf(src); - if (index >= 0) - cachedScripts.splice(index, 1); - script_1.remove(); - setState({ - loaded: true, - error: true, - }); - }; - script_1.addEventListener('load', onScriptLoad_1); - script_1.addEventListener('complete', onScriptLoad_1); - script_1.addEventListener('error', onScriptError_1); - document.body.appendChild(script_1); - return function () { - script_1.removeEventListener('load', onScriptLoad_1); - script_1.removeEventListener('error', onScriptError_1); - }; - } - }, [src]); - return [state.loaded, state.error]; -} - -/* eslint-disable */ var callPaystackPop = function (paystackArgs) { - // @ts-ignore - var handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs); - handler && handler.openIframe(); + var paystack = new PaystackPop(); + paystack.newTransaction(paystackArgs); }; function usePaystackPayment(hookConfig) { - var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; 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, connect_account = args.connect_account, connect_split = args.connect_split; - if (scriptLoaded) { - var paystackArgs = { - callback: onSuccess ? onSuccess : function () { return null; }, - onClose: onClose ? onClose : function () { return null; }, - key: publicKey, - ref: reference, - email: email, - firstname: firstname, - lastname: lastname, - phone: phone, - amount: amount, - currency: currency, - plan: plan, - quantity: quantity, - channels: channels, - subaccount: subaccount, - transaction_charge: transaction_charge, - bearer: bearer, - label: label, - metadata: metadata, - split: split, - split_code: split_code, - connect_split: connect_split, - connect_account: connect_account, - 'data-custom-button': args['data-custom-button'] || '', - }; - callPaystackPop(paystackArgs); + var paystackArgs = { + onSuccess: onSuccess ? onSuccess : function () { return null; }, + onCancel: onClose ? onClose : function () { return null; }, + key: publicKey, + ref: reference, + email: email, + firstname: firstname, + lastname: lastname, + amount: amount, + currency: currency, + plan: plan, + subaccount: subaccount, + transaction_charge: transaction_charge, + bearer: bearer, + label: label, + metadata: metadata, + }; + if (phone) { + paystackArgs.phone = phone; } - } - useEffect(function () { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); + if (quantity) { + paystackArgs.quantity = quantity; + } + if (channels) { + paystackArgs.channels = channels; } - }, [scriptError]); + if (split) { + paystackArgs.split = split; + } + if (split_code) { + paystackArgs.split_code = split_code; + } + if (connect_split) { + paystackArgs.connect_split = connect_split; + } + if (connect_account) { + paystackArgs.connect_account = connect_account; + } + if (args['data-custom-button']) { + paystackArgs['data-custom-button'] = args['data-custom-button']; + } + callPaystackPop(paystackArgs); + } return initializePayment; } diff --git a/dist/index.es.js.map b/dist/index.es.js.map index 6eecbb3..31709dd 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 {HookConfig, InitializePayment} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): 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 connect_account,\n connect_split,\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 connect_split,\n connect_account,\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,MAAM,EAAE,IAAI;AACZ,gBAAA,KAAK,EAAE,KAAK;AACb,aAAA,CAAC,CAAC;SACJ;aAAM;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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,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 +{"version":3,"file":"index.es.js","sources":["../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 PaystackPop from '@paystack/inline-js';\n\nexport const callPaystackPop = (paystackArgs: Record): void => {\n const paystack = new PaystackPop();\n paystack.newTransaction(paystackArgs);\n};\n","import {HookConfig, InitializePayment} from './types';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): InitializePayment {\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\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 connect_account,\n connect_split,\n } = args;\n const paystackArgs: Record = {\n onSuccess: onSuccess ? onSuccess : () => null,\n onCancel: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n amount,\n currency,\n plan,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n };\n\n if (phone) {\n paystackArgs.phone = phone;\n }\n\n if (quantity) {\n paystackArgs.quantity = quantity;\n }\n\n if (channels) {\n paystackArgs.channels = channels;\n }\n\n if (split) {\n paystackArgs.split = split;\n }\n\n if (split_code) {\n paystackArgs.split_code = split_code;\n }\n\n if (connect_split) {\n paystackArgs.connect_split = connect_split;\n }\n\n if (connect_account) {\n paystackArgs.connect_account = connect_account;\n }\n\n if (args['data-custom-button']) {\n paystackArgs['data-custom-button'] = args['data-custom-button'];\n }\n\n callPaystackPop(paystackArgs);\n }\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,eAAe,GAAG,UAAC,YAAiC,EAAA;AAC/D,IAAA,IAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC;AACnC,IAAA,QAAQ,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC;;ACFuB,SAAA,kBA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,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/dist/index.js b/dist/index.js index 1504f0f..b0738dc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,6 @@ 'use strict'; +var PaystackPop = require('@paystack/inline-js'); var React = require('react'); /****************************************************************************** @@ -47,104 +48,59 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; -var cachedScripts = []; -function usePaystackScript() { - var src = 'https://js.paystack.co/v1/inline.js'; - var _a = React.useState({ - loaded: false, - error: false, - }), state = _a[0], setState = _a[1]; - React.useEffect(function () { - if (cachedScripts.includes(src)) { - setState({ - loaded: true, - error: false, - }); - } - else { - cachedScripts.push(src); - var script_1 = document.createElement('script'); - script_1.src = src; - script_1.async = true; - var onScriptLoad_1 = function () { - setState({ - loaded: true, - error: false, - }); - }; - var onScriptError_1 = function () { - var index = cachedScripts.indexOf(src); - if (index >= 0) - cachedScripts.splice(index, 1); - script_1.remove(); - setState({ - loaded: true, - error: true, - }); - }; - script_1.addEventListener('load', onScriptLoad_1); - script_1.addEventListener('complete', onScriptLoad_1); - script_1.addEventListener('error', onScriptError_1); - document.body.appendChild(script_1); - return function () { - script_1.removeEventListener('load', onScriptLoad_1); - script_1.removeEventListener('error', onScriptError_1); - }; - } - }, [src]); - return [state.loaded, state.error]; -} - -/* eslint-disable */ var callPaystackPop = function (paystackArgs) { - // @ts-ignore - var handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs); - handler && handler.openIframe(); + var paystack = new PaystackPop(); + paystack.newTransaction(paystackArgs); }; function usePaystackPayment(hookConfig) { - var _a = usePaystackScript(), scriptLoaded = _a[0], scriptError = _a[1]; 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, connect_account = args.connect_account, connect_split = args.connect_split; - if (scriptLoaded) { - var paystackArgs = { - callback: onSuccess ? onSuccess : function () { return null; }, - onClose: onClose ? onClose : function () { return null; }, - key: publicKey, - ref: reference, - email: email, - firstname: firstname, - lastname: lastname, - phone: phone, - amount: amount, - currency: currency, - plan: plan, - quantity: quantity, - channels: channels, - subaccount: subaccount, - transaction_charge: transaction_charge, - bearer: bearer, - label: label, - metadata: metadata, - split: split, - split_code: split_code, - connect_split: connect_split, - connect_account: connect_account, - 'data-custom-button': args['data-custom-button'] || '', - }; - callPaystackPop(paystackArgs); + var paystackArgs = { + onSuccess: onSuccess ? onSuccess : function () { return null; }, + onCancel: onClose ? onClose : function () { return null; }, + key: publicKey, + ref: reference, + email: email, + firstname: firstname, + lastname: lastname, + amount: amount, + currency: currency, + plan: plan, + subaccount: subaccount, + transaction_charge: transaction_charge, + bearer: bearer, + label: label, + metadata: metadata, + }; + if (phone) { + paystackArgs.phone = phone; } - } - React.useEffect(function () { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); + if (quantity) { + paystackArgs.quantity = quantity; + } + if (channels) { + paystackArgs.channels = channels; } - }, [scriptError]); + if (split) { + paystackArgs.split = split; + } + if (split_code) { + paystackArgs.split_code = split_code; + } + if (connect_split) { + paystackArgs.connect_split = connect_split; + } + if (connect_account) { + paystackArgs.connect_account = connect_account; + } + if (args['data-custom-button']) { + paystackArgs['data-custom-button'] = args['data-custom-button']; + } + callPaystackPop(paystackArgs); + } return initializePayment; } diff --git a/dist/index.js.map b/dist/index.js.map index b5e2b89..04838df 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 {HookConfig, InitializePayment} from './types';\nimport usePaystackScript from './paystack-script';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): 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 connect_account,\n connect_split,\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 connect_split,\n connect_account,\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,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;SACJ;aAAM;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,KAA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A,IAAM,gBAAgB,GAAGC,gBAAU,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 +{"version":3,"file":"index.js","sources":["../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 PaystackPop from '@paystack/inline-js';\n\nexport const callPaystackPop = (paystackArgs: Record): void => {\n const paystack = new PaystackPop();\n paystack.newTransaction(paystackArgs);\n};\n","import {HookConfig, InitializePayment} from './types';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): InitializePayment {\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\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 connect_account,\n connect_split,\n } = args;\n const paystackArgs: Record = {\n onSuccess: onSuccess ? onSuccess : () => null,\n onCancel: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n amount,\n currency,\n plan,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n };\n\n if (phone) {\n paystackArgs.phone = phone;\n }\n\n if (quantity) {\n paystackArgs.quantity = quantity;\n }\n\n if (channels) {\n paystackArgs.channels = channels;\n }\n\n if (split) {\n paystackArgs.split = split;\n }\n\n if (split_code) {\n paystackArgs.split_code = split_code;\n }\n\n if (connect_split) {\n paystackArgs.connect_split = connect_split;\n }\n\n if (connect_account) {\n paystackArgs.connect_account = connect_account;\n }\n\n if (args['data-custom-button']) {\n paystackArgs['data-custom-button'] = args['data-custom-button'];\n }\n\n callPaystackPop(paystackArgs);\n }\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":["createContext","useContext","forwardRef"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,eAAe,GAAG,UAAC,YAAiC,EAAA;AAC/D,IAAA,IAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC;AACnC,IAAA,QAAQ,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC;;ACFuB,SAAA,kBAAkB,CAAC,UAAsB,EAAA;IAC/D,SAAS,iBAAiB,CAAC,EAA8D,EAAA;AAA7D,QAAA,IAAA,MAAM,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AA,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/dist/paystack-actions.d.ts b/dist/paystack-actions.d.ts index 8c618f0..0fe1c23 100644 --- a/dist/paystack-actions.d.ts +++ b/dist/paystack-actions.d.ts @@ -1 +1 @@ -export declare let callPaystackPop: (paystackArgs: Record) => void; +export declare const callPaystackPop: (paystackArgs: Record) => void; diff --git a/example/src/dist/index.es.js b/example/src/dist/index.es.js index 82817ea..4c0774e 100644 --- a/example/src/dist/index.es.js +++ b/example/src/dist/index.es.js @@ -1,282 +1,141 @@ -import React, {useState, useEffect, createContext, forwardRef, useContext} from 'react'; +import PaystackPop from '@paystack/inline-js'; +import React, { 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); +/****************************************************************************** +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; }; -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'; - var _a = useState({ - loaded: false, - error: false, - }), - state = _a[0], - setState = _a[1]; - useEffect( - function () { - if (cachedScripts.includes(src)) { - setState({ - loaded: true, - error: false, - }); - } else { - cachedScripts.push(src); - var script_1 = document.createElement('script'); - script_1.src = src; - script_1.async = true; - var onScriptLoad_1 = function () { - setState({ - loaded: true, - error: false, - }); - }; - var onScriptError_1 = function () { - var index = cachedScripts.indexOf(src); - if (index >= 0) cachedScripts.splice(index, 1); - script_1.remove(); - setState({ - loaded: true, - error: true, - }); - }; - script_1.addEventListener('load', onScriptLoad_1); - script_1.addEventListener('complete', onScriptLoad_1); - script_1.addEventListener('error', onScriptError_1); - document.body.appendChild(script_1); - return function () { - script_1.removeEventListener('load', onScriptLoad_1); - script_1.removeEventListener('error', onScriptError_1); - }; - } - }, - [src], - ); - return [state.loaded, state.error]; -} - -/* eslint-disable */ var callPaystackPop = function (paystackArgs) { - // @ts-ignore - var handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs); - handler && handler.openIframe(); + console.log('paystackArgs', paystackArgs); + var paystack = new PaystackPop(); + paystack.newTransaction(paystackArgs); }; function usePaystackPayment(hookConfig) { - var _a = usePaystackScript(), - scriptLoaded = _a[0], - scriptError = _a[1]; - 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: onSuccess - ? onSuccess - : function () { - return null; - }, - onClose: onClose - ? onClose - : function () { - return null; - }, - key: publicKey, - ref: reference, - email: email, - firstname: firstname, - lastname: lastname, - phone: phone, - amount: amount, - currency: currency, - plan: plan, - quantity: quantity, - channels: channels, - subaccount: subaccount, - transaction_charge: transaction_charge, - bearer: bearer, - label: label, - metadata: metadata, - split: split, - split_code: split_code, - 'data-custom-button': args['data-custom-button'] || '', - }; - callPaystackPop(paystackArgs); + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; + 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, connect_account = args.connect_account, connect_split = args.connect_split; + var paystackArgs = { + onSuccess: onSuccess ? onSuccess : function () { return null; }, + onCancel: onClose ? onClose : function () { return null; }, + key: publicKey, + ref: reference, + email: email, + firstname: firstname, + lastname: lastname, + amount: amount, + currency: currency, + plan: plan, + subaccount: subaccount, + transaction_charge: transaction_charge, + bearer: bearer, + label: label, + metadata: metadata, + }; + if (phone) { + paystackArgs.phone = phone; + } + if (quantity) { + paystackArgs.quantity = quantity; + } + if (channels) { + paystackArgs.channels = channels; + } + if (split) { + paystackArgs.split = split; + } + if (split_code) { + paystackArgs.split_code = split_code; + } + if (connect_split) { + paystackArgs.connect_split = connect_split; + } + if (connect_account) { + paystackArgs.connect_account = connect_account; + } + if (args['data-custom-button']) { + paystackArgs['data-custom-button'] = args['data-custom-button']; + } + callPaystackPop(paystackArgs); } - } - useEffect( - function () { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); - } - }, - [scriptError], - ); - return initializePayment; + return initializePayment; } var PaystackButton = function (_a) { - 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 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; - }, + 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, - 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 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), - 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}); + var children = _a.children, ref = _a.ref; + 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 var PaystackConsumer = forwardRef(function (_a, ref) { - var children = _a.children, - paraSuccess = _a.onSuccess, - paraClose = _a.onClose, - others = __rest(_a, ['children', 'onSuccess', 'onClose']); - var onSuccess = paraSuccess - ? paraSuccess - : function () { - return null; - }; - var onClose = paraClose - ? paraClose - : function () { - return null; - }; - return React.createElement( - PaystackProvider, - __assign({}, others, {onSuccess: onSuccess, onClose: onClose}), - React.createElement(PaystackConsumerChild, {ref: ref}, children), - ); + var children = _a.children, paraSuccess = _a.onSuccess, paraClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); + var onSuccess = paraSuccess ? paraSuccess : function () { return null; }; + var onClose = paraClose ? paraClose : function () { return null; }; + return (React.createElement(PaystackProvider, __assign({}, others, { onSuccess: onSuccess, onClose: onClose }), + React.createElement(PaystackConsumerChild, { ref: ref }, children))); }); -export {PaystackButton, PaystackConsumer, usePaystackPayment}; +export { PaystackButton, PaystackConsumer, usePaystackPayment }; //# sourceMappingURL=index.es.js.map diff --git a/example/src/dist/index.es.js.map b/example/src/dist/index.es.js.map index 2920845..9cec202 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 {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,MAAM,EAAE,IAAI;AACZ,gBAAA,KAAK,EAAE,KAAK;AACb,aAAA,CAAC,CAAC;SACJ;aAAM;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,oBAAA,aAAa,CAAC,MAAM,CA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,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.es.js","sources":["../../../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 PaystackPop from '@paystack/inline-js';\n\nexport const callPaystackPop = (paystackArgs: Record): void => {\n console.log('paystackArgs', paystackArgs)\n const paystack = new PaystackPop();\n paystack.newTransaction(paystackArgs);\n};\n","import {HookConfig, InitializePayment} from './types';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): InitializePayment {\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\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 connect_account,\n connect_split,\n } = args;\n const paystackArgs: Record = {\n onSuccess: onSuccess ? onSuccess : () => null,\n onCancel: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n amount,\n currency,\n plan,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n };\n\n if (phone) {\n paystackArgs.phone = phone;\n }\n\n if (quantity) {\n paystackArgs.quantity = quantity;\n }\n\n if (channels) {\n paystackArgs.channels = channels;\n }\n\n if (split) {\n paystackArgs.split = split;\n }\n\n if (split_code) {\n paystackArgs.split_code = split_code;\n }\n\n if (connect_split) {\n paystackArgs.connect_split = connect_split;\n }\n\n if (connect_account) {\n paystackArgs.connect_account = connect_account;\n }\n\n if (args['data-custom-button']) {\n paystackArgs['data-custom-button'] = args['data-custom-button'];\n }\n\n callPaystackPop(paystackArgs);\n }\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,eAAe,GAAG,UAAC,YAAiC,EAAA;AAC/D,IAAA,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,YAAY,CAAC,CAAA;AACzC,IAAA,IAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC;AACnC,IAAA,QAAQ,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC;;ACHuB,SAAA,kBAAkB,CAAC,UAAsB,EAAA;IAC/D,SAAS,iBAAiB,CAAC,EAA8D,EAAA;AAA7D,QAAA,IAAA,MAAM,YAAA,EAAE,SAAS,GAAA,EAAA,CAAA,SAAA,EAAE,OAAO,GAAA,EAAA,CAAA,OAAA,CAAA;AACpD,QAAA,IAAM,IAAI,GAAO,QAAA,CAAA,QAAA,CAAA,EAAA,EAAA,UAAU,CAAK,EAAA,MAAM,CAAC,CAAC;AAGtC,QAAA,IAAA,SAAS,GAoBP,IAAI,CAAA,SApBG,EACT,SAAS,GAmBP,IAAI,CAnBG,SAAA,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AG,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 3a47f46..d33ba60 100644 --- a/example/src/dist/index.js +++ b/example/src/dist/index.js @@ -1,283 +1,142 @@ 'use strict'; +var PaystackPop = require('@paystack/inline-js'); 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); +/****************************************************************************** +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; }; -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'; - var _a = React.useState({ - loaded: false, - error: false, - }), - state = _a[0], - setState = _a[1]; - React.useEffect( - function () { - if (cachedScripts.includes(src)) { - setState({ - loaded: true, - error: false, - }); - } else { - cachedScripts.push(src); - var script_1 = document.createElement('script'); - script_1.src = src; - script_1.async = true; - var onScriptLoad_1 = function () { - setState({ - loaded: true, - error: false, - }); - }; - var onScriptError_1 = function () { - var index = cachedScripts.indexOf(src); - if (index >= 0) cachedScripts.splice(index, 1); - script_1.remove(); - setState({ - loaded: true, - error: true, - }); - }; - script_1.addEventListener('load', onScriptLoad_1); - script_1.addEventListener('complete', onScriptLoad_1); - script_1.addEventListener('error', onScriptError_1); - document.body.appendChild(script_1); - return function () { - script_1.removeEventListener('load', onScriptLoad_1); - script_1.removeEventListener('error', onScriptError_1); - }; - } - }, - [src], - ); - return [state.loaded, state.error]; -} - -/* eslint-disable */ var callPaystackPop = function (paystackArgs) { - // @ts-ignore - var handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs); - handler && handler.openIframe(); + console.log('paystackArgs', paystackArgs); + var paystack = new PaystackPop(); + paystack.newTransaction(paystackArgs); }; function usePaystackPayment(hookConfig) { - var _a = usePaystackScript(), - scriptLoaded = _a[0], - scriptError = _a[1]; - 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: onSuccess - ? onSuccess - : function () { - return null; - }, - onClose: onClose - ? onClose - : function () { - return null; - }, - key: publicKey, - ref: reference, - email: email, - firstname: firstname, - lastname: lastname, - phone: phone, - amount: amount, - currency: currency, - plan: plan, - quantity: quantity, - channels: channels, - subaccount: subaccount, - transaction_charge: transaction_charge, - bearer: bearer, - label: label, - metadata: metadata, - split: split, - split_code: split_code, - 'data-custom-button': args['data-custom-button'] || '', - }; - callPaystackPop(paystackArgs); + function initializePayment(_a) { + var config = _a.config, onSuccess = _a.onSuccess, onClose = _a.onClose; + 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, connect_account = args.connect_account, connect_split = args.connect_split; + var paystackArgs = { + onSuccess: onSuccess ? onSuccess : function () { return null; }, + onCancel: onClose ? onClose : function () { return null; }, + key: publicKey, + ref: reference, + email: email, + firstname: firstname, + lastname: lastname, + amount: amount, + currency: currency, + plan: plan, + subaccount: subaccount, + transaction_charge: transaction_charge, + bearer: bearer, + label: label, + metadata: metadata, + }; + if (phone) { + paystackArgs.phone = phone; + } + if (quantity) { + paystackArgs.quantity = quantity; + } + if (channels) { + paystackArgs.channels = channels; + } + if (split) { + paystackArgs.split = split; + } + if (split_code) { + paystackArgs.split_code = split_code; + } + if (connect_split) { + paystackArgs.connect_split = connect_split; + } + if (connect_account) { + paystackArgs.connect_account = connect_account; + } + if (args['data-custom-button']) { + paystackArgs['data-custom-button'] = args['data-custom-button']; + } + callPaystackPop(paystackArgs); } - } - React.useEffect( - function () { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); - } - }, - [scriptError], - ); - return initializePayment; + return initializePayment; } var PaystackButton = function (_a) { - 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 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; - }, + 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, - 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 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), - 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}); + var children = _a.children, ref = _a.ref; + 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 var PaystackConsumer = React.forwardRef(function (_a, ref) { - var children = _a.children, - paraSuccess = _a.onSuccess, - paraClose = _a.onClose, - others = __rest(_a, ['children', 'onSuccess', 'onClose']); - var onSuccess = paraSuccess - ? paraSuccess - : function () { - return null; - }; - var onClose = paraClose - ? paraClose - : function () { - return null; - }; - return React.createElement( - PaystackProvider, - __assign({}, others, {onSuccess: onSuccess, onClose: onClose}), - React.createElement(PaystackConsumerChild, {ref: ref}, children), - ); + var children = _a.children, paraSuccess = _a.onSuccess, paraClose = _a.onClose, others = __rest(_a, ["children", "onSuccess", "onClose"]); + var onSuccess = paraSuccess ? paraSuccess : function () { return null; }; + var onClose = paraClose ? paraClose : function () { return null; }; + return (React.createElement(PaystackProvider, __assign({}, others, { onSuccess: onSuccess, onClose: onClose }), + React.createElement(PaystackConsumerChild, { ref: ref }, children))); }); exports.PaystackButton = PaystackButton; diff --git a/example/src/dist/index.js.map b/example/src/dist/index.js.map index 608e889..577cb6b 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 {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,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,Y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AE,OAAO,aAA+B,CAAC;AAEpF,IAAA,IAAM,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,GAAGC,gBAAU,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 +{"version":3,"file":"index.js","sources":["../../../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 PaystackPop from '@paystack/inline-js';\n\nexport const callPaystackPop = (paystackArgs: Record): void => {\n console.log('paystackArgs', paystackArgs)\n const paystack = new PaystackPop();\n paystack.newTransaction(paystackArgs);\n};\n","import {HookConfig, InitializePayment} from './types';\nimport {callPaystackPop} from './paystack-actions';\n\nexport default function usePaystackPayment(hookConfig: HookConfig): InitializePayment {\n function initializePayment({config, onSuccess, onClose}: Parameters[0]): void {\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 connect_account,\n connect_split,\n } = args;\n const paystackArgs: Record = {\n onSuccess: onSuccess ? onSuccess : () => null,\n onCancel: onClose ? onClose : () => null,\n key: publicKey,\n ref: reference,\n email,\n firstname,\n lastname,\n amount,\n currency,\n plan,\n subaccount,\n transaction_charge,\n bearer,\n label,\n metadata,\n };\n\n if (phone) {\n paystackArgs.phone = phone;\n }\n\n if (quantity) {\n paystackArgs.quantity = quantity;\n }\n\n if (channels) {\n paystackArgs.channels = channels;\n }\n\n if (split) {\n paystackArgs.split = split;\n }\n\n if (split_code) {\n paystackArgs.split_code = split_code;\n }\n\n if (connect_split) {\n paystackArgs.connect_split = connect_split;\n }\n\n if (connect_account) {\n paystackArgs.connect_account = connect_account;\n }\n\n if (args['data-custom-button']) {\n paystackArgs['data-custom-button'] = args['data-custom-button'];\n }\n\n callPaystackPop(paystackArgs);\n }\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":["createContext","use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AM,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,GAAGC,gBAAU,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/paystack-actions.d.ts b/example/src/dist/paystack-actions.d.ts index 8c618f0..0fe1c23 100644 --- a/example/src/dist/paystack-actions.d.ts +++ b/example/src/dist/paystack-actions.d.ts @@ -1 +1 @@ -export declare let callPaystackPop: (paystackArgs: Record) => void; +export declare const callPaystackPop: (paystackArgs: Record) => void; diff --git a/example/src/dist/types.d.ts b/example/src/dist/types.d.ts index 700debf..20ded01 100644 --- a/example/src/dist/types.d.ts +++ b/example/src/dist/types.d.ts @@ -1,5 +1,5 @@ -type Currency = 'NGN' | 'GHS' | 'USD' | 'ZAR' | 'KES' | 'XOF'; -type PaymentChannels = 'bank' | 'card' | 'qr' | 'ussd' | 'mobile_money' | 'eft' | 'bank_transfer' | 'payattitude'; +export type Currency = 'NGN' | 'GHS' | 'USD' | 'ZAR' | 'KES' | 'XOF'; +export type PaymentChannels = 'bank' | 'card' | 'qr' | 'ussd' | 'mobile_money' | 'eft' | 'bank_transfer' | 'payattitude'; type Bearer = 'account' | 'subaccount'; type phone = number | string; interface PaystackCustomFields { @@ -13,6 +13,10 @@ interface PaystackMetadata { interface PaystackMetadata { [key: string]: any; } +interface PaystackConnectSplit { + account_id: string; + share: number; +} export type callback = (response?: any) => void; export interface PaystackProps { publicKey: string; @@ -23,8 +27,8 @@ export interface PaystackProps { phone?: phone; reference?: string; metadata?: PaystackMetadata; - currency?: Currency; - channels?: PaymentChannels[]; + currency?: Currency | string; + channels?: PaymentChannels[] | string[]; label?: string; plan?: string; quantity?: number; @@ -34,10 +38,13 @@ export interface PaystackProps { 'data-custom-button'?: string; split_code?: string; split?: Record; + connect_split?: PaystackConnectSplit[]; + connect_account?: string; } export type InitializePayment = (options: { onSuccess?: callback; onClose?: callback; - config?: PaystackProps; + config?: Omit; }) => void; +export type HookConfig = Omit, 'publicKey'> & Pick; export {}; diff --git a/example/src/dist/use-paystack.d.ts b/example/src/dist/use-paystack.d.ts index 51688f4..19ba7b0 100644 --- a/example/src/dist/use-paystack.d.ts +++ b/example/src/dist/use-paystack.d.ts @@ -1,2 +1,2 @@ -import { InitializePayment, PaystackProps } from './types'; -export default function usePaystackPayment(hookConfig: Partial): InitializePayment; +import { HookConfig, InitializePayment } from './types'; +export default function usePaystackPayment(hookConfig: HookConfig): InitializePayment; diff --git a/libs/paystack-actions.ts b/libs/paystack-actions.ts index 1d610c9..ef26f41 100644 --- a/libs/paystack-actions.ts +++ b/libs/paystack-actions.ts @@ -1,6 +1,6 @@ -/* eslint-disable */ -export let callPaystackPop = (paystackArgs: Record): void => { - // @ts-ignore - const handler = window.PaystackPop && window.PaystackPop.setup(paystackArgs); - handler && handler.openIframe(); +import PaystackPop from '@paystack/inline-js'; + +export const callPaystackPop = (paystackArgs: Record): void => { + const paystack = new PaystackPop(); + paystack.newTransaction(paystackArgs); }; diff --git a/libs/paystack-script.ts b/libs/paystack-script.ts deleted file mode 100644 index 80c30be..0000000 --- a/libs/paystack-script.ts +++ /dev/null @@ -1,62 +0,0 @@ -import {useState, useEffect} from 'react'; - -const cachedScripts: string[] = []; -interface IScriptResult { - loaded: boolean; - error: boolean; -} - -export default function usePaystackScript(): boolean[] { - const src = 'https://js.paystack.co/v1/inline.js'; - - const [state, setState] = useState({ - loaded: false, - error: false, - }); - - useEffect((): any => { - if (cachedScripts.includes(src)) { - setState({ - loaded: true, - error: false, - }); - } else { - cachedScripts.push(src); - - const script = document.createElement('script'); - script.src = src; - script.async = true; - - const onScriptLoad = (): void => { - setState({ - loaded: true, - error: false, - }); - }; - - const onScriptError = (): void => { - const index = cachedScripts.indexOf(src); - if (index >= 0) cachedScripts.splice(index, 1); - script.remove(); - - setState({ - loaded: true, - error: true, - }); - }; - - script.addEventListener('load', onScriptLoad); - script.addEventListener('complete', onScriptLoad); - script.addEventListener('error', onScriptError); - - document.body.appendChild(script); - - return (): void => { - script.removeEventListener('load', onScriptLoad); - script.removeEventListener('error', onScriptError); - }; - } - }, [src]); - - return [state.loaded, state.error]; -} diff --git a/libs/src/types/@paystack/inline-js/index.d.ts b/libs/src/types/@paystack/inline-js/index.d.ts new file mode 100644 index 0000000..b1ec7ea --- /dev/null +++ b/libs/src/types/@paystack/inline-js/index.d.ts @@ -0,0 +1,2 @@ +//@paystack/inline-js/index.d.ts +declare module '@paystack/inline-js'; diff --git a/libs/test/paystack-button.test.tsx b/libs/test/paystack-button.test.tsx index e313462..27cec7f 100644 --- a/libs/test/paystack-button.test.tsx +++ b/libs/test/paystack-button.test.tsx @@ -3,7 +3,6 @@ import React from 'react'; import {renderHook, cleanup, act} from '@testing-library/react-hooks'; import {render, fireEvent} from '@testing-library/react'; import {callPaystackPop} from '../paystack-actions'; -import usePaystackScript from '../paystack-script'; import PaystackButton from '../paystack-button'; import {config} from './fixtures'; @@ -21,7 +20,6 @@ describe('', () => { beforeEach(() => { // @ts-ignore callPaystackPop = jest.fn(); - renderHook(() => usePaystackScript()); }); afterAll(() => { diff --git a/libs/test/paystack-provider.test.tsx b/libs/test/paystack-provider.test.tsx index f59b7db..5e92b71 100644 --- a/libs/test/paystack-provider.test.tsx +++ b/libs/test/paystack-provider.test.tsx @@ -1,9 +1,7 @@ import React from 'react'; -// @ts-ignore -import {renderHook, cleanup, act} from '@testing-library/react-hooks'; +import {renderHook, cleanup} from '@testing-library/react-hooks'; import {render, fireEvent} from '@testing-library/react'; import {callPaystackPop} from '../paystack-actions'; -import usePaystackScript from '../paystack-script'; import PaystackConsumer from '../paystack-consumer'; import {config} from './fixtures'; @@ -20,7 +18,6 @@ describe('', () => { beforeEach(() => { // @ts-ignore callPaystackPop = jest.fn(); - renderHook(() => usePaystackScript()); }); afterAll(() => { diff --git a/libs/test/paystack-script.test.ts b/libs/test/paystack-script.test.ts deleted file mode 100644 index ad7a297..0000000 --- a/libs/test/paystack-script.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -// @ts-ignore -import {renderHook, cleanup} from '@testing-library/react-hooks'; -import usePaystackScript from '../paystack-script'; - -describe('usePaystackScript()', () => { - afterAll(() => { - cleanup(); - document.body.innerHTML = ''; - }); - - it('adds the script to the dom', () => { - const {result} = renderHook(() => usePaystackScript()); - - expect(result.current[0]).toBe(false); - expect(result.current[1]).toBe(false); - expect(document.getElementsByTagName('script')).toBeDefined(); - }); - - it('Will not load multi inline script', () => { - renderHook(() => usePaystackScript()); - const {result} = renderHook(() => usePaystackScript()); - - expect(result.current[0]).toBe(true); - expect(result.current[1]).toBe(false); - expect(document.getElementsByTagName('script').length).toBe(1); - expect(document.body.innerHTML).toMatch(new RegExp('https://js.paystack.co/v1/inline.js')); - }); -}); diff --git a/libs/test/use-paystack.test.tsx b/libs/test/use-paystack.test.tsx index 72fd733..71d696a 100644 --- a/libs/test/use-paystack.test.tsx +++ b/libs/test/use-paystack.test.tsx @@ -1,10 +1,8 @@ -// @ts-ignore import {renderHook, cleanup, act} from '@testing-library/react-hooks'; import {render, fireEvent} from '@testing-library/react'; import React from 'react'; import usePaystackPayment from '../use-paystack'; import {callPaystackPop} from '../paystack-actions'; -import usePaystackScript from '../paystack-script'; import {config} from './fixtures'; import {Currency, PaymentChannels} from '../types'; @@ -16,7 +14,6 @@ describe('usePaystackPayment()', () => { beforeEach(() => { // @ts-ignore callPaystackPop = jest.fn(); - renderHook(() => usePaystackScript()); }); afterAll(() => { diff --git a/libs/use-paystack.ts b/libs/use-paystack.ts index 2bc1432..4950f1b 100644 --- a/libs/use-paystack.ts +++ b/libs/use-paystack.ts @@ -1,16 +1,8 @@ -import {useEffect} from 'react'; import {HookConfig, InitializePayment} from './types'; -import usePaystackScript from './paystack-script'; import {callPaystackPop} from './paystack-actions'; export default function usePaystackPayment(hookConfig: HookConfig): InitializePayment { - const [scriptLoaded, scriptError] = usePaystackScript(); - function initializePayment({config, onSuccess, onClose}: Parameters[0]): void { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); - } - const args = {...hookConfig, ...config}; const { @@ -35,42 +27,58 @@ export default function usePaystackPayment(hookConfig: HookConfig): InitializePa connect_account, connect_split, } = args; + const paystackArgs: Record = { + onSuccess: onSuccess ? onSuccess : () => null, + onCancel: onClose ? onClose : () => null, + key: publicKey, + ref: reference, + email, + firstname, + lastname, + amount, + currency, + plan, + subaccount, + transaction_charge, + bearer, + label, + metadata, + }; - if (scriptLoaded) { - const paystackArgs: Record = { - callback: onSuccess ? onSuccess : () => null, - onClose: onClose ? onClose : () => null, - key: publicKey, - ref: reference, - email, - firstname, - lastname, - phone, - amount, - currency, - plan, - quantity, - channels, - subaccount, - transaction_charge, - bearer, - label, - metadata, - split, - split_code, - connect_split, - connect_account, - 'data-custom-button': args['data-custom-button'] || '', - }; - callPaystackPop(paystackArgs); + if (phone) { + paystackArgs.phone = phone; + } + + if (quantity) { + paystackArgs.quantity = quantity; + } + + if (channels) { + paystackArgs.channels = channels; + } + + if (split) { + paystackArgs.split = split; } - } - useEffect(() => { - if (scriptError) { - throw new Error('Unable to load paystack inline script'); + if (split_code) { + paystackArgs.split_code = split_code; } - }, [scriptError]); + + if (connect_split) { + paystackArgs.connect_split = connect_split; + } + + if (connect_account) { + paystackArgs.connect_account = connect_account; + } + + if (args['data-custom-button']) { + paystackArgs['data-custom-button'] = args['data-custom-button']; + } + + callPaystackPop(paystackArgs); + } return initializePayment; } diff --git a/package.json b/package.json index 28335e0..c17379e 100644 --- a/package.json +++ b/package.json @@ -28,13 +28,14 @@ "paystack", "Gateway" ], - "author": "Ayeni Olusegun ", + "author": "Olusegun Ayeni ", "license": "MIT", "bugs": { "url": "https://github.com/iamraphson/react-paystack/issues" }, "homepage": "https://github.com/iamraphson/react-paystack#readme", "peerDependencies": { + "@paystack/inline-js": "^2.18.0", "react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0", "react-dom": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0" }, @@ -73,6 +74,7 @@ "rollup-plugin-peer-deps-external": "^2.2.4", "rollup-plugin-typescript2": "^0.36.0", "ts-jest": "^29.1.0", - "typescript": "^5.0.3" + "typescript": "^5.0.3", + "@paystack/inline-js": "^2.18.0" } }