Nodejs SDK

TendoPay Client SDK to integrate the TendoPay payment solution with your own Ecommerce.


Changelog

2.0.0

  • Usage of the TendoPay API v2.0
  • The payments flow has some breaking changes, please make sure you follow the updated documentation if you upgrade to v2.x.x

Requirements

Features

  • Make a purchase and securely and redirect the payment to the TendoPay platform.
  • Get purchase information as a callback.

Installation

npm install tendopay --save

Or

yarn add tendopay

Identify your E-Commerce by adding the following to your .env

## Merchant and client Credentials
## For sandbox API credentials login to: https://sandbox.tendopay.ph/merchants/login
## For Production API Credentials login to: https://app.tendopay.ph/merchants/login
CLIENT_ID=
CLIENT_SECRET=

Usage

Make sure to properly handle Promise rejections in the following implementations

Setup

Start by creating the TendoPayClient that you will later use to communicte with the TendoPay platform.

const tendopay = require('tendopay');

const TendoPayClient = tendopay.Client;
const tendoPayClient = new TendoPayClient();

To enable sandbox mode, create the client class with a true property:

...
... new TendoPayClient(true);

1. Make a purchase

To redirect the user to the TendoPay platform with the proper credentials and purchase information, you need to generate the unique TendoPayURL hash.

Payment(...) is a method from the root module, and returns an instance of a TendoPay Payment that will be used below to generate the URL. getTendoPayURL(...) is an async method from the TendoPayClient, and returns the generated URL from the latter's payment property.


Payment:

ParameterDescriptionRequiredTypeExample
amountThe price of the paymentYesInteger1000
currencyThe currency the order is placed in. Only PHP is supported.YesString'PHP'
merchantOrderIdThe order IDYesString'#123123'
descriptionA description of the orderNoString'Test order'
billingCityThe billing address' cityNoString'Manila'
billingAddressThe billing addressNoString'123 Street'
billingPostcodeThe billing postal codeNoString'1234'
shippingCityThe shipping address' cityNoString'Manila'
shippingAddressThe shipping addressNoString'456 Street'
shippingPostcodeThe shipping postal codeNoString'2134'
userIdThe user IDNoString'123'
  • Example Implementation
const userId = '123';
const orderId = '#123123123123';
const orderPrice = 999;
const orderAmount = +orderPrice;
const orderTitle = 'Test Order #1';
const currency = 'PHP';
const billing = { city: 'Manila', 'address': '123 Street', 'postcode': '1234' }
const shipping = { city: 'Manila', 'address': '456 Street', 'postcode': '4567' }

const tendoPayPayment = new tendopay.Payment({
  merchantOrderId: orderId,
  amount: orderAmount,
  currency: currency,
  description: orderTitle,
  billingCity: billing.city,
  billingAddress: billing.address,
  billingPostcode: billing.postcode,
  shippingCity: shipping.city,
  shippingAddress: shipping.address,
  shippingPostcode: shipping.postcode,
  userId: userId
});

tendoPayClient.payment = tendoPayPayment;

redirect(await tendoPayClient.getTendoPayURL());

2. Handle the TendoPay callback

Once the TendoPay platform has handled the payment request, it will redirect to your website with a callback. You should add a handler for that callback in the REDIRECT_URL GET route specified in your .env.

isCallbackRequest(...) is a method from the TendoPayClient, and will assert if the received GET request is legitimate. verifyTransaction(...) is a async method from the TendoPayClient, and will make sure the request you POSTed to the TendoPay platform is correct. VerifyTransactionRequest(...) is a class from the root module, and creates a request instance readable by the verifyTransaction method.


VerifyTransactionRequest:

ParameterDescriptionRequiredTypeExample
statusThe status of the transaction - ['PAID', 'FAILED', 'CANCELED']YesString'PAID'
transactionIdThe transaction idYesString'#123123'
xSignatureThe signature hash shared with TendoPayYesString'abcdefghijklmnopqrstuvwxyz1234567890'
amountThe transaction amountYes if status == 'PAID'String'1000'
messageThe error messageYes if status == 'FAILED'String'There was an error'
merchantOrderIdThe order IDNoString'#123123'
  • Example Implementation
const merchantOrderId = '#123123123';

if (TendoPayClient.isCallbackRequest({request: req})) {
  const transactionStatus = await tendoPayClient.verifyTransaction({
    merchantOrderId,
    verificationRequest: new tendopay.VerifyTransactionRequest({
      requestParams: req.query
    })
  });

  res.json({
    success: transactionStatus,
    query: req.query
  });
} else {
  res.json({
    error: 'Not a callback request'
  });
}

3. Notify the TendoPay platform about the purchase

When you have received and processed the TendoPay response, you need to let the platform know the status of the purchase.

NotifyRequest(...) is a method from the root module, and will create a TendoPay ready request instance. getTransactionDetail(...) is an async method from the TendoPayClient and will return the information you need about the payment.

  • Example Implementation
const {transactionNumber} = new tendopay.NotifyRequest(req.body);
const {merchantId, merchantOrderId, amount, status} =
await tendoPayClient.getTransactionDetail(transactionNumber);

console.log({merchantId, merchantOrderId, amount, status});
// Search Merchant side transaction by merchantOrderId
// Check if the transaction is already processed
// The process should stop here if this transaction is already done.
// return 200 if this is a duplicated notification

switch (status) {
case tendopay.Constants.values.PURCHASE_TRANSACTION_SUCCESS:
  // The transaction is successfully completed
  // Do merchant job here
  break;
  case tendopay.Constants.values.PURCHASE_TRANSACTION_FAILURE:
  // The transaction is unsuccessfully completed.
  // Do merchant job here
  break;
  case tendopay.Constants.values.CANCEL_TRANSACTION_SUCCESS:
  // the previous transaction is successfully cancelled
  // Do merchant job here
  break;
}

res.status(200).json();

4. Request a cancellation of the purchase

If a purchase is eligible (see terms & conditions), you can use the Javascript SDK to request its cancellation.

cancelTransaction(...) is an async method from the TendoPayClient, and will request the cancellation from the TendoPay platform provided a transaction ID.

  • Example Implementation
const transactionNumber = 'TEST-OID-123324567890';
const {status, message} = await tendoPayClient.cancelTransaction({ transactionNumber });

console.log({status, message});

switch (status) {
  case 200:
  // The transaction has been successfully cancelled
  // Do merchant job here
  break;
  default:
  // There was an issue cancelling the transaction
  // Do merchant job here
  break;
}

FAQ

I'm having issues using the SDK, where can I get support ?

Help! I can't find the credentials for the .env file ?

Can I use this SDK on the client side ?

  • The TendoPay SDK should ONLY be used on the server side. Revealing your merchant credentials to the public might endanger your merchant account by allowing people to hack and create fake purchases.

I'm a bit lost, can I get an example ?