@appboxo/web-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.
- package/README.md +68 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,6 +41,7 @@ const sdk = new AppboxoWebSDK({
|
|
|
41
41
|
baseUrl?: string; // Optional, default: "https://dashboard.appboxo.com/api/v1"
|
|
42
42
|
debug?: boolean; // Optional, default: false
|
|
43
43
|
onGetAuthCode?: () => Promise<string>; // Optional, for automatic auth code retrieval
|
|
44
|
+
onPaymentRequest?: (params: PaymentRequest) => Promise<PaymentResponse>; // Optional, for handling payment requests
|
|
44
45
|
});
|
|
45
46
|
```
|
|
46
47
|
|
|
@@ -73,6 +74,43 @@ The SDK will try these in order:
|
|
|
73
74
|
2. Callback function
|
|
74
75
|
3. Mock auth code (fallback)
|
|
75
76
|
|
|
77
|
+
### Payment Processing
|
|
78
|
+
|
|
79
|
+
When a miniapp calls `appboxo.pay()`, the SDK will call your `onPaymentRequest` callback. Process the payment and return the result.
|
|
80
|
+
|
|
81
|
+
Payment status values: `'paid'`, `'failed'`, `'in_process'`, or `'cancelled'`.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const sdk = new AppboxoWebSDK({
|
|
85
|
+
clientId: "your-client-id",
|
|
86
|
+
appId: "your-app-id",
|
|
87
|
+
onPaymentRequest: async (paymentData) => {
|
|
88
|
+
// Process payment with your backend
|
|
89
|
+
const response = await fetch('/api/payments/process', {
|
|
90
|
+
method: 'POST',
|
|
91
|
+
body: JSON.stringify(paymentData)
|
|
92
|
+
});
|
|
93
|
+
const result = await response.json();
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
...paymentData,
|
|
97
|
+
status: result.status, // 'paid', 'failed', 'in_process', or 'cancelled'
|
|
98
|
+
hostappOrderId: result.orderId,
|
|
99
|
+
transactionId: result.transactionId, // optional
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Optional: listen for payment completion
|
|
105
|
+
sdk.onPaymentComplete((success, data) => {
|
|
106
|
+
if (success) {
|
|
107
|
+
console.log('Payment succeeded:', data);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Note: When `onPaymentRequest` is set, the SDK automatically tells the miniapp it supports `AppBoxoWebAppPay`, so it can call `appboxo.pay()`.
|
|
113
|
+
|
|
76
114
|
## Methods
|
|
77
115
|
|
|
78
116
|
| Method | Description |
|
|
@@ -100,16 +138,41 @@ SDK handles these miniapp events:
|
|
|
100
138
|
### React Example
|
|
101
139
|
|
|
102
140
|
```tsx
|
|
141
|
+
import { AppboxoWebSDK } from "@appboxo/web-sdk";
|
|
142
|
+
import type { PaymentRequest, PaymentResponse } from "@appboxo/web-sdk";
|
|
143
|
+
|
|
103
144
|
function App() {
|
|
104
145
|
const iframeRef = useRef<HTMLIFrameElement>(null);
|
|
105
146
|
const [sdk, setSdk] = useState<AppboxoWebSDK | null>(null);
|
|
106
147
|
|
|
107
148
|
useEffect(() => {
|
|
108
|
-
const s = new AppboxoWebSDK({
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
149
|
+
const s = new AppboxoWebSDK({
|
|
150
|
+
clientId: "your-client-id",
|
|
151
|
+
appId: "your-app-id",
|
|
152
|
+
onPaymentRequest: async (paymentData: PaymentRequest): Promise<PaymentResponse> => {
|
|
153
|
+
const response = await fetch('/api/payments/process', {
|
|
154
|
+
method: 'POST',
|
|
155
|
+
headers: { 'Content-Type': 'application/json' },
|
|
156
|
+
body: JSON.stringify(paymentData)
|
|
157
|
+
});
|
|
158
|
+
const result = await response.json();
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
...paymentData,
|
|
162
|
+
status: result.status,
|
|
163
|
+
hostappOrderId: result.hostappOrderId,
|
|
164
|
+
};
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
s.setAuthCode("your-auth-code");
|
|
169
|
+
s.onLoginComplete((success, data) => {
|
|
170
|
+
console.log('Login:', success ? 'success' : 'failed', data);
|
|
171
|
+
});
|
|
172
|
+
s.onPaymentComplete((success, data) => {
|
|
173
|
+
console.log('Payment:', success ? 'success' : 'failed', data);
|
|
112
174
|
});
|
|
175
|
+
|
|
113
176
|
setSdk(s);
|
|
114
177
|
return () => s.destroy();
|
|
115
178
|
}, []);
|
|
@@ -121,7 +184,7 @@ function App() {
|
|
|
121
184
|
}
|
|
122
185
|
};
|
|
123
186
|
|
|
124
|
-
return <iframe ref={iframeRef} onLoad={handleLoad} src="
|
|
187
|
+
return <iframe ref={iframeRef} onLoad={handleLoad} src="https://your-miniapp.com" />;
|
|
125
188
|
}
|
|
126
189
|
```
|
|
127
190
|
|