Node.JS SDK

Accept payments quickly and easily with eWAY and Node.js using the eWAY Rapid Node.js SDK. Using the eWAY Node.js SDK provides convenient access to all the features of eWAY’s Rapid API, so you can create transactions, process refunds, query transactions, create token customers, and more!

Getting Started

Requirements

The eWAY Node.js SDK requires Node version 0.10 and requires bluebird, express-uri-template, lodash and request-promise. We recommend installing using the npm package manager to install it.

To make development and testing with eWAY easy, get a free partner account which provides access to the eWAY Sandbox.

Install

Using npm, the eWAY Node.js SDK can be installed with the following command:

1
$ npm install eway-rapid

BUILD

Once installed, the eWAY Node.js SDK can be included in your project by requiring it:

1
var rapid = require('eway-rapid');

Now you are all set to build your eWAY integration! You can find code samples for integrating various eWAY functions as part of the eWAY Rapid API reference.

Example Integration

To demonstrate adding the eWAY ​​Node.js SDK, this example will show how to accept a payment with the Responsive Shared Page.

1. Add the eWAY Node.js SDK
In the project directory, run the npm command to install the eWAY Node.js SDK
1
$ npm install eway-rapid

Once completed, the SDK and its dependencies will be downloaded and added to your project’s node_modules directory.

2. Load the eWAY Node.js SDK
In the file where you wish to initiate your eWAY transaction, add the eWAY SDK:
1
var rapid = require('eway-rapid');
Now the eWAY client can be created with your Rapid API key & password. These can be found in Sandbox or Live MYeWAY – remember that different credentials are needed for sandbox to production.

1
2
3
4
5
// eWAY Credentials
var key = 'A1001CTYCxgwTHvadCWKyV9m/ixKCimCqN/cv5/2+SiU0iNc267zZAdNMpqUkizVY9tG7J',
password = 's4nydboX',
endpoint = 'sandbox';// Create the eWAY Client
var client = rapid.createClient(key, password, endpoint);
3. Create a Shared Payment URL

In order to send a customer to the Responsive Shared Page, a URL must be generated using the createTransaction function. This accepts a JSON object – more details of the variables that can be passed can be found in the Rapid API reference.

This step would usually take place after collecting the customer’s information in a checkout process. For this example, just some dummy data will be used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
client.createTransaction(rapid.Enum.Method.RESPONSIVE_SHARED, {
"Payment": {
"TotalAmount": 100
},
// Change these to your server
"RedirectUrl": "http://www.eway.com.au",
"CancelUrl": "http://www.eway.com.au",
"TransactionType": "Purchase"
})
.then(function (response) {
if (response.getErrors().length == 0) {
var redirectURL = response.get('SharedPaymentUrl');
} else {
response.getErrors().forEach(function(error) {
console.log("Response Messages: " + rapid.getMessage(error, "en"));
});
}
})
.catch(function(reason) {
reason.getErrors().forEach(function(error) {
console.log("Response Messages: " + rapid.getMessage(error, "en"));
});
});
4. ​Redirect the Customer


​Usually once the shared page URL has been created, the customer can be immediately redirected. Alternatively, you can display the URL (how this is displayed will depend on your Node.js framework):

1
'<a href="' + $sharedURL + '">Pay with our secure payment page</a>'

Now a link will be displayed which goes to the Responsive Shared Page created with the details submitted in the previous step.

5. ​​Fetch the Result

Once the customer has gone to the Responsive Shared Page and submitted their payment information, they will be redirected back to the RedirectUrl specified in the request. This page should use the AccessCode (passed as a query variable) to fetch the result of the transaction.

1
client.queryTransaction(accessCode)

Key information such as the success of the transaction and the transaction ID can be found in the response. If the transaction wasn’t successful, the reason can be found in the ResponsiveMessage. An example of handling this information would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.then(function (response) {
if (response.get('Transactions[0].TransactionStatus')) {
console.log('Payment successful! ID: ' + response.get('Transactions[0].TransactionID'));
} else {
var errorCodes = response.get('Transactions[0].ResponseMessage').split(', ');
errorCodes.forEach(function(errorCode) {
console.log("Response Message: " + rapid.getMessage(errorCode, "en"));
});
}
})
.catch(function(reason) {
reason.getErrors().forEach(function(error) {
console.log("Response Messages: " + rapid.getMessage(error, "en"));
});
});

That’s it – a complete Responsive Shared Page integration!

For more information on the functions available in the eWAY Node.js SDK, check out the eWAY Rapid API Reference.

Become an eWAY merchant today.

With 24/7 support, over 250 integrations and 20+ years experience – the team at eWAY are here to provide you with the leading all-in-one payments solution.