The AgilePay Node node.js SDK provides a convenient access to the AgilePay API from applications written in server-side JavaScript.
This package is promise based with promises in mind instead of callback approach.
You can use the promise
style or async
await
style.
Please keep in mind that this package is for use with server-side Node that uses AgilePay secret keys. This package should not be used on the client side. Also note that this package is still in development.
See the Node API docs.
Install the package with:
npm install agile-pay --save
- Register for an account and get your key and secret at AgilePay.
- Add dependency 'agile-pay' in your package.json file.
- Require 'agile-pay' in your file
const agilePay = require('agile-pay')
const client = new agilePay({
'api_key': 'key',
'api_secret': 'secret'
});
promise
example:
agilePay.gateway().create('stripe', { 'secret_key': 'stripe-secret-key' })
.then(res => res.getBody())
.catch(err => err.getStatusCode()));
async
await
examples:
(async function () {
try {
const response = await agilePay.gateway().create('stripe', { 'secret_key': 'stripe-secret-key' });
console.log(response.getStatusCode());
} catch (error) {
console.log(error.getStatusCode());
}
})();
or
async function gateway() {
try {
const response = await agilePay.gateway().create('stripe', { 'secret_key': 'stripe-secret-key' });
console.log(response.getStatusCode());
} catch (error) {
console.log(error.getStatusCode());
}
}
gateway();
In this case the payment method will be retained with the provided gateway, please check the availability of transaction store in the gateways
Gateways list -> http://docs.agilepay.io/#!/gateway
Gateway token -> http://docs.agilepay.io/#!/payment-method-create-gateway-token
promise
example:
agilePay.paymentMethod().createGatewayToken('gateway-reference', {
number: '4111111111111111',
expiry_year: 17,
expiry_month: 12,
cvv: 123,
holder_name: 'John Smith',
})
.then(res => res.getBody().token)
.catch(res => {
// something went wrong
});
async
await
examples:
(async function () {
try {
const gatewayToken = await agilePay.paymentMethod().createGatewayToken('gateway-reference', {
number: '4111111111111111',
expiry_year: 17,
expiry_month: 12,
cvv: 123,
holder_name: 'John Smith',
});
// The response will contain a payment method **token** which is used to perform transactions against the payment method
const token = gatewayToken.getBody().token;
} catch (err) {
// something went wrong
}
})();
or
async function gatewayToken() {
try {
const gatewayToken = await agilePay.paymentMethod().createGatewayToken('gateway-reference', {
number: '4111111111111111',
expiry_year: 17,
expiry_month: 12,
cvv: 123,
holder_name: 'John Smith',
});
// The response will contain a payment method **token** which is used to perform transactions against the payment method
const token = gatewayToken.getBody().token;
return token;
} catch (err) {
// something went wrong
}
}
gatewayToken();
promise
example:
agilePay.transaction()
.setPaymentMethod('payment-method-token')
.auth(5000, 'GBP')
.then(res => {
if (res.getBody().successful) {
// the authorisationwas successful
} else {
// the gateway responded with some errors
res.getBody().errors;
}
})
.catch(res => {
// something went wrong
});
async
await
example:
async function transactionReference() {
try {
const transaction = await agilePay.transaction()
.setPaymentMethod('payment-method-token')
.auth(5000, 'GBP');
if (transaction.getBody().successful) {
const transaction = transaction.getBody().successful;
return transaction;
} else {
const error = transaction.getBody().errors;
return error
}
} catch (err) {
// something went wrong
}
}
// The response will contain a **reference** which can be used for second steps transactions such as **void**, **capture** and **refund**
transactionReference();
agilePay.transaction('authorised-transaction-reference').void()
.then(res => {
if (res.getBody().successful) {
// the pre-authorisation has been successfully cancelled
}
}).catch(res => {
// something went wrong
});
agilePay.transaction('authorised-transaction-reference').capture()
.then(res => {
if (res.getBody().successful) {
// the pre-authorisation has been successfully cancelled
}
}).catch(res => {
// something went wrong
});
agilePay.transaction('authorised-transaction-reference').credit()
.then(res => {
if (res.getBody().successful) {
// the pre-authorisation has been successfully cancelled
}
}).catch(res => {
// something went wrong
});
Below the response object available methods.
Retrieves the entire response
Retrieves the response body
Retrieves the response status code
A big thanks to Ary Homebrew for the huge contribution he has given to this package.