@cosmosapp/pay_sdkServeur

Démarrage rapide

Le flux de bout en bout le plus simple : créer une intention de paiement, la compléter dans le portefeuille et valider la transaction.

Si vous voulez simplement voir un paiement fonctionner de bout en bout, commencez ici. Le premier exemple est l'intégration la plus courte possible ; le second montre la répartition réaliste serveur-crée / navigateur-complète.

import { Client, Assets } from '@cosmosapp/pay_sdk';

const client = new Client({ apiKey: process.env.COSMOS_PAY_API_KEY });

// 1. Create a SEP-7 `pay` intent (no source → pay URI + QR).
const intent = await client.paymentIntents.createPay({
  destination: 'GCALNQQBXAPZ2WIRSDDBMSTAKCUH5SG6U76YBFLQLIXJTF7FE5AX7AOO',
  amount: '120.1234567',
  asset: Assets.USDC, // fills the verified mainnet issuer for you
  msg: 'Order #24',
});

console.log(intent.uri); // web+stellar:pay?destination=...
console.log(intent.qr);  // data:image/png;base64,...  (render this)

// 2. After the payer submits a transaction, validate it.
const outcome = await intent.validate('<txHash>');
if (outcome.valid) console.log('Settled!', outcome.status);

Répartition complète (le serveur crée, le navigateur complète)

// ── server.ts ──────────────────────────────────────────────────────
import { Client, Assets } from '@cosmosapp/pay_sdk';
const client = new Client({ apiKey: process.env.COSMOS_PAY_API_KEY });

export async function createIntent() {
  const intent = await client.paymentIntents.createPay({
    destination: 'G...MERCHANT',
    amount: '10',
    asset: Assets.USDC,
  });
  return intent.toJSON(); // send this payload to the browser
}

export function confirm(intentId: string, txHash: string) {
  return client.paymentIntents.validate(intentId, { txHash });
}
// ── browser.ts ─────────────────────────────────────────────────────
import { WebClient } from '@cosmosapp/pay_sdk/web';

const intent = await fetch('/api/create-intent', { method: 'POST' }).then((r) => r.json());
const { txHash, account, wallet } = await new WebClient().pay(intent);

await fetch('/api/confirm', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ intentId: intent.id, txHash }),
});

Références

  • Client : le point d'entrée principal du SDK, un client unique et atomique qui expose chaque ressource via son propre gestionnaire (une conception atomique à client unique).
  • WebClient

Sur cette page