Skip to content

Commit

Permalink
handle user rejected error
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec committed Sep 9, 2024
1 parent b592af1 commit fa83be0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
74 changes: 52 additions & 22 deletions src/pay/components/PayProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { useAccount, useConnect } from 'wagmi';
import { useWaitForTransactionReceipt } from 'wagmi';
import { useValue } from '../../internal/hooks/useValue';
import type { LifeCycleStatus } from '../../transaction';
import {
GENERIC_ERROR_MESSAGE,
USER_REJECTED_ERROR,
} from '../../transaction/constants';
import { useCallsStatus } from '../../transaction/hooks/useCallsStatus';
import { useWriteContracts } from '../../transaction/hooks/useWriteContracts';
import { isUserRejectedRequestError } from '../../transaction/utils/isUserRejectedRequestError';
import { useCommerceContracts } from '../hooks/useCommerceContracts';

type PayContextType = {
Expand Down Expand Up @@ -121,30 +126,55 @@ export function PayProvider({
}, [receipt]);

const handleSubmit = useCallback(async () => {
if (lifeCycleStatus.statusName === 'success') {
// Open Coinbase Commerce receipt
window.open(
`https://commerce.coinbase.com/pay/${chargeId}/receipt`,
'_blank',
'noopener,noreferrer',
);
return;
}
try {
if (lifeCycleStatus.statusName === 'success') {
// Open Coinbase Commerce receipt
window.open(
`https://commerce.coinbase.com/pay/${chargeId}/receipt`,
'_blank',
'noopener,noreferrer',
);
return;
}

if (isConnected) {
// Fetch contracts
await fetchContracts();
} else {
// Prompt for wallet connection
await connectAsync({ connector: connectors[0] });
}
if (isConnected) {
// Fetch contracts
await fetchContracts();
} else {
// Prompt for wallet connection
await connectAsync({ connector: connectors[0] });
}

if (contractsRef.current) {
await writeContractsAsync({
contracts: contractsRef.current,
});
} else {
console.error('Contracts are not available');
if (contractsRef.current) {
await writeContractsAsync({
contracts: contractsRef.current,
});
} else {
console.error('Contracts are not available');
setErrorMessage(GENERIC_ERROR_MESSAGE);
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'PmPPc01', // Pay module PayProvider component 01 error
error: 'Contracts are not available',
message: GENERIC_ERROR_MESSAGE,
},
});
}
} catch (error) {
if (isUserRejectedRequestError(error)) {
setErrorMessage(USER_REJECTED_ERROR);
} else {
setErrorMessage(GENERIC_ERROR_MESSAGE);
setLifeCycleStatus({
statusName: 'error',
statusData: {
code: 'PmPPc02', // Pay module PayProvider component 02 error
error: JSON.stringify(error),
message: GENERIC_ERROR_MESSAGE,
},
});
}
}
}, [
connectAsync,
Expand Down
1 change: 1 addition & 0 deletions src/transaction/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const GENERIC_ERROR_MESSAGE = 'Something went wrong. Please try again.';
export const METHOD_NOT_SUPPORTED_ERROR_SUBSTRING =
'this request method is not supported';
export const SEND_CALLS_NOT_SUPPORTED_ERROR = 'SEND_CALLS_NOT_SUPPORTED_ERROR';
export const USER_REJECTED_ERROR = 'Request denied.';

0 comments on commit fa83be0

Please sign in to comment.