Skip to content

Commit dc94215

Browse files
Paulo Cabralwaldyrious
authored andcommitted
Add transaction sample
1 parent 6378bb6 commit dc94215

File tree

8 files changed

+229
-0
lines changed

8 files changed

+229
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Base Url endpoint
2+
BASE_URL = 'https://api-sandbox.uphold.com'
3+
4+
ACCESS_TOKEN = ''
5+
DESTINATION_EMAIL_ACCOUNT = 'asdf.asdf@email.com'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# User-to-user transaction
2+
3+
This sample project demonstrates how to perform a transaction from one Uphold user to another,
4+
with the latter identified by their email address.
5+
For further background, please refer to the [API documentation](https://uphold.com/en/developer/api/documentation).
6+
7+
## Summary
8+
9+
This sample project performs the following actions:
10+
11+
- Create and commit a transaction
12+
- Display the data about the transaction
13+
14+
## Requirements
15+
16+
- Node.js v13.14.0 or later
17+
- An account at <https://sandbox.uphold.com> with at least $1 USD of available funds
18+
- An access token from that account, to perform authenticated requests to the Uphold API
19+
(see the [authentication](../../authentication) examples for how to obtain one)
20+
21+
## Setup
22+
23+
- Run `npm install` (or `yarn install`)
24+
- Create a `.env` file based on the `.env.example` file, and populate it with the required data.
25+
26+
## Run
27+
28+
Run `node index.js`.
29+
30+
The code will locate a card with nonzero balance in the source account, and prepare a $1 USD transaction
31+
from that card to the account identified by the email in the `.env` file.
32+
33+
The result will depend on the status of the destination email:
34+
35+
- If it is already associated with an existing Sandbox account, the transaction will be completed immediately
36+
and the funds will become available in the recipient's account.
37+
- If no Sandbox account exists with that email, an "invite"-type transaction will be created,
38+
which will be executed when the associated account is created.
39+
This invite can be cancelled by the sender while the recipient hasn't registered
40+
(which is useful if you use a dummy email address for this).
54.7 KB
Loading
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 options = {
31+
method: "POST",
32+
headers: {
33+
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
34+
"content-type": "application/json",
35+
},
36+
data,
37+
url: `${process.env.BASE_URL}/v0/me/cards/${myCardID}/transactions?commit=true`,
38+
};
39+
40+
const response = axios(options)
41+
.then((response) => {
42+
return response.data;
43+
})
44+
.catch((error) => {
45+
error.response.data.errors
46+
? console.log(JSON.stringify(error.response.data.errors, null, 2).error)
47+
: console.log(JSON.stringify(error, null, 2).error);
48+
throw error;
49+
});
50+
51+
return response;
52+
}
53+
54+
/**
55+
* Get the first card with available balance (if one exists).
56+
*/
57+
58+
export async function getCardWithFunds() {
59+
try {
60+
const response = await axios.get(`${process.env.BASE_URL}/v0/me/cards`, {
61+
headers: {
62+
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
63+
},
64+
});
65+
66+
// Get the the first card with nonzero available balance
67+
return response.data.filter(card => { return Number(card.available) > 0 })[0];
68+
} catch (error) {
69+
error.response.data.errors
70+
? console.log(JSON.stringify(error.response.data.errors, null, 2).error)
71+
: console.log(JSON.stringify(error, null, 2).error);
72+
throw error;
73+
}
74+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Dependencies.
3+
*/
4+
5+
import _ from "lodash";
6+
import dotenv from "dotenv";
7+
import path from "path";
8+
import {
9+
createAndCommitTransaction,
10+
getCardWithFunds,
11+
} from "./ct-transaction.js";
12+
13+
dotenv.config({ path: path.resolve() + "/.env" });
14+
15+
(async () => {
16+
// Locate a card that can be used as the source for the transaction.
17+
const sourceCard = await getCardWithFunds();
18+
// Define the destination as an email address.
19+
const destination = `${process.env.DESTINATION_EMAIL_ACCOUNT}`;
20+
21+
const data = {
22+
denomination: {
23+
amount: "1",
24+
currency: "USD",
25+
},
26+
destination,
27+
};
28+
29+
const transaction = await createAndCommitTransaction(data, sourceCard.id);
30+
31+
if (transaction) {
32+
console.log('Transaction:', transaction);
33+
}
34+
})();

rest-api/javascript/transaction/client-create-transaction/package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "uphold-ct",
3+
"version": "0.0.1",
4+
"description": "Uphold rest Api test client transaction",
5+
"license": "MIT",
6+
"main": "index.js",
7+
"type": "module",
8+
"dependencies": {
9+
"axios": "^0.20.0",
10+
"btoa": "^1.2.1",
11+
"colors": "^1.4.0",
12+
"dotenv": "^8.2.0",
13+
"js-base64": "^3.5.2",
14+
"lodash": "^4.17.20",
15+
"qs": "^6.9.4"
16+
},
17+
"engines": {
18+
"node": ">=13"
19+
},
20+
"scripts": {
21+
"run": "node index.js "
22+
}
23+
}

0 commit comments

Comments
 (0)