-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6378bb6
commit dc94215
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
rest-api/javascript/transaction/client-create-transaction/.env.example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Base Url endpoint | ||
BASE_URL = 'https://api-sandbox.uphold.com' | ||
|
||
ACCESS_TOKEN = '' | ||
DESTINATION_EMAIL_ACCOUNT = '[email protected]' |
2 changes: 2 additions & 0 deletions
2
rest-api/javascript/transaction/client-create-transaction/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules/ |
40 changes: 40 additions & 0 deletions
40
rest-api/javascript/transaction/client-create-transaction/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# User-to-user transaction | ||
|
||
This sample project demonstrates how to perform a transaction from one Uphold user to another, | ||
with the latter identified by their email address. | ||
For further background, please refer to the [API documentation](https://uphold.com/en/developer/api/documentation). | ||
|
||
## Summary | ||
|
||
This sample project performs the following actions: | ||
|
||
- Create and commit a transaction | ||
- Display the data about the transaction | ||
|
||
## Requirements | ||
|
||
- Node.js v13.14.0 or later | ||
- An account at <https://sandbox.uphold.com> with at least $1 USD of available funds | ||
- An access token from that account, to perform authenticated requests to the Uphold API | ||
(see the [authentication](../../authentication) examples for how to obtain one) | ||
|
||
## Setup | ||
|
||
- Run `npm install` (or `yarn install`) | ||
- Create a `.env` file based on the `.env.example` file, and populate it with the required data. | ||
|
||
## Run | ||
|
||
Run `node index.js`. | ||
|
||
The code will locate a card with nonzero balance in the source account, and prepare a $1 USD transaction | ||
from that card to the account identified by the email in the `.env` file. | ||
|
||
The result will depend on the status of the destination email: | ||
|
||
- If it is already associated with an existing Sandbox account, the transaction will be completed immediately | ||
and the funds will become available in the recipient's account. | ||
- If no Sandbox account exists with that email, an "invite"-type transaction will be created, | ||
which will be executed when the associated account is created. | ||
This invite can be cancelled by the sender while the recipient hasn't registered | ||
(which is useful if you use a dummy email address for this). |
Binary file added
BIN
+54.7 KB
.../javascript/transaction/client-create-transaction/assets/transaction-scopes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions
74
rest-api/javascript/transaction/client-create-transaction/ct-transaction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Dependencies. | ||
*/ | ||
|
||
import axios from "axios"; | ||
import colors from "colors"; | ||
import dotenv from "dotenv"; | ||
import path from "path"; | ||
|
||
colors.setTheme({ | ||
silly: "rainbow", | ||
input: "grey", | ||
verbose: "cyan", | ||
prompt: "grey", | ||
info: "green", | ||
data: "grey", | ||
help: "cyan", | ||
warn: "yellow", | ||
debug: "blue", | ||
error: "red", | ||
}); | ||
|
||
dotenv.config({ path: path.resolve() + "/.env" }); | ||
|
||
/** | ||
* Create and commit a transaction. | ||
*/ | ||
|
||
export async function createAndCommitTransaction(data = {}, myCardID = null) { | ||
const options = { | ||
method: "POST", | ||
headers: { | ||
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`, | ||
"content-type": "application/json", | ||
}, | ||
data, | ||
url: `${process.env.BASE_URL}/v0/me/cards/${myCardID}/transactions?commit=true`, | ||
}; | ||
|
||
const response = axios(options) | ||
.then((response) => { | ||
return response.data; | ||
}) | ||
.catch((error) => { | ||
error.response.data.errors | ||
? console.log(JSON.stringify(error.response.data.errors, null, 2).error) | ||
: console.log(JSON.stringify(error, null, 2).error); | ||
throw error; | ||
}); | ||
|
||
return response; | ||
} | ||
|
||
/** | ||
* Get the first card with available balance (if one exists). | ||
*/ | ||
|
||
export async function getCardWithFunds() { | ||
try { | ||
const response = await axios.get(`${process.env.BASE_URL}/v0/me/cards`, { | ||
headers: { | ||
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`, | ||
}, | ||
}); | ||
|
||
// Get the the first card with nonzero available balance | ||
return response.data.filter(card => { return Number(card.available) > 0 })[0]; | ||
} catch (error) { | ||
error.response.data.errors | ||
? console.log(JSON.stringify(error.response.data.errors, null, 2).error) | ||
: console.log(JSON.stringify(error, null, 2).error); | ||
throw error; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
rest-api/javascript/transaction/client-create-transaction/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Dependencies. | ||
*/ | ||
|
||
import _ from "lodash"; | ||
import dotenv from "dotenv"; | ||
import path from "path"; | ||
import { | ||
createAndCommitTransaction, | ||
getCardWithFunds, | ||
} from "./ct-transaction.js"; | ||
|
||
dotenv.config({ path: path.resolve() + "/.env" }); | ||
|
||
(async () => { | ||
// Locate a card that can be used as the source for the transaction. | ||
const sourceCard = await getCardWithFunds(); | ||
// Define the destination as an email address. | ||
const destination = `${process.env.DESTINATION_EMAIL_ACCOUNT}`; | ||
|
||
const data = { | ||
denomination: { | ||
amount: "1", | ||
currency: "USD", | ||
}, | ||
destination, | ||
}; | ||
|
||
const transaction = await createAndCommitTransaction(data, sourceCard.id); | ||
|
||
if (transaction) { | ||
console.log('Transaction:', transaction); | ||
} | ||
})(); |
51 changes: 51 additions & 0 deletions
51
rest-api/javascript/transaction/client-create-transaction/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
rest-api/javascript/transaction/client-create-transaction/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "uphold-ct", | ||
"version": "0.0.1", | ||
"description": "Uphold rest Api test client transaction", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"type": "module", | ||
"dependencies": { | ||
"axios": "^0.20.0", | ||
"btoa": "^1.2.1", | ||
"colors": "^1.4.0", | ||
"dotenv": "^8.2.0", | ||
"js-base64": "^3.5.2", | ||
"lodash": "^4.17.20", | ||
"qs": "^6.9.4" | ||
}, | ||
"engines": { | ||
"node": ">=13" | ||
}, | ||
"scripts": { | ||
"run": "node index.js " | ||
} | ||
} |