diff --git a/package.json b/package.json index 41d0126..5802e8b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@icpay/sdk", - "version": "1.3.41", + "version": "1.3.42", "description": "Official icpay SDK for Internet Computer payments", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/errors.ts b/src/errors.ts index e48ce27..c685f1a 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -58,6 +58,8 @@ export const ICPAY_ERROR_CODES = { // Transaction errors TRANSACTION_HISTORY_FETCH_FAILED: 'TRANSACTION_HISTORY_FETCH_FAILED', + PAYMENT_HISTORY_FETCH_FAILED: 'PAYMENT_HISTORY_FETCH_FAILED', + PAYMENTS_BY_PRINCIPAL_FETCH_FAILED: 'PAYMENTS_BY_PRINCIPAL_FETCH_FAILED', SEND_FUNDS_USD_FAILED: 'SEND_FUNDS_USD_FAILED', // Ledger errors diff --git a/src/index.ts b/src/index.ts index 9d11273..27321bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,8 +11,9 @@ import { LedgerBalance, PriceCalculationRequest, PriceCalculationResult, - TransactionHistoryRequest, - TransactionHistoryResponse, + PaymentHistoryRequest, + PaymentHistoryResponse, + GetPaymentsByPrincipalRequest, LedgerInfo, SendFundsUsdRequest } from './types'; @@ -1059,10 +1060,10 @@ export class Icpay { } /** - * Get transaction history for the account (private method) + * Get payment history for account (requires secret key) */ - async getTransactionHistory(request: TransactionHistoryRequest = {}): Promise { - this.requireSecretKey('getTransactionHistory'); + async getPaymentHistory(request: PaymentHistoryRequest = {}): Promise { + this.requireSecretKey('getPaymentHistory'); try { const params = new URLSearchParams(); @@ -1074,10 +1075,10 @@ export class Icpay { if (request.limit) params.append('limit', request.limit.toString()); if (request.offset) params.append('offset', request.offset.toString()); - const response = await this.privateApiClient!.get(`/sdk/transactions/history?${params.toString()}`); + const response = await this.privateApiClient!.get(`/sdk/payments/history?${params.toString()}`); return { - transactions: response.data.transactions.map((tx: any) => ({ + payments: response.data.payments.map((tx: any) => ({ id: tx.id, status: tx.status, amount: tx.amount, @@ -1100,8 +1101,53 @@ export class Icpay { }; } catch (error) { throw new IcpayError({ - code: 'TRANSACTION_HISTORY_FETCH_FAILED', - message: 'Failed to fetch transaction history', + code: 'PAYMENT_HISTORY_FETCH_FAILED', + message: 'Failed to fetch payment history', + details: error + }); + } + } + + /** + * Get payments by principal ID (for connected wallet) - checks both sender_principal_id and expected_sender_principal + */ + async getPaymentsByPrincipal(request: GetPaymentsByPrincipalRequest): Promise { + this.requireSecretKey('getPaymentsByPrincipal'); + try { + const params = new URLSearchParams(); + + if (request.limit) params.append('limit', request.limit.toString()); + if (request.offset) params.append('offset', request.offset.toString()); + if (request.status) params.append('status', request.status); + + const response = await this.privateApiClient!.get(`/sdk/payments/by-principal/${request.principalId}?${params.toString()}`); + + return { + payments: response.data.payments.map((tx: any) => ({ + id: tx.id, + status: tx.status, + amount: tx.amount, + ledgerCanisterId: tx.ledgerCanisterId, + ledgerSymbol: tx.ledgerSymbol, + fromAddress: tx.fromAddress, + toAddress: tx.toAddress, + fee: tx.fee, + decimals: tx.decimals, + tokenPrice: tx.tokenPrice, + expectedSenderPrincipal: tx.expectedSenderPrincipal, + metadata: tx.metadata, + createdAt: new Date(tx.createdAt), + updatedAt: new Date(tx.updatedAt) + })), + total: response.data.total, + limit: response.data.limit, + offset: response.data.offset, + hasMore: response.data.hasMore + }; + } catch (error) { + throw new IcpayError({ + code: 'PAYMENTS_BY_PRINCIPAL_FETCH_FAILED', + message: 'Failed to fetch payments by principal', details: error }); } diff --git a/src/types/index.ts b/src/types/index.ts index d120f24..b1096ea 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -191,7 +191,7 @@ export interface PriceCalculationResult { decimals: number; } -export interface TransactionHistoryRequest { +export interface PaymentHistoryRequest { accountId?: string; ledgerCanisterId?: string; fromTimestamp?: Date; @@ -201,7 +201,7 @@ export interface TransactionHistoryRequest { offset?: number; } -export interface TransactionHistoryItem { +export interface PaymentHistoryItem { id: string; transactionId: number; status: 'pending' | 'completed' | 'failed'; @@ -221,14 +221,21 @@ export interface TransactionHistoryItem { updatedAt: Date; } -export interface TransactionHistoryResponse { - transactions: TransactionHistoryItem[]; +export interface PaymentHistoryResponse { + payments: PaymentHistoryItem[]; total: number; limit: number; offset: number; hasMore: boolean; } +export interface GetPaymentsByPrincipalRequest { + principalId: string; + limit?: number; + offset?: number; + status?: 'pending' | 'completed' | 'failed'; +} + export interface LedgerInfo { id: string; name: string;