Skip to content
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

[FIX] pass source_currency params properly #1

Merged
merged 2 commits into from
Mar 11, 2015
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
23 changes: 10 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const async = require('async');
const uuid = require('node-uuid');
const RippleRestV1 = require(__dirname+'/lib/clients/rest_v1.js');
const http = require('superagent');
const _ = require('lodash');

var Client = function(options) {
this.api = options.api || 'http://localhost:5990/';
this.api = options.api || 'https://api.ripple.com/';
this.account = options.account;
this.secret = options.secret || '';
this.lastPaymentHash = options.lastPaymentHash;
Expand Down Expand Up @@ -108,21 +109,17 @@ Client.prototype.getAccountBalance = function(callback){
Client.prototype.buildPayment = function(opts, callback){

var amount = opts.amount + "+" + opts.currency;
if (opts.issuer) {
amount += ("+"+ opts.issuer);
}
amount += !_.isEmpty(opts.to_issuer) ? '+' + opts.to_issuer : '';

var url = this.api+'v1/accounts/'+this.account+'/payments/paths/'+opts.recipient+'/'+amount;
var sourceCurrenciesParam = {};
// Source currencies comes in as an array, iterate through it and build a query string param to append to the url
var sourceCurrenciesString = '';

// Source currencies comes in as an array, build a query string param to append to the url
if (opts.source_currencies && opts.source_currencies.length > 0) {
var sourceCurrenciesString = '';
var sourceCurrencies = opts.source_currencies;
for (var i = 0; i < sourceCurrencies.length; i++) {
sourceCurrenciesString = sourceCurrenciesString + sourceCurrencies[i];
if (i < sourceCurrencies.length-1) {
sourceCurrenciesString = sourceCurrenciesString + ',';
}
}
sourceCurrenciesString = opts.source_currencies.join(',');
//TODO: look into this encode/decode issue
sourceCurrenciesString += !_.isEmpty(opts.from_issuer) ? decodeURIComponent('%20') + opts.from_issuer : '';
sourceCurrenciesParam.source_currencies = sourceCurrenciesString;
}

Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ripple-rest-client",
"version": "1.13.1",
"version": "1.13.2",
"description": "A Node.js client library for interacting with Ripple REST servers.",
"main": "./index.js",
"scripts": {
Expand All @@ -22,13 +22,14 @@
"url": "https://github.com/ripple/ripple-rest-client/issues"
},
"dependencies": {
"request": "2.x.x",
"node-uuid": "^1.4.1",
"async": "^0.9.0",
"require-all-to-camel": "^1.0.0",
"bluebird": "^2.3.4",
"superagent": "^0.19.1",
"require-all": "0.0.8"
"lodash": "^3.5.0",
"node-uuid": "^1.4.1",
"request": "2.x.x",
"require-all": "0.0.8",
"require-all-to-camel": "^1.0.0",
"superagent": "^0.19.1"
},
"devDependencies": {
"mocha": "1.16.x",
Expand Down
38 changes: 27 additions & 11 deletions test/build_payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,45 @@ describe('Ripple REST Client buildPayment', function() {
it('should successfully build payment', function(done){

var newPayment = {
currency: 'XRP',
amount: 0.05,
currency: 'GWD',
amount: 10,
recipient: fixtures.ripple_address.destination_account,
source_currencies: ['USD','XRP']
source_currencies: ['GWD'],
from_issuer: fixtures.ripple_address.destination_account,
to_issuer: fixtures.ripple_address.destination_account
};

client.buildPayment(newPayment, function(error, response){
assert(!error);
assert.deepEqual(fixtures.responses['1_4_0'].success.buildPayment, response);
done();
});
});

it('should successfully build payment with multiple source currencies', function(done){

var newPayment = {
currency: 'GWD',
amount: 10,
recipient: fixtures.ripple_address.destination_account,
source_currencies: ['GWD', 'USD'],
from_issuer: fixtures.ripple_address.destination_account,
to_issuer: fixtures.ripple_address.destination_account
};

client.buildPayment(newPayment, function(error, response){
assert(response);
assert(response.payments);
assert(response.payments.length > 0);
assert(response.payments[0].source_account, fixtures.ripple_address.source_account);
assert.strictEqual(response.success, true);
assert(!error);
assert.deepEqual(fixtures.responses['1_4_0'].success.buildPaymentMultipleSourceCurrencies, response);
done();
});

});

it('should fail due to an empty payment object', function(done){
var newPayment = {};

client.buildPayment(newPayment, function(error, response){
assert.strictEqual(error.success, false);
assert.strictEqual(error.error_type, 'invalid_request');
assert(!response);
assert.deepEqual(fixtures.responses['1_4_0'].errors.invalid_request, error);
done();
});
});
Expand Down
70 changes: 68 additions & 2 deletions test/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,73 @@
module.exports = {
ripple_address: {
source_account: 'rMinhWxZz4jeHoJGyddtmwg6dWhyqQKtJz',
destination_account: 'rscJF4TWS2jBe43MvUomTtCcyrbtTRMSNr',
source_account: 'r4p4gZaWSq8Cs1d8mn1jaGqVU1HUns1ek3',
destination_account: 'rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL',
source_account_secret: process.env.RIPPLE_ACCOUNT_SECRET
},
responses: {
'1_4_0': {
errors: {
invalid_request: {
success: false,
error_type: 'invalid_request',
error: 'restINVALID_PARAMETER',
message: 'Parameter is not a valid Ripple address: destination_account'
}
},
success: {
buildPayment: {
"success": true,
"payments": [
{
"source_account": "r4p4gZaWSq8Cs1d8mn1jaGqVU1HUns1ek3",
"source_tag": "",
"source_amount": {
"value": "10",
"currency": "GWD",
"issuer": "rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL"
},
"source_slippage": "0",
"destination_account": "rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL",
"destination_tag": "",
"destination_amount": {
"value": "10",
"currency": "GWD",
"issuer": "rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL"
},
"invoice_id": "",
"paths": "[]",
"partial_payment": false,
"no_direct_ripple": false
}
]
},
buildPaymentMultipleSourceCurrencies: {
"success": true,
"payments": [
{
"source_account": "r4p4gZaWSq8Cs1d8mn1jaGqVU1HUns1ek3",
"source_tag": "",
"source_amount": {
"value": "10",
"currency": "GWD",
"issuer": ""
},
"source_slippage": "0",
"destination_account": "rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL",
"destination_tag": "",
"destination_amount": {
"value": "10",
"currency": "GWD",
"issuer": "rDmSZbgLbw7qkkgDXMWoiSQX7VZ6KknWcL"
},
"invoice_id": "",
"paths": "[]",
"partial_payment": false,
"no_direct_ripple": false
}
]
}
}
}
}
};