UNPKG

bc-payments-sdk

Version:

BetterCommerce's Payments NodeJS SDK is a complete solution for storefront clients that integrate payments. `bc-payments-sdk` is a single point interface for storefront clients for interacting with payment gateways.

430 lines (429 loc) 18.8 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JuspayPayment = void 0; // Package Imports const bc_juspay_sdk_1 = require("bc-juspay-sdk"); const BasePaymentProvider_1 = require("../../base/entity/BasePaymentProvider"); const enums_1 = require("../../constants/enums"); /** * Class {JuspayPayment} is a concrete implementation of a payment provider. * It provides a set of methods that can be used to interact with the Juspay payment gateway. * * @class JuspayPayment * @extends {BasePaymentProvider} * @implements {IPaymentProvider} * @implements {IJuspayPaymentProvider} * * @remark * This class is responsible for initializing the Juspay SDK and providing the * concrete implementation of the Juspay payment provider methods. */ class JuspayPayment extends BasePaymentProvider_1.BasePaymentProvider { /** * Initializes a payment intent using the Juspay payment gateway. * * This method initializes the SDK and attempts to create a payment intent * with the provided data. If successful, it returns the result of the intent * creation. If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference * - Card: https://docs.juspay.io/api-reference/docs/express-checkout/credit--debit-card-transaction * - UPI: https://docs.juspay.io/api-reference/docs/express-checkout/upi-transaction * - Net Banking: https://docs.juspay.io/api-reference/docs/express-checkout/netbanking-payment * - Wallet: https://docs.juspay.io/api-reference/docs/express-checkout/wallet-payment * * @param data - The payment intent data required by Juspay. * @returns A promise that resolves to the result of the payment intent creation * or an object with error details if an error occurs. */ async initPaymentIntent(data) { try { if (super.initSDK()) { if (data === null || data === void 0 ? void 0 : data.paymentMode) { let paymentResult; const { paymentMode } = data, rest = __rest(data, ["paymentMode"]); switch (data === null || data === void 0 ? void 0 : data.paymentMode) { case enums_1.JuspayPaymentType.CARD: paymentResult = await bc_juspay_sdk_1.ExpressCheckout.Payment.creditDebitCardPayment(Object.assign({}, rest)); break; case enums_1.JuspayPaymentType.UPI: paymentResult = await bc_juspay_sdk_1.ExpressCheckout.Payment.upiIntentPayment(Object.assign({}, rest)); break; case enums_1.JuspayPaymentType.NET_BANKING: paymentResult = await bc_juspay_sdk_1.ExpressCheckout.Payment.netbankingPayment(Object.assign({}, rest)); break; case enums_1.JuspayPaymentType.WALLET: paymentResult = await bc_juspay_sdk_1.ExpressCheckout.Payment.walletPayment(Object.assign({}, rest)); break; } return paymentResult; } } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } requestPayment(data) { throw new Error("Method not implemented."); } /** * Retrieves the details of an order from Juspay. * * This method attempts to retrieve the order details with the provided data. * If successful, it returns the result of the order details request. * If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/order-status-api * * @param data - The order ID required by Juspay. * @returns A promise that resolves to the result of the order details request * or an object with error details if an error occurs. */ async getOrderDetails(data) { try { if (super.initSDK()) { const orderDetailsResult = await bc_juspay_sdk_1.ExpressCheckout.Order.get(data); return orderDetailsResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Retrieves the payment methods available for the merchant from Juspay. * * This method attempts to retrieve the payment methods with the provided data. * If successful, it returns the result of the payment methods request. * If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/payment-methods * * @param data - The payment method data required by Juspay. * @returns A promise that resolves to the result of the payment methods request * or an object with error details if an error occurs. */ async getPaymentMethods(data) { try { if (super.initSDK()) { const paymentMethods = await bc_juspay_sdk_1.ExpressCheckout.Merchant.paymentMethods(data); return paymentMethods; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Retrieves the customer details from Juspay. * * This method attempts to retrieve the customer details with the provided data. * If successful, it returns the result of the customer details request. * If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/getcustomer * * @param data - The customer ID required by Juspay. * @returns A promise that resolves to the result of the customer details request * or an object with error details if an error occurs. */ async getCustomer(data) { try { if (super.initSDK()) { const customerDetails = await bc_juspay_sdk_1.ExpressCheckout.Customer.get(data); return customerDetails; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Creates a customer in Juspay. * * This method initializes the SDK and attempts to create a customer * with the provided data. If successful, it returns the result of the * create customer request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/createcustomer * * @param data - The customer data required by Juspay. * @returns A promise that resolves to the result of the create customer request * or an object with error details if an error occurs. */ async createCustomer(data) { try { if (super.initSDK()) { const createCustomerResult = await bc_juspay_sdk_1.ExpressCheckout.Customer.create(data); return createCustomerResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Creates an order in Juspay. * * This method initializes the SDK and attempts to create an order * with the provided data. If successful, it returns the result of the * create order request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/create-order-api * * @param data - The order data required by Juspay. * @returns A promise that resolves to the result of the create order request * or an object with error details if an error occurs. */ async createOrder(data) { try { if (super.initSDK()) { const createOrderResult = await bc_juspay_sdk_1.ExpressCheckout.Order.create(data); return createOrderResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Updates an order in Juspay. * * This method initializes the SDK and attempts to update an order * with the provided data. If successful, it returns the result of the * update order request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/update-order-api * * @param data - The order data required by Juspay. * @returns A promise that resolves to the result of the update order request * or an object with error details if an error occurs. */ async updateOrder(data) { try { if (super.initSDK()) { const updateOrderResult = await bc_juspay_sdk_1.ExpressCheckout.Order.update(data); return updateOrderResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Retrieves the card information from Juspay. * * This method initializes the SDK and attempts to retrieve the card information * with the provided data. If successful, it returns the result of the card information * request. If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/card-info * * @param data - The card data required by Juspay. * @returns A promise that resolves to the result of the card information request * or an object with error details if an error occurs. */ async getCardInfo(data) { try { if (super.initSDK()) { const cardInfo = await bc_juspay_sdk_1.ExpressCheckout.Card.binInfo(data); return cardInfo; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Tokenizes a card using Juspay. * * This method initializes the SDK and attempts to tokenize a card * with the provided data. If successful, it returns the result of the * tokenization request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://juspay.io/in/docs/api-reference/docs/express-checkout/tokenize * * @param data - The card data required by Juspay. * @returns A promise that resolves to the result of the tokenization request * or an object with error details if an error occurs. */ async tokenizeCard(data) { try { if (super.initSDK()) { const tokenizeCardResult = await bc_juspay_sdk_1.ExpressCheckout.Card.tokenize(data); return tokenizeCardResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Saves a card using Juspay. * * This method initializes the SDK and attempts to save a card * with the provided data. If successful, it returns the result of the * save card request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/add-card * * @param data - The card data required by Juspay. * @returns A promise that resolves to the result of the save card request * or an object with error details if an error occurs. */ async saveCard(data) { try { if (super.initSDK()) { const addCardResult = await bc_juspay_sdk_1.ExpressCheckout.Card.create(data); return addCardResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Deletes a card using Juspay. * * This method initializes the SDK and attempts to delete a card * with the provided data. If successful, it returns the result of the * delete card request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/delete-card * * @param data - The card data required by Juspay. * @returns A promise that resolves to the result of the delete card request * or an object with error details if an error occurs. */ async deleteCard(data) { try { if (super.initSDK()) { const deleteCardResult = await bc_juspay_sdk_1.ExpressCheckout.Card.delete(data); return deleteCardResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Retrieves the cards associated with the merchant from Juspay. * * This method initializes the SDK and attempts to retrieve the cards * with the provided data. If successful, it returns the result of the cards * request. If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/list-stored-cards * * @param data - The card data required by Juspay. * @returns A promise that resolves to the result of the cards request * or an object with error details if an error occurs. */ async getCards(data) { try { if (super.initSDK()) { const listStoredCardsResult = await bc_juspay_sdk_1.ExpressCheckout.Card.listAll(data); return listStoredCardsResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Verifies a VPA using Juspay. * * This method initializes the SDK and attempts to verify a VPA * with the provided data. If successful, it returns the result of the * verification request. If the SDK initialization fails, it returns null. * In case of an error during the process, it returns an object containing * the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/verify-vpa * * @param data - The VPA data required by Juspay. * @returns A promise that resolves to the result of the verification request * or an object with error details if an error occurs. */ async verifyVPA(data) { try { if (super.initSDK()) { const listStoredCardsResult = await bc_juspay_sdk_1.ExpressCheckout.UPI.verifyVPA(data); return listStoredCardsResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } /** * Retrieves the offers associated with the merchant from Juspay. * * This method initializes the SDK and attempts to retrieve the offers * with the provided data. If successful, it returns the result of the offers * request. If the SDK initialization fails, it returns null. In case of an * error during the process, it returns an object containing the error details. * * API Reference - https://docs.juspay.io/api-reference/docs/express-checkout/offer-list * * @param data - The offer data required by Juspay. * @returns A promise that resolves to the result of the offers request * or an object with error details if an error occurs. */ async getOffers(data) { try { if (super.initSDK()) { const listStoredCardsResult = await bc_juspay_sdk_1.ExpressCheckout.Offers.list(data); return listStoredCardsResult; } return null; } catch (error) { return { hasError: true, error: error === null || error === void 0 ? void 0 : error.message }; } } } exports.JuspayPayment = JuspayPayment;