Skip to content

Commit ad5a717

Browse files
author
Paulo Cabral
committed
Add js transaction example between 2 accounts
1 parent 77a5abd commit ad5a717

File tree

7 files changed

+608
-0
lines changed

7 files changed

+608
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CLIENT TRANSACTION
2+
==================
3+
4+
# Warning
5+
This example uses a previous created `Bearer Token` (please look at authentication examples) and performs:
6+
7+
- One transaction + commit operation in a single request
8+
- List last transaction performed
9+
10+
# Scenario
11+
Moving funds from cards/accounts/etc
12+
13+
14+
## Stack
15+
- JS/ES6
16+
- `node` v13.14.0 +
17+
18+
## Setup
19+
- see the `package.json`
20+
- type: `npm install` or `yarn install`
21+
- Create a `.env` file based on the `.env.example` file and populate with the required DATA
22+
23+
## RUN
24+
- node index.js
25+
26+
27+
Loading
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Dependencies.
3+
*/
4+
5+
import axios from "axios";
6+
import colors from "colors";
7+
import dotenv from "dotenv";
8+
import path from "path";
9+
10+
colors.setTheme({
11+
silly: "rainbow",
12+
input: "grey",
13+
verbose: "cyan",
14+
prompt: "grey",
15+
info: "green",
16+
data: "grey",
17+
help: "cyan",
18+
warn: "yellow",
19+
debug: "blue",
20+
error: "red",
21+
});
22+
23+
dotenv.config({ path: path.resolve() + "/.env" });
24+
25+
/**
26+
* Create and commit a transaction.
27+
*/
28+
29+
export async function createAndCommitTransaction(data = {}, myCardID = null) {
30+
const url = `${process.env.BASE_URL}/v0/me/cards/${myCardID}/transactions?commit=true`;
31+
32+
const options = {
33+
method: "POST",
34+
headers: {
35+
Authorization: `Bearer ${process.env.TOKEN}`,
36+
"content-type": "application/json",
37+
},
38+
data,
39+
url,
40+
};
41+
42+
const r = axios(options)
43+
.then((response) => {
44+
return response.data;
45+
})
46+
.catch((error) => {
47+
error.response.data.errors
48+
? console.log(JSON.stringify(error.response.data.errors, null, 2).error)
49+
: console.log(JSON.stringify(error, null, 2).error);
50+
throw error;
51+
});
52+
53+
return r;
54+
}
55+
56+
/**
57+
* List user Transactions.
58+
*/
59+
60+
export async function listUserTransactions() {
61+
try {
62+
const r = await axios.get(`${process.env.BASE_URL}/v0/me/transactions`, {
63+
headers: {
64+
Authorization: `Bearer ${process.env.TOKEN}`,
65+
Range: "items=0-50",
66+
},
67+
});
68+
return r.data;
69+
} catch (error) {
70+
error.response.data.errors
71+
? console.log(JSON.stringify(error.response.data.errors, null, 2).error)
72+
: console.log(JSON.stringify(error, null, 2).error);
73+
throw error;
74+
}
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Dependencies.
3+
*/
4+
5+
import _ from "lodash";
6+
import dotenv from "dotenv";
7+
import path from "path";
8+
import {
9+
createAndCommitTransaction,
10+
listUserTransactions,
11+
} from "./ct-transaction.js";
12+
13+
dotenv.config({ path: path.resolve() + "/.env" });
14+
15+
(async () => {
16+
// Define the source. This case is using a CARD ID with `money`.
17+
// Define destination. This particular uses an uphold email account!
18+
})();

0 commit comments

Comments
 (0)