Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 55 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
LedgerBalance,
PriceCalculationRequest,
PriceCalculationResult,
TransactionHistoryRequest,
TransactionHistoryResponse,
PaymentHistoryRequest,
PaymentHistoryResponse,
GetPaymentsByPrincipalRequest,
LedgerInfo,
SendFundsUsdRequest
} from './types';
Expand Down Expand Up @@ -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<TransactionHistoryResponse> {
this.requireSecretKey('getTransactionHistory');
async getPaymentHistory(request: PaymentHistoryRequest = {}): Promise<PaymentHistoryResponse> {
this.requireSecretKey('getPaymentHistory');
try {
const params = new URLSearchParams();

Expand All @@ -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,
Expand All @@ -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<PaymentHistoryResponse> {
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
});
}
Expand Down
15 changes: 11 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export interface PriceCalculationResult {
decimals: number;
}

export interface TransactionHistoryRequest {
export interface PaymentHistoryRequest {
accountId?: string;
ledgerCanisterId?: string;
fromTimestamp?: Date;
Expand All @@ -201,7 +201,7 @@ export interface TransactionHistoryRequest {
offset?: number;
}

export interface TransactionHistoryItem {
export interface PaymentHistoryItem {
id: string;
transactionId: number;
status: 'pending' | 'completed' | 'failed';
Expand All @@ -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;
Expand Down