Skip to content

Commit 30f1efc

Browse files
authored
feat: add option to pass a reference, tidy up callbacks (#6)
1 parent b8d2b9b commit 30f1efc

File tree

12 files changed

+92
-44
lines changed

12 files changed

+92
-44
lines changed

app/components/homepage.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function BasicPayment() {
5555
amountInUsd="25.00"
5656
recipientWallet="0x742d35Cc6639C0532c29e54F3D4B48E4d7d5f1E0"
5757
paymentConfig={{
58+
reference: "your_payment_reference",
5859
rnApiClientId: <YOUR_CLIENT_ID>,
5960
supportedCurrencies: ["ETH-sepolia-sepolia", "FAU-sepolia", "USDC-mainnet"],
6061
}}
@@ -80,10 +81,10 @@ export function BasicPayment() {
8081
totalTax: "0.00",
8182
},
8283
}}
83-
onSuccess={(requestId) => {
84+
onPaymentSuccess={(requestId) => {
8485
console.log("Payment successful:", requestId);
8586
}}
86-
onError={(error) => {
87+
onPaymentError={(error) => {
8788
console.error("Payment failed:", error);
8889
}}
8990
>
@@ -104,7 +105,8 @@ export function PaymentWithWallet() {
104105
recipientWallet="0x742d35Cc6639C0532c29e54F3D4B48E4d7d5f1E0"
105106
walletAccount={walletClient} // Pass existing wallet
106107
paymentConfig={{
107-
rnApiClientId: <YOUR_CLIENT_ID>,
108+
reference: "your_payment_reference",
109+
rnApiClientId: <YOUR_CLIENT_ID>,
108110
supportedCurrencies: ["ETH-sepolia-sepolia", "FAU-sepolia", "USDC-mainnet"],
109111
}}
110112
receiptInfo={{
@@ -133,10 +135,10 @@ export function PaymentWithWallet() {
133135
showRequestScanUrl: true,
134136
showReceiptDownload: true,
135137
}}
136-
onSuccess={(requestId) => {
138+
onPaymentSuccess={(requestId) => {
137139
console.log("Payment successful:", requestId);
138140
}}
139-
onError={(error) => {
141+
onPaymentError={(error) => {
140142
console.error("Payment failed:", error);
141143
}}
142144
>

app/components/payment-widget-wrapper.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export function PaymentWidgetWrapper({
7979
totalUSD: "10.0",
8080
},
8181
}}
82-
onSuccess={handleSuccess}
83-
onError={handleError}
82+
onPaymentSuccess={handleSuccess}
83+
onPaymentError={handleError}
8484
>
8585
<div className="flex p-3 bg-blue-500 items-center rounded-2xl text-white">
8686
Pay using crypto

public/r/payment-widget.json

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

registry/default/payment-widget/README.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ function App() {
7474
totalTax: 0.00,
7575
},
7676
}}
77-
onSuccess={(requestId, transactionReceipts) => console.log("Payment successful:", requestId, transactionReceipts)}
78-
onError={(error) => console.error("Payment failed:", error)}
77+
onPaymentSuccess={(requestId, transactionReceipts) => console.log("Payment successful:", requestId, transactionReceipts)}
78+
onPaymentError={(error) => console.error("Payment failed:", error)}
7979
/>
8080
);
8181
}
@@ -212,11 +212,11 @@ function App() {
212212

213213
### Event Handlers
214214

215-
#### `onSuccess` (optional)
215+
#### `onPaymentSuccess` (optional)
216216
- **Type**: `(requestId: string) => void | Promise<void>`
217217
- **Description**: Callback function called when payment is successfully completed. Receives the Request Network request ID.
218218

219-
#### `onError` (optional)
219+
#### `onPaymentError` (optional)
220220
- **Type**: `(error: PaymentError) => void | Promise<void>`
221221
- **Description**: Callback function called when payment fails. Receives detailed error information.
222222

@@ -234,6 +234,35 @@ function App() {
234234
</PaymentWidget>
235235
```
236236

237+
## PaymentWidget Props
238+
239+
### onComplete
240+
Optional callback that fires when the user closes the payment widget from the success screen. Use this to handle post-payment cleanup or navigation.
241+
242+
```tsx
243+
<PaymentWidget
244+
// ... other props
245+
onComplete={() => {
246+
// Close modal, redirect user, etc.
247+
console.log("Payment flow completed");
248+
}}
249+
/>
250+
```
251+
252+
### PaymentConfig.reference
253+
Optional string to associate with the payment request for your own tracking purposes. This reference will be stored with the Request Network payment data.
254+
255+
```tsx
256+
<PaymentWidget
257+
paymentConfig={{
258+
rnApiClientId: "your-client-id",
259+
reference: "invoice-12345", // Your internal reference
260+
supportedCurrencies: ["ETH-sepolia-sepolia"]
261+
}}
262+
// ... other props
263+
/>
264+
```
265+
237266
## Styling and Theming
238267

239268
The Payment Widget uses Tailwind CSS and respects your application's design system through CSS custom properties. The following variables can be customized:
@@ -280,8 +309,8 @@ function PaymentWithExistingWallet() {
280309
receiptInfo={{
281310
// ... your receipt info
282311
}}
283-
onSuccess={(requestId) => console.log("Payment successful:", requestId)}
284-
onError={(error) => console.error("Payment failed:", error)}
312+
onPaymentSuccess={(requestId) => console.log("Payment successful:", requestId)}
313+
onPaymentError={(error) => console.error("Payment failed:", error)}
285314
>
286315
Pay with My Connected Wallet
287316
</PaymentWidget>
@@ -333,7 +362,7 @@ The widget includes comprehensive error handling for common scenarios:
333362
- **Invalid wallet addresses**
334363
- **API rate limiting**
335364

336-
All errors are passed to the `onError` callback with detailed error information for debugging and user feedback.
365+
All errors are passed to the `onPaymentError` callback with detailed error information for debugging and user feedback.
337366

338367
## Browser Support
339368

registry/default/payment-widget/components/payment-confirmation.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export function PaymentConfirmation({
3232
amountInUsd,
3333
recipientWallet,
3434
connectedWalletAddress,
35-
paymentConfig: { rnApiClientId, feeInfo },
35+
paymentConfig: { rnApiClientId, feeInfo, reference },
3636
receiptInfo: { companyInfo: { name: companyName } = {} },
37-
onError,
37+
onPaymentError,
3838
walletAccount,
3939
} = usePaymentWidgetContext();
4040
const { isExecuting, executePayment } = usePayment(
@@ -58,6 +58,7 @@ export function PaymentConfirmation({
5858
amountInUsd,
5959
recipientWallet,
6060
paymentCurrency: selectedCurrency.id,
61+
reference,
6162
feeInfo,
6263
customerInfo: {
6364
email: buyerInfo.email,
@@ -97,7 +98,7 @@ export function PaymentConfirmation({
9798
}
9899
setLocalError(errorMessage);
99100

100-
onError?.(paymentError);
101+
onPaymentError?.(paymentError);
101102
}
102103
};
103104

registry/default/payment-widget/components/payment-modal.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function PaymentModal({
2727
isOpen,
2828
handleModalOpenChange,
2929
}: PaymentModalProps) {
30-
const { isWalletOverride, receiptInfo, onSuccess } =
30+
const { isWalletOverride, receiptInfo, onPaymentSuccess, onComplete } =
3131
usePaymentWidgetContext();
3232

3333
const [activeStep, setActiveStep] = useState<
@@ -63,7 +63,7 @@ export function PaymentModal({
6363
transactionReceipts[transactionReceipts.length - 1].transactionHash,
6464
);
6565
setActiveStep("payment-success");
66-
await onSuccess?.(requestId, transactionReceipts);
66+
await onPaymentSuccess?.(requestId, transactionReceipts);
6767
};
6868

6969
const reset = () => {
@@ -80,6 +80,7 @@ export function PaymentModal({
8080
// reset modal state when closing from success step
8181
if (!isOpen && activeStep === "payment-success") {
8282
reset();
83+
onComplete?.();
8384
}
8485
handleModalOpenChange(isOpen);
8586
}}

registry/default/payment-widget/context/payment-widget-context/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface PaymentWidgetContextValue {
1111
isWalletOverride: boolean;
1212

1313
paymentConfig: {
14+
reference?: string;
1415
rnApiClientId: string;
1516
feeInfo?: FeeInfo;
1617
supportedCurrencies: string[];
@@ -23,11 +24,12 @@ export interface PaymentWidgetContextValue {
2324

2425
receiptInfo: ReceiptInfo;
2526

26-
onSuccess?: (
27+
onPaymentSuccess?: (
2728
requestId: string,
2829
transactionReceipts: TransactionReceipt[],
2930
) => void | Promise<void>;
30-
onError?: (error: PaymentError) => void | Promise<void>;
31+
onPaymentError?: (error: PaymentError) => void | Promise<void>;
32+
onComplete?: () => void | Promise<void>;
3133
}
3234

3335
export const PaymentWidgetContext =

registry/default/payment-widget/context/payment-widget-context/payment-widget-provider.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ interface PaymentWidgetProviderProps {
1818
>;
1919
uiConfig?: PaymentWidgetProps["uiConfig"];
2020
receiptInfo: ReceiptInfo;
21-
onSuccess?: (
21+
onPaymentSuccess?: (
2222
requestId: string,
2323
transactionReceipts: TransactionReceipt[],
2424
) => void | Promise<void>;
25-
onError?: (error: PaymentError) => void | Promise<void>;
25+
onPaymentError?: (error: PaymentError) => void | Promise<void>;
26+
onComplete?: () => void | Promise<void>;
2627
}
2728

2829
export function PaymentWidgetProvider({
@@ -33,8 +34,9 @@ export function PaymentWidgetProvider({
3334
paymentConfig,
3435
uiConfig,
3536
receiptInfo,
36-
onSuccess,
37-
onError,
37+
onPaymentSuccess,
38+
onPaymentError,
39+
onComplete,
3840
}: PaymentWidgetProviderProps) {
3941
const { address } = useAccount();
4042

@@ -51,6 +53,7 @@ export function PaymentWidgetProvider({
5153
connectedWalletAddress,
5254
isWalletOverride,
5355
paymentConfig: {
56+
reference: paymentConfig.reference,
5457
rnApiClientId: paymentConfig.rnApiClientId,
5558
feeInfo: paymentConfig.feeInfo,
5659
supportedCurrencies: paymentConfig.supportedCurrencies,
@@ -60,8 +63,9 @@ export function PaymentWidgetProvider({
6063
showRequestScanUrl: uiConfig?.showRequestScanUrl ?? true,
6164
},
6265
receiptInfo,
63-
onSuccess,
64-
onError,
66+
onPaymentSuccess,
67+
onPaymentError,
68+
onComplete,
6569
}),
6670
[
6771
amountInUsd,
@@ -72,11 +76,13 @@ export function PaymentWidgetProvider({
7276
paymentConfig.rnApiClientId,
7377
paymentConfig.feeInfo,
7478
paymentConfig.supportedCurrencies,
79+
paymentConfig.reference,
7580
uiConfig?.showReceiptDownload,
7681
uiConfig?.showRequestScanUrl,
7782
receiptInfo,
78-
onSuccess,
79-
onError,
83+
onPaymentSuccess,
84+
onPaymentError,
85+
onComplete,
8086
],
8187
);
8288

registry/default/payment-widget/hooks/use-payment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export const usePayment = (network: string, walletAccount?: WalletClient) => {
100100
paymentCurrency: params.paymentCurrency,
101101
feeInfo: params.feeInfo,
102102
customerInfo: params.customerInfo,
103+
reference: params.reference,
103104
},
104105
sendTransaction: wrappedSendTransaction,
105106
waitForTransaction: wrappedWaitForTransaction,

registry/default/payment-widget/payment-widget.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ export function PaymentWidget({
8989
recipientWallet,
9090
paymentConfig,
9191
receiptInfo,
92-
onSuccess,
93-
onError,
92+
onPaymentSuccess,
93+
onPaymentError,
9494
uiConfig,
9595
walletAccount,
9696
children,
@@ -104,8 +104,8 @@ export function PaymentWidget({
104104
paymentConfig={paymentConfig}
105105
uiConfig={uiConfig}
106106
receiptInfo={receiptInfo}
107-
onSuccess={onSuccess}
108-
onError={onError}
107+
onPaymentSuccess={onPaymentSuccess}
108+
onPaymentError={onPaymentError}
109109
>
110110
<PaymentWidgetInner>{children}</PaymentWidgetInner>
111111
</PaymentWidgetProvider>

0 commit comments

Comments
 (0)