11import React , { createContext , useCallback , useMemo , useState } from 'react' ;
2- import { Modal , View , ActivityIndicator , } from 'react-native' ;
2+ import { Modal , SafeAreaView , ActivityIndicator } from 'react-native' ;
33import { WebView , WebViewMessageEvent } from 'react-native-webview' ;
4- import {
5- PaystackParams ,
6- PaystackProviderProps ,
7- } from './types' ;
4+ import { PaystackParams , PaystackProviderProps } from './types' ;
85import { validateParams , paystackHtmlContent , generatePaystackParams , handlePaystackMessage } from './utils' ;
96import { styles } from './styles' ;
107
118export const PaystackContext = createContext < {
12- popup : {
13- checkout : ( params : PaystackParams ) => void ;
14- newTransaction : ( params : PaystackParams ) => void ;
15- } ;
9+ popup : {
10+ checkout : ( params : PaystackParams ) => void ;
11+ newTransaction : ( params : PaystackParams ) => void ;
12+ } ;
1613} | null > ( null ) ;
1714
1815export const PaystackProvider : React . FC < PaystackProviderProps > = ( {
19- publicKey,
20- currency,
21- defaultChannels = [ 'card' ] ,
22- debug = false ,
23- children,
24- onGlobalSuccess,
25- onGlobalCancel,
16+ publicKey,
17+ currency,
18+ defaultChannels = [ 'card' ] ,
19+ debug = false ,
20+ children,
21+ onGlobalSuccess,
22+ onGlobalCancel,
2623} ) => {
27- const [ visible , setVisible ] = useState ( false ) ;
28- const [ params , setParams ] = useState < PaystackParams | null > ( null ) ;
29- const [ method , setMethod ] = useState < 'checkout' | 'newTransaction' > ( 'checkout' ) ;
24+ const [ visible , setVisible ] = useState ( false ) ;
25+ const [ params , setParams ] = useState < PaystackParams | null > ( null ) ;
26+ const [ method , setMethod ] = useState < 'checkout' | 'newTransaction' > ( 'checkout' ) ;
3027
31- const fallbackRef = useMemo ( ( ) => `ref_${ Date . now ( ) } ` , [ ] ) ;
28+ const fallbackRef = useMemo ( ( ) => `ref_${ Date . now ( ) } ` , [ ] ) ;
3229
33- const open = useCallback (
34- ( params : PaystackParams , selectedMethod : 'checkout' | 'newTransaction' ) => {
35- if ( debug ) console . log ( `[Paystack] Opening modal with method: ${ selectedMethod } ` ) ;
36- if ( ! validateParams ( params , debug ) ) return ;
37- setParams ( params ) ;
38- setMethod ( selectedMethod ) ;
39- setVisible ( true ) ;
40- } ,
41- [ debug ]
42- ) ;
30+ const open = useCallback (
31+ ( params : PaystackParams , selectedMethod : 'checkout' | 'newTransaction' ) => {
32+ if ( debug ) console . log ( `[Paystack] Opening modal with method: ${ selectedMethod } ` ) ;
33+ if ( ! validateParams ( params , debug ) ) return ;
34+ setParams ( params ) ;
35+ setMethod ( selectedMethod ) ;
36+ setVisible ( true ) ;
37+ } ,
38+ [ debug ] ,
39+ ) ;
4340
44- const checkout = ( params : PaystackParams ) => open ( params , 'checkout' ) ;
45- const newTransaction = ( params : PaystackParams ) => open ( params , 'newTransaction' ) ;
41+ const checkout = ( params : PaystackParams ) => open ( params , 'checkout' ) ;
42+ const newTransaction = ( params : PaystackParams ) => open ( params , 'newTransaction' ) ;
4643
47- const close = ( ) => {
48- setVisible ( false ) ;
49- setParams ( null ) ;
50- }
44+ const close = ( ) => {
45+ setVisible ( false ) ;
46+ setParams ( null ) ;
47+ } ;
5148
52- const handleMessage = ( event : WebViewMessageEvent ) => {
53- handlePaystackMessage ( {
54- event,
55- debug,
56- params,
57- onGlobalSuccess,
58- onGlobalCancel,
59- close,
60- } ) ;
61- } ;
49+ const handleMessage = ( event : WebViewMessageEvent ) => {
50+ handlePaystackMessage ( {
51+ event,
52+ debug,
53+ params,
54+ onGlobalSuccess,
55+ onGlobalCancel,
56+ close,
57+ } ) ;
58+ } ;
6259
63- const paystackHTML = useMemo ( ( ) => {
64- if ( ! params ) return '' ;
65- return paystackHtmlContent (
66- generatePaystackParams ( {
67- publicKey,
68- email : params . email ,
69- amount : params . amount ,
70- reference : params . reference || fallbackRef ,
71- metadata : params . metadata ,
72- ...( currency && { currency } ) ,
73- channels : defaultChannels ,
74- plan : params . plan ,
75- invoice_limit : params . invoice_limit ,
76- subaccount : params . subaccount ,
77- split : params . split ,
78- split_code : params . split_code ,
79- } ) ,
80- method
81- ) ;
82- } , [ params , method ] ) ;
60+ const paystackHTML = useMemo ( ( ) => {
61+ if ( ! params ) return '' ;
62+ return paystackHtmlContent (
63+ generatePaystackParams ( {
64+ publicKey,
65+ email : params . email ,
66+ amount : params . amount ,
67+ reference : params . reference || fallbackRef ,
68+ metadata : params . metadata ,
69+ ...( currency && { currency } ) ,
70+ channels : defaultChannels ,
71+ plan : params . plan ,
72+ invoice_limit : params . invoice_limit ,
73+ subaccount : params . subaccount ,
74+ split : params . split ,
75+ split_code : params . split_code ,
76+ } ) ,
77+ method ,
78+ ) ;
79+ } , [ params , method ] ) ;
8380
84- if ( debug && visible ) {
85- console . log ( '[Paystack] HTML Injected:' , paystackHTML ) ;
86- }
81+ if ( debug && visible ) {
82+ console . log ( '[Paystack] HTML Injected:' , paystackHTML ) ;
83+ }
8784
88- return (
89- < PaystackContext . Provider value = { { popup : { checkout, newTransaction } } } >
90- { children }
91- < Modal visible = { visible } transparent animationType = "slide" >
92- < View style = { styles . container } >
93- < WebView
94- originWhitelist = { [ "*" ] }
95- source = { { html : paystackHTML } }
96- onMessage = { handleMessage }
97- javaScriptEnabled
98- domStorageEnabled
99- startInLoadingState
100- onLoadStart = { ( ) => debug && console . log ( '[Paystack] WebView Load Start' ) }
101- onLoadEnd = { ( ) => debug && console . log ( '[Paystack] WebView Load End' ) }
102- renderLoading = { ( ) => < ActivityIndicator size = "large" /> }
103- />
104- </ View >
105- </ Modal >
106- </ PaystackContext . Provider >
107- ) ;
108- } ;
85+ return (
86+ < PaystackContext . Provider value = { { popup : { checkout, newTransaction } } } >
87+ { children }
88+ < Modal visible = { visible } transparent animationType = "slide" >
89+ < SafeAreaView style = { styles . container } >
90+ < WebView
91+ originWhitelist = { [ '*' ] }
92+ source = { { html : paystackHTML } }
93+ onMessage = { handleMessage }
94+ javaScriptEnabled
95+ domStorageEnabled
96+ startInLoadingState
97+ onLoadStart = { ( ) => debug && console . log ( '[Paystack] WebView Load Start' ) }
98+ onLoadEnd = { ( ) => debug && console . log ( '[Paystack] WebView Load End' ) }
99+ renderLoading = { ( ) => < ActivityIndicator size = "large" /> }
100+ />
101+ </ SafeAreaView >
102+ </ Modal >
103+ </ PaystackContext . Provider >
104+ ) ;
105+ } ;
0 commit comments