1.0.0 • Published 2 months ago

@lucadiba/satispay-client v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

A JavaScript Node.js client for Satispay APIs with built-in TypeScript types. This package supports both ESM and CommonJS.


Installation

Install the package using your preferred package manager:

npm install @lucadiba/satispay-client

Usage

Import the package in your Node.js application:

import Satispay from "@lucadiba/satispay-client";

Initialize the client

Initialize the client with your keyId and privateKey. You can get both using this package. See the Authentication section for more details.

const satispay = new Satispay.Client({
  keyId: "ldg9sbq283og7ua1abpj989kbbm2g60us6f18c1sciq...",
  privateKey: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBg...",
  environment: "sandbox", // optional, defaults to "production"
});

Create a payment

const payment = await satispay.payments.create({
  flow: "MATCH_CODE",
  amountUnit: 100,
  currency: "EUR",
});

// Save the payment id
const paymentId = payment.id;

// Redirect the user to the redirectUrl
const redirectUrl = payment.redirect_url;

Get a payment

satispay.payments.get({
  id: "payment_id",
});

Get all payments

satispay.payments.getAll();

Update a payment

satispay.payments.update({
  id: "payment_id",
  action: "CANCEL",
});

Authentication

The Satispay API uses an authentication method based on a RSA key pair. You can generate a new key pair using the generateKeyPair method:

import Satispay from "@lucadiba/satispay-client";

const { publicKey, privateKey } =
  await Satispay.Authentication.generateKeyPair();

Then, you can use the authenticateWithToken method to get the keyId needed to initialize the client. The token is a 6 characters string that you can find in the Satispay Business Dashboard. It can only be used once, so you need to save the keyId, which can be reused and does not expire.

const { keyId } = await Satispay.Authentication.authenticateWithToken({
  token: "623ECX",
  publicKey,
});

Finally, you can initialize the client:

const satispay = new Satispay.Client({
  keyId,
  privateKey,
});

Errors

Handling errors

This package throws an error when the Satispay API returns an error. You can catch the error and handle it as you prefer.

If the errors comes from the Satispay API, the error will be an instance of SatispayError. There is a utility method to check if an error is a SatispayError.

import { SatispayError } from "@lucadiba/satispay-client";

try {
  await satispay.payments.create({
    flow: "MATCH_CODE",
    amountUnit: -100,
    currency: "EUR",
  });
} catch (error) {
  if (SatispayError.isSatispayError(error)) {
    // The SatispayError type is automatically inferred
    console.error({
      message: error.message,
      data: error.data,
      code: error.code,
      status: error.status,
    });
  } else {
    // The type of error is unknown
    console.error(error);
  }
}

Contributing

Contributions, issues and feature requests are welcome!

License

Copyright © 2022 Luca Dibattista. This project is MIT licensed.

1.0.0

2 months ago

0.1.6

6 months ago

0.1.5

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.1.0

8 months ago