> ## Documentation Index
> Fetch the complete documentation index at: https://trust-link-tsn.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Accept a wallet-to-TIN payment

> Collect a Solana wallet payment for a registered TIN and confirm the recipient's credit.

Add a checkout option that lets the sender pay a recipient by their ten-digit TIN. The sender approves the payment details and then approves the wallet deposit. Use the two resulting Solana signatures to confirm the deposit and the recipient's credit before you mark the order paid.

## Before you begin

* [Install the TSN SDK](https://trust-link-tsn.mintlify.site/sdk/getting-started.md). Connect a wallet with `signMessage` and `signTransaction` support.
* Get the Receiver URL, Solana RPC URL, and an accepted token mint for your TSN deployment. Fund the sender wallet with that token and enough SOL for the deposit transaction fee. [Check your endpoints](https://trust-link-tsn.mintlify.site/developers/getting-started.md#check-your-endpoints).
* Have a registered recipient TIN. If you need one for your test, [create a TIN](https://trust-link-tsn.mintlify.site/developers/create-tin.md) first.
* Resolve the recipient TIN in your trusted backend. Show the sender the TIN and display name from the lookup. Pass the resolved `ownerPubkeyHash` to the example. [Configure recipient lookup](https://trust-link-tsn.mintlify.site/sdk/configuration.md#protected-identity-and-owner-data).

Copy `tsn-docs/sdk/examples/payment-flow.ts` and `tsn-docs/sdk/examples/credit-deposit.ts` from the protocol checkout into your application. Save the three TypeScript blocks below as one module named `wallet-payment.ts` beside those files. Call the functions from your app with its wallet adapter, endpoints, and protected storage. This example uses Node.js ESM. For a browser integration, see the [XP wallet flow](https://github.com/bigdreamsweb3/trustlink-pay/blob/main/protocol-tests/ui/public/tsn-dapp.js) and [browser SDK entry point](https://github.com/bigdreamsweb3/trustlink-pay/blob/main/protocol-tests/ui/recipient-credit-sdk.ts).

## 1. Submit the payment intent

Call `startWalletPayment` from your **Approve payment** action after the sender reviews the recipient, token, amount, and fee. Pass a decimal string such as `"0.01"`. The SDK uses the token's registered decimals. Generate one `paymentId` per payment and reuse it if the response is lost. The example sets the TSN sender fee to zero. The wallet still pays the Solana deposit fee.

```ts theme={null}
import { PublicKey, Transaction } from "@solana/web3.js";
import { getConnection, type CompleteTsnFundingRequest } from "@trustlink/tsn-sdk";
import {
  isPaymentFinalized,
  preparePaymentFunding,
  prepareWalletPayment,
  readPaymentStatus,
  submitPaymentFunding,
  submitPaymentIntent,
  waitForPaymentReadiness,
  type PaymentSession,
  type PreparedPayment,
} from "./payment-flow.js";

type Wallet = {
  publicKey: PublicKey;
  signMessage(message: Uint8Array): Promise<Uint8Array>;
  signTransaction(transaction: Transaction): Promise<Transaction>;
};

export async function startWalletPayment(input: {
  receiverUrl: string;
  rpcUrl: string;
  wallet: Wallet;
  recipient: { tin: string; ownerPubkeyHash: string; displayName?: string };
  paymentId: string;
  mint: string;
  amount: string;
  retainIntent(request: PreparedPayment["request"]): Promise<void>;
}) {
  const prepared = await prepareWalletPayment({
    receiverUrl: input.receiverUrl,
    connection: getConnection(input.rpcUrl),
    sender: input.wallet.publicKey,
    mint: new PublicKey(input.mint),
    paymentId: input.paymentId,
    recipientTin: input.recipient.tin,
    recipientOwnerHash: input.recipient.ownerPubkeyHash,
    recipientDisplayName: input.recipient.displayName,
    amount: input.amount,
    senderFeeAmount: "0",
    totalTokenRequiredUi: Number(input.amount),
    signMessage: message => input.wallet.signMessage(message),
  });

  await input.retainIntent(prepared.request);
  return submitPaymentIntent(prepared);
}
```

Save `session.workId` from the `POST /node/intents` response. Implement `retainIntent` using protected application state. Keep the private funding note opening out of logs, analytics, public receipts, and persistent browser storage.

## 2. Approve and submit funding

Call `waitForPaymentReadiness(session, signal)` to wait for `FUNDING_READY` and load the deposit details. Use an `AbortSignal` to stop waiting when the sender leaves the screen. When the call resolves, enable **Approve funding** and pass the returned readiness to `approveWalletFunding` when the sender clicks it.

```ts theme={null}
export async function approveWalletFunding(
  session: PaymentSession,
  readiness: Awaited<ReturnType<typeof waitForPaymentReadiness>>,
  wallet: Wallet,
  retainCompletion: (completion: CompleteTsnFundingRequest) => Promise<void>,
) {
  const completion = await preparePaymentFunding({
    session,
    readiness,
    sender: wallet.publicKey,
    signTransaction: async deposit => {
      const transaction = Transaction.from(deposit.serialize({
        requireAllSignatures: false,
        verifySignatures: false,
      }));
      transaction.lastValidBlockHeight = deposit.lastValidBlockHeight;
      const signed = await wallet.signTransaction(transaction);
      return signed.serialize();
    },
  });

  await retainCompletion(completion);
  return submitPaymentFunding(session, completion);
}
```

The helper fetches a fresh blockhash just before the wallet prompt. Implement `retainCompletion` to save the exact signed bytes and blockhash context together before the call to `POST /node/network/funding/complete`. Keep the completion with the same `workId` so you can recover a lost response.

## 3. Confirm the credit

Poll `GET /api/work-status/{workId}` with `readWalletPayment`. Display funding and credit separately. After both report `CONFIRMED`, the helper checks that both transactions finalized without errors on your Solana RPC.

```ts theme={null}
export async function readWalletPayment(session: PaymentSession, rpcUrl: string) {
  const status = await readPaymentStatus(session);
  const finalized = await isPaymentFinalized(getConnection(rpcUrl), status);

  return {
    fundingStatus: status.status,
    fundingSignature: status.result?.signature ?? null,
    creditStatus: status.settlement?.status ?? null,
    creditSignature: status.settlement?.result?.signature ?? null,
    finalized,
  };
}
```

Mark the order paid when `finalized` is `true`. If funding is confirmed and credit is still processing, show **Deposit confirmed · Credit pending**. Keep both signatures with the order. The recipient can then [read their updated TIN balance](https://trust-link-tsn.mintlify.site/sdk/owner-balance.md).

## Recover an interrupted payment

If the intent response is lost, submit the saved message, signature, and funding note with the same payment ID. If the funding response is lost, check the original `workId` and any recorded signature. Use the exact saved completion for a retry. For a stale approval or a rejected credit, follow [payment troubleshooting](https://trust-link-tsn.mintlify.site/sdk/troubleshooting.md) to recover the original payment. [Status fields](https://trust-link-tsn.mintlify.site/developers/payment-intent-lifecycle.md) · [Service endpoints](https://trust-link-tsn.mintlify.site/api-reference/services.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.