@dev_innovations_labs/phonepe-pg-sdk 1.0.2 → 1.0.3

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.
@@ -1,13 +1,12 @@
1
- import { PhonePePaymentInit, PhonePeCredentials } from "./types";
2
1
  declare global {
3
2
  interface Window {
4
3
  PhonePeCheckout: any;
5
4
  }
6
5
  }
7
- interface Props extends PhonePeCredentials {
8
- request: PhonePePaymentInit;
9
- onSuccess: (d: any) => void;
10
- onError: (e: any) => void;
6
+ interface Props {
7
+ redirectUrl: string;
8
+ onSuccess?: (data: any) => void;
9
+ onError?: (err: any) => void;
11
10
  }
12
- export declare const PhonePeCheckoutWeb: ({ clientId, clientSecret, environment, request, onSuccess, onError }: Props) => any;
11
+ export declare const PhonePeCheckoutWeb: ({ redirectUrl, onSuccess, onError }: Props) => any;
13
12
  export {};
@@ -1,8 +1,7 @@
1
- import { PhonePePaymentInit, PhonePeCredentials } from "./types";
2
- interface Props extends PhonePeCredentials {
3
- request: PhonePePaymentInit;
4
- onSuccess: (d: any) => void;
5
- onError: (e: any) => void;
1
+ interface Props {
2
+ redirectUrl: string;
3
+ onSuccess?: (data: any) => void;
4
+ onError?: (err: any) => void;
6
5
  }
7
- export declare const PhonePeCheckoutNative: ({ clientId, clientSecret, environment, request, onSuccess, onError }: Props) => any;
6
+ export declare const PhonePeCheckoutNative: ({ redirectUrl, onSuccess, onError }: Props) => any;
8
7
  export {};
package/dist/index.esm.js CHANGED
@@ -72,53 +72,30 @@ class PhonePeNode {
72
72
  }
73
73
  }
74
74
 
75
- const PhonePeCheckoutWeb = ({ clientId, clientSecret, environment, request, onSuccess, onError }) => {
76
- const urls = PhonePeUrls[normalizeEnvironment(environment)];
75
+ const PhonePeCheckoutWeb = ({ redirectUrl, onSuccess, onError }) => {
77
76
  useEffect(() => {
77
+ if (!redirectUrl)
78
+ return;
78
79
  const script = document.createElement("script");
79
- script.src = urls.script;
80
+ script.src = "https://mercury.phonepe.com/web/bundle/checkout.js";
80
81
  script.async = true;
81
82
  document.body.appendChild(script);
82
- script.onload = () => init();
83
- return () => document.body.removeChild(script);
84
- }, []);
85
- const init = async () => {
86
- try {
87
- const tokenResp = await fetch(urls.token, {
88
- method: "POST",
89
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
90
- body: new URLSearchParams({
91
- client_id: clientId,
92
- client_secret: clientSecret,
93
- grant_type: "client_credentials",
94
- client_version: "1"
95
- })
96
- });
97
- const token = (await tokenResp.json()).access_token;
98
- const payload = {
99
- ...request,
100
- merchantOrderId: v4(),
101
- merchantTransactionId: v4()
102
- };
103
- const resp = await fetch(urls.pay, {
104
- method: "POST",
105
- headers: {
106
- "Content-Type": "application/json",
107
- Authorization: `O-Bearer ${token}`
108
- },
109
- body: JSON.stringify(payload)
110
- });
111
- const data = await resp.json();
112
- window.PhonePeCheckout.transact({
113
- tokenUrl: data.redirectUrl,
114
- type: "IFRAME",
115
- callback: () => onSuccess(data)
116
- });
117
- }
118
- catch (err) {
119
- onError(err);
120
- }
121
- };
83
+ script.onload = () => {
84
+ try {
85
+ window.PhonePeCheckout.transact({
86
+ tokenUrl: redirectUrl,
87
+ type: "IFRAME",
88
+ callback: () => onSuccess?.({ status: "success" })
89
+ });
90
+ }
91
+ catch (err) {
92
+ onError?.(err);
93
+ }
94
+ };
95
+ return () => {
96
+ document.body.removeChild(script);
97
+ };
98
+ }, [redirectUrl]);
122
99
  return null;
123
100
  };
124
101
 
@@ -415,127 +415,20 @@ if (process.env.NODE_ENV === 'production') {
415
415
 
416
416
  var jsxRuntimeExports = jsxRuntime.exports;
417
417
 
418
- // Unique ID creation requires a high quality random # generator. In the browser we therefore
419
- // require the crypto API and do not support built-in fallback to lower quality random number
420
- // generators (like Math.random()).
421
- let getRandomValues;
422
- const rnds8 = new Uint8Array(16);
423
- function rng() {
424
- // lazy load so that environments that need to polyfill have a chance to do so
425
- if (!getRandomValues) {
426
- // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
427
- getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
428
-
429
- if (!getRandomValues) {
430
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
431
- }
432
- }
433
-
434
- return getRandomValues(rnds8);
435
- }
436
-
437
- /**
438
- * Convert array of 16 byte values to UUID string format of the form:
439
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
440
- */
441
-
442
- const byteToHex = [];
443
-
444
- for (let i = 0; i < 256; ++i) {
445
- byteToHex.push((i + 0x100).toString(16).slice(1));
446
- }
447
-
448
- function unsafeStringify(arr, offset = 0) {
449
- // Note: Be careful editing this code! It's been tuned for performance
450
- // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
451
- return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
452
- }
453
-
454
- const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
455
- var native = {
456
- randomUUID
457
- };
458
-
459
- function v4(options, buf, offset) {
460
- if (native.randomUUID && true && !options) {
461
- return native.randomUUID();
462
- }
463
-
464
- options = options || {};
465
- const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
466
-
467
- rnds[6] = rnds[6] & 0x0f | 0x40;
468
- rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
469
-
470
- return unsafeStringify(rnds);
471
- }
472
-
473
- const PhonePeUrls = {
474
- production: {
475
- token: "https://api.phonepe.com/apis/identity-manager/v1/oauth/token",
476
- pay: "https://api.phonepe.com/apis/pg/checkout/v2/pay",
477
- status: "https://api.phonepe.com/apis/pg/checkout/v2/order/status",
478
- script: "https://mercury.phonepe.com/web/bundle/checkout.js"
479
- },
480
- sandbox: {
481
- token: "https://api-preprod.phonepe.com/apis/identity-manager/v1/oauth/token",
482
- pay: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay",
483
- status: "https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/order/status",
484
- script: "https://mercury.phonepe.com/web/bundle/checkout.js"
485
- }
486
- };
487
-
488
- function normalizeEnvironment(env) {
489
- const e = String(env).toLowerCase().trim();
490
- if (e === "production" || e === "prod")
491
- return "production";
492
- if (e === "sandbox")
493
- return "sandbox";
494
- throw new Error(`Invalid environment: ${env}`);
495
- }
496
-
497
- const PhonePeCheckoutNative = ({ clientId, clientSecret, environment, request, onSuccess, onError }) => {
498
- const urls = PhonePeUrls[normalizeEnvironment(environment)];
418
+ const PhonePeCheckoutNative = ({ redirectUrl, onSuccess, onError }) => {
499
419
  const [url, setUrl] = useState();
500
- useEffect(() => { init(); }, []);
501
- const init = async () => {
502
- try {
503
- const tokenResp = await fetch(urls.token, {
504
- method: "POST",
505
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
506
- body: new URLSearchParams({
507
- client_id: clientId,
508
- client_secret: clientSecret,
509
- grant_type: "client_credentials",
510
- client_version: "1"
511
- })
512
- });
513
- const token = (await tokenResp.json()).access_token;
514
- const payload = {
515
- ...request,
516
- merchantOrderId: v4(),
517
- merchantTransactionId: v4()
518
- };
519
- const payResp = await fetch(urls.pay, {
520
- method: "POST",
521
- headers: {
522
- "Content-Type": "application/json",
523
- Authorization: `O-Bearer ${token}`
524
- },
525
- body: JSON.stringify(payload)
526
- });
527
- const data = await payResp.json();
528
- setUrl(data.redirectUrl);
529
- }
530
- catch (err) {
531
- onError(err);
532
- }
533
- };
420
+ useEffect(() => {
421
+ setUrl(redirectUrl);
422
+ }, [redirectUrl]);
534
423
  if (!url)
535
424
  return null;
536
- return (jsxRuntimeExports.jsx(WebView, { source: { uri: url }, onNavigationStateChange: nav => {
537
- if (nav.url.includes("callback"))
538
- onSuccess(nav);
425
+ return (jsxRuntimeExports.jsx(WebView, { source: { uri: url }, onNavigationStateChange: (nav) => {
426
+ if (nav.url.includes("success") || nav.url.includes("callback")) {
427
+ onSuccess && onSuccess(nav);
428
+ }
429
+ if (nav.url.includes("error") || nav.url.includes("failed")) {
430
+ onError && onError(nav);
431
+ }
539
432
  } }));
540
433
  };
541
434
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev_innovations_labs/phonepe-pg-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.esm.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,82 +1,47 @@
1
-
2
1
  import { useEffect } from "react";
3
- import { v4 as uuidv4 } from "uuid";
4
- import { PhonePePaymentInit, PhonePeCredentials } from "./types";
5
- import { PhonePeUrls } from "./env";
6
- import { normalizeEnvironment } from "./utils";
7
2
 
8
3
  declare global {
9
- interface Window { PhonePeCheckout: any }
4
+ interface Window {
5
+ PhonePeCheckout: any;
6
+ }
10
7
  }
11
8
 
12
- interface Props extends PhonePeCredentials {
13
- request: PhonePePaymentInit;
14
- onSuccess: (d:any) => void;
15
- onError: (e:any) => void;
9
+ interface Props {
10
+ redirectUrl: string;
11
+ onSuccess?: (data: any) => void;
12
+ onError?: (err: any) => void;
16
13
  }
17
14
 
18
15
  export const PhonePeCheckoutWeb = ({
19
- clientId,
20
- clientSecret,
21
- environment,
22
- request,
16
+ redirectUrl,
23
17
  onSuccess,
24
18
  onError
25
19
  }: Props) => {
26
20
 
27
- const urls = PhonePeUrls[normalizeEnvironment(environment)];
28
-
29
21
  useEffect(() => {
22
+ if (!redirectUrl) return;
23
+
30
24
  const script = document.createElement("script");
31
- script.src = urls.script;
25
+ script.src = "https://mercury.phonepe.com/web/bundle/checkout.js";
32
26
  script.async = true;
33
27
  document.body.appendChild(script);
34
28
 
35
- script.onload = () => init();
36
- return () => document.body.removeChild(script);
37
- }, []);
38
-
39
- const init = async () => {
40
- try {
41
- const tokenResp = await fetch(urls.token, {
42
- method: "POST",
43
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
44
- body: new URLSearchParams({
45
- client_id: clientId,
46
- client_secret: clientSecret,
47
- grant_type: "client_credentials",
48
- client_version: "1"
49
- })
50
- });
51
-
52
- const token = (await tokenResp.json()).access_token;
53
-
54
- const payload = {
55
- ...request,
56
- merchantOrderId: uuidv4(),
57
- merchantTransactionId: uuidv4()
58
- };
59
-
60
- const resp = await fetch(urls.pay, {
61
- method: "POST",
62
- headers: {
63
- "Content-Type": "application/json",
64
- Authorization: `O-Bearer ${token}`
65
- },
66
- body: JSON.stringify(payload)
67
- });
68
-
69
- const data = await resp.json();
70
-
71
- window.PhonePeCheckout.transact({
72
- tokenUrl: data.redirectUrl,
73
- type: "IFRAME",
74
- callback: () => onSuccess(data)
75
- });
76
- } catch (err) {
77
- onError(err);
78
- }
79
- };
29
+ script.onload = () => {
30
+ try {
31
+ window.PhonePeCheckout.transact({
32
+ tokenUrl: redirectUrl,
33
+ type: "IFRAME",
34
+ callback: () => onSuccess?.({ status: "success" })
35
+ });
36
+ } catch (err) {
37
+ onError?.(err);
38
+ }
39
+ };
40
+
41
+ return () => {
42
+ document.body.removeChild(script);
43
+ };
44
+ }, [redirectUrl]);
80
45
 
81
46
  return null;
82
47
  };
@@ -1,76 +1,36 @@
1
-
2
- import { useEffect, useState } from "react";
1
+ import React, { useState, useEffect } from "react";
3
2
  import { WebView } from "react-native-webview";
4
- import { v4 as uuidv4 } from "uuid";
5
- import { PhonePePaymentInit, PhonePeCredentials } from "./types";
6
- import { PhonePeUrls } from "./env";
7
- import { normalizeEnvironment } from "./utils";
8
3
 
9
- interface Props extends PhonePeCredentials {
10
- request: PhonePePaymentInit;
11
- onSuccess: (d:any) => void;
12
- onError: (e:any) => void;
4
+ interface Props {
5
+ redirectUrl: string;
6
+ onSuccess?: (data: any) => void;
7
+ onError?: (err: any) => void;
13
8
  }
14
9
 
15
10
  export const PhonePeCheckoutNative = ({
16
- clientId,
17
- clientSecret,
18
- environment,
19
- request,
11
+ redirectUrl,
20
12
  onSuccess,
21
13
  onError
22
14
  }: Props) => {
23
15
 
24
- const urls = PhonePeUrls[normalizeEnvironment(environment)];
25
16
  const [url, setUrl] = useState<string>();
26
17
 
27
- useEffect(() => { init(); }, []);
28
-
29
- const init = async () => {
30
- try {
31
- const tokenResp = await fetch(urls.token, {
32
- method: "POST",
33
- headers: { "Content-Type": "application/x-www-form-urlencoded" },
34
- body: new URLSearchParams({
35
- client_id: clientId,
36
- client_secret: clientSecret,
37
- grant_type: "client_credentials",
38
- client_version: "1"
39
- })
40
- });
41
-
42
- const token = (await tokenResp.json()).access_token;
43
-
44
- const payload = {
45
- ...request,
46
- merchantOrderId: uuidv4(),
47
- merchantTransactionId: uuidv4()
48
- };
49
-
50
- const payResp = await fetch(urls.pay, {
51
- method: "POST",
52
- headers: {
53
- "Content-Type": "application/json",
54
- Authorization: `O-Bearer ${token}`
55
- },
56
- body: JSON.stringify(payload)
57
- });
58
-
59
- const data = await payResp.json();
60
- setUrl(data.redirectUrl);
61
-
62
- } catch (err) {
63
- onError(err);
64
- }
65
- };
18
+ useEffect(() => {
19
+ setUrl(redirectUrl);
20
+ }, [redirectUrl]);
66
21
 
67
22
  if (!url) return null;
68
23
 
69
24
  return (
70
25
  <WebView
71
26
  source={{ uri: url }}
72
- onNavigationStateChange={nav => {
73
- if (nav.url.includes("callback")) onSuccess(nav);
27
+ onNavigationStateChange={(nav) => {
28
+ if (nav.url.includes("success") || nav.url.includes("callback")) {
29
+ onSuccess && onSuccess(nav);
30
+ }
31
+ if (nav.url.includes("error") || nav.url.includes("failed")) {
32
+ onError && onError(nav);
33
+ }
74
34
  }}
75
35
  />
76
36
  );