diff --git a/.gitignore b/.gitignore index fc8299d..7e4fd53 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ npm-debug.log* yarn-debug.log* yarn-error.log* lerna-debug.log* +test # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/package.json b/package.json index bcf86da..fe2936d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-bkash", - "version": "2.0.2", + "version": "2.0.3", "description": "A React Component for Accepting Bkash Payments using React", "author": "Shahriar Shojib ", "license": "MIT", diff --git a/src/bkash.d.ts b/src/bkash.types.ts similarity index 77% rename from src/bkash.d.ts rename to src/bkash.types.ts index de8d74b..733e977 100644 --- a/src/bkash.d.ts +++ b/src/bkash.types.ts @@ -1,6 +1,6 @@ import { ICreatePaymentResponse, IPaymentRequest } from './utils'; -type BkashConfig = { +export type BkashConfig = { paymentMode: string; paymentRequest: { amount: string; @@ -12,15 +12,10 @@ type BkashConfig = { onClose: () => void; }; -interface BkashScript { +export interface BkashScript { init(config: BkashConfig): void; create(): BkashScript; execute(): BkashScript; onSuccess(data: ICreatePaymentResponse): void; onError(): void; } -declare global { - interface Window { - bKash: BkashScript; - } -} diff --git a/src/index.tsx b/src/index.tsx index f3d83ee..4951c17 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,7 +9,7 @@ import React, { useRef, useState, } from 'react'; -import { BkashConfig } from './bkash'; +import { BkashConfig, BkashScript } from './bkash.types'; import { useAsync } from './hooks/useAsync'; import { BkashComponentConfig, @@ -21,6 +21,8 @@ import { post, } from './utils'; +declare const bKash: BkashScript; + export type BkashButtonProps = { onSuccess: BkashSuccessFunction; onClose: () => void; @@ -98,10 +100,10 @@ export const BkashButton: FC = ({ try { const result = await config.onCreatePayment(request); paymentID.current = result.paymentID; - window.bKash.create().onSuccess(result); + bKash.create().onSuccess(result); } catch (error) { setPaymentAPIError(error); - window.bKash.create().onError(); + bKash.create().onError(); } return; } @@ -112,12 +114,12 @@ export const BkashButton: FC = ({ config.additionalHeaders || {} ); if (result.error !== null) { - window.bKash.create().onError(); + bKash.create().onError(); setPaymentAPIError(result.error); return; } paymentID.current = result.data.paymentID; - window.bKash.create().onSuccess(result.data); + bKash.create().onSuccess(result.data); }, executeRequestOnAuthorization: async () => { @@ -128,7 +130,7 @@ export const BkashButton: FC = ({ onSuccess(result); } catch (error) { setPaymentAPIError(error); - window.bKash.execute().onError(); + bKash.execute().onError(); } return; } @@ -141,7 +143,7 @@ export const BkashButton: FC = ({ if (result.error !== null) { setPaymentAPIError(result.error); - window.bKash.execute().onError(); + bKash.execute().onError(); return; } onSuccess(result.data); @@ -151,14 +153,12 @@ export const BkashButton: FC = ({ onClose: () => onClose(), }; - if (!window.bKash) { + if (typeof bKash === 'undefined') { setBkashNotFoundError(true); return; } - if (window.bKash) { - window.bKash.init(bkashConfig); - } + bKash.init(bkashConfig); }, [config, onClose, onSuccess]); // Load dependencies & setup bkash diff --git a/tsconfig.json b/tsconfig.json index 94c81f6..50d4a6e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "outDir": "dist", - "module": "esnext", + "module": "CommonJS", "target": "ES6", "lib": ["es6", "dom", "es2016", "es2017"], "sourceMap": true,