@cosmosapp/pay_sdkServeur
Recettes
Recettes pratiques de bout en bout : paiement, dons, webhooks idempotents, réconciliation et testnet/mainnet.
Des solutions prêtes à copier-coller pour les scénarios courants, assemblées à partir des éléments documentés dans le reste de cette documentation.
Des modèles de bout en bout à copier-coller. Tous les extraits côté serveur supposent :
import { Client, Assets } from '@cosmosapp/pay_sdk';
const client = new Client({ apiKey: process.env.COSMOS_PAY_API_KEY });1. Paiement (le serveur crée, le navigateur paie, le webhook confirme)
// server: create the intent for an order
export async function startCheckout(orderId: string, total: string) {
const intent = await client.paymentIntents.createPay({
destination: process.env.MERCHANT_ACCOUNT,
amount: total,
asset: Assets.USDC,
reference: orderId, // ties the intent to your order
msg: `Order ${orderId}`,
});
return intent.toJSON(); // → send to the browser
}// browser: complete it with the user's wallet
import { WebClient } from '@cosmosapp/pay_sdk/web';
const { txHash } = await new WebClient().pay(intentPayload);
await fetch('/api/confirm', { method: 'POST', body: JSON.stringify({ id: intentPayload.id, txHash }) });// server: fulfill on the webhook (don't trust the browser alone)
client.webhooks.on('paymentIntentSucceeded', async (event) => {
await markOrderPaid(event.data.reference, event.data.id);
});2. Don / cagnotte à pourboires (montant ouvert)
const intent = await client.paymentIntents.createPay({
destination: process.env.MERCHANT_ACCOUNT,
asset: Assets.USDC, // no amount → the wallet asks the user
msg: 'Support us 💜',
});
// browser: await new WebClient().pay(intent, { amount: enteredAmount });3. Gestion idempotente des webhooks
const processed = new Set<string>(); // use your DB in production
client.webhooks.on('event', (event) => {
if (processed.has(event.id)) return; // event.id is stable, so dedupe on it
processed.add(event.id);
handle(event);
});4. Réconcilier un paiement côté serveur
// You received a txHash out-of-band (e.g. polled the browser). Finalize it:
const outcome = await client.paymentIntents.validate(intentId, { txHash });
if (!outcome.valid) {
console.warn('payment not valid:', outcome.reason); // amount/destination/memo mismatch
}5. Testnet vs mainnet
import { Assets, TestnetAssets } from '@cosmosapp/pay_sdk';
// dv_ key → testnet → use TestnetAssets.USDC
// prod_ key → mainnet → use Assets.USDC
const asset = process.env.COSMOS_PAY_API_KEY.startsWith('prod_') ? Assets.USDC : TestnetAssets.USDC;6. Catalogue : produits + clients
const product = await client.products.create({ name: 'Pro', amount: '49.00', assetCode: 'USDC', kind: 'recurring' });
const customer = await client.customers.create({ name: 'Acme', email: 'billing@acme.com', account: 'G...' });
// then create intents referencing the customer's account as destination/source.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
- Assets: Typed asset / wallet catalog or address helper.
- TestnetAssets: Typed asset / wallet catalog or address helper.