@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/README.md CHANGED
@@ -0,0 +1,204 @@
1
+ ๐Ÿ“ฑ PhonePe PG SDK
2
+ @dev_innovations_labs/phonepe-pg-sdk
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>
4
+
5
+ ------------------------------------------------------------------------
6
+
7
+ # ๐Ÿ–ผ๏ธ Screenshots
8
+
9
+ ### ๐ŸŒ Web Checkout
10
+
11
+ ![Web
12
+ Checkout](https://dummyimage.com/800x400/000/fff&text=Web+Checkout+Preview)
13
+
14
+ ### ๐Ÿ“ฑ React Native Checkout
15
+
16
+ ![React Native
17
+ Checkout](https://dummyimage.com/800x400/333/fff&text=React+Native+WebView+Checkout)
18
+
19
+ ------------------------------------------------------------------------
20
+
21
+ # ๐Ÿš€ Features
22
+
23
+ - ๐Ÿ” OAuth Token Generation (Checkout v2)
24
+ - ๐Ÿ’ณ Initiate Payments (Web, Mobile, UPI, Cards)
25
+ - ๐Ÿ”„ WebView Support for React Native
26
+ - ๐Ÿงช Status Check API
27
+ - ๐ŸŒ Sandbox & Production Environments
28
+ - ๐ŸŽฏ TypeScript Fully Supported
29
+ - ๐Ÿงฉ Multi-platform Unified SDK
30
+ - ๐Ÿงต Auto-environment normalization
31
+
32
+ ------------------------------------------------------------------------
33
+
34
+ # ๐Ÿ“ฆ Installation
35
+
36
+ ``` bash
37
+ npm install @dev_innovations_labs/phonepe-pg-sdk
38
+ ```
39
+
40
+ ------------------------------------------------------------------------
41
+
42
+ # โš™๏ธ Environments
43
+
44
+ Input Interpreted As
45
+ ------------------------------ ----------------
46
+ production, Production, prod production
47
+ sandbox, Sandbox, SANDBOX sandbox
48
+
49
+ ------------------------------------------------------------------------
50
+
51
+ # ๐Ÿ“š Usage
52
+
53
+ ------------------------------------------------------------------------
54
+
55
+ ## ๐ŸŸฆ 1. Node.js Example
56
+
57
+ ``` ts
58
+ import { PhonePeNode } from "@dev_innovations_labs/phonepe-pg-sdk";
59
+
60
+ const phonepe = new PhonePeNode({
61
+ clientId: "YOUR_CLIENT_ID",
62
+ clientSecret: "YOUR_SECRET",
63
+ environment: "production"
64
+ });
65
+
66
+ const res = await phonepe.createPayment({
67
+ merchantId: "M123",
68
+ merchantOrderId: "uuid-123",
69
+ amount: 10000,
70
+ redirectUrl: "https://yourapp.com/redirect",
71
+ callbackUrl: "https://yourapp.com/callback"
72
+ });
73
+
74
+ console.log(res.redirectUrl);
75
+ ```
76
+
77
+ ------------------------------------------------------------------------
78
+
79
+ ## ๐ŸŒ 2. React Web Example
80
+
81
+ ``` jsx
82
+ import { PhonePeCheckoutWeb } from "@dev_innovations_labs/phonepe-pg-sdk";
83
+
84
+ export default function Checkout() {
85
+ return (
86
+ <PhonePeCheckoutWeb
87
+ environment="sandbox"
88
+ clientId="YOUR_CLIENT_ID"
89
+ clientSecret="YOUR_SECRET"
90
+ request={{
91
+ merchantId: "M123",
92
+ merchantOrderId: "uuid-321",
93
+ amount: 10000,
94
+ redirectUrl: "https://yourapp.com/redirect",
95
+ callbackUrl: "https://yourapp.com/callback"
96
+ }}
97
+ onSuccess={(r) => console.log("Success", r)}
98
+ onError={(e) => console.log("Error", e)}
99
+ />
100
+ );
101
+ }
102
+ ```
103
+
104
+ ------------------------------------------------------------------------
105
+
106
+ ## ๐Ÿ“ฑ 3. React Native Example
107
+
108
+ ``` jsx
109
+ import { PhonePeCheckoutNative } from "@dev_innovations_labs/phonepe-pg-sdk/native";
110
+
111
+ export default function App() {
112
+ return (
113
+ <PhonePeCheckoutNative
114
+ environment="production"
115
+ clientId="YOUR_CLIENT_ID"
116
+ clientSecret="YOUR_SECRET"
117
+ request={{
118
+ merchantId: "M123",
119
+ merchantOrderId: "uuid-xyz",
120
+ amount: 10000,
121
+ redirectUrl: "https://yourapp.com/redirect",
122
+ callbackUrl: "https://yourapp.com/callback"
123
+ }}
124
+ onSuccess={(data) => console.log("Success", data)}
125
+ onError={(e) => console.log("Error", e)}
126
+ />
127
+ );
128
+ }
129
+ ```
130
+
131
+ ------------------------------------------------------------------------
132
+
133
+ # ๐Ÿ“ก Status API
134
+
135
+ ``` ts
136
+ const status = await phonepe.checkStatus("uuid-1234");
137
+ console.log(status);
138
+ ```
139
+
140
+ ------------------------------------------------------------------------
141
+
142
+ # ๐Ÿ“˜ API Table
143
+
144
+ API Platform Description
145
+ -------------------------- -------------- -------------------------------
146
+ `createPayment()` Node/Web/RN Initiates PhonePe Checkout v2
147
+ `checkStatus()` Node Checks transaction status
148
+ `PhonePeCheckoutWeb` React Web In-App iFrame Checkout
149
+ `PhonePeCheckoutNative` React Native Mobile WebView Checkout
150
+ `normalizeEnvironment()` Internal Normalizes env strings
151
+
152
+ ------------------------------------------------------------------------
153
+
154
+ # ๐Ÿงฉ Folder Structure
155
+
156
+ src/
157
+ index.ts
158
+ native.ts
159
+ PhonePeNode.ts
160
+ PhonePeReact.tsx
161
+ PhonePeReactNative.tsx
162
+ utils.ts
163
+ types.ts
164
+ dist/
165
+
166
+ ------------------------------------------------------------------------
167
+
168
+ # ๐Ÿ— Roadmap
169
+
170
+ - [ ] Add PhonePe payouts API
171
+ - [ ] Add checksum support for PG v1
172
+ - [ ] Add UI components for receipts
173
+ - [ ] Add webhooks helpers
174
+
175
+ ------------------------------------------------------------------------
176
+
177
+ # ๐Ÿ‘จโ€๐Ÿ’ป Contributors
178
+
179
+ Name Role
180
+ ----------------------- ----------------
181
+ Dev Innovations Labs Lead Developer
182
+ Open Source Community Coming Soon
183
+
184
+ ------------------------------------------------------------------------
185
+
186
+ # ๐Ÿ“ Changelog
187
+
188
+ ### v1.0.0
189
+
190
+ - Initial release\
191
+ - Multi-platform SDK\
192
+ - React Web + RN support\
193
+ - Token + Payment + Status APIs\
194
+ - Normalized environment handling
195
+
196
+ ------------------------------------------------------------------------
197
+
198
+ # ๐Ÿ“„ License
199
+
200
+ MIT ยฉ Dev Innovations Labs
201
+
202
+ ```{=html}
203
+ </div>
204
+ ```
@@ -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) {