@dev_innovations_labs/phonepe-pg-sdk 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rollup.config.js CHANGED
@@ -1,26 +1,43 @@
1
+
1
2
  import typescript from "rollup-plugin-typescript2";
3
+ import resolve from "@rollup/plugin-node-resolve";
4
+ import commonjs from "@rollup/plugin-commonjs";
2
5
 
3
6
  export default [
4
- {
5
- input: "src/index.ts",
6
- output: [
7
- { file: "dist/index.js", format: "cjs" },
8
- { file: "dist/index.esm.js", format: "esm" }
9
- ],
10
- plugins: [typescript()],
11
- external: ["axios", "uuid", "react", "react/jsx-runtime", "react-native", "react-native-webview"]
12
- },
13
- {
14
- input: "src/native.ts",
15
- output: [
16
- { file: "dist/native.js", format: "cjs" },
17
- { file: "dist/native.esm.js", format: "esm" }
18
- ],
19
- plugins: [typescript()],
20
- external: [
21
- "react",
22
- "react-native",
23
- "react-native-webview"
24
- ]
25
- }
7
+ {
8
+ input: "src/index.ts",
9
+ output: [{ file: "dist/index.esm.js", format: "esm" }],
10
+ plugins: [
11
+ resolve({ extensions: [".ts", ".tsx"] }),
12
+ commonjs(),
13
+ typescript({
14
+ clean: true,
15
+ tsconfigOverride: {
16
+ compilerOptions: {
17
+ jsx: "react-jsx",
18
+ jsxImportSource: "react"
19
+ }
20
+ }
21
+ })
22
+ ],
23
+ external: ["react","react/jsx-runtime","axios","uuid"]
24
+ },
25
+ {
26
+ input: "src/native.ts",
27
+ output: [{ file: "dist/native.esm.js", format: "esm" }],
28
+ plugins: [
29
+ resolve({ extensions: [".ts", ".tsx"] }),
30
+ commonjs(),
31
+ typescript({
32
+ clean: true,
33
+ tsconfigOverride: {
34
+ compilerOptions: {
35
+ jsx: "react-jsx",
36
+ jsxImportSource: "react"
37
+ }
38
+ }
39
+ })
40
+ ],
41
+ external: ["react","react-native","react-native-webview"]
42
+ }
26
43
  ];
@@ -1,3 +1,4 @@
1
+
1
2
  import axios from "axios";
2
3
  import { v4 as uuidv4 } from "uuid";
3
4
  import { PhonePeCredentials, PhonePePaymentInit } from "./types";
@@ -12,8 +13,7 @@ export class PhonePeNode {
12
13
  }
13
14
 
14
15
  private get urls() {
15
- const env = normalizeEnvironment(this.creds.environment);
16
- return PhonePeUrls[env];
16
+ return PhonePeUrls[normalizeEnvironment(this.creds.environment)];
17
17
  }
18
18
 
19
19
  async getToken() {
@@ -25,7 +25,7 @@ export class PhonePeNode {
25
25
  });
26
26
 
27
27
  const res = await axios.post(this.urls.token, body, {
28
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
28
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
29
29
  });
30
30
 
31
31
  return res.data.access_token;
@@ -35,21 +35,15 @@ export class PhonePeNode {
35
35
  const token = await this.getToken();
36
36
 
37
37
  const payload = {
38
- merchantId: data.merchantId,
38
+ ...data,
39
39
  merchantOrderId: uuidv4(),
40
- merchantTransactionId: uuidv4(),
41
- amount: data.amount,
42
- mobileNumber: data.mobileNumber,
43
- redirectUrl: data.redirectUrl,
44
- callbackUrl: data.callbackUrl,
45
- deviceContext: data.deviceContext,
46
- paymentScope: data.paymentScope,
40
+ merchantTransactionId: uuidv4()
47
41
  };
48
42
 
49
43
  const res = await axios.post(this.urls.pay, payload, {
50
44
  headers: {
51
45
  "Content-Type": "application/json",
52
- Authorization: `O-Bearer ${token}`,
46
+ Authorization: `O-Bearer ${token}`
53
47
  }
54
48
  });
55
49
 
@@ -59,15 +53,16 @@ export class PhonePeNode {
59
53
  async getStatus(merchantId: string, merchantOrderId: string) {
60
54
  const token = await this.getToken();
61
55
 
62
- const res = await axios.post(this.urls.status, {
63
- merchantId,
64
- merchantOrderId
65
- }, {
66
- headers: {
67
- "Content-Type": "application/json",
68
- Authorization: `O-Bearer ${token}`,
56
+ const res = await axios.post(
57
+ this.urls.status,
58
+ { merchantId, merchantOrderId },
59
+ {
60
+ headers: {
61
+ "Content-Type": "application/json",
62
+ Authorization: `O-Bearer ${token}`
63
+ }
69
64
  }
70
- });
65
+ );
71
66
 
72
67
  return res.data;
73
68
  }
@@ -1,38 +1,30 @@
1
- import React, { useEffect } from "react";
1
+
2
+ import { useEffect } from "react";
2
3
  import { v4 as uuidv4 } from "uuid";
3
- import { PhonePeCredentials, PhonePePaymentInit } from "./types";
4
+ import { PhonePePaymentInit, PhonePeCredentials } from "./types";
4
5
  import { PhonePeUrls } from "./env";
5
6
  import { normalizeEnvironment } from "./utils";
6
7
 
7
8
  declare global {
8
- interface Window {
9
- PhonePeCheckout: any;
10
- }
9
+ interface Window { PhonePeCheckout: any }
11
10
  }
12
11
 
13
12
  interface Props extends PhonePeCredentials {
14
13
  request: PhonePePaymentInit;
15
- onSuccess: (data: any) => void;
16
- onError: (err: any) => void;
14
+ onSuccess: (d:any) => void;
15
+ onError: (e:any) => void;
17
16
  }
18
17
 
19
- export const PhonePeCheckoutWeb: React.FC<Props> = ({
20
- environment,
18
+ export const PhonePeCheckoutWeb = ({
21
19
  clientId,
22
20
  clientSecret,
21
+ environment,
23
22
  request,
24
23
  onSuccess,
25
24
  onError
26
- }) => {
27
-
28
- const env = normalizeEnvironment(environment);
29
- const urls = PhonePeUrls[env];
30
-
31
- if (!urls) {
32
- onError(`Invalid environment: ${environment}`);
33
- return;
34
- }
25
+ }: Props) => {
35
26
 
27
+ const urls = PhonePeUrls[normalizeEnvironment(environment)];
36
28
 
37
29
  useEffect(() => {
38
30
  const script = document.createElement("script");
@@ -41,6 +33,7 @@ export const PhonePeCheckoutWeb: React.FC<Props> = ({
41
33
  document.body.appendChild(script);
42
34
 
43
35
  script.onload = () => init();
36
+ return () => document.body.removeChild(script);
44
37
  }, []);
45
38
 
46
39
  const init = async () => {
@@ -64,7 +57,7 @@ export const PhonePeCheckoutWeb: React.FC<Props> = ({
64
57
  merchantTransactionId: uuidv4()
65
58
  };
66
59
 
67
- const payResp = await fetch(urls.pay, {
60
+ const resp = await fetch(urls.pay, {
68
61
  method: "POST",
69
62
  headers: {
70
63
  "Content-Type": "application/json",
@@ -73,12 +66,12 @@ export const PhonePeCheckoutWeb: React.FC<Props> = ({
73
66
  body: JSON.stringify(payload)
74
67
  });
75
68
 
76
- const payData = await payResp.json();
69
+ const data = await resp.json();
77
70
 
78
71
  window.PhonePeCheckout.transact({
79
- tokenUrl: payData.redirectUrl,
72
+ tokenUrl: data.redirectUrl,
80
73
  type: "IFRAME",
81
- callback: () => onSuccess(payData)
74
+ callback: () => onSuccess(data)
82
75
  });
83
76
  } catch (err) {
84
77
  onError(err);
@@ -1,36 +1,30 @@
1
- import React, { useState, useEffect } from "react";
1
+
2
+ import { useEffect, useState } from "react";
2
3
  import { WebView } from "react-native-webview";
3
- import { v4 } from "uuid";
4
+ import { v4 as uuidv4 } from "uuid";
5
+ import { PhonePePaymentInit, PhonePeCredentials } from "./types";
4
6
  import { PhonePeUrls } from "./env";
5
- import { PhonePeCredentials, PhonePePaymentInit } from "./types";
6
7
  import { normalizeEnvironment } from "./utils";
7
8
 
8
9
  interface Props extends PhonePeCredentials {
9
10
  request: PhonePePaymentInit;
10
- onSuccess: (data: any) => void;
11
- onError: (err: any) => void;
11
+ onSuccess: (d:any) => void;
12
+ onError: (e:any) => void;
12
13
  }
13
14
 
14
- export const PhonePeCheckoutNative: React.FC<Props> = ({
15
+ export const PhonePeCheckoutNative = ({
15
16
  clientId,
16
17
  clientSecret,
17
18
  environment,
18
19
  request,
19
20
  onSuccess,
20
21
  onError
21
- }) => {
22
- const [url, setUrl] = useState<string>();
23
- const env = normalizeEnvironment(environment);
24
- const urls = PhonePeUrls[env];
22
+ }: Props) => {
25
23
 
26
- if (!urls) {
27
- onError(`Invalid environment: ${environment}`);
28
- return;
29
- }
24
+ const urls = PhonePeUrls[normalizeEnvironment(environment)];
25
+ const [url, setUrl] = useState<string>();
30
26
 
31
- useEffect(() => {
32
- init();
33
- }, []);
27
+ useEffect(() => { init(); }, []);
34
28
 
35
29
  const init = async () => {
36
30
  try {
@@ -44,12 +38,13 @@ export const PhonePeCheckoutNative: React.FC<Props> = ({
44
38
  client_version: "1"
45
39
  })
46
40
  });
41
+
47
42
  const token = (await tokenResp.json()).access_token;
48
43
 
49
44
  const payload = {
50
45
  ...request,
51
- merchantOrderId: v4(),
52
- merchantTransactionId: v4()
46
+ merchantOrderId: uuidv4(),
47
+ merchantTransactionId: uuidv4()
53
48
  };
54
49
 
55
50
  const payResp = await fetch(urls.pay, {
@@ -61,21 +56,22 @@ export const PhonePeCheckoutNative: React.FC<Props> = ({
61
56
  body: JSON.stringify(payload)
62
57
  });
63
58
 
64
- const { redirectUrl } = await payResp.json();
65
- setUrl(redirectUrl);
59
+ const data = await payResp.json();
60
+ setUrl(data.redirectUrl);
61
+
66
62
  } catch (err) {
67
63
  onError(err);
68
64
  }
69
65
  };
70
66
 
71
- return url ? (
67
+ if (!url) return null;
68
+
69
+ return (
72
70
  <WebView
73
71
  source={{ uri: url }}
74
- onNavigationStateChange={(nav) => {
75
- if (nav.url.includes("callback")) {
76
- onSuccess(nav);
77
- }
72
+ onNavigationStateChange={nav => {
73
+ if (nav.url.includes("callback")) onSuccess(nav);
78
74
  }}
79
75
  />
80
- ) : null;
76
+ );
81
77
  };
package/src/env.ts CHANGED
@@ -1,15 +1,15 @@
1
+
1
2
  export const PhonePeUrls = {
2
3
  production: {
3
4
  token: "https://api.phonepe.com/apis/identity-manager/v1/oauth/token",
4
5
  pay: "https://api.phonepe.com/apis/pg/checkout/v2/pay",
5
6
  status: "https://api.phonepe.com/apis/pg/checkout/v2/order/status",
6
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
7
+ script: "https://mercury.phonepe.com/web/bundle/checkout.js"
7
8
  },
8
-
9
9
  sandbox: {
10
10
  token: "https://api-preprod.phonepe.com/apis/identity-manager/v1/oauth/token",
11
11
  pay: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay",
12
12
  status: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/order/status",
13
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
13
+ script: "https://mercury.phonepe.com/web/bundle/checkout.js"
14
14
  }
15
15
  };
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+
1
2
  export * from "./PhonePeNode";
2
3
  export * from "./PhonePeReact";
3
4
  export * from "./types";
package/src/native.ts CHANGED
@@ -1 +1,2 @@
1
+
1
2
  export * from "./PhonePeReactNative";
@@ -1,4 +1 @@
1
- declare module "react-native-webview" {
2
- import { ComponentType } from "react";
3
- export const WebView: ComponentType<any>;
4
- }
1
+ declare module "react-native-webview";
package/src/types.ts CHANGED
@@ -1,9 +1,8 @@
1
- export type PhonePeEnvironment = "Production" | "Sandbox";
2
1
 
3
2
  export interface PhonePeCredentials {
4
3
  clientId: string;
5
4
  clientSecret: string;
6
- environment: PhonePeEnvironment;
5
+ environment: string;
7
6
  }
8
7
 
9
8
  export interface PhonePePaymentInit {
package/src/utils.ts CHANGED
@@ -1,12 +1,7 @@
1
- import { PhonePeEnvironment } from "./types";
2
1
 
3
- export function normalizeEnvironment(env: PhonePeEnvironment): "production" | "sandbox" {
2
+ export function normalizeEnvironment(env: string): "production" | "sandbox" {
4
3
  const e = String(env).toLowerCase().trim();
5
-
6
4
  if (e === "production" || e === "prod") return "production";
7
5
  if (e === "sandbox") return "sandbox";
8
-
9
- throw new Error(
10
- `Invalid environment "${env}". Expected "Production" or "Sandbox".`
11
- );
6
+ throw new Error(`Invalid environment: ${env}`);
12
7
  }
package/tsconfig.json CHANGED
@@ -2,13 +2,16 @@
2
2
  "compilerOptions": {
3
3
  "target": "ES2020",
4
4
  "module": "ESNext",
5
- "declaration": true,
6
5
  "jsx": "react-jsx",
7
- "esModuleInterop": true,
8
- "allowSyntheticDefaultImports": true,
6
+ "jsxImportSource": "react",
7
+ "declaration": true,
9
8
  "skipLibCheck": true,
10
9
  "moduleResolution": "Node",
10
+ "esModuleInterop": true,
11
+ "allowSyntheticDefaultImports": true,
11
12
  "outDir": "dist"
12
13
  },
13
- "include": ["src"]
14
- }
14
+ "include": [
15
+ "src"
16
+ ]
17
+ }
package/dist/index.js DELETED
@@ -1,142 +0,0 @@
1
- 'use strict';
2
-
3
- var axios = require('axios');
4
- var uuid = require('uuid');
5
- var react = require('react');
6
-
7
- const PhonePeUrls = {
8
- production: {
9
- token: "https://api.phonepe.com/apis/identity-manager/v1/oauth/token",
10
- pay: "https://api.phonepe.com/apis/pg/checkout/v2/pay",
11
- status: "https://api.phonepe.com/apis/pg/checkout/v2/order/status",
12
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
13
- },
14
- sandbox: {
15
- token: "https://api-preprod.phonepe.com/apis/identity-manager/v1/oauth/token",
16
- pay: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay",
17
- status: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/order/status",
18
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
19
- }
20
- };
21
-
22
- function normalizeEnvironment(env) {
23
- const e = String(env).toLowerCase().trim();
24
- if (e === "production" || e === "prod")
25
- return "production";
26
- if (e === "sandbox")
27
- return "sandbox";
28
- throw new Error(`Invalid environment "${env}". Expected "Production" or "Sandbox".`);
29
- }
30
-
31
- class PhonePeNode {
32
- constructor(creds) {
33
- this.creds = creds;
34
- }
35
- get urls() {
36
- const env = normalizeEnvironment(this.creds.environment);
37
- return PhonePeUrls[env];
38
- }
39
- async getToken() {
40
- const body = new URLSearchParams({
41
- client_id: this.creds.clientId,
42
- client_secret: this.creds.clientSecret,
43
- grant_type: "client_credentials",
44
- client_version: "1"
45
- });
46
- const res = await axios.post(this.urls.token, body, {
47
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
48
- });
49
- return res.data.access_token;
50
- }
51
- async createPayment(data) {
52
- const token = await this.getToken();
53
- const payload = {
54
- merchantId: data.merchantId,
55
- merchantOrderId: uuid.v4(),
56
- merchantTransactionId: uuid.v4(),
57
- amount: data.amount,
58
- mobileNumber: data.mobileNumber,
59
- redirectUrl: data.redirectUrl,
60
- callbackUrl: data.callbackUrl,
61
- deviceContext: data.deviceContext,
62
- paymentScope: data.paymentScope,
63
- };
64
- const res = await axios.post(this.urls.pay, payload, {
65
- headers: {
66
- "Content-Type": "application/json",
67
- Authorization: `O-Bearer ${token}`,
68
- }
69
- });
70
- return res.data;
71
- }
72
- async getStatus(merchantId, merchantOrderId) {
73
- const token = await this.getToken();
74
- const res = await axios.post(this.urls.status, {
75
- merchantId,
76
- merchantOrderId
77
- }, {
78
- headers: {
79
- "Content-Type": "application/json",
80
- Authorization: `O-Bearer ${token}`,
81
- }
82
- });
83
- return res.data;
84
- }
85
- }
86
-
87
- const PhonePeCheckoutWeb = ({ environment, clientId, clientSecret, request, onSuccess, onError }) => {
88
- const env = normalizeEnvironment(environment);
89
- const urls = PhonePeUrls[env];
90
- if (!urls) {
91
- onError(`Invalid environment: ${environment}`);
92
- return;
93
- }
94
- react.useEffect(() => {
95
- const script = document.createElement("script");
96
- script.src = urls.script;
97
- script.async = true;
98
- document.body.appendChild(script);
99
- script.onload = () => init();
100
- }, []);
101
- const init = async () => {
102
- try {
103
- const tokenResp = await fetch(urls.token, {
104
- method: "POST",
105
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
106
- body: new URLSearchParams({
107
- client_id: clientId,
108
- client_secret: clientSecret,
109
- grant_type: "client_credentials",
110
- client_version: "1"
111
- })
112
- });
113
- const token = (await tokenResp.json()).access_token;
114
- const payload = {
115
- ...request,
116
- merchantOrderId: uuid.v4(),
117
- merchantTransactionId: uuid.v4()
118
- };
119
- const payResp = await fetch(urls.pay, {
120
- method: "POST",
121
- headers: {
122
- "Content-Type": "application/json",
123
- Authorization: `O-Bearer ${token}`
124
- },
125
- body: JSON.stringify(payload)
126
- });
127
- const payData = await payResp.json();
128
- window.PhonePeCheckout.transact({
129
- tokenUrl: payData.redirectUrl,
130
- type: "IFRAME",
131
- callback: () => onSuccess(payData)
132
- });
133
- }
134
- catch (err) {
135
- onError(err);
136
- }
137
- };
138
- return null;
139
- };
140
-
141
- exports.PhonePeCheckoutWeb = PhonePeCheckoutWeb;
142
- exports.PhonePeNode = PhonePeNode;
package/dist/native.js DELETED
@@ -1,83 +0,0 @@
1
- 'use strict';
2
-
3
- var jsxRuntime = require('react/jsx-runtime');
4
- var react = require('react');
5
- var reactNativeWebview = require('react-native-webview');
6
- var uuid = require('uuid');
7
-
8
- const PhonePeUrls = {
9
- production: {
10
- token: "https://api.phonepe.com/apis/identity-manager/v1/oauth/token",
11
- pay: "https://api.phonepe.com/apis/pg/checkout/v2/pay",
12
- status: "https://api.phonepe.com/apis/pg/checkout/v2/order/status",
13
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
14
- },
15
- sandbox: {
16
- token: "https://api-preprod.phonepe.com/apis/identity-manager/v1/oauth/token",
17
- pay: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay",
18
- status: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/order/status",
19
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
20
- }
21
- };
22
-
23
- function normalizeEnvironment(env) {
24
- const e = String(env).toLowerCase().trim();
25
- if (e === "production" || e === "prod")
26
- return "production";
27
- if (e === "sandbox")
28
- return "sandbox";
29
- throw new Error(`Invalid environment "${env}". Expected "Production" or "Sandbox".`);
30
- }
31
-
32
- const PhonePeCheckoutNative = ({ clientId, clientSecret, environment, request, onSuccess, onError }) => {
33
- const [url, setUrl] = react.useState();
34
- const env = normalizeEnvironment(environment);
35
- const urls = PhonePeUrls[env];
36
- if (!urls) {
37
- onError(`Invalid environment: ${environment}`);
38
- return;
39
- }
40
- react.useEffect(() => {
41
- init();
42
- }, []);
43
- const init = async () => {
44
- try {
45
- const tokenResp = await fetch(urls.token, {
46
- method: "POST",
47
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
48
- body: new URLSearchParams({
49
- client_id: clientId,
50
- client_secret: clientSecret,
51
- grant_type: "client_credentials",
52
- client_version: "1"
53
- })
54
- });
55
- const token = (await tokenResp.json()).access_token;
56
- const payload = {
57
- ...request,
58
- merchantOrderId: uuid.v4(),
59
- merchantTransactionId: uuid.v4()
60
- };
61
- const payResp = await fetch(urls.pay, {
62
- method: "POST",
63
- headers: {
64
- "Content-Type": "application/json",
65
- Authorization: `O-Bearer ${token}`
66
- },
67
- body: JSON.stringify(payload)
68
- });
69
- const { redirectUrl } = await payResp.json();
70
- setUrl(redirectUrl);
71
- }
72
- catch (err) {
73
- onError(err);
74
- }
75
- };
76
- return url ? (jsxRuntime.jsx(reactNativeWebview.WebView, { source: { uri: url }, onNavigationStateChange: (nav) => {
77
- if (nav.url.includes("callback")) {
78
- onSuccess(nav);
79
- }
80
- } })) : null;
81
- };
82
-
83
- exports.PhonePeCheckoutNative = PhonePeCheckoutNative;