@blocklet/payment-react 1.19.17 → 1.19.19

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.
Files changed (55) hide show
  1. package/README.md +313 -0
  2. package/es/checkout/form.js +2 -2
  3. package/es/components/auto-topup/index.d.ts +14 -0
  4. package/es/components/auto-topup/index.js +417 -0
  5. package/es/components/auto-topup/modal.d.ts +35 -0
  6. package/es/components/auto-topup/modal.js +734 -0
  7. package/es/components/auto-topup/product-card.d.ts +13 -0
  8. package/es/components/auto-topup/product-card.js +173 -0
  9. package/es/components/collapse.d.ts +13 -0
  10. package/es/components/collapse.js +76 -0
  11. package/es/components/input.d.ts +2 -1
  12. package/es/components/input.js +64 -13
  13. package/es/components/label.d.ts +2 -1
  14. package/es/components/label.js +2 -1
  15. package/es/index.d.ts +4 -1
  16. package/es/index.js +7 -1
  17. package/es/libs/util.js +2 -1
  18. package/es/locales/en.js +56 -0
  19. package/es/locales/zh.js +56 -0
  20. package/es/payment/form/index.js +6 -0
  21. package/es/payment/product-item.js +19 -12
  22. package/lib/checkout/form.js +2 -2
  23. package/lib/components/auto-topup/index.d.ts +14 -0
  24. package/lib/components/auto-topup/index.js +451 -0
  25. package/lib/components/auto-topup/modal.d.ts +35 -0
  26. package/lib/components/auto-topup/modal.js +803 -0
  27. package/lib/components/auto-topup/product-card.d.ts +13 -0
  28. package/lib/components/auto-topup/product-card.js +149 -0
  29. package/lib/components/collapse.d.ts +13 -0
  30. package/lib/components/collapse.js +74 -0
  31. package/lib/components/input.d.ts +2 -1
  32. package/lib/components/input.js +66 -24
  33. package/lib/components/label.d.ts +2 -1
  34. package/lib/components/label.js +3 -1
  35. package/lib/index.d.ts +4 -1
  36. package/lib/index.js +24 -0
  37. package/lib/libs/util.js +2 -1
  38. package/lib/locales/en.js +56 -0
  39. package/lib/locales/zh.js +56 -0
  40. package/lib/payment/form/index.js +6 -0
  41. package/lib/payment/product-item.js +21 -12
  42. package/package.json +9 -9
  43. package/src/checkout/form.tsx +2 -2
  44. package/src/components/auto-topup/index.tsx +449 -0
  45. package/src/components/auto-topup/modal.tsx +773 -0
  46. package/src/components/auto-topup/product-card.tsx +156 -0
  47. package/src/components/collapse.tsx +82 -0
  48. package/src/components/input.tsx +71 -22
  49. package/src/components/label.tsx +8 -2
  50. package/src/index.ts +7 -0
  51. package/src/libs/util.ts +1 -0
  52. package/src/locales/en.tsx +59 -0
  53. package/src/locales/zh.tsx +57 -0
  54. package/src/payment/form/index.tsx +6 -0
  55. package/src/payment/product-item.tsx +20 -13
package/README.md CHANGED
@@ -51,6 +51,8 @@ function App() {
51
51
  - `CheckoutTable` - Pricing table display
52
52
  - `CheckoutDonate` - Donation widget
53
53
  - `OverdueInvoicePayment` - Handle overdue invoice payments
54
+ - `AutoTopup` - Auto-recharge configuration display card
55
+ - `AutoTopupModal` - Auto-recharge configuration modal
54
56
 
55
57
  ### Form Components
56
58
  - `FormInput` - Base form input component
@@ -226,6 +228,132 @@ const LazyComponent = createLazyComponent(async () => {
226
228
  });
227
229
  ```
228
230
 
231
+ ### Auto-Topup Components
232
+
233
+ The auto-topup feature allows users to automatically recharge their credit balance when it falls below a specified threshold. This helps ensure uninterrupted service usage.
234
+
235
+ #### AutoTopup
236
+
237
+ Display and manage auto-recharge configurations with different rendering modes.
238
+
239
+ ```tsx
240
+ import { AutoTopup, PaymentProvider } from '@blocklet/payment-react';
241
+
242
+ function CreditManagementPage() {
243
+ return (
244
+ <PaymentProvider session={session}>
245
+ {/* Default mode - fully expanded display */}
246
+ <AutoTopup
247
+ currencyId="credit-currency-id"
248
+ onConfigChange={(config) => {
249
+ console.log('Auto-topup config updated:', config);
250
+ }}
251
+ />
252
+
253
+ {/* Simple mode - collapsed by default, expandable */}
254
+ <AutoTopup
255
+ currencyId="credit-currency-id"
256
+ mode="simple"
257
+ onConfigChange={(config) => {
258
+ // Handle configuration changes
259
+ refreshCreditBalance();
260
+ }}
261
+ />
262
+
263
+ {/* Custom mode - full control over rendering */}
264
+ <AutoTopup
265
+ currencyId="credit-currency-id"
266
+ mode="custom"
267
+ onConfigChange={(config) => console.log('Config updated:', config)}
268
+ >
269
+ {(openModal, config, paymentData, loading) => (
270
+ <div>
271
+ {loading ? (
272
+ <div>Loading auto-topup configuration...</div>
273
+ ) : (
274
+ <div>
275
+ <h3>Auto Recharge Status</h3>
276
+ <p>Status: {config?.enabled ? 'Active' : 'Inactive'}</p>
277
+ {config?.enabled && (
278
+ <p>
279
+ Threshold: {config.threshold} {config.currency?.symbol}
280
+ </p>
281
+ )}
282
+ {paymentData?.balanceInfo && (
283
+ <p>
284
+ Wallet Balance: {paymentData.balanceInfo.token} {config?.rechargeCurrency?.symbol}
285
+ </p>
286
+ )}
287
+ <button onClick={openModal}>Configure Auto-Topup</button>
288
+ </div>
289
+ )}
290
+ </div>
291
+ )}
292
+ </AutoTopup>
293
+ </PaymentProvider>
294
+ );
295
+ }
296
+ ```
297
+
298
+ #### AutoTopupModal
299
+
300
+ Configure auto-recharge settings including threshold, payment method, and purchase amount.
301
+
302
+ ```tsx
303
+ import { AutoTopupModal, PaymentProvider } from '@blocklet/payment-react';
304
+ import { useState } from 'react';
305
+
306
+ function AutoRechargeSettings({ currencyId }) {
307
+ const [modalOpen, setModalOpen] = useState(false);
308
+
309
+ const handleSuccess = (config) => {
310
+ console.log('Auto-recharge configured:', config);
311
+ setModalOpen(false);
312
+ // Refresh the parent component or update state
313
+ };
314
+
315
+ const handleError = (error) => {
316
+ console.error('Configuration failed:', error);
317
+ };
318
+
319
+ return (
320
+ <PaymentProvider session={session}>
321
+ <button onClick={() => setModalOpen(true)}>
322
+ Setup Auto-Recharge
323
+ </button>
324
+
325
+ <AutoTopupModal
326
+ open={modalOpen}
327
+ onClose={() => setModalOpen(false)}
328
+ currencyId={currencyId}
329
+ onSuccess={handleSuccess}
330
+ onError={handleError}
331
+ defaultEnabled={true} // Start with auto-recharge enabled
332
+ />
333
+ </PaymentProvider>
334
+ );
335
+ }
336
+ ```
337
+
338
+ #### Component Props
339
+
340
+ **AutoTopup Props:**
341
+ - `currencyId` [Required] - The currency ID for auto-recharge
342
+ - `onConfigChange` [Optional] - Callback when configuration changes: `(config: AutoRechargeConfig) => void`
343
+ - `mode` [Optional] - Rendering mode: `'default' | 'simple' | 'custom'`
344
+ - `sx` [Optional] - Custom styles
345
+ - `children` [Optional] - Custom render function for `custom` mode: `(openModal, config, paymentData, loading) => ReactNode`
346
+
347
+ **AutoTopupModal Props:**
348
+ - `open` [Required] - Whether modal is open
349
+ - `onClose` [Required] - Close modal callback
350
+ - `currencyId` [Required] - The currency ID for auto-recharge
351
+ - `customerId` [Optional] - Customer ID (defaults to current session user)
352
+ - `onSuccess` [Optional] - Success callback: `(config: AutoRechargeConfig) => void`
353
+ - `onError` [Optional] - Error callback: `(error: any) => void`
354
+ - `defaultEnabled` [Optional] - Whether to default the enabled state to true
355
+
356
+
229
357
  ## Complete Examples
230
358
 
231
359
  ### Donation Page Example
@@ -319,6 +447,191 @@ function DonationPage() {
319
447
  }
320
448
  ```
321
449
 
450
+ ### Auto-Topup Integration Example
451
+
452
+ Complete example showing how to integrate auto-topup functionality into a credit management dashboard.
453
+
454
+ ```tsx
455
+ import {
456
+ PaymentProvider,
457
+ AutoTopup,
458
+ AutoTopupModal,
459
+ CustomerInvoiceList,
460
+ Amount
461
+ } from '@blocklet/payment-react';
462
+ import { useState, useEffect } from 'react';
463
+ import { Grid, Card, CardContent, Typography, Button } from '@mui/material';
464
+
465
+ function CreditDashboard() {
466
+ const [session, setSession] = useState(null);
467
+ const [creditCurrencies, setCreditCurrencies] = useState([]);
468
+ const [showSetupModal, setShowSetupModal] = useState(false);
469
+ const [selectedCurrency, setSelectedCurrency] = useState(null);
470
+
471
+ useEffect(() => {
472
+ // Initialize session and fetch credit currencies
473
+ const initializeDashboard = async () => {
474
+ const userSession = await fetchSession();
475
+ setSession(userSession);
476
+
477
+ const currencies = await fetchCreditCurrencies();
478
+ setCreditCurrencies(currencies);
479
+ };
480
+
481
+ initializeDashboard();
482
+ }, []);
483
+
484
+ const handleAutoTopupChange = (currencyId, config) => {
485
+ console.log(`Auto-topup updated for ${currencyId}:`, config);
486
+ // Update local state or refetch data
487
+ };
488
+
489
+ return (
490
+ <PaymentProvider session={session}>
491
+ <Grid container spacing={3}>
492
+ {/* Credit Balance Cards with Auto-Topup */}
493
+ {creditCurrencies.map((currency) => (
494
+ <Grid item xs={12} md={6} key={currency.id}>
495
+ <Card>
496
+ <CardContent>
497
+ <Typography variant="h6" gutterBottom>
498
+ {currency.name} Balance
499
+ </Typography>
500
+
501
+ {/* Current balance display */}
502
+ <Typography variant="h4" color="primary" gutterBottom>
503
+ <Amount
504
+ amount={currency.balance}
505
+ decimal={currency.decimal}
506
+ symbol={currency.symbol}
507
+ />
508
+ </Typography>
509
+
510
+ {/* Auto-topup configuration */}
511
+ <AutoTopup
512
+ currencyId={currency.id}
513
+ mode="simple"
514
+ onConfigChange={(config) =>
515
+ handleAutoTopupChange(currency.id, config)
516
+ }
517
+ sx={{ mt: 2 }}
518
+ />
519
+
520
+ {/* Manual setup button for currencies without auto-topup */}
521
+ <Button
522
+ variant="outlined"
523
+ onClick={() => {
524
+ setSelectedCurrency(currency);
525
+ setShowSetupModal(true);
526
+ }}
527
+ sx={{ mt: 1 }}
528
+ >
529
+ Configure Auto-Recharge
530
+ </Button>
531
+ </CardContent>
532
+ </Card>
533
+ </Grid>
534
+ ))}
535
+
536
+ {/* Usage History */}
537
+ <Grid item xs={12}>
538
+ <Card>
539
+ <CardContent>
540
+ <Typography variant="h6" gutterBottom>
541
+ Credit Usage History
542
+ </Typography>
543
+ <CustomerInvoiceList
544
+ customer_id={session?.user?.did}
545
+ type="table"
546
+ include_staking
547
+ status="paid,open"
548
+ />
549
+ </CardContent>
550
+ </Card>
551
+ </Grid>
552
+ </Grid>
553
+
554
+ {/* Auto-topup Setup Modal */}
555
+ {showSetupModal && selectedCurrency && (
556
+ <AutoTopupModal
557
+ open={showSetupModal}
558
+ onClose={() => {
559
+ setShowSetupModal(false);
560
+ setSelectedCurrency(null);
561
+ }}
562
+ currencyId={selectedCurrency.id}
563
+ onSuccess={(config) => {
564
+ console.log('Auto-topup configured:', config);
565
+ handleAutoTopupChange(selectedCurrency.id, config);
566
+ setShowSetupModal(false);
567
+ setSelectedCurrency(null);
568
+ }}
569
+ onError={(error) => {
570
+ console.error('Auto-topup setup failed:', error);
571
+ }}
572
+ defaultEnabled={true}
573
+ />
574
+ )}
575
+ </PaymentProvider>
576
+ );
577
+ }
578
+
579
+ // Custom auto-topup display using custom mode
580
+ function CustomAutoTopupDisplay({ currencyId }) {
581
+ return (
582
+ <AutoTopup
583
+ currencyId={currencyId}
584
+ mode="custom"
585
+ >
586
+ {(openModal, config, paymentData, loading) => {
587
+ if (loading) return <div>Loading...</div>;
588
+
589
+ return (
590
+ <Card variant="outlined">
591
+ <CardContent>
592
+ <Typography variant="subtitle1" gutterBottom>
593
+ Smart Auto-Recharge
594
+ </Typography>
595
+
596
+ {config?.enabled ? (
597
+ <div>
598
+ <Typography color="success.main" gutterBottom>
599
+ ✓ Active - Recharges when balance drops below {config.threshold} {config.currency?.symbol}
600
+ </Typography>
601
+
602
+ <Typography variant="body2" color="text.secondary">
603
+ Next recharge: {config.quantity}x {config.price?.product?.name}
604
+ </Typography>
605
+
606
+ {paymentData?.balanceInfo && (
607
+ <Typography variant="body2" sx={{ mt: 1 }}>
608
+ Wallet Balance: {paymentData.balanceInfo.token} {config.rechargeCurrency?.symbol}
609
+ </Typography>
610
+ )}
611
+ </div>
612
+ ) : (
613
+ <Typography color="text.secondary" gutterBottom>
614
+ Auto-recharge is not configured
615
+ </Typography>
616
+ )}
617
+
618
+ <Button
619
+ variant="contained"
620
+ size="small"
621
+ onClick={openModal}
622
+ sx={{ mt: 2 }}
623
+ >
624
+ {config?.enabled ? 'Modify Settings' : 'Setup Auto-Recharge'}
625
+ </Button>
626
+ </CardContent>
627
+ </Card>
628
+ );
629
+ }}
630
+ </AutoTopup>
631
+ );
632
+ }
633
+ ```
634
+
322
635
  ### Subscription Management Example
323
636
 
324
637
  - `ResumeSubscription` component
@@ -53,9 +53,9 @@ export default function CheckoutForm({
53
53
  );
54
54
  }
55
55
  }, [type, mode, data, extraParams]);
56
- const handlePaid = () => {
56
+ const handlePaid = (result) => {
57
57
  setState({ completed: true });
58
- onPaid?.(data);
58
+ onPaid?.(result);
59
59
  };
60
60
  const handleError = (err) => {
61
61
  console.error(err);
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ import { SxProps } from '@mui/material';
3
+ import type { AutoRechargeConfig } from '@blocklet/payment-types';
4
+ export interface AutoTopupCardProps {
5
+ currencyId: string;
6
+ onConfigChange?: (config: AutoRechargeConfig) => void;
7
+ sx?: SxProps;
8
+ mode?: 'default' | 'simple' | 'custom';
9
+ children?: (openModal: () => void, config: AutoRechargeConfig | null, paymentData: {
10
+ paymentInfo: any;
11
+ balanceInfo: any;
12
+ } | null, loading: boolean) => React.ReactNode;
13
+ }
14
+ export default function AutoTopupCard({ currencyId, onConfigChange, sx, mode, children, }: AutoTopupCardProps): JSX.Element | null;