-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransnotify.js
More file actions
64 lines (52 loc) · 2.16 KB
/
Copy pathtransnotify.js
File metadata and controls
64 lines (52 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express');
const app = express();
var StellarSdk = require('stellar-sdk');
// environment variables
process.env.NODE_ENV = 'test';
const config = require('./config/config.js');
var server = new StellarSdk.Server(config.stellar.horizon_server);
var notifyAccount =config.stellar.account_id;
console.log("listening for transactions to address " + notifyAccount);
var txDetailsHandler = function(transactionResult, paymentResponse){
if (transactionResult.memo_type == 'text'){
let memo = transactionResult.memo;
let tagUserType = memo.substr(0,1);
let tagUserId = memo.substr(1,memo.length);
let assetCode = (paymentResponse.asset_type == 'native') ? 'XLM' : paymentResponse.asset_code;
console.log('TAG User Type: ' + tagUserType);
console.log('TAG UserID: ' + tagUserId);
console.log('amount received: ' + paymentResponse.amount);
console.log('asset type: ' + paymentResponse.asset_type);
console.log(assetCode);
//TODO: insert your API call for Tagcash here
}
else{
console.log('no memo included in this transaction.')
}
//console.log(transactionResult);
}
var paymentHandler = function (paymentResponse) {
console.log("new payment activity");
//get the transaction_id from links
var startIndex = paymentResponse._links.transaction.href.lastIndexOf('/') + 1;
var len = paymentResponse._links.transaction.href.length;
var strCount = len - startIndex;
var transId = paymentResponse._links.transaction.href.substr(startIndex, strCount);
//get transaction details from transactionh_hash
server.transactions()
.transaction(paymentResponse.transaction_hash)
.call()
.then(function (transactionResult) {
txDetailsHandler(transactionResult, paymentResponse);
})
.catch(function (err) {
console.error(err);
});
}
//monitor payment operations for an account
var transStream = server.payments()
.cursor('now')
.forAccount(notifyAccount)
.stream({
onmessage: paymentHandler
});