@school24/paymentjs-rn 1.4.1 → 1.4.5

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,30 +1,3 @@
1
1
  import React from "react";
2
2
  import type { PaymentJSProps } from "./types";
3
- interface EnhancedPaymentJSProps extends PaymentJSProps {
4
- /**
5
- * Environment: 'test' or 'live'
6
- */
7
- environment?: "test" | "live";
8
- /**
9
- * Shopper reference (required for stored cards)
10
- */
11
- shopperReference?: string;
12
- /**
13
- * Amount in minor units (e.g. 100 = 1.00 AUD)
14
- */
15
- amount?: number;
16
- /**
17
- * Currency (default: AUD)
18
- */
19
- currency?: string;
20
- /**
21
- * Whether to store the card on first payment
22
- */
23
- storePaymentMethod?: boolean;
24
- /**
25
- * Optional base URL for the payment page (defaults to your hosted HTML)
26
- */
27
- baseUrl?: string;
28
- }
29
- declare const PaymentJS: React.FC<EnhancedPaymentJSProps>;
30
- export default PaymentJS;
3
+ export default function PaymentJS(props: PaymentJSProps): React.JSX.Element;
package/dist/PaymentJS.js CHANGED
@@ -33,60 +33,122 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.default = PaymentJS;
36
37
  const react_1 = __importStar(require("react"));
37
38
  const react_native_1 = require("react-native");
38
39
  const react_native_webview_1 = require("react-native-webview");
39
- const PaymentJS = (props) => {
40
- const { uri: customUri, onSuccess, onFailure, onDebug, debug, style, environment = "test", shopperReference = "USER123", amount = 100, currency = "AUD", storePaymentMethod = true, baseUrl = "https://www.school24.net.au/valpay-gateway", } = props;
41
- // Auto-build URI with query params if customUri not provided
40
+ function safeBoolString(v) {
41
+ return (v ? "true" : "false");
42
+ }
43
+ function normalizeBase(url) {
44
+ return url.replace(/\/+$/, "");
45
+ }
46
+ function PaymentJS(props) {
47
+ const { uri, pageUrl = "https://client.brandsyncstudio.com/paymentjs/payment-rn.html", lambdaUrl = "https://qb3epa62mry53z3woeiecuhpve0scpjb.lambda-url.ap-southeast-2.on.aws", mode = "pay", environment = "test", shopperReference = "", amount = 0, currency = "AUD", storePaymentMethod = false, ts, style, debug = false, onPaymentSuccess, onPaymentFailure, onEncryptSuccess, onEncryptFailure, onDebug, } = props;
48
+ // Build final URL if uri isn't provided
42
49
  const finalUri = (0, react_1.useMemo)(() => {
43
- if (customUri)
44
- return customUri;
45
- const params = new URLSearchParams({
46
- env: environment,
47
- shopperReference,
48
- amount: amount.toString(),
49
- currency,
50
- storePaymentMethod: storePaymentMethod.toString(),
51
- });
52
- return `${baseUrl}?${params.toString()}`;
53
- }, [customUri, environment, shopperReference, amount, currency, storePaymentMethod, baseUrl]);
50
+ if (uri)
51
+ return uri;
52
+ const params = new URLSearchParams();
53
+ // Required by HTML:
54
+ params.set("mode", mode);
55
+ params.set("env", environment);
56
+ params.set("baseUrl", normalizeBase(lambdaUrl));
57
+ params.set("currency", currency);
58
+ // Optional but commonly needed:
59
+ if (shopperReference)
60
+ params.set("shopperReference", shopperReference);
61
+ params.set("storePaymentMethod", safeBoolString(storePaymentMethod));
62
+ // Pay mode requires amount
63
+ if (mode === "pay") {
64
+ params.set("amount", String(amount !== null && amount !== void 0 ? amount : 0));
65
+ }
66
+ // cache buster so Expo/WebView refreshes
67
+ params.set("ts", String(ts !== null && ts !== void 0 ? ts : Date.now()));
68
+ return `${pageUrl}?${params.toString()}`;
69
+ }, [
70
+ uri,
71
+ pageUrl,
72
+ lambdaUrl,
73
+ mode,
74
+ environment,
75
+ shopperReference,
76
+ amount,
77
+ currency,
78
+ storePaymentMethod,
79
+ ts,
80
+ ]);
54
81
  const handleMessage = (0, react_1.useCallback)((event) => {
55
- var _a, _b;
82
+ var _a;
56
83
  try {
57
- const data = (_b = (_a = event === null || event === void 0 ? void 0 : event.nativeEvent) === null || _a === void 0 ? void 0 : _a.data) !== null && _b !== void 0 ? _b : event === null || event === void 0 ? void 0 : event.data;
58
- if (!data)
84
+ const raw = (_a = event === null || event === void 0 ? void 0 : event.nativeEvent) === null || _a === void 0 ? void 0 : _a.data;
85
+ if (!raw)
59
86
  return;
60
- const message = JSON.parse(data);
61
- if (message.source !== "PaymentJS")
87
+ const message = JSON.parse(raw);
88
+ if (message.source !== "PaymentJS" || !message.event)
62
89
  return;
90
+ if (message.event === "DEBUG") {
91
+ if (debug)
92
+ console.log("💳 PaymentJS DEBUG:", message.payload);
93
+ onDebug === null || onDebug === void 0 ? void 0 : onDebug(message.payload);
94
+ return;
95
+ }
63
96
  switch (message.event) {
64
97
  case "PAYMENT_SUCCESS":
65
- onSuccess(message.payload);
66
- break;
98
+ onPaymentSuccess === null || onPaymentSuccess === void 0 ? void 0 : onPaymentSuccess(message.payload);
99
+ return;
67
100
  case "PAYMENT_ERROR":
68
101
  case "CONFIG_ERROR":
69
- onFailure === null || onFailure === void 0 ? void 0 : onFailure(message.payload);
70
- break;
71
- case "DEBUG":
72
- if (debug)
73
- console.log("💳 PaymentJS DEBUG:", message.payload);
74
- onDebug === null || onDebug === void 0 ? void 0 : onDebug(message.payload);
102
+ onPaymentFailure === null || onPaymentFailure === void 0 ? void 0 : onPaymentFailure(message.payload);
103
+ return;
104
+ case "ENCRYPT_SUCCESS":
105
+ onEncryptSuccess === null || onEncryptSuccess === void 0 ? void 0 : onEncryptSuccess(message.payload);
106
+ return;
107
+ case "ENCRYPT_ERROR":
108
+ onEncryptFailure === null || onEncryptFailure === void 0 ? void 0 : onEncryptFailure(message.payload);
75
109
  return;
76
110
  default:
77
- console.warn("Unknown PaymentJS event:", message.event);
111
+ if (debug)
112
+ console.warn("Unknown PaymentJS event:", message.event);
113
+ onDebug === null || onDebug === void 0 ? void 0 : onDebug({ step: "UNKNOWN_EVENT", message });
114
+ return;
78
115
  }
79
116
  }
80
117
  catch (err) {
81
- console.error("Failed to parse WebView message:", err);
82
- onFailure === null || onFailure === void 0 ? void 0 : onFailure({
118
+ if (debug)
119
+ console.error("Failed to parse WebView message:", err);
120
+ onPaymentFailure === null || onPaymentFailure === void 0 ? void 0 : onPaymentFailure({
83
121
  message: "Invalid message from payment WebView",
84
- error: err,
122
+ error: String(err),
85
123
  });
86
124
  }
87
- }, [onSuccess, onFailure, onDebug, debug]);
125
+ }, [
126
+ debug,
127
+ onDebug,
128
+ onPaymentSuccess,
129
+ onPaymentFailure,
130
+ onEncryptSuccess,
131
+ onEncryptFailure,
132
+ ]);
133
+ // Optional: upfront validation to avoid “Invalid config”
134
+ const configError = (0, react_1.useMemo)(() => {
135
+ if (!finalUri)
136
+ return "Missing payment page URL";
137
+ if (mode === "pay") {
138
+ if (!shopperReference)
139
+ return "shopperReference is required in pay mode";
140
+ if (!amount || amount <= 0)
141
+ return "amount > 0 is required in pay mode";
142
+ }
143
+ if (!lambdaUrl)
144
+ return "lambdaUrl is required";
145
+ return "";
146
+ }, [finalUri, mode, shopperReference, amount, lambdaUrl]);
147
+ if (configError) {
148
+ return (react_1.default.createElement(react_native_1.View, { style: [{ flex: 1, justifyContent: "center", padding: 16 }, style] },
149
+ react_1.default.createElement(react_native_1.Text, { style: { color: "red", textAlign: "center" } }, configError)));
150
+ }
88
151
  return (react_1.default.createElement(react_native_1.View, { style: [{ flex: 1, backgroundColor: "#fff" }, style] },
89
152
  react_1.default.createElement(react_native_webview_1.WebView, { source: { uri: finalUri }, javaScriptEnabled: true, domStorageEnabled: true, originWhitelist: ["*"], startInLoadingState: true, renderLoading: () => (react_1.default.createElement(react_native_1.View, { style: { flex: 1, justifyContent: "center", alignItems: "center" } },
90
- react_1.default.createElement(react_native_1.ActivityIndicator, { size: "large", color: "#2563eb" }))), onMessage: handleMessage, allowsBackForwardNavigationGestures: true, setSupportMultipleWindows: false })));
91
- };
92
- exports.default = PaymentJS;
153
+ react_1.default.createElement(react_native_1.ActivityIndicator, { size: "large" }))), onMessage: handleMessage, allowsBackForwardNavigationGestures: true, setSupportMultipleWindows: false })));
154
+ }
package/dist/types.d.ts CHANGED
@@ -1,31 +1,49 @@
1
- import { StyleProp, ViewStyle } from "react-native";
2
- export type PaymentJSEvent = "PAYMENT_SUCCESS" | "PAYMENT_ERROR" | "CONFIG_ERROR" | "DEBUG";
3
- export interface PaymentJSMessage {
4
- source: "PaymentJS";
5
- event: PaymentJSEvent;
6
- payload: any;
1
+ import type { ViewStyle } from "react-native";
2
+ export type PaymentJSMode = "pay" | "encrypt";
3
+ export type PaymentJSEnvironment = "test" | "live";
4
+ export type PaymentJSEvent = "PAYMENT_SUCCESS" | "PAYMENT_ERROR" | "ENCRYPT_SUCCESS" | "ENCRYPT_ERROR" | "CONFIG_ERROR" | "DEBUG";
5
+ export type PaymentJSMessage = {
6
+ source?: string;
7
+ event?: PaymentJSEvent;
8
+ payload?: any;
7
9
  timestamp?: string;
8
- }
9
- export interface PaymentJSProps {
10
- uri?: string;
10
+ };
11
+ export type PaymentJSProps = {
11
12
  /**
12
- * Called when payment completes successfully
13
+ * Optional: provide a fully built URL.
14
+ * If omitted, we auto-build from props.
13
15
  */
14
- onSuccess: (payload: any) => void;
16
+ uri?: string;
15
17
  /**
16
- * Called when payment fails or config error occurs
18
+ * Hosted HTML page URL (without querystring).
19
+ * Example: "https://pay.school24.net/payment.html"
17
20
  */
18
- onFailure?: (payload: any) => void;
19
- onDebug?: (payload: any) => void;
21
+ pageUrl?: string;
20
22
  /**
21
- * Optional container styling
23
+ * Lambda baseUrl (passed to the HTML as baseUrl=...)
24
+ * Example: "https://xxxxx.lambda-url.ap-southeast-2.on.aws"
22
25
  */
23
- style?: StyleProp<ViewStyle>;
24
- environment?: "test" | "live";
26
+ lambdaUrl?: string;
27
+ mode?: PaymentJSMode;
28
+ environment?: PaymentJSEnvironment;
25
29
  shopperReference?: string;
30
+ /** Required for pay mode */
26
31
  amount?: number;
27
32
  currency?: string;
33
+ /** If true, ask Adyen to store the card */
28
34
  storePaymentMethod?: boolean;
29
- baseUrl?: string;
35
+ /** Extra cache buster */
36
+ ts?: number;
37
+ /** React Native style for wrapper container */
38
+ style?: ViewStyle;
39
+ /** Debug logs */
30
40
  debug?: boolean;
31
- }
41
+ onPaymentSuccess?: (result: any) => void;
42
+ onPaymentFailure?: (err: any) => void;
43
+ onEncryptSuccess?: (payload: {
44
+ paymentMethod: any;
45
+ meta?: any;
46
+ }) => void;
47
+ onEncryptFailure?: (err: any) => void;
48
+ onDebug?: (debug: any) => void;
49
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@school24/paymentjs-rn",
3
- "version": "1.4.1",
3
+ "version": "1.4.5",
4
4
  "description": "React Native WebView wrapper for PaymentJS (Adyen)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",