@dev_innovations_labs/phonepe-pg-sdk 1.0.1 → 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/README.md CHANGED
@@ -1,32 +1,6 @@
1
- ::: {align="center"}
2
- ```{=html}
3
- <h1>
4
- ```
5
1
  📱 PhonePe PG SDK
6
- ```{=html}
7
- </h1>
8
- ```
9
- ```{=html}
10
- <h3>
11
- ```
12
2
  @dev_innovations_labs/phonepe-pg-sdk
13
- ```{=html}
14
- </h3>
15
- ```
16
- ```{=html}
17
- <p>
18
- ```
19
- A unified PhonePe Payment Gateway SDK for
20
- `<b>`{=html}Node.js`</b>`{=html}, `<b>`{=html}React Web`</b>`{=html},
21
- and `<b>`{=html}React Native`</b>`{=html} using the latest Checkout v2
22
- APIs.
23
- ```{=html}
24
- </p>
25
- ```
26
- `<img src="https://img.shields.io/npm/v/@dev_innovations_labs/phonepe-pg-sdk?color=blue&style=for-the-badge" />`{=html}
27
- `<img src="https://img.shields.io/npm/dm/@dev_innovations_labs/phonepe-pg-sdk?style=for-the-badge" />`{=html}
28
- `<img src="https://img.shields.io/badge/PhonePe-PG%20SDK-purple?style=for-the-badge" />`{=html}
29
- :::
3
+ <p align="center"> A unified PhonePe Payment Gateway SDK for <b>Node.js</b>, <b>React Web</b>, and <b>React Native</b> using the latest Checkout v2 APIs. </p> <p align="center"> <img src="https://img.shields.io/npm/v/@dev_innovations_labs/phonepe-pg-sdk?color=blue&style=for-the-badge" /> <img src="https://img.shields.io/npm/dm/@dev_innovations_labs/phonepe-pg-sdk?style=for-the-badge" /> <img src="https://img.shields.io/badge/PhonePe-PG%20SDK-purple?style=for-the-badge" /> </p>
30
4
 
31
5
  ------------------------------------------------------------------------
32
6
 
@@ -1,5 +1,4 @@
1
- import React from "react";
2
- import { PhonePeCredentials, PhonePePaymentInit } from "./types";
1
+ import { PhonePePaymentInit, PhonePeCredentials } from "./types";
3
2
  declare global {
4
3
  interface Window {
5
4
  PhonePeCheckout: any;
@@ -7,8 +6,8 @@ declare global {
7
6
  }
8
7
  interface Props extends PhonePeCredentials {
9
8
  request: PhonePePaymentInit;
10
- onSuccess: (data: any) => void;
11
- onError: (err: any) => void;
9
+ onSuccess: (d: any) => void;
10
+ onError: (e: any) => void;
12
11
  }
13
- export declare const PhonePeCheckoutWeb: React.FC<Props>;
12
+ export declare const PhonePeCheckoutWeb: ({ clientId, clientSecret, environment, request, onSuccess, onError }: Props) => any;
14
13
  export {};
@@ -1,9 +1,8 @@
1
- import React from "react";
2
- import { PhonePeCredentials, PhonePePaymentInit } from "./types";
1
+ import { PhonePePaymentInit, PhonePeCredentials } from "./types";
3
2
  interface Props extends PhonePeCredentials {
4
3
  request: PhonePePaymentInit;
5
- onSuccess: (data: any) => void;
6
- onError: (err: any) => void;
4
+ onSuccess: (d: any) => void;
5
+ onError: (e: any) => void;
7
6
  }
8
- export declare const PhonePeCheckoutNative: React.FC<Props>;
7
+ export declare const PhonePeCheckoutNative: ({ clientId, clientSecret, environment, request, onSuccess, onError }: Props) => any;
9
8
  export {};
package/dist/index.esm.js CHANGED
@@ -7,13 +7,13 @@ const PhonePeUrls = {
7
7
  token: "https://api.phonepe.com/apis/identity-manager/v1/oauth/token",
8
8
  pay: "https://api.phonepe.com/apis/pg/checkout/v2/pay",
9
9
  status: "https://api.phonepe.com/apis/pg/checkout/v2/order/status",
10
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
10
+ script: "https://mercury.phonepe.com/web/bundle/checkout.js"
11
11
  },
12
12
  sandbox: {
13
13
  token: "https://api-preprod.phonepe.com/apis/identity-manager/v1/oauth/token",
14
14
  pay: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay",
15
15
  status: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/order/status",
16
- script: "https://mercury.phonepe.com/web/bundle/checkout.js",
16
+ script: "https://mercury.phonepe.com/web/bundle/checkout.js"
17
17
  }
18
18
  };
19
19
 
@@ -23,7 +23,7 @@ function normalizeEnvironment(env) {
23
23
  return "production";
24
24
  if (e === "sandbox")
25
25
  return "sandbox";
26
- throw new Error(`Invalid environment "${env}". Expected "Production" or "Sandbox".`);
26
+ throw new Error(`Invalid environment: ${env}`);
27
27
  }
28
28
 
29
29
  class PhonePeNode {
@@ -31,8 +31,7 @@ class PhonePeNode {
31
31
  this.creds = creds;
32
32
  }
33
33
  get urls() {
34
- const env = normalizeEnvironment(this.creds.environment);
35
- return PhonePeUrls[env];
34
+ return PhonePeUrls[normalizeEnvironment(this.creds.environment)];
36
35
  }
37
36
  async getToken() {
38
37
  const body = new URLSearchParams({
@@ -42,59 +41,46 @@ class PhonePeNode {
42
41
  client_version: "1"
43
42
  });
44
43
  const res = await axios.post(this.urls.token, body, {
45
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
44
+ headers: { "Content-Type": "application/x-www-form-urlencoded" }
46
45
  });
47
46
  return res.data.access_token;
48
47
  }
49
48
  async createPayment(data) {
50
49
  const token = await this.getToken();
51
50
  const payload = {
52
- merchantId: data.merchantId,
51
+ ...data,
53
52
  merchantOrderId: v4(),
54
- merchantTransactionId: v4(),
55
- amount: data.amount,
56
- mobileNumber: data.mobileNumber,
57
- redirectUrl: data.redirectUrl,
58
- callbackUrl: data.callbackUrl,
59
- deviceContext: data.deviceContext,
60
- paymentScope: data.paymentScope,
53
+ merchantTransactionId: v4()
61
54
  };
62
55
  const res = await axios.post(this.urls.pay, payload, {
63
56
  headers: {
64
57
  "Content-Type": "application/json",
65
- Authorization: `O-Bearer ${token}`,
58
+ Authorization: `O-Bearer ${token}`
66
59
  }
67
60
  });
68
61
  return res.data;
69
62
  }
70
63
  async getStatus(merchantId, merchantOrderId) {
71
64
  const token = await this.getToken();
72
- const res = await axios.post(this.urls.status, {
73
- merchantId,
74
- merchantOrderId
75
- }, {
65
+ const res = await axios.post(this.urls.status, { merchantId, merchantOrderId }, {
76
66
  headers: {
77
67
  "Content-Type": "application/json",
78
- Authorization: `O-Bearer ${token}`,
68
+ Authorization: `O-Bearer ${token}`
79
69
  }
80
70
  });
81
71
  return res.data;
82
72
  }
83
73
  }
84
74
 
85
- const PhonePeCheckoutWeb = ({ environment, clientId, clientSecret, request, onSuccess, onError }) => {
86
- const env = normalizeEnvironment(environment);
87
- const urls = PhonePeUrls[env];
88
- if (!urls) {
89
- onError(`Invalid environment: ${environment}`);
90
- return;
91
- }
75
+ const PhonePeCheckoutWeb = ({ clientId, clientSecret, environment, request, onSuccess, onError }) => {
76
+ const urls = PhonePeUrls[normalizeEnvironment(environment)];
92
77
  useEffect(() => {
93
78
  const script = document.createElement("script");
94
79
  script.src = urls.script;
95
80
  script.async = true;
96
81
  document.body.appendChild(script);
97
82
  script.onload = () => init();
83
+ return () => document.body.removeChild(script);
98
84
  }, []);
99
85
  const init = async () => {
100
86
  try {
@@ -114,7 +100,7 @@ const PhonePeCheckoutWeb = ({ environment, clientId, clientSecret, request, onSu
114
100
  merchantOrderId: v4(),
115
101
  merchantTransactionId: v4()
116
102
  };
117
- const payResp = await fetch(urls.pay, {
103
+ const resp = await fetch(urls.pay, {
118
104
  method: "POST",
119
105
  headers: {
120
106
  "Content-Type": "application/json",
@@ -122,11 +108,11 @@ const PhonePeCheckoutWeb = ({ environment, clientId, clientSecret, request, onSu
122
108
  },
123
109
  body: JSON.stringify(payload)
124
110
  });
125
- const payData = await payResp.json();
111
+ const data = await resp.json();
126
112
  window.PhonePeCheckout.transact({
127
- tokenUrl: payData.redirectUrl,
113
+ tokenUrl: data.redirectUrl,
128
114
  type: "IFRAME",
129
- callback: () => onSuccess(payData)
115
+ callback: () => onSuccess(data)
130
116
  });
131
117
  }
132
118
  catch (err) {