npm package: @mhayk/sagepay
Description: A Node.JS module using the Sage Pay REST API that is based on REST principles.
Author: Mhayk Whandson
Latest version: 1.0.15

# OPayo

A Node.js Client for interacting with the OPayo(SagePay) API

[![NPM version](https://img.shields.io/npm/v/@mhayk/sagepay)](https://www.npmjs.com/package/opayo)
[![Build Status](https://img.shields.io/travis/com/tolujimoh/gocardless-pro-node.svg)]()

## Documentation

See SagePay [API Reference](https://developer.sage.com/api/payments/api/)

## Installation

Install the package with

```sh
npm install @mhayk/sagepay --save
```

or

```sh
yarn add @mhayk/sagepay
```

## Usage

Create a `Client` instance and provide the access_token and environment you want to use.

```js
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

```ts
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

```js
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

```sh
$ npm install
$ npm test
```

or

```sh
$ 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=
```

<!-- ```sh
$ export SANDBOX_ACCESS_TOKEN='Your Sandbox Access Token'
$ npm test
``` -->