Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
waldyrious committed Nov 15, 2020
1 parent 74d1ea4 commit 29b11c3
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 487 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Base Url endpoint
BASE_URL='https://api-sandbox.uphold.com'
BASE_URL = 'https://api-sandbox.uphold.com'

TOKEN =''
DESTINATION_EMAIL_ACCOUNT=''

SOURCE_CARD_ID=''
SOURCE_CARD_SECURITY_CODE=''
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
@@ -1,20 +1,40 @@
# Client transaction
# User-to-user transaction

This sample project demonstrates how to perform a transaction between 2 different accounts. For further background, please refer to the [API documentation](https://uphold.com/en/developer/api/documentation)
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 example uses a previous created `Bearer Token` (please look at authentication examples) and moves funds between 2 different accounts.

This sample project performs the following actions:
- Executes a transaction + commit operation in a single request
- List last transaction performed

- Create and commit a transaction
- Display the data about the transaction

## Requirements
- `node` v13.14.0 +

- 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 `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`

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).
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ dotenv.config({ path: path.resolve() + "/.env" });
*/

export async function createAndCommitTransaction(data = {}, myCardID = null) {
const url = `${process.env.BASE_URL}/v0/me/cards/${myCardID}/transactions?commit=true`;

const options = {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
"content-type": "application/json",
},
data,
url,
url: `${process.env.BASE_URL}/v0/me/cards/${myCardID}/transactions?commit=true`,
};

const r = axios(options)
const response = axios(options)
.then((response) => {
return response.data;
})
Expand All @@ -50,22 +48,23 @@ export async function createAndCommitTransaction(data = {}, myCardID = null) {
throw error;
});

return r;
return response;
}

/**
* List user Transactions.
* Get the first card with available balance (if one exists).
*/

export async function listUserTransactions() {
export async function getCardWithFunds() {
try {
const r = await axios.get(`${process.env.BASE_URL}/v0/me/transactions`, {
const response = await axios.get(`${process.env.BASE_URL}/v0/me/cards`, {
headers: {
Authorization: `Bearer ${process.env.TOKEN}`,
Range: "items=0-50",
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
},
});
return r.data;

// 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)
Expand Down
18 changes: 7 additions & 11 deletions rest-api/javascript/transaction/client-create-transaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,28 @@ import dotenv from "dotenv";
import path from "path";
import {
createAndCommitTransaction,
listUserTransactions,
getCardWithFunds,
} from "./ct-transaction.js";

dotenv.config({ path: path.resolve() + "/.env" });

(async () => {
// Define destination, in this particular example we are going to use an uphold account email
// Define the source, in this case que are using a CARD ID with money.
// 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 sourceCardID = `${process.env.SOURCE_CARD_ID}`;

const data = {
denomination: {
amount: "1",
currency: "USD",
},
destination,
securityCode: `${process.env.SOURCE_CARD_SECURITY_CODE}`, // CARD Security Code!
};

const tran = await createAndCommitTransaction(data, sourceCardID);
const transaction = await createAndCommitTransaction(data, sourceCard.id);

if (tran) {
// Ok, lets list the last transaction performed!
const allTrans = await listUserTransactions();
const orderDesc = _.sortBy(allTrans, (o) => o.createdAt).reverse();
console.log(JSON.stringify(_.head(orderDesc), null, 2));
if (transaction) {
console.log('Transaction:', transaction);
}
})();
Loading

0 comments on commit 29b11c3

Please sign in to comment.