opayo
v1.0.1
Published
A Node.JS module using the Sage Pay REST API that is based on REST principles.
Downloads
352
Readme
OPayo
A Node.js Client for interracting with the OPayo(SagePay) API
Documentation
See SagePay API Reference
Installation
Install the package with
npm install @mhayk/sagepay --save
or
yarn add @mhayk/sagepay
Usage
Create a Client
instance and provide the access_token and environment you want to use.
const sagepay = require("@mhayk/sagepay");
const Client = sagepay.Client;
// Gets Access Token stored in environment variable
const VENDOR_NAME = process.env.VENDOR_NAME
const SANDBOX_INTEGRATION_KEY = process.env.SANDBOX_INTEGRATION_KEY
const SANDBOX_INTEGRATION_PASSWORD = process.env.SANDBOX_INTEGRATION_PASSWORD
const client = new Client({
vendor_name: VENDOR_NAME,
integration_key: SANDBOX_INTEGRATION_KEY,
integration_password: SANDBOX_INTEGRATION_PASSWORD,
environment: "sandbox",
card: "007",
});
console.log(client)
const merchant = await client.merchant_session_keys.create(
{ vendorName: VENDOR_NAME }
);
const { expiry, merchantSessionKey } = merchant
console.log(`Expiry: ${expiry}, Merchant Session Key: ${merchantSessionKey}`)
client.merchantSessionKey = merchantSessionKey;
const card = {
cardDetails: {
cardholderName: 'TEST',
cardNumber: '4929000005559',
expiryDate: '0821',
securityCode: '123'
}
}
const newCardIdentifier = await client.card_identifiers.create(card)
const { cardIdentifier } = newCardIdentifier
console.log(`Card Identifier: ${cardIdentifier}`)
const payment = {
transactionType: 'Payment',
paymentMethod: {
card: {
merchantSessionKey,
cardIdentifier,
}
},
vendorTxCode: '0102030405-faraan',
amount: 100,
currency: 'GBP',
description: 'MHAYK TEST LIBRARY',
customerFirstName: 'MHAYK WHANDSON',
customerLastName: 'DA SILVA LIMA',
billingAddress: {
address1: "11, Worton Road",
city: 'London',
postalCode: "TW7 6HJ",
country: "GB"
}
}
const transaction = await client.transactions.create(payment)
console.log(transaction)
Usage with Typescript
import { Client } from "@mhayk/sagepay";
// Gets Access Token stored in environment variable
const VENDOR_NAME = process.env.VENDOR_NAME
const SANDBOX_INTEGRATION_KEY = process.env.SANDBOX_INTEGRATION_KEY
const SANDBOX_INTEGRATION_PASSWORD = process.env.SANDBOX_INTEGRATION_PASSWORD
const client = new Client({
vendor_name: VENDOR_NAME,
integration_key: SANDBOX_INTEGRATION_KEY,
integration_password: SANDBOX_INTEGRATION_PASSWORD,
environment: "sandbox",
card: "007",
});
console.log(client)
const merchant = await client.merchant_session_keys.create(
{ vendorName: VENDOR_NAME }
);
const { expiry, merchantSessionKey } = merchant
console.log(`Expiry: ${expiry}, Merchant Session Key: ${merchantSessionKey}`)
client.merchantSessionKey = merchantSessionKey;
const card = {
cardDetails: {
cardholderName: 'TEST',
cardNumber: '4929000005559',
expiryDate: '0821',
securityCode: '123'
}
}
const newCardIdentifier = await client.card_identifiers.create(card)
const { cardIdentifier } = newCardIdentifier
console.log(`Card Identifier: ${cardIdentifier}`)
const payment = {
transactionType: 'Payment',
paymentMethod: {
card: {
merchantSessionKey,
cardIdentifier,
}
},
vendorTxCode: '0102030405-faraan',
amount: 100,
currency: 'GBP',
description: 'MHAYK TEST LIBRARY',
customerFirstName: 'MHAYK WHANDSON',
customerLastName: 'DA SILVA LIMA',
billingAddress: {
address1: "11, Worton Road",
city: 'London',
postalCode: "TW7 6HJ",
country: "GB"
}
}
const transaction = await client.transactions.create(payment)
console.log(transaction)
Events
The SagePay Client instance emits request
and response
events
const sagepay = require("@mhayk/sagepay");
const Client = sagepay.Client;
const VENDOR_NAME = process.env.VENDOR_NAME
const SANDBOX_INTEGRATION_KEY = process.env.SANDBOX_INTEGRATION_KEY
const SANDBOX_INTEGRATION_PASSWORD = process.env.SANDBOX_INTEGRATION_PASSWORD
// Gets Access Token stored in environment variable
const access_token = process.env.ACCESS_TOKEN;
const environment = "sandbox";
const client = new Client({
vendor_name: VENDOR_NAME,
integration_key: SANDBOX_INTEGRATION_KEY,
integration_password: SANDBOX_INTEGRATION_PASSWORD,
environment: "sandbox",
card: "007",
});
// Add the request event handler function:
client.on("request", request => {
// Carry out some actions
});
// Add the request event handler function:
client.on("response", response => {
// Carry out some actions
});
Avaliable resources
- Merchant Session Keys
- Card Identifiers
- Transactions
Development
Running tests
$ npm install
$ npm test
or
$ yarn
$ yarn test
To use your Sandbox Access Token to run the tests, you need to set the file .env with the following variables:
VENDOR_NAME=
SANDBOX_INTEGRATION_KEY=
SANDBOX_INTEGRATION_PASSWORD=