Skip to content

Commit

Permalink
chore: add transactions example (#451)
Browse files Browse the repository at this point in the history
Co-authored-by: Samuel El-Borai <[email protected]>
  • Loading branch information
stainless-app[bot] and dgellow authored Aug 2, 2024
1 parent 68637cf commit 51f36d6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
97 changes: 97 additions & 0 deletions examples/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env -S npm run tsn -T

//
// Run with: LITHIC_API_KEY=<your_api_key> ./examples/transactions.ts
//

import assert from 'assert';
import Lithic from '../src';

const client = new Lithic({
environment: 'sandbox',
});

// Sleep function to add delays to simulate more realistic interactions with the sandbox API
function sleep(ms: number) {
console.log(`Waiting ${ms}ms`);
return new Promise((resolve) => setTimeout(resolve, ms));
}

async function simulateAuthDeclined(card: Lithic.Cards.Card) {
console.log('Simulate a transaction declined...');

const authResponse = await client.transactions.simulateAuthorization({
pan: card.pan!,
amount: 999999999999,
descriptor: 'coffee shop',
});

await sleep(5000);

const transaction = await client.transactions.retrieve(authResponse.token!);
assert.strictEqual(transaction.result, 'DECLINED', 'Authorization was not declined');

console.log('Done');
}

async function simulateAuthClearing(card: Lithic.Cards.Card) {
console.log('Simulate a transaction clearing...');

const authResponse = await client.transactions.simulateAuthorization({
pan: card.pan!,
amount: 50,
descriptor: 'coffee shop',
});

await sleep(5000);

await client.transactions.simulateClearing({
token: authResponse.token!,
});

await sleep(5000);

const transaction = await client.transactions.retrieve(authResponse.token!);

assert.strictEqual(transaction.status, 'SETTLED', 'Transaction was not settled');

console.log('Done');
}

async function simulatePaginatedTransaction(card: Lithic.Cards.Card) {
console.log('Simulate a paginated transaction...');

const firstPage = await client.transactions.list({
card_token: card.token,
account_token: card.account_token,
});

const countItemsInFirstPage = firstPage.data.length;
if (countItemsInFirstPage > 1) {
const secondPage = await client.transactions.list({
card_token: card.token,
account_token: card.account_token,
starting_after: firstPage.data[0]!.token,
});

assert.strictEqual(firstPage.data[1]!.token, secondPage.data[0]!.token);
}

console.log('Done');
}

async function main() {
const card = await client.cards.create({ type: 'VIRTUAL' });
console.log(`Created new card with token '${card.token}'`);

await sleep(2000);

await simulateAuthDeclined(card);
await simulateAuthClearing(card);
await simulatePaginatedTransaction(card);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
4 changes: 2 additions & 2 deletions examples/upload_evidence.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env -S npm run tsn -T

//
// Run with: LITHIC_API_KEY=<your_api_key> yarn tsn examples/upload_evidence.ts
// Run with: LITHIC_API_KEY=<your_api_key> ./examples/upload_evidence.ts
//

import fs from 'fs';
import assert from 'assert';
import Lithic from 'lithic';
import Lithic from '../src';

const lithic = new Lithic({ environment: 'sandbox' });

Expand Down

0 comments on commit 51f36d6

Please sign in to comment.