Skip to content

Add transaction sample #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules/
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).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 rest-api/javascript/transaction/client-create-transaction/index.js
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);
}
})();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 "
}
}