|
1 | 1 | --- |
2 | 2 | title: "getPaymentStatus" |
| 3 | +description: "Check the status of a payment transaction" |
3 | 4 | --- |
4 | 5 |
|
5 | | -## Overview |
| 6 | +Defined in the [Base Account SDK](https://github.com/base/account-sdk) |
6 | 7 |
|
| 8 | +<Info> |
7 | 9 | The `getPaymentStatus` function allows you to check the status of a payment transaction after it has been submitted. Use this to track whether a payment has been completed, is still pending, or has failed. |
8 | | - |
9 | | -## Function Signature |
10 | | - |
11 | | -```typescript |
12 | | -/** |
13 | | - * Checks the status of a payment |
14 | | - * @throws {Error} If the transaction ID is invalid |
15 | | - */ |
16 | | -declare function getPaymentStatus(options: GetPaymentStatusOptions): Promise<PaymentStatus>; |
17 | | -``` |
| 10 | +</Info> |
18 | 11 |
|
19 | 12 | ## Parameters |
20 | 13 |
|
21 | | -### GetPaymentStatusOptions |
| 14 | +<ParamField body="id" type="string" required> |
| 15 | +Transaction hash from the pay result that you want to check the status of. |
22 | 16 |
|
23 | | -```typescript |
24 | | -interface GetPaymentStatusOptions { |
25 | | - /** Transaction hash from pay result */ |
26 | | - id: string; |
27 | | - /** Must match testnet setting used in pay */ |
28 | | - testnet?: boolean; |
29 | | -} |
30 | | -``` |
| 17 | +**Pattern:** `^0x[0-9a-fA-F]{64}$` |
| 18 | +</ParamField> |
| 19 | + |
| 20 | +<ParamField body="testnet" type="boolean"> |
| 21 | +Must match the testnet setting used in the original pay call. Default: false |
| 22 | +</ParamField> |
31 | 23 |
|
32 | 24 | ## Returns |
33 | 25 |
|
34 | | -### PaymentStatus |
35 | | - |
36 | | -```typescript |
37 | | -interface PaymentStatus { |
38 | | - /** Current status of the payment */ |
39 | | - status: "completed" | "pending" | "failed" | "not_found"; |
40 | | - /** Original transaction hash */ |
41 | | - id: string; |
42 | | - /** Human-readable status message */ |
43 | | - message: string; |
44 | | - /** Sender address (present for pending, completed, and failed) */ |
45 | | - sender?: string; |
46 | | - /** Amount that was sent (present for completed transactions) */ |
47 | | - amount?: string; |
48 | | - /** Address that received the payment (present for completed transactions) */ |
49 | | - recipient?: string; |
50 | | - /** Error details (present for failed status) */ |
51 | | - error?: string; |
52 | | -} |
53 | | -``` |
| 26 | +<ResponseField name="result" type="PaymentStatus"> |
| 27 | +Payment status information including current state and details. |
| 28 | + |
| 29 | +<Expandable title="PaymentStatus properties"> |
| 30 | +<ResponseField name="status" type="string"> |
| 31 | +Current status of the payment. |
| 32 | + |
| 33 | +**Possible values:** |
| 34 | +- `"completed"`: Payment successfully processed and confirmed |
| 35 | +- `"pending"`: Payment still being processed by the network |
| 36 | +- `"failed"`: Payment failed to process (funds not transferred) |
| 37 | +- `"not_found"`: Transaction ID not found or invalid |
| 38 | +</ResponseField> |
| 39 | + |
| 40 | +<ResponseField name="id" type="string"> |
| 41 | +Original transaction hash that was queried. |
| 42 | +</ResponseField> |
| 43 | + |
| 44 | +<ResponseField name="message" type="string"> |
| 45 | +Human-readable status message explaining the current state. |
| 46 | +</ResponseField> |
| 47 | + |
| 48 | +<ResponseField name="sender" type="string"> |
| 49 | +Sender address (present for pending, completed, and failed statuses). |
| 50 | +</ResponseField> |
| 51 | + |
| 52 | +<ResponseField name="amount" type="string"> |
| 53 | +Amount that was sent (present for completed transactions). |
| 54 | +</ResponseField> |
| 55 | + |
| 56 | +<ResponseField name="recipient" type="string"> |
| 57 | +Address that received the payment (present for completed transactions). |
| 58 | +</ResponseField> |
54 | 59 |
|
55 | | -## Status Types |
| 60 | +<ResponseField name="error" type="string"> |
| 61 | +Error details (present for failed status). |
| 62 | +</ResponseField> |
| 63 | +</Expandable> |
| 64 | +</ResponseField> |
56 | 65 |
|
57 | | -- **`completed`**: Payment has been successfully processed and confirmed on the blockchain |
58 | | -- **`pending`**: Payment is still being processed by the network |
59 | | -- **`failed`**: Payment failed to process (funds are not transferred) |
60 | | -- **`not_found`**: Transaction ID not found or invalid |
61 | 66 |
|
62 | | -## Usage Example |
| 67 | +<RequestExample> |
| 68 | +```typescript Basic Status Check |
| 69 | +import { getPaymentStatus } from '@base-org/account'; |
63 | 70 |
|
64 | | -```typescript |
| 71 | +const status = await getPaymentStatus({ |
| 72 | + id: "0xabcd1234...", |
| 73 | + testnet: false |
| 74 | +}); |
| 75 | + |
| 76 | +console.log("Payment status:", status.status); |
| 77 | +``` |
| 78 | + |
| 79 | +```typescript Complete Payment Flow |
65 | 80 | import { pay, getPaymentStatus } from '@base-org/account'; |
66 | 81 |
|
67 | | -async function sendAndTrackPayment() { |
68 | | - try { |
69 | | - // First, send a payment |
70 | | - const payResult = await pay({ |
71 | | - amount: "10.50", |
72 | | - to: "0x1234567890123456789012345678901234567890", |
73 | | - testnet: false |
74 | | - }); |
75 | | - |
76 | | - if (payResult.success) { |
77 | | - console.log("Payment submitted:", payResult.id); |
78 | | - |
79 | | - // Check payment status |
80 | | - const status = await getPaymentStatus({ |
81 | | - id: payResult.id, |
82 | | - testnet: false |
83 | | - }); |
84 | | - |
85 | | - console.log("Payment status:", status.status); |
86 | | - console.log("Status message:", status.message); |
87 | | - |
88 | | - if (status.status === "completed") { |
89 | | - console.log("Payment completed successfully!"); |
90 | | - } else if (status.status === "pending") { |
91 | | - console.log("Payment is still processing..."); |
92 | | - } else if (status.status === "failed") { |
93 | | - console.log("Payment failed:", status.message); |
94 | | - } |
95 | | - } |
96 | | - } catch (error) { |
97 | | - console.error("Error:", error); |
98 | | - } |
| 82 | +// Send payment |
| 83 | +const payResult = await pay({ |
| 84 | + amount: "10.50", |
| 85 | + to: "0x1234567890123456789012345678901234567890" |
| 86 | +}); |
| 87 | + |
| 88 | +if (payResult.success) { |
| 89 | + // Check status |
| 90 | + const status = await getPaymentStatus({ |
| 91 | + id: payResult.id, |
| 92 | + testnet: false |
| 93 | + }); |
| 94 | + |
| 95 | + console.log("Status:", status.status); |
| 96 | +} |
| 97 | +``` |
| 98 | +</RequestExample> |
| 99 | + |
| 100 | +<ResponseExample> |
| 101 | +```typescript Completed Payment |
| 102 | +{ |
| 103 | + status: "completed", |
| 104 | + id: "0xabcd1234...", |
| 105 | + message: "Payment completed successfully", |
| 106 | + sender: "0x742d35Cc4Bf53E0e6C42E5d9F0A8D2F6D8A8B7C9", |
| 107 | + amount: "10.50", |
| 108 | + recipient: "0x1234567890123456789012345678901234567890" |
99 | 109 | } |
100 | 110 | ``` |
101 | 111 |
|
102 | | -## Polling for Status Updates |
| 112 | +```typescript Pending Payment |
| 113 | +{ |
| 114 | + status: "pending", |
| 115 | + id: "0xabcd1234...", |
| 116 | + message: "Payment is being processed", |
| 117 | + sender: "0x742d35Cc4Bf53E0e6C42E5d9F0A8D2F6D8A8B7C9" |
| 118 | +} |
| 119 | +``` |
103 | 120 |
|
104 | | -For pending payments, you may want to poll the status periodically: |
| 121 | +```typescript Failed Payment |
| 122 | +{ |
| 123 | + status: "failed", |
| 124 | + id: "0xabcd1234...", |
| 125 | + message: "Payment failed due to insufficient balance", |
| 126 | + sender: "0x742d35Cc4Bf53E0e6C42E5d9F0A8D2F6D8A8B7C9", |
| 127 | + error: "Insufficient balance" |
| 128 | +} |
| 129 | +``` |
105 | 130 |
|
106 | | -```typescript |
107 | | -async function pollPaymentStatus(transactionId: string, testnet: boolean = false) { |
108 | | - const maxAttempts = 30; |
109 | | - const pollInterval = 2000; // 2 seconds |
110 | | - |
111 | | - for (let attempt = 0; attempt < maxAttempts; attempt++) { |
112 | | - try { |
113 | | - const status = await getPaymentStatus({ |
114 | | - id: transactionId, |
115 | | - testnet |
116 | | - }); |
117 | | - |
118 | | - if (status.status === "completed") { |
119 | | - console.log("Payment completed!"); |
120 | | - return status; |
121 | | - } else if (status.status === "failed") { |
122 | | - console.log("Payment failed:", status.message); |
123 | | - return status; |
124 | | - } |
125 | | - |
126 | | - // Still pending, wait before next check |
127 | | - await new Promise(resolve => setTimeout(resolve, pollInterval)); |
128 | | - } catch (error) { |
129 | | - console.error("Error checking payment status:", error); |
130 | | - break; |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - console.log("Payment status check timed out"); |
135 | | - return null; |
| 131 | +```typescript Transaction Not Found |
| 132 | +{ |
| 133 | + status: "not_found", |
| 134 | + id: "0xabcd1234...", |
| 135 | + message: "Transaction not found" |
136 | 136 | } |
137 | 137 | ``` |
| 138 | +</ResponseExample> |
138 | 139 |
|
139 | 140 | ## Error Handling |
140 | 141 |
|
141 | | -The `getPaymentStatus` function can throw errors for: |
142 | | - |
143 | | -- Invalid transaction ID format |
144 | | -- Network connection issues |
145 | | -- Transaction not found |
146 | | - |
147 | | -Always wrap calls to `getPaymentStatus` in a try-catch block to handle these errors gracefully. |
| 142 | +| Code | Message | Description | |
| 143 | +| ---- | ------- | ----------- | |
| 144 | +| 4100 | Invalid transaction ID | The provided transaction ID format is invalid | |
| 145 | +| 4200 | Transaction not found | No transaction found with the given ID | |
| 146 | +| 4300 | Network error | Unable to connect to the Base network to check status | |
148 | 147 |
|
149 | 148 | import PolicyBanner from "/snippets/PolicyBanner.mdx"; |
150 | 149 |
|
|
0 commit comments