diff --git a/lib/index.ts b/lib/index.ts index 81c97b6..bb00ef1 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,4 @@ export { default as WebpayPlus } from './transbank/webpay/webpay_plus'; -export { default as WebpayPlusModal } from './transbank/webpay/modal'; export { default as Oneclick } from './transbank/webpay/oneclick'; export { default as TransaccionCompleta } from './transbank/webpay/transaccion_completa'; export { default as PatpassComercio } from './transbank/patpass'; @@ -9,4 +8,4 @@ export { default as CommitDetail } from './transbank/webpay/transaccion_completa export { default as Options } from './transbank/common/options'; export { default as Environment } from './transbank/webpay/common/environment'; export { default as IntegrationApiKeys } from './transbank/common/integration_api_keys'; -export { default as IntegrationCommerceCodes } from './transbank/common/integration_commerce_codes'; \ No newline at end of file +export { default as IntegrationCommerceCodes } from './transbank/common/integration_commerce_codes'; diff --git a/lib/transbank/common/base_transaction.ts b/lib/transbank/common/base_transaction.ts index 173b23e..0bfd838 100644 --- a/lib/transbank/common/base_transaction.ts +++ b/lib/transbank/common/base_transaction.ts @@ -8,9 +8,11 @@ class BaseTransaction { * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { + if (options === null) + throw new Error("Options can't be null."); this.options = options; } } -export default BaseTransaction; \ No newline at end of file +export default BaseTransaction; diff --git a/lib/transbank/common/options.ts b/lib/transbank/common/options.ts index 99e9454..1b56d87 100644 --- a/lib/transbank/common/options.ts +++ b/lib/transbank/common/options.ts @@ -12,7 +12,7 @@ class Options { * Production, each has a unique URL. */ environment: string; /** Timeout for requests in milliseconds */ - timeout: number; + timeout?: number; /** * Create an instance of Options @@ -23,10 +23,11 @@ class Options { * @param timeout Timeout for requests in milliseconds */ constructor(commerceCode: string, apiKey: string, environment: string, timeout?: number) { + const defaultTimeout = 1000 * 60 * 10; this.commerceCode = commerceCode; this.apiKey = apiKey; this.environment = environment; - this.timeout = timeout ?? 60000; + this.timeout = timeout ?? defaultTimeout; } } diff --git a/lib/transbank/common/request_service.ts b/lib/transbank/common/request_service.ts index 095fb95..ca09ed7 100644 --- a/lib/transbank/common/request_service.ts +++ b/lib/transbank/common/request_service.ts @@ -57,7 +57,7 @@ const RequestService = { method: request.method, url: options.environment + request.endpoint, headers: requestHeaders, - timeout: 10000, + timeout: options.timeout, data: request.toJson(), }) .then((response) => { diff --git a/lib/transbank/patpass/index.ts b/lib/transbank/patpass/index.ts index 4ee8ade..b7ea7e6 100644 --- a/lib/transbank/patpass/index.ts +++ b/lib/transbank/patpass/index.ts @@ -1,8 +1,4 @@ -import Options from '../common/options'; -import Environment from './common/environment'; import _Inscription from './inscription'; -import IntegrationApiKeys from '../common/integration_api_keys'; -import IntegrationCommerceCodes from '../common/integration_commerce_codes'; module PatpassComercio { @@ -11,46 +7,6 @@ module PatpassComercio { */ export const Inscription: typeof _Inscription = _Inscription; - /** - * Contains currently configured Commerce Code, Api Key and Environment - */ - export let options: Options; - - /** - * @returns currently configured Commerce Code and Api Key - */ - export const getDefaultOptions = () => { - return PatpassComercio.options; - }; - - /** - * This methods configures the module to point to the Production Environment with the given params. - * @param _commerceCode Commerce Code given by Transbank when contracting the product - * @param _apiKey Api Key given by Transbank when you sucessfuly validate your integration - */ - export const configureForProduction = (_commerceCode: string, _apiKey: string) => { - PatpassComercio.options = new Options(_commerceCode, _apiKey, Environment.Production); - }; - - /** - * This methods configures the module to point to the Integration Environment with the given params. - * You can check use the credentials provided in the official docs. - * https://transbankdevelopers.cl/documentacion/como_empezar#codigos-de-comercio - * @param _commerceCode Commerce Code given by Transbank. - * @param _apiKey Api Key given by Transbank. - */ - export const configureForIntegration = (_commerceCode: string, _apiKey: string) => { - PatpassComercio.options = new Options(_commerceCode, _apiKey, Environment.Integration); - }; - - /** - * This method configures the module to use Patpass Comercio in the Integration environment. - */ - export const configureForTesting = () => { - PatpassComercio.options = new Options(IntegrationCommerceCodes.PATPASS_COMERCIO, IntegrationApiKeys.PATPASS_COMERCIO, Environment.Integration); - }; - - } export default PatpassComercio; diff --git a/lib/transbank/patpass/inscription.ts b/lib/transbank/patpass/inscription.ts index fe733e5..43fbbd6 100644 --- a/lib/transbank/patpass/inscription.ts +++ b/lib/transbank/patpass/inscription.ts @@ -1,23 +1,43 @@ import BaseTransaction from '../common/base_transaction'; import Options from '../common/options'; -import PatpassComercio from '.'; import { StatusRequest, StartRequest } from './requests'; import RequestService from '../common/request_service'; -import IntegrationCommerceCodes from '../common/integration_commerce_codes'; -import IntegrationApiKeys from '../common/integration_api_keys'; import Environment from './common/environment'; class Inscription extends BaseTransaction { /** * Constructor class Inscription PatpassComercio. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || PatpassComercio.getDefaultOptions() || new Options(IntegrationCommerceCodes.PATPASS_COMERCIO, IntegrationApiKeys.PATPASS_COMERCIO, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `Inscription` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Inscription` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): Inscription + { + return new Inscription(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `Inscription` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Inscription` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): Inscription + { + return new Inscription(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Starts a card inscription process * @param url URL to which Transbank will redirect after cardholder finish enrolling their card diff --git a/lib/transbank/webpay/modal/index.ts b/lib/transbank/webpay/modal/index.ts deleted file mode 100644 index ba05061..0000000 --- a/lib/transbank/webpay/modal/index.ts +++ /dev/null @@ -1,55 +0,0 @@ -import IntegrationApiKeys from '../../common/integration_api_keys'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import Environment from '../common/environment'; -import Options from '../../common/options'; -import _Transaction from './transaction'; - -module WebpayPlusModal { - /** - * Contains methods used to create, commit, refund and capture Transactions. - */ - export const Transaction: typeof _Transaction = _Transaction; - - /** - * Contains currently configured Commerce Code, Api Key and Environment - */ - export let options: Options; - - /** - * @returns currently configured Commerce Code and Api Key - */ - export const getDefaultOptions = () => { - return WebpayPlusModal.options; - }; - - /** - * This methods configures the module to point to the Production Environment with the given params. - * @param _commerceCode Commerce Code given by Transbank when contracting the product - * @param _apiKey Api Key given by Transbank when you sucessfuly validate your integration - */ - export const configureForProduction = (_commerceCode: string, _apiKey: string) => { - WebpayPlusModal.options = new Options(_commerceCode, _apiKey, Environment.Production); - }; - - /** - * This methods configures the module to point to the Integration Environment with the given params. - * You can check use the credentials provided in the official docs. - * https://transbankdevelopers.cl/documentacion/como_empezar#codigos-de-comercio - * @param _commerceCode Commerce Code given by Transbank. - * @param _apiKey Api Key given by Transbank. - */ - export const configureForIntegration = (_commerceCode: string, _apiKey: string) => { - WebpayPlusModal.options = new Options(_commerceCode, _apiKey, Environment.Integration); - }; - - /** - * This method configures the module to use Webpay Plus in the Integration environment. - */ - export const configureWebpayPlusForTesting = () => { - WebpayPlusModal.options = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MODAL, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - -} - -export default WebpayPlusModal; \ No newline at end of file diff --git a/lib/transbank/webpay/modal/requests/index.ts b/lib/transbank/webpay/modal/requests/index.ts deleted file mode 100644 index a112cdc..0000000 --- a/lib/transbank/webpay/modal/requests/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { ModalCreateRequest } from './modal_create_request'; - -export { - ModalCreateRequest -}; diff --git a/lib/transbank/webpay/modal/requests/modal_create_request.ts b/lib/transbank/webpay/modal/requests/modal_create_request.ts deleted file mode 100644 index 2375566..0000000 --- a/lib/transbank/webpay/modal/requests/modal_create_request.ts +++ /dev/null @@ -1,26 +0,0 @@ -import ApiConstants from '../../../common/api_constants'; -import RequestBase from '../../../common/request_base'; - -class ModalCreateRequest extends RequestBase { - buyOrder: string; - sessionId: string; - amount: number; - - constructor(buyOrder: string, sessionId: string, amount: number) { - super(`${ApiConstants.WEBPAY_ENDPOINT}/transactions`, 'POST'); - - this.buyOrder = buyOrder; - this.sessionId = sessionId; - this.amount = amount; - } - - toJson(): string { - return JSON.stringify({ - buy_order: this.buyOrder, - session_id: this.sessionId, - amount: this.amount, - }); - } -} - -export { ModalCreateRequest }; diff --git a/lib/transbank/webpay/modal/transaction.ts b/lib/transbank/webpay/modal/transaction.ts deleted file mode 100644 index af77cbb..0000000 --- a/lib/transbank/webpay/modal/transaction.ts +++ /dev/null @@ -1,77 +0,0 @@ -import Options from '../../common/options'; -import BaseTransaction from '../../common/base_transaction'; -import WebpayPlusModal from './'; -import { ModalCreateRequest } from './requests'; -import RequestService from '../../common/request_service'; -import { CommitRequest, RefundRequest, StatusRequest } from '../webpay_plus/requests'; -import ValidationUtil from '../../common/validation_util'; -import ApiConstants from '../../common/api_constants'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; -import Environment from '../common/environment'; - -/** - * Contains methods to interact with WebpayPlus API - */ -class Transaction extends BaseTransaction { - - /** - * Constructor class Webpay Plus Modal transaction. - * @param options (Optional) You can pass options to use a custom configuration. - */ - constructor(options: Options) { - options = options || WebpayPlusModal.getDefaultOptions() || new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MODAL, IntegrationApiKeys.WEBPAY, Environment.Integration); - super(options); - } - - /** - * Create a Webpay Plus transaction. - * @param buyOrder Commerce buy order, make sure this is unique. - * @param sessionId You can use this field to pass session data if needed. - * @param amount Transaction amount - */ - async create( - buyOrder: string, - sessionId: string, - amount: number - ){ - ValidationUtil.hasTextWithMaxLength(buyOrder, ApiConstants.BUY_ORDER_LENGTH, "buyOrder"); - ValidationUtil.hasTextWithMaxLength(sessionId, ApiConstants.SESSION_ID_LENGTH, "sessionId"); - let createRequest = new ModalCreateRequest(buyOrder, sessionId, amount); - return RequestService.perform(createRequest, this.options); - } - - /** - * Commit a transaction, this should be invoked after the card holder pays - * @param token Unique transaction identifier - */ - async commit(token: string){ - ValidationUtil.hasTextWithMaxLength(token, ApiConstants.TOKEN_LENGTH, "token"); - return RequestService.perform(new CommitRequest(token), this.options); - } - /** - * Obtain the status of a specific transaction - * @param token Unique transaction identifier - */ - async status(token: string){ - ValidationUtil.hasTextWithMaxLength(token, ApiConstants.TOKEN_LENGTH, "token"); - return RequestService.perform(new StatusRequest(token), this.options); - } - - /** - * Request a refund of a specific transaction, if you refund for the full amount and you're within - * the time window the transaction will be reversed. If you're past that window or refund for less - * than the total amount the transaction will be void. - * @param token Unique transaction identifier - * @param amount Amount to be refunded - */ - async refund( - token: string, - amount: number - ){ - ValidationUtil.hasTextWithMaxLength(token, ApiConstants.TOKEN_LENGTH, "token"); - return RequestService.perform(new RefundRequest(token, amount), this.options); - } -} - -export default Transaction; diff --git a/lib/transbank/webpay/oneclick/index.ts b/lib/transbank/webpay/oneclick/index.ts index 4cbe13e..3d7b97a 100644 --- a/lib/transbank/webpay/oneclick/index.ts +++ b/lib/transbank/webpay/oneclick/index.ts @@ -1,9 +1,5 @@ -import Options from '../../common/options'; -import Environment from '../common/environment'; import _MallInscription from './mall_inscription'; import _MallTransaction from './mall_transaction'; -import IntegrationApiKeys from '../../common/integration_api_keys'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; module Oneclick { @@ -16,51 +12,6 @@ module Oneclick { */ export const MallTransaction: typeof _MallTransaction = _MallTransaction; - /** - * Contains currently configured Commerce Code, Api Key and Environment - */ - export let options: Options; - - /** - * @returns currently configured Commerce Code and Api Key - */ - export const getDefaultOptions = () => { - return Oneclick.options; - }; - - /** - * This methods configures the module to point to the Production Environment with the given params. - * @param _commerceCode Commerce Code given by Transbank when contracting the product - * @param _apiKey Api Key given by Transbank when you sucessfuly validate your integration - */ - export const configureForProduction = (_commerceCode: string, _apiKey: string) => { - Oneclick.options = new Options(_commerceCode, _apiKey, Environment.Production); - }; - - /** - * This methods configures the module to point to the Integration Environment with the given params. - * You can check use the credentials provided in the official docs. - * https://transbankdevelopers.cl/documentacion/como_empezar#codigos-de-comercio - * @param _commerceCode Commerce Code given by Transbank. - * @param _apiKey Api Key given by Transbank. - */ - export const configureForIntegration = (_commerceCode: string, _apiKey: string) => { - Oneclick.options = new Options(_commerceCode, _apiKey, Environment.Integration); - }; - - /** - * This method configures the module to use Oneclick Mall in the Integration environment. - */ - export const configureOneclickMallForTesting = () => { - Oneclick.options = new Options(IntegrationCommerceCodes.ONECLICK_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Oneclick Mall deferred in the Integration environment. - */ - export const configureOneclickMallDeferredForTesting = () => { - Oneclick.options = new Options(IntegrationCommerceCodes.ONECLICK_MALL_DEFERRED, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; } export default Oneclick; diff --git a/lib/transbank/webpay/oneclick/mall_inscription.ts b/lib/transbank/webpay/oneclick/mall_inscription.ts index 09ea45c..83cc627 100644 --- a/lib/transbank/webpay/oneclick/mall_inscription.ts +++ b/lib/transbank/webpay/oneclick/mall_inscription.ts @@ -1,25 +1,45 @@ import BaseTransaction from '../../common/base_transaction'; import Options from '../../common/options'; -import Oneclick from '.'; import { DeleteRequest, FinishRequest, StartRequest } from './requests'; import RequestService from '../../common/request_service'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import Environment from '../common/environment'; class MallInscription extends BaseTransaction { /** * Constructor class MallInscription Oneclick. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || Oneclick.getDefaultOptions() || new Options(IntegrationCommerceCodes.ONECLICK_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `MallInscription` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallInscription` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): MallInscription + { + return new MallInscription(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `MallInscription` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallInscription` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): MallInscription + { + return new MallInscription(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Starts a card inscription process * @param username Cardholder's username diff --git a/lib/transbank/webpay/oneclick/mall_transaction.ts b/lib/transbank/webpay/oneclick/mall_transaction.ts index 3ae37e9..902e4d2 100644 --- a/lib/transbank/webpay/oneclick/mall_transaction.ts +++ b/lib/transbank/webpay/oneclick/mall_transaction.ts @@ -1,4 +1,3 @@ -import Oneclick from '.'; import Options from '../../common/options'; import TransactionDetail from '../common/transaction_detail'; import BaseTransaction from '../../common/base_transaction'; @@ -6,21 +5,42 @@ import RequestService from '../../common/request_service'; import { AuthorizeRequest, CaptureRequest, RefundRequest, StatusRequest } from './requests'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import Environment from '../common/environment'; class MallTransaction extends BaseTransaction { /** * Constructor class MallTransaction Oneclick. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || Oneclick.getDefaultOptions() || new Options(IntegrationCommerceCodes.ONECLICK_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `MallTransaction` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `MallTransaction` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Authorizes a payment to be charde onto the cardholder's card * @param username Cardholder's username diff --git a/lib/transbank/webpay/transaccion_completa/index.ts b/lib/transbank/webpay/transaccion_completa/index.ts index e4958d0..374549f 100644 --- a/lib/transbank/webpay/transaccion_completa/index.ts +++ b/lib/transbank/webpay/transaccion_completa/index.ts @@ -1,9 +1,5 @@ -import Options from '../../common/options'; -import Environment from '../common/environment'; import _Transaction from './transaction'; import _MallTransaction from './mall_transaction'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; module TransaccionCompleta { @@ -17,94 +13,6 @@ module TransaccionCompleta { */ export const MallTransaction: typeof _MallTransaction = _MallTransaction; - /** - * Contains currently configured Commerce Code, Api Key and Environment - */ - export let options: Options; - - /** - * @returns currently configured Commerce Code and Api Key - */ - export const getDefaultOptions = () => { - return TransaccionCompleta.options; - }; - - /** - * This methods configures the module to point to the Production Environment with the given params. - * @param _commerceCode Commerce Code given by Transbank when contracting the product - * @param _apiKey Api Key given by Transbank when you sucessfuly validate your integration - */ - export const configureForProduction = (_commerceCode: string, _apiKey: string) => { - TransaccionCompleta.options = new Options(_commerceCode, _apiKey, Environment.Production); - }; - - /** - * This methods configures the module to point to the Integration Environment with the given params. - * You can check use the credentials provided in the official docs. - * https://transbankdevelopers.cl/documentacion/como_empezar#codigos-de-comercio - * @param _commerceCode Commerce Code given by Transbank. - * @param _apiKey Api Key given by Transbank. - */ - export const configureForIntegration = (_commerceCode: string, _apiKey: string) => { - TransaccionCompleta.options = new Options(_commerceCode, _apiKey, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa in the Integration environment. - */ - export const configureForTesting = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Deferred in the Integration environment. - */ - export const configureForTestingDeferred = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_DEFERRED, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Mall in the Integration environment. - */ - export const configureForTestingMall = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Mall Deferred in the Integration environment. - */ - export const configureForTestingMallDeferred = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL_DEFERRED, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa without CVV in the Integration environment. - */ - export const configureForTestingNoCVV = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_SIN_CVV, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Deferred without CVV in the Integration environment. - */ - export const configureForTestingDeferredNoCVV = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_DEFERRED_SIN_CVV, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Mall without CVV in the Integration environment. - */ - export const configureForTestingMallNoCVV = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL_SIN_CVV, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Transaccion Completa Mall Deferred without CVV in the Integration environment. - */ - export const configureForTestingMallDeferredNoCVV = () => { - TransaccionCompleta.options = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL_DEFERRED_SIN_CVV, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - } export default TransaccionCompleta; diff --git a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts index 2f8f16a..6d863f5 100644 --- a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts +++ b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts @@ -1,4 +1,3 @@ -import TransaccionCompleta from '.'; import Options from '../../common/options'; import InstallmentDetail from '../common/installments_detail'; import TransaccionCompletaCommitDetail from './common/commit_detail'; @@ -8,8 +7,6 @@ import { InstallmentsRequest, MallCommitRequest, MallCreateRequest, MallRefundRe import RequestService from '../../common/request_service'; import MallCaptureRequest from './requests/mall_capture_request'; import Environment from '../common/environment'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; @@ -17,13 +14,36 @@ class MallTransaction extends BaseTransaction { /** * Constructor class MallTransaction. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || TransaccionCompleta.getDefaultOptions() || new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `MallTransaction` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `MallTransaction` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Create Transaccion Completa Mall transaction * @param buyOrder Commerce buy order, make sure this is unique. diff --git a/lib/transbank/webpay/transaccion_completa/transaction.ts b/lib/transbank/webpay/transaccion_completa/transaction.ts index 673025b..e1a4be7 100644 --- a/lib/transbank/webpay/transaccion_completa/transaction.ts +++ b/lib/transbank/webpay/transaccion_completa/transaction.ts @@ -1,11 +1,8 @@ -import TransaccionCompleta from '.'; import Options from '../../common/options'; import BaseTransaction from '../../common/base_transaction'; import { CaptureRequest, CommitRequest, CreateRequest, InstallmentsRequest, RefundRequest, StatusRequest } from './requests'; import RequestService from '../../common/request_service'; import Environment from '../common/environment'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; @@ -13,13 +10,36 @@ class Transaction extends BaseTransaction { /** * Constructor class transaction. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || TransaccionCompleta.getDefaultOptions() || new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `Transaction` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Transaction` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): Transaction + { + return new Transaction(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `Transaction` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Transaction` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): Transaction + { + return new Transaction(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Create a new Transaccion Completa transaction * @param buyOrder Commerce buy order, make sure this is unique. diff --git a/lib/transbank/webpay/webpay_plus/index.ts b/lib/transbank/webpay/webpay_plus/index.ts index 5a3524e..c34c8de 100644 --- a/lib/transbank/webpay/webpay_plus/index.ts +++ b/lib/transbank/webpay/webpay_plus/index.ts @@ -1,9 +1,5 @@ -import Environment from '../common/environment'; -import Options from '../../common/options'; import _Transaction from './transaction'; import _MallTransaction from './mall_transaction'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; module WebpayPlus { @@ -17,65 +13,6 @@ module WebpayPlus { */ export const MallTransaction: typeof _MallTransaction = _MallTransaction; - /** - * Contains currently configured Commerce Code, Api Key and Environment - */ - export let options: Options; - - /** - * @returns currently configured Commerce Code and Api Key - */ - export const getDefaultOptions = () => { - return WebpayPlus.options; - }; - - /** - * This methods configures the module to point to the Production Environment with the given params. - * @param _commerceCode Commerce Code given by Transbank when contracting the product - * @param _apiKey Api Key given by Transbank when you sucessfuly validate your integration - */ - export const configureForProduction = (_commerceCode: string, _apiKey: string) => { - WebpayPlus.options = new Options(_commerceCode, _apiKey, Environment.Production); - }; - - /** - * This methods configures the module to point to the Integration Environment with the given params. - * You can check use the credentials provided in the official docs. - * https://transbankdevelopers.cl/documentacion/como_empezar#codigos-de-comercio - * @param _commerceCode Commerce Code given by Transbank. - * @param _apiKey Api Key given by Transbank. - */ - export const configureForIntegration = (_commerceCode: string, _apiKey: string) => { - WebpayPlus.options = new Options(_commerceCode, _apiKey, Environment.Integration); - }; - - /** - * This method configures the module to use Webpay Plus in the Integration environment. - */ - export const configureForTesting = () => { - WebpayPlus.options = new Options(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Webpay Plus Deferred in the Integration environment. - */ - export const configureForTestingDeferred = () => { - WebpayPlus.options = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_DEFERRED, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Webpay Plus Mall in the Integration environment. - */ - export const configureForTestingMall = () => { - WebpayPlus.options = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; - - /** - * This method configures the module to use Webpay Plus Mall Deferred in the Integration environment. - */ - export const configureForTestingMallDeferred = () => { - WebpayPlus.options = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MALL_DEFERRED, IntegrationApiKeys.WEBPAY, Environment.Integration); - }; } export default WebpayPlus; diff --git a/lib/transbank/webpay/webpay_plus/mall_transaction.ts b/lib/transbank/webpay/webpay_plus/mall_transaction.ts index 28c7036..a5eb171 100644 --- a/lib/transbank/webpay/webpay_plus/mall_transaction.ts +++ b/lib/transbank/webpay/webpay_plus/mall_transaction.ts @@ -1,12 +1,9 @@ -import WebpayPlus from '.'; import Options from '../../common/options'; import TransactionDetail from '../common/transaction_detail'; import BaseTransaction from '../../common/base_transaction'; import { CommitRequest, MallCreateRequest, MallRefundRequest, StatusRequest } from './requests'; import RequestService from '../../common/request_service'; import MallCaptureRequest from '../transaccion_completa/requests/mall_capture_request'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import Environment from '../common/environment'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; @@ -15,13 +12,36 @@ class MallTransaction extends BaseTransaction { /** * Constructor class Webpay Plus transaction. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || WebpayPlus.getDefaultOptions() || new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `MallTransaction` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `MallTransaction` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `MallTransaction` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): MallTransaction + { + return new MallTransaction(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Create a Webpay Plus Mall transaction. * @param buyOrder Commerce buy order, make sure this is unique. diff --git a/lib/transbank/webpay/webpay_plus/transaction.ts b/lib/transbank/webpay/webpay_plus/transaction.ts index c268880..c8f152d 100644 --- a/lib/transbank/webpay/webpay_plus/transaction.ts +++ b/lib/transbank/webpay/webpay_plus/transaction.ts @@ -1,10 +1,7 @@ import Options from '../../common/options'; -import WebpayPlus from './'; import BaseTransaction from '../../common/base_transaction'; import { CaptureRequest, CommitRequest, CreateRequest, RefundRequest, StatusRequest } from './requests'; import RequestService from '../../common/request_service'; -import IntegrationCommerceCodes from '../../common/integration_commerce_codes'; -import IntegrationApiKeys from '../../common/integration_api_keys'; import Environment from '../common/environment'; import ValidationUtil from '../../common/validation_util'; import ApiConstants from '../../common/api_constants'; @@ -16,13 +13,36 @@ class Transaction extends BaseTransaction { /** * Constructor class Webpay Plus transaction. - * @param options (Optional) You can pass options to use a custom configuration. + * @param options You can pass options to use a custom configuration. */ constructor(options: Options) { - options = options || WebpayPlus.getDefaultOptions() || new Options(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY, Environment.Integration); super(options); } + /** + * Creates and returns an instance of `Transaction` configured for the integration environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Transaction` configured for the test environment (Environment.Integration). + */ + static buildForIntegration(commerceCode: string, apiKey: string): Transaction + { + return new Transaction(new Options(commerceCode, apiKey, Environment.Integration)); + } + + /** + * Creates and returns an instance of `Transaction` configured for the production environment. + * + * @param commerceCode The commerce code. + * @param apiKey The API key used for authentication. + * @return A new instance of `Transaction` configured for the production environment (Environment.Production). + */ + static buildForProduction(commerceCode: string, apiKey: string): Transaction + { + return new Transaction(new Options(commerceCode, apiKey, Environment.Production)); + } + /** * Create a Webpay Plus transaction. * @param buyOrder Commerce buy order, make sure this is unique. diff --git a/package.json b/package.json index 261d509..9560f83 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "devDependencies": { "@types/jest": "^29.5.11", "jest": "^29.7.0", + "nock": "^13.5.5", "ts-jest": "^29.1.2", "typedoc": "^0.25.7", "typescript": "^5.3.3" diff --git a/tests/common/options.test.ts b/tests/common/options.test.ts new file mode 100644 index 0000000..9d99236 --- /dev/null +++ b/tests/common/options.test.ts @@ -0,0 +1,49 @@ +import axios from 'axios'; +import RequestService from '../../lib/transbank/common/request_service'; +import { CreateRequest } from '../../lib/transbank/webpay/webpay_plus/requests'; +import { Options, Environment } from '../../lib'; + +test('creates Options object', () => { + let options = new Options('123456', 'asdfg', Environment.Integration); + expect(options.commerceCode).toBe('123456'); + expect(options.apiKey).toBe('asdfg'); + expect(options.environment).toBe(Environment.Integration); +}); + +test('creates Options object with default timeout', () => { + let options = new Options('123456', 'asdfg', Environment.Integration); + expect(options.timeout).toBe(600000); +}); + +test('the timeout parameter is set successfully', async () => { + const request = new CreateRequest( + 'ordenCompra12345678', + 'sesion1234557545', + 10000, + 'https://return.url' + ); + const options = new Options( + '597055555532', + '579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C', + Environment.Integration, + 20 + ); + + const handleTimeout = ( + reject: (reason?: any) => void + ): void => { + setTimeout(() => { + reject({ code: 'ECONNABORTED', message: 'timeout of 20ms exceeded' }); + }, 2000); + }; + + jest.spyOn(axios, 'request').mockImplementation(() => { + return new Promise(handleTimeout); + }); + + await expect(RequestService.perform(request, options)).rejects.toThrow( + 'AxiosError: timeout of 20ms exceeded' + ); + + (axios.request as jest.Mock).mockRestore(); +}); diff --git a/tests/mocks/oneclick_data.ts b/tests/mocks/oneclick_data.ts new file mode 100644 index 0000000..76f5d07 --- /dev/null +++ b/tests/mocks/oneclick_data.ts @@ -0,0 +1,42 @@ +export const ONECLICK_MALL_TRANSACTION_STATUS_RESPONSE_MOCK = { + vci: "TSY", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { + card_number: "6623" + }, + accounting_date: "0731", + transaction_date: "2021-07-31T23:31:14.249Z", + details: [ + { + amount: 1000, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555536", + buy_order: "500894028" + }, + { + amount: 2000, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555537", + buy_order: "1936357040" + } + ] +}; + +export const ONECLICK_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK = +{ + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 +}; + + diff --git a/tests/mocks/transaccion_completa_data.ts b/tests/mocks/transaccion_completa_data.ts new file mode 100644 index 0000000..4f38c01 --- /dev/null +++ b/tests/mocks/transaccion_completa_data.ts @@ -0,0 +1,65 @@ +export const FULL_TX_TRANSACTION_STATUS_RESPONSE_MOCK = { + vci: "TSY", + amount: 1000.0, + status: "AUTHORIZED", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { card_number: "6623" }, + accounting_date: "0731", + transaction_date: "2021-07-31T23:31:14.249Z", + authorization_code: "1213", + payment_type_code: "VD", + response_code: 0, + installments_number: 0, + installments_amount: 1000 +}; + +export const FULL_TX_TRANSACTION_CAPTURE_RESPONSE_MOCK = +{ + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 +}; + +export const FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK = { + details: [ + { + amount: 1922, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555574", + buy_order: "O-36681" + }, + { + amount: 1922, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555575", + buy_order: "O-36682" + } + ], + buy_order: "O-99701", + session_id: "S-23531", + card_detail: { + card_number: "6623" + }, + accounting_date: "0822", + transaction_date: "2024-08-23T00:15:56.920Z" +}; + +export const FULL_TX_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK = +{ + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 +}; + + diff --git a/tests/mocks/webpay_data.ts b/tests/mocks/webpay_data.ts new file mode 100644 index 0000000..ae28599 --- /dev/null +++ b/tests/mocks/webpay_data.ts @@ -0,0 +1,65 @@ +export const WEBPAY_TRANSACTION_STATUS_RESPONSE_MOCK = { + vci: "TSY", + amount: 1000.0, + status: "AUTHORIZED", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { card_number: "6623" }, + accounting_date: "0731", + transaction_date: "2021-07-31T23:31:14.249Z", + authorization_code: "1213", + payment_type_code: "VD", + response_code: 0, + installments_number: 0 +}; + +export const WEBPAY_TRANSACTION_CAPTURE_RESPONSE_MOCK = +{ + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 +}; + +export const WEBPAY_MALL_TRANSACTION_STATUS_RESPONSE_MOCK = { + vci: "TSY", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { + card_number: "6623" + }, + accounting_date: "0731", + transaction_date: "2021-07-31T23:31:14.249Z", + details: [ + { + amount: 1000, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555536", + buy_order: "500894028" + }, + { + amount: 2000, + status: "AUTHORIZED", + authorization_code: "1213", + payment_type_code: "VN", + response_code: 0, + installments_number: 0, + commerce_code: "597055555537", + buy_order: "1936357040" + } + ] +}; + +export const WEBPAY_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK = +{ + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 +}; + + diff --git a/tests/patpass/inscription.test.ts b/tests/patpass/inscription.test.ts new file mode 100644 index 0000000..0b89471 --- /dev/null +++ b/tests/patpass/inscription.test.ts @@ -0,0 +1,85 @@ +import { IntegrationApiKeys, IntegrationCommerceCodes, PatpassComercio } from "../../lib"; +import ApiConstants from "../../lib/transbank/common/api_constants"; +import Environment from "../../lib/transbank/patpass/common/environment"; +import nock from 'nock'; + +describe('PatpassComercioTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.PATPASS_COMERCIO_ENDPOINT}`; + const testToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + + afterAll(() => { + nock.cleanAll(); + }); + + it('start', async () => { + + const urlResponse = 'https://pagoautomaticocontarjetasint.transbank.cl/nuevo-ic-rest/tokenComercioLogin'; + const mockResponse = { + token: testToken, + url: urlResponse + }; + + nock(apiUrl) + .post('/patInscription') + .reply(200, mockResponse); + + const urlRequest = 'https://localhost:8081/patpass-comercio/commit'; + const name = 'nombre'; + const firstLastName = 'apellido'; + const secondLastName = 'sapellido'; + const rut = '14140066-5'; + const serviceId = 'service123'; + const finalUrl = 'https://localhost:8081/patpass-comercio/final'; + const maxAmount = 0; + const phoneNumber = '123456734'; + const mobileNumber = '123456723'; + const patpassName = 'nombre del patpass'; + const personEmail = 'alba.cardenas@continuum.cl'; + const commerceEmail = 'alba.cardenas@continuum.cl'; + const address = 'huerfanos 101'; + const city = 'Santiago'; + + const response = await PatpassComercio.Inscription + .buildForIntegration(IntegrationCommerceCodes.PATPASS_COMERCIO, IntegrationApiKeys.PATPASS_COMERCIO) + .start( + urlRequest, + name, + firstLastName, + secondLastName, + rut, + serviceId, + finalUrl, + maxAmount, + phoneNumber, + mobileNumber, + patpassName, + personEmail, + commerceEmail, + address, + city + ); + + expect(response.token).toBe(testToken); + expect(response.url).toBe(urlResponse); + }); + + it('status', async () => { + + const urlResponse = 'https://pagoautomaticocontarjetasint.transbank.cl/nuevo-ic-rest/tokenVoucherLogin'; + const mockResponse = { + authorized: true, + voucherUrl: urlResponse + }; + + nock(apiUrl) + .post('/status') + .reply(200, mockResponse); + + const response = await PatpassComercio.Inscription + .buildForIntegration(IntegrationCommerceCodes.PATPASS_COMERCIO, IntegrationApiKeys.PATPASS_COMERCIO) + .status(testToken) + + expect(response.authorized).toBeTruthy(); + expect(response.voucherUrl).toBe(urlResponse); + }); +}); diff --git a/tests/webpay/common/environment.test.ts b/tests/webpay/common/environment.test.ts new file mode 100644 index 0000000..4429172 --- /dev/null +++ b/tests/webpay/common/environment.test.ts @@ -0,0 +1,9 @@ +import { Environment } from '../../../lib'; + +test('correct integration environment url', () => { + expect(Environment.Integration).toBe('https://webpay3gint.transbank.cl'); +}); + +test('correct production environment url', () => { + expect(Environment.Production).toBe('https://webpay3g.transbank.cl'); +}); diff --git a/tests/webpay/oneclick/mall_inscription.test.ts b/tests/webpay/oneclick/mall_inscription.test.ts new file mode 100644 index 0000000..2e3495a --- /dev/null +++ b/tests/webpay/oneclick/mall_inscription.test.ts @@ -0,0 +1,59 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Oneclick, Options, TransactionDetail, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; + +describe('InscriptionOneclickMallTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.ONECLICK_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.ONECLICK_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + + afterAll(() => { + nock.cleanAll(); + }); + + test('start', async () => { + const expectedResponse = { + token: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', + url_webpay: 'https://webpay3gint.transbank.cl/webpayserver/bp_multicode_inscription.cgi' + }; + const username = "steve"; + const email = "steve.rogers@continuum.cl"; + const returnUrl = "https://localhost:8081/oneclick-mall/finish"; + + nock(apiUrl) + .post(`/inscriptions`) + .reply(200, expectedResponse); + + const response = await new Oneclick.MallInscription(option) + .start(username, email, returnUrl); + + expect(response.url_webpay).toBe(expectedResponse.url_webpay); + expect(response.token).toBe(expectedResponse.token); + + }); + + test('finish', async () => { + const expectedResponse = { + response_code: 0, + authorization_code: '1213', + card_type: 'Visa', + card_number: 'XXXXXXXXXXXX6623', + tbk_user: 'aaaaaaaaaaaaa-bbbbbbbb-cccccc' + }; + const testToken = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + + nock(apiUrl) + .put(`/inscriptions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new Oneclick.MallInscription(option) + .finish(testToken); + + expect(response.response_code).toBe(expectedResponse.response_code); + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.card_type).toBe(expectedResponse.card_type); + expect(response.card_number).toBe(expectedResponse.card_number); + expect(response.tbk_user).toBe(expectedResponse.tbk_user); + }); + + +}); diff --git a/tests/webpay/oneclick/mall_transaction.test.ts b/tests/webpay/oneclick/mall_transaction.test.ts new file mode 100644 index 0000000..d4bf235 --- /dev/null +++ b/tests/webpay/oneclick/mall_transaction.test.ts @@ -0,0 +1,96 @@ +import nock from 'nock'; +import { randomInt } from 'crypto'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Oneclick, Options, TransactionDetail } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { ONECLICK_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK, ONECLICK_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/oneclick_data'; + +describe('OneclickMallTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.ONECLICK_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.ONECLICK_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + + afterAll(() => { + nock.cleanAll(); + }); + + test('authorize', async () => { + const expectedResponse = ONECLICK_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + const username = "goncafa"; + const tbkUser = "aaaaaaaaaaaaa-bbbbbbbb-cccccc"; + const buyOrder = randomInt(0, 1000).toString(); + const buyOrderMallOne = randomInt(0, 1000).toString(); + const amountMallOne = 1000; + const mallOneCommerceCode = '597055555536'; + const buyOrderMallTwo = randomInt(0, 1000).toString(); + const amountMallTwo = 1000; + const mallTwoCommerceCode = '597055555537'; + + let mallDetails = [ + new TransactionDetail(amountMallOne, mallOneCommerceCode, buyOrderMallOne), + new TransactionDetail(amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo) + ]; + + nock(apiUrl).post(`/transactions`).reply(200, expectedResponse); + const response = await new Oneclick.MallTransaction(option) + .authorize(username, tbkUser, buyOrder, mallDetails); + testResponse(response, expectedResponse); + }); + + test('refund', async () => { + const type = 'REVERSED'; + const buyOrder = '500894028'; + nock(apiUrl).post(`/transactions/${buyOrder}/refunds`).reply(200, { type }); + const childBuyOrder = '500894028'; + const childCommerceCode = '597055555536'; + const amount = 1000; + + const response = await new Oneclick.MallTransaction(option) + .refund(buyOrder, childCommerceCode, childBuyOrder, amount); + expect(response.type).toBe(type); + }); + + test('status', async () => { + const expectedResponse = ONECLICK_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).get(`/transactions/${expectedResponse.buy_order}`).reply(200, expectedResponse); + + const response = await new Oneclick.MallTransaction(option) + .status(expectedResponse.buy_order); + testResponse(response, expectedResponse); + }); + + test('capture', async () => { + const expectedResponse = ONECLICK_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/capture`).reply(200, expectedResponse); + const commerceCode = "597055555537"; + const childBuyOrder = '500894028-1'; + const authorization = '1213'; + const amount = 1000; + const response = await new Oneclick.MallTransaction(option) + .capture(commerceCode, childBuyOrder, authorization, amount); + + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.authorization_date).toBe(expectedResponse.authorization_date); + expect(response.captured_amount).toBe(expectedResponse.captured_amount); + expect(response.response_code).toBe(expectedResponse.response_code); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + testDetailResponse(response.details[0], expectedResponse.details[0]); + testDetailResponse(response.details[1], expectedResponse.details[1]); + } + + function testDetailResponse(detailResponse: any, expectedDetailResponse: any) { + expect(detailResponse.amount).toBe(expectedDetailResponse.amount); + expect(detailResponse.status).toBe(expectedDetailResponse.status); + expect(detailResponse.authorization_code).toBe(expectedDetailResponse.authorization_code); + expect(detailResponse.payment_type_code).toBe(expectedDetailResponse.payment_type_code); + expect(detailResponse.response_code).toBe(expectedDetailResponse.response_code); + expect(detailResponse.installments_number).toBe(expectedDetailResponse.installments_number); + expect(detailResponse.commerce_code).toBe(expectedDetailResponse.commerce_code); + expect(detailResponse.buy_order).toBe(expectedDetailResponse.buy_order); + } + +}); diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts new file mode 100644 index 0000000..b8d595e --- /dev/null +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -0,0 +1,99 @@ +import nock from 'nock'; +import { randomInt } from 'crypto'; +import { CommitDetail, Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta, TransactionDetail } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/transaccion_completa_data'; + +describe('MallFullTransactionTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + const testToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + + afterAll(() => { + nock.cleanAll(); + }); + + test('create', async () => { + const token = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + const buyOrder = "O-" + randomInt(0, 1000).toString(); + const sessionId = "S-" + randomInt(0, 1000).toString(); + const cvv = 123; + const cardNumber = 'XXXXXXXXXXXX6623'; + const month = '12'; + const year = '28'; + const buyOrderMallOne = randomInt(0, 1000).toString(); + const amountMallOne = 1000; + const mallOneCommerceCode = '597055555536'; + const buyOrderMallTwo = randomInt(0, 1000).toString(); + const amountMallTwo = 1000; + const mallTwoCommerceCode = '597055555537'; + let mallDetails = [ + new TransactionDetail(amountMallOne, mallOneCommerceCode, buyOrderMallOne), + new TransactionDetail(amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo) + ]; + nock(apiUrl).post('/transactions').reply(200, { token }); + + const response = await new TransaccionCompleta.MallTransaction(option).create( + buyOrder, + sessionId, + cardNumber, + year + "/" + month, + mallDetails, + cvv + ); + expect(response.token).toBe(testToken); + }); + + test('commit', async () => { + const buyOrderMallOne = randomInt(0, 1000).toString(); + const mallOneCommerceCode = '597055555536'; + const idQueryInstallmentsOne = 1; + const deferredPeriodIndexOne = 0; + const buyOrderMallTwo = randomInt(0, 1000).toString(); + const mallTwoCommerceCode = '597055555537'; + const idQueryInstallmentsTwo = 1; + const deferredPeriodIndexTwo = 0; + + let mallDetails = [ + new CommitDetail(mallOneCommerceCode, + buyOrderMallOne, + idQueryInstallmentsOne, + deferredPeriodIndexOne, + false + ), + new CommitDetail(buyOrderMallTwo, + mallTwoCommerceCode, + idQueryInstallmentsTwo, + deferredPeriodIndexTwo, + false + ) + ]; + const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}`).reply(200, expectedResponse); + const response = await new TransaccionCompleta.MallTransaction(option) + .commit(testToken, mallDetails); + testResponse(response, expectedResponse); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + testDetailResponse(response.details[0], expectedResponse.details[0]); + testDetailResponse(response.details[1], expectedResponse.details[1]); + } + + function testDetailResponse(detailResponse: any, expectedDetailResponse: any) { + expect(detailResponse.amount).toBe(expectedDetailResponse.amount); + expect(detailResponse.status).toBe(expectedDetailResponse.status); + expect(detailResponse.authorization_code).toBe(expectedDetailResponse.authorization_code); + expect(detailResponse.payment_type_code).toBe(expectedDetailResponse.payment_type_code); + expect(detailResponse.response_code).toBe(expectedDetailResponse.response_code); + expect(detailResponse.installments_number).toBe(expectedDetailResponse.installments_number); + expect(detailResponse.commerce_code).toBe(expectedDetailResponse.commerce_code); + expect(detailResponse.buy_order).toBe(expectedDetailResponse.buy_order); + } +}); diff --git a/tests/webpay/transaccion_completa/mall_transaction_2.test.ts b/tests/webpay/transaccion_completa/mall_transaction_2.test.ts new file mode 100644 index 0000000..9de9bf5 --- /dev/null +++ b/tests/webpay/transaccion_completa/mall_transaction_2.test.ts @@ -0,0 +1,78 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { FULL_TX_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK, FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/transaccion_completa_data'; + +describe('MallFullTransactionTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + const testToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + + afterAll(() => { + nock.cleanAll(); + }); + + test('status', async () => { + const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).get(`/transactions/${testToken}`).reply(200, expectedResponse); + + const response = await new TransaccionCompleta.MallTransaction(option) + .status(testToken); + testResponse(response, expectedResponse); + }); + + test('refund', async () => { + const type = 'REVERSED'; + + nock(apiUrl) + .post(`/transactions/${testToken}/refunds`) + .reply(200, { type }); + + const childBuyOrder = '500894028'; + const childCommerceCode = '597055555536'; + const amount = 1000; + + const response = await new TransaccionCompleta.MallTransaction(option) + .refund(testToken, childBuyOrder, childCommerceCode, amount); + expect(response.type).toBe(type); + }); + + test('capture', async () => { + const expectedResponse = FULL_TX_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}/capture`).reply(200, expectedResponse); + + const commerceCode = "597055555537"; + const buyOrder = 'order_123'; + const authorization = '1213'; + const amount = 1000; + + const response = await new TransaccionCompleta.MallTransaction(option) + .capture(testToken, commerceCode, buyOrder, authorization, amount); + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.authorization_date).toBe(expectedResponse.authorization_date); + expect(response.captured_amount).toBe(expectedResponse.captured_amount); + expect(response.response_code).toBe(expectedResponse.response_code); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + testDetailResponse(response.details[0], expectedResponse.details[0]); + testDetailResponse(response.details[1], expectedResponse.details[1]); + } + + function testDetailResponse(detailResponse: any, expectedDetailResponse: any) { + expect(detailResponse.amount).toBe(expectedDetailResponse.amount); + expect(detailResponse.status).toBe(expectedDetailResponse.status); + expect(detailResponse.authorization_code).toBe(expectedDetailResponse.authorization_code); + expect(detailResponse.payment_type_code).toBe(expectedDetailResponse.payment_type_code); + expect(detailResponse.response_code).toBe(expectedDetailResponse.response_code); + expect(detailResponse.installments_number).toBe(expectedDetailResponse.installments_number); + expect(detailResponse.commerce_code).toBe(expectedDetailResponse.commerce_code); + expect(detailResponse.buy_order).toBe(expectedDetailResponse.buy_order); + } +}); diff --git a/tests/webpay/transaccion_completa/transaction.test.ts b/tests/webpay/transaccion_completa/transaction.test.ts new file mode 100644 index 0000000..1c5f2b2 --- /dev/null +++ b/tests/webpay/transaccion_completa/transaction.test.ts @@ -0,0 +1,99 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta } from '../../../lib'; +import { randomInt } from 'crypto'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { FULL_TX_TRANSACTION_CAPTURE_RESPONSE_MOCK, FULL_TX_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/transaccion_completa_data'; + +describe('FullTransactionTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const testToken = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + const option = new Options(IntegrationCommerceCodes.TRANSACCION_COMPLETA, IntegrationApiKeys.WEBPAY, Environment.Integration); + + afterAll(() => { + nock.cleanAll(); + }); + + test('create', async () => { + const token = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + const buyOrder = "O-" + randomInt(0, 1000).toString(); + const sessionId = "S-" + randomInt(0, 1000).toString(); + const amount = 1000; + const cvv = 123; + const cardNumber = 'XXXXXXXXXXXX6623'; + const month = '12'; + const year = '28'; + nock(apiUrl).post('/transactions').reply(200, { token }); + const response = await new TransaccionCompleta.Transaction(option).create( + buyOrder, + sessionId, + amount, + cvv, + cardNumber, + year + "/" + month + ); + expect(response.token).toBe(testToken); + }); + + + test('commit', async () => { + const expectedResponse = FULL_TX_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}`).reply(200, expectedResponse); + const response = await new TransaccionCompleta.Transaction(option).commit(testToken); + testResponse(response, expectedResponse); + }); + + test('refund', async () => { + const amount = 1000; + const type = "REVERSED"; + const expectedResponse = { type }; + nock(apiUrl).post(`/transactions/${testToken}/refunds`).reply(200, expectedResponse); + const response = await new TransaccionCompleta.Transaction(option).refund(testToken, amount); + expect(response.type).toBe(type); + }); + + test('status', async () => { + const expectedResponse = FULL_TX_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).get(`/transactions/${testToken}`).reply(200, expectedResponse); + const response = await new TransaccionCompleta.Transaction(option).status(testToken); + testResponse(response, expectedResponse); + }); + + test('capture', async () => { + const expectedResponse = FULL_TX_TRANSACTION_CAPTURE_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}/capture`).reply(200, expectedResponse); + const buyOrder = 'order_123'; + const authorization = '1213'; + const amount = 1000; + const response = await new TransaccionCompleta.Transaction(option).capture(testToken, buyOrder, authorization, amount); + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.authorization_date).toBe(expectedResponse.authorization_date); + expect(response.captured_amount).toBe(expectedResponse.captured_amount); + expect(response.response_code).toBe(expectedResponse.response_code); + }); + + test('installments', async () => { + const installmentsNumber = 2; + const expectedResponse = { installments_amount: 615, id_query_installments: 55114821}; + nock(apiUrl).post(`/transactions/${testToken}/installments`).reply(200, expectedResponse); + const response = await new TransaccionCompleta.Transaction(option).installments(testToken, installmentsNumber); + expect(response.installments_amount).toBe(expectedResponse.installments_amount); + expect(response.id_query_installments).toBe(expectedResponse.id_query_installments); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.amount).toBe(expectedResponse.amount); + expect(response.status).toBe(expectedResponse.status); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.transaction_date).toBe(expectedResponse.transaction_date); + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.payment_type_code).toBe(expectedResponse.payment_type_code); + expect(response.response_code).toBe(expectedResponse.response_code); + expect(response.installments_number).toBe(expectedResponse.installments_number); + } +}); + + diff --git a/tests/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts new file mode 100644 index 0000000..9a317bc --- /dev/null +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -0,0 +1,97 @@ +import nock from 'nock'; +import { randomInt } from 'crypto'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransactionDetail, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { WEBPAY_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK, WEBPAY_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/webpay_data'; + +describe('WebpayPlusMallTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + const testToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + + afterAll(() => { + nock.cleanAll(); + }); + + test('create', async () => { + const urlResponse = 'https://webpay3gint.transbank.cl/webpayserver/initTransaction'; + nock(apiUrl) + .post('/transactions') + .reply(200, { + token: testToken, + url: urlResponse + }); + + const returnUrl = 'https://www.google.com'; + const buyOrder = randomInt(0, 1000).toString(); + const sessionId = randomInt(0, 1000).toString(); + const buyOrderMallOne = randomInt(0, 1000).toString(); + const amountMallOne = 1000; + const mallOneCommerceCode = '597055555536'; + const buyOrderMallTwo = randomInt(0, 1000).toString(); + const amountMallTwo = 1000; + const mallTwoCommerceCode = '597055555537'; + + let mallDetails = [ + new TransactionDetail(amountMallOne, mallOneCommerceCode, buyOrderMallOne), + new TransactionDetail(amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo) + ]; + + const response = await new WebpayPlus.MallTransaction(option) + .create(buyOrder, sessionId, returnUrl, mallDetails); + + expect(response.token).toBe(testToken); + expect(response.url).toBe(urlResponse); + }); + + test('commit', async () => { + const expectedResponse = WEBPAY_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl) + .put(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new WebpayPlus.MallTransaction(option) + .commit(testToken); + testResponse(response, expectedResponse); + }); + + test('refund', async () => { + const type = 'REVERSED'; + + nock(apiUrl) + .post(`/transactions/${testToken}/refunds`) + .reply(200, { type }); + + const childBuyOrder = '500894028'; + const childCommerceCode = '597055555536'; + const amount = 1000; + + const response = await new WebpayPlus.MallTransaction(option) + .refund(testToken, childBuyOrder, childCommerceCode, amount); + + expect(response.type).toBe(type); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + testDetailResponse(response.details[0], expectedResponse.details[0]); + testDetailResponse(response.details[1], expectedResponse.details[1]); + } + + function testDetailResponse(detailResponse: any, expectedDetailResponse: any) { + expect(detailResponse.amount).toBe(expectedDetailResponse.amount); + expect(detailResponse.status).toBe(expectedDetailResponse.status); + expect(detailResponse.authorization_code).toBe(expectedDetailResponse.authorization_code); + expect(detailResponse.payment_type_code).toBe(expectedDetailResponse.payment_type_code); + expect(detailResponse.response_code).toBe(expectedDetailResponse.response_code); + expect(detailResponse.installments_number).toBe(expectedDetailResponse.installments_number); + expect(detailResponse.commerce_code).toBe(expectedDetailResponse.commerce_code); + expect(detailResponse.buy_order).toBe(expectedDetailResponse.buy_order); + } + +}); diff --git a/tests/webpay/webpay_plus/mall_transaction_2.test.ts b/tests/webpay/webpay_plus/mall_transaction_2.test.ts new file mode 100644 index 0000000..0ce93b5 --- /dev/null +++ b/tests/webpay/webpay_plus/mall_transaction_2.test.ts @@ -0,0 +1,68 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { WEBPAY_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK, WEBPAY_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/webpay_data'; + +describe('WebpayPlusMallTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const option = new Options(IntegrationCommerceCodes.WEBPAY_PLUS_MALL, IntegrationApiKeys.WEBPAY, Environment.Integration); + const testToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; + + afterAll(() => { + nock.cleanAll(); + }); + + test('status', async () => { + const expectedResponse = WEBPAY_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl) + .get(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new WebpayPlus.MallTransaction(option) + .status(testToken); + testResponse(response, expectedResponse); + }); + + test('capture', async () => { + const expectedResponse = WEBPAY_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK; + nock(apiUrl) + .put(`/transactions/${testToken}/capture`) + .reply(200, expectedResponse); + + const commerceCode = "597055555537"; + const buyOrder = 'order_123'; + const authorization = '1213'; + const amount = 1000; + + const response = await new WebpayPlus.MallTransaction(option) + .capture(commerceCode, testToken, buyOrder, authorization, amount); + + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.authorization_date).toBe(expectedResponse.authorization_date); + expect(response.captured_amount).toBe(expectedResponse.captured_amount); + expect(response.response_code).toBe(expectedResponse.response_code); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + testDetailResponse(response.details[0], expectedResponse.details[0]); + testDetailResponse(response.details[1], expectedResponse.details[1]); + } + + function testDetailResponse(detailResponse: any, expectedDetailResponse: any) { + expect(detailResponse.amount).toBe(expectedDetailResponse.amount); + expect(detailResponse.status).toBe(expectedDetailResponse.status); + expect(detailResponse.authorization_code).toBe(expectedDetailResponse.authorization_code); + expect(detailResponse.payment_type_code).toBe(expectedDetailResponse.payment_type_code); + expect(detailResponse.response_code).toBe(expectedDetailResponse.response_code); + expect(detailResponse.installments_number).toBe(expectedDetailResponse.installments_number); + expect(detailResponse.commerce_code).toBe(expectedDetailResponse.commerce_code); + expect(detailResponse.buy_order).toBe(expectedDetailResponse.buy_order); + } + +}); diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts new file mode 100644 index 0000000..4c80e43 --- /dev/null +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -0,0 +1,100 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; +import { WEBPAY_TRANSACTION_CAPTURE_RESPONSE_MOCK, WEBPAY_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/webpay_data'; + +describe('WebpayPlusTest', () => { + const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; + const testToken = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + + afterAll(() => { + nock.cleanAll(); + }); + + test('create', async () => { + const urlResponse = "https://webpay3gint.transbank.cl/webpayserver/initTransaction"; + const mockResponse = { + token: testToken, + url: urlResponse + }; + nock(apiUrl).post('/transactions').reply(200, mockResponse); + + const buyOrder = 'order_123'; + const sessionId = 'session_123'; + const amount = 1000; + const returnUrl = "https://www.google.com"; + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .create(buyOrder, sessionId, amount, returnUrl); + expect(response.token).toBe(testToken); + expect(response.url).toBe(urlResponse); + }); + + + test('commit', async () => { + const expectedResponse = WEBPAY_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}`).reply(200, expectedResponse); + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .commit(testToken); + testResponse(response, expectedResponse); + }); + + test('refund', async () => { + const amount = 1000; + const type = "REVERSED"; + const mockResponse = { type }; + nock(apiUrl).post(`/transactions/${testToken}/refunds`).reply(200, mockResponse); + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .refund(testToken, amount); + expect(response.type).toBe(type); + }); + + test('status', async () => { + const expectedResponse = WEBPAY_TRANSACTION_STATUS_RESPONSE_MOCK; + nock(apiUrl).get(`/transactions/${testToken}`).reply(200, expectedResponse); + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .status(testToken); + testResponse(response, expectedResponse); + }); + + test('capture', async () => { + const expectedResponse = WEBPAY_TRANSACTION_CAPTURE_RESPONSE_MOCK; + nock(apiUrl).put(`/transactions/${testToken}/capture`).reply(200, expectedResponse); + const buyOrder = 'order_123'; + const authorization = '1213'; + const amount = 1000; + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .capture(testToken, buyOrder, authorization, amount); + + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.authorization_date).toBe(expectedResponse.authorization_date); + expect(response.captured_amount).toBe(expectedResponse.captured_amount); + expect(response.response_code).toBe(expectedResponse.response_code); + }); + + function testResponse(response: any, expectedResponse: any) { + expect(response.vci).toBe(expectedResponse.vci); + expect(response.amount).toBe(expectedResponse.amount); + expect(response.status).toBe(expectedResponse.status); + expect(response.buy_order).toBe(expectedResponse.buy_order); + expect(response.session_id).toBe(expectedResponse.session_id); + expect(response.card_detail.card_number).toBe(expectedResponse.card_detail.card_number); + expect(response.accounting_date).toBe(expectedResponse.accounting_date); + expect(response.transaction_date).toBe(expectedResponse.transaction_date); + expect(response.authorization_code).toBe(expectedResponse.authorization_code); + expect(response.payment_type_code).toBe(expectedResponse.payment_type_code); + expect(response.response_code).toBe(expectedResponse.response_code); + expect(response.installments_number).toBe(expectedResponse.installments_number); + } +}); + + diff --git a/tests/webpay_plus/common.test.ts b/tests/webpay_plus/common.test.ts deleted file mode 100644 index c7b20fc..0000000 --- a/tests/webpay_plus/common.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Options } from '../../lib/'; -import { Environment } from '../../lib/'; - -test('creates Options object', () => { - let options = new Options('123456', 'asdfg', Environment.Integration); - expect(options.commerceCode).toBe('123456'); - expect(options.apiKey).toBe('asdfg'); - expect(options.environment).toBe(Environment.Integration); -}); - -test('correct integration environment url', () => { - expect(Environment.Integration).toBe('https://webpay3gint.transbank.cl'); -}); - -test('correct production environment url', () => { - expect(Environment.Production).toBe('https://webpay3g.transbank.cl'); -});