Skip to content

Commit

Permalink
Merge pull request #18 from shahriar-shojib/fix/bkash-on-window
Browse files Browse the repository at this point in the history
Fix/bkash on window
  • Loading branch information
shahriar-shojib authored Feb 28, 2022
2 parents 8143405 + eb1ec1c commit 5babb83
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <shahriar_shojib@hotmail.com>",
"license": "MIT",
Expand Down
9 changes: 2 additions & 7 deletions src/bkash.d.ts → src/bkash.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ICreatePaymentResponse, IPaymentRequest } from './utils';

type BkashConfig = {
export type BkashConfig = {
paymentMode: string;
paymentRequest: {
amount: string;
Expand All @@ -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;
}
}
22 changes: 11 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +21,8 @@ import {
post,
} from './utils';

declare const bKash: BkashScript;

export type BkashButtonProps = {
onSuccess: BkashSuccessFunction;
onClose: () => void;
Expand Down Expand Up @@ -98,10 +100,10 @@ export const BkashButton: FC<BkashButtonProps> = ({
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;
}
Expand All @@ -112,12 +114,12 @@ export const BkashButton: FC<BkashButtonProps> = ({
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 () => {
Expand All @@ -128,7 +130,7 @@ export const BkashButton: FC<BkashButtonProps> = ({
onSuccess(result);
} catch (error) {
setPaymentAPIError(error);
window.bKash.execute().onError();
bKash.execute().onError();
}
return;
}
Expand All @@ -141,7 +143,7 @@ export const BkashButton: FC<BkashButtonProps> = ({

if (result.error !== null) {
setPaymentAPIError(result.error);
window.bKash.execute().onError();
bKash.execute().onError();
return;
}
onSuccess(result.data);
Expand All @@ -151,14 +153,12 @@ export const BkashButton: FC<BkashButtonProps> = ({
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
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"outDir": "dist",
"module": "esnext",
"module": "CommonJS",
"target": "ES6",
"lib": ["es6", "dom", "es2016", "es2017"],
"sourceMap": true,
Expand Down

0 comments on commit 5babb83

Please sign in to comment.