From ec470bd692178c438af6d98da33c2fcf5e5685b6 Mon Sep 17 00:00:00 2001 From: snyk-bot Date: Fri, 19 Apr 2024 18:20:14 +0000 Subject: [PATCH 01/44] fix: upgrade axios from 1.6.7 to 1.6.8 Snyk has created this PR to upgrade axios from 1.6.7 to 1.6.8. See this package in npm: https://www.npmjs.com/package/axios See this project in Snyk: https://app.snyk.io/org/transbankdevelopers/project/985273de-bcb8-4a9a-9676-22a7645722e1?utm_source=github&utm_medium=referral&page=upgrade-pr --- package-lock.json | 37 ++++++++++++++++++++----------------- package.json | 2 +- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 088c501..82955bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "transbank-sdk", - "version": "4.0.0", + "version": "5.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "transbank-sdk", - "version": "4.0.0", + "version": "5.0.0", "license": "BSD-3-Clause", "dependencies": { - "axios": "^1.6.5" + "axios": "^1.6.8" }, "devDependencies": { "@types/jest": "^29.5.11", @@ -17,6 +17,9 @@ "ts-jest": "^29.1.2", "typedoc": "^0.25.7", "typescript": "^5.3.3" + }, + "engines": { + "node": ">=18.0.0" } }, "node_modules/@ampproject/remapping": { @@ -1216,11 +1219,11 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.6.7", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", - "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "version": "1.6.8", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", + "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", "dependencies": { - "follow-redirects": "^1.15.4", + "follow-redirects": "^1.15.6", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -1828,9 +1831,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.5", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", - "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "funding": [ { "type": "individual", @@ -4887,11 +4890,11 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "axios": { - "version": "1.6.7", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", - "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "version": "1.6.8", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", + "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", "requires": { - "follow-redirects": "^1.15.4", + "follow-redirects": "^1.15.6", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -5327,9 +5330,9 @@ } }, "follow-redirects": { - "version": "1.15.5", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", - "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==" + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==" }, "form-data": { "version": "4.0.0", diff --git a/package.json b/package.json index 4ef79be..de88821 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,6 @@ "typescript": "^5.3.3" }, "dependencies": { - "axios": "^1.6.5" + "axios": "^1.6.8" } } From 68da30279f88ba27f14d51cfd723799910018a10 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 9 Jul 2024 11:47:16 -0400 Subject: [PATCH 02/44] feat: default timeout const --- lib/transbank/common/options.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/transbank/common/options.ts b/lib/transbank/common/options.ts index 99e9454..55f74a4 100644 --- a/lib/transbank/common/options.ts +++ b/lib/transbank/common/options.ts @@ -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; } } From b4e2560af1f178a01a22373898e01b9dfee04390 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 9 Jul 2024 11:47:29 -0400 Subject: [PATCH 03/44] refactor: use option timeout in patpass --- lib/transbank/common/request_service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) => { From fd304d1d7309b61bdf55cdf219fd6336bce6c5f0 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 9 Jul 2024 16:24:20 -0400 Subject: [PATCH 04/44] test: reorganized test folder --- tests/webpay/common/environment.test.ts | 9 +++++++++ tests/webpay_plus/common.test.ts | 17 ----------------- 2 files changed, 9 insertions(+), 17 deletions(-) create mode 100644 tests/webpay/common/environment.test.ts delete mode 100644 tests/webpay_plus/common.test.ts diff --git a/tests/webpay/common/environment.test.ts b/tests/webpay/common/environment.test.ts new file mode 100644 index 0000000..8769ee1 --- /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'); +}); \ No newline at end of file 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'); -}); From 35d7606b63bafaa167a4689d5d7e73102a9a0cc8 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 9 Jul 2024 16:24:28 -0400 Subject: [PATCH 05/44] test: update option test --- tests/common/options.test.ts | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/common/options.test.ts diff --git a/tests/common/options.test.ts b/tests/common/options.test.ts new file mode 100644 index 0000000..0324bbb --- /dev/null +++ b/tests/common/options.test.ts @@ -0,0 +1,50 @@ +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 = ( + resolve: (value?: unknown) => void, + 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(); +}); From bc8d92924e622a748a511d46fd2c895da9df2f2c Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 9 Jul 2024 16:27:41 -0400 Subject: [PATCH 06/44] test: add final line --- tests/webpay/common/environment.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webpay/common/environment.test.ts b/tests/webpay/common/environment.test.ts index 8769ee1..4429172 100644 --- a/tests/webpay/common/environment.test.ts +++ b/tests/webpay/common/environment.test.ts @@ -6,4 +6,4 @@ test('correct integration environment url', () => { test('correct production environment url', () => { expect(Environment.Production).toBe('https://webpay3g.transbank.cl'); -}); \ No newline at end of file +}); From 5535efd87470a5b815fa88b373212554990c12dd Mon Sep 17 00:00:00 2001 From: Carlos Tribiec Date: Wed, 24 Jul 2024 18:00:44 -0400 Subject: [PATCH 07/44] refactor: make timeout field optional on the Options class --- lib/transbank/common/options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transbank/common/options.ts b/lib/transbank/common/options.ts index 55f74a4..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 From a02df3956e58aee283e08fa88bad42bc07400b31 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:52:37 -0500 Subject: [PATCH 08/44] feat: add build method for webpay plus transaction --- .../webpay/webpay_plus/transaction.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/transbank/webpay/webpay_plus/transaction.ts b/lib/transbank/webpay/webpay_plus/transaction.ts index c268880..e2a6999 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'; @@ -19,10 +16,33 @@ class Transaction extends BaseTransaction { * @param options (Optional) 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. From 94230fff4480995768ffd3eeff85013572ce86a3 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:53:07 -0500 Subject: [PATCH 09/44] feat: add build method for webpay plus mall transaction --- .../webpay/webpay_plus/mall_transaction.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/transbank/webpay/webpay_plus/mall_transaction.ts b/lib/transbank/webpay/webpay_plus/mall_transaction.ts index 28c7036..551a091 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'; @@ -18,10 +15,33 @@ class MallTransaction extends BaseTransaction { * @param options (Optional) 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. From b15d4d3aa7d722daea799a39a7f10e708ba1ab06 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:53:44 -0500 Subject: [PATCH 10/44] feat: add build method for oneclick mall transaction --- .../webpay/oneclick/mall_transaction.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/transbank/webpay/oneclick/mall_transaction.ts b/lib/transbank/webpay/oneclick/mall_transaction.ts index 3ae37e9..796fce1 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,8 +5,6 @@ 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 { @@ -17,10 +14,33 @@ class MallTransaction extends BaseTransaction { * @param options (Optional) 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 From ad749a2900b4a99618a4ad9a08cc69fc27aa245f Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:54:10 -0500 Subject: [PATCH 11/44] feat: add build method for oneclick mall inscription --- .../webpay/oneclick/mall_inscription.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/transbank/webpay/oneclick/mall_inscription.ts b/lib/transbank/webpay/oneclick/mall_inscription.ts index 09ea45c..ccac30e 100644 --- a/lib/transbank/webpay/oneclick/mall_inscription.ts +++ b/lib/transbank/webpay/oneclick/mall_inscription.ts @@ -1,12 +1,9 @@ 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 { @@ -16,10 +13,33 @@ class MallInscription extends BaseTransaction { * @param options (Optional) 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 From 9f253f82962003a30a9efcc1d7d838bb660dfdce Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:55:03 -0500 Subject: [PATCH 12/44] feat: add build method for full transaction --- .../transaccion_completa/transaction.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/transbank/webpay/transaccion_completa/transaction.ts b/lib/transbank/webpay/transaccion_completa/transaction.ts index 673025b..6c391b6 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'; @@ -16,10 +13,33 @@ class Transaction extends BaseTransaction { * @param options (Optional) 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. From df90c3aa6b879c71d78b067a49fe758600523a36 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:55:16 -0500 Subject: [PATCH 13/44] feat: add build method for mall full transaction --- .../transaccion_completa/mall_transaction.ts | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts index 2f8f16a..36add50 100644 --- a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts +++ b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts @@ -20,10 +20,33 @@ class MallTransaction extends BaseTransaction { * @param options (Optional) 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. From a4246234afe4fad28dec0c281781a4dc332e93a4 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:55:55 -0500 Subject: [PATCH 14/44] feat: add build method for patpass --- lib/transbank/patpass/inscription.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/transbank/patpass/inscription.ts b/lib/transbank/patpass/inscription.ts index fe733e5..b5d5b86 100644 --- a/lib/transbank/patpass/inscription.ts +++ b/lib/transbank/patpass/inscription.ts @@ -18,6 +18,30 @@ class Inscription extends BaseTransaction { 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 From 8627b8787b1a8ff4b0ff0610bb90823e78d3842b Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:59:39 -0500 Subject: [PATCH 15/44] feat: remove other initialization methods for webpay plus --- lib/transbank/webpay/webpay_plus/index.ts | 63 ----------------------- 1 file changed, 63 deletions(-) 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; From cb81866c66e3555252690ac3e571e3b84bc12ce5 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 19:59:52 -0500 Subject: [PATCH 16/44] feat: remove other initialization methods for oneclick --- lib/transbank/webpay/oneclick/index.ts | 49 -------------------------- 1 file changed, 49 deletions(-) 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; From 42ce70ca4be6bf3f0649d2aea3426b3c1e4623f7 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:00:16 -0500 Subject: [PATCH 17/44] feat: remove other initialization methods for full transaction --- .../webpay/transaccion_completa/index.ts | 92 ------------------- 1 file changed, 92 deletions(-) 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; From dbfed23ce6ad1a3e031e148d85df3813c93c404f Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:01:48 -0500 Subject: [PATCH 18/44] feat: add option validation --- lib/transbank/common/base_transaction.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; From ec9d63d140dae8c8d894bbfba23bceec8e7f9206 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:02:25 -0500 Subject: [PATCH 19/44] chore: add nock library for test --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index de88821..84685e0 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" From 1b373813af17626da2cfa3dd048a819f662130cc Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:02:46 -0500 Subject: [PATCH 20/44] test: add oneclick tests --- .../webpay/oneclick/mall_inscription.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/webpay/oneclick/mall_inscription.test.ts diff --git a/tests/webpay/oneclick/mall_inscription.test.ts b/tests/webpay/oneclick/mall_inscription.test.ts new file mode 100644 index 0000000..609bc0a --- /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 = "http://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); + }); + + +}); From a6db02067eb0cc6b6992491b95a24b1629975e1d Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:03:01 -0500 Subject: [PATCH 21/44] test: add oneclick tests --- .../webpay/oneclick/mall_transaction.test.ts | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 tests/webpay/oneclick/mall_transaction.test.ts diff --git a/tests/webpay/oneclick/mall_transaction.test.ts b/tests/webpay/oneclick/mall_transaction.test.ts new file mode 100644 index 0000000..11bdf7d --- /dev/null +++ b/tests/webpay/oneclick/mall_transaction.test.ts @@ -0,0 +1,193 @@ +import nock from 'nock'; +import { randomInt } from 'crypto'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Oneclick, Options, TransactionDetail, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; + +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 = generateJsonResponse(); + 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); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + 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 = generateJsonResponse(); + nock(apiUrl) + .get(`/transactions/${expectedResponse.buy_order}`) + .reply(200, expectedResponse); + + const response = await new Oneclick.MallTransaction(option) + .status(expectedResponse.buy_order); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + + test('capture', async () => { + const expectedResponse = { + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 + }; + + 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 generateJsonResponse(): any { + return { + 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" + } + ] + }; + } +}); From 43ac984b66df6903b10e7aaee546c2672501dbb7 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:03:21 -0500 Subject: [PATCH 22/44] test: add full transaction tests --- .../mall_transaction.test.ts | 244 ++++++++++++++++++ .../transaccion_completa/transaction.test.ts | 157 +++++++++++ 2 files changed, 401 insertions(+) create mode 100644 tests/webpay/transaccion_completa/mall_transaction.test.ts create mode 100644 tests/webpay/transaccion_completa/transaction.test.ts 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..24a5cae --- /dev/null +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -0,0 +1,244 @@ +import nock from 'nock'; +import { randomInt } from 'crypto'; +import { CommitDetail, Environment, InstallmentDetail, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta, TransactionDetail } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; + +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 amount = 1000; + 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 = generateJsonResponse(); + + nock(apiUrl) + .put(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new TransaccionCompleta.MallTransaction(option) + .commit(testToken, mallDetails); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + test('status', async () => { + const expectedResponse = generateJsonResponse(); + + nock(apiUrl) + .get(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new TransaccionCompleta.MallTransaction(option) + .status(testToken); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + test('refund', async () => { + const url = `/${apiUrl}/transactions/${testToken}/refunds`; + 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 = { + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 + }; + + nock(apiUrl) + .put(`/transactions/${testToken}/capture`) + .reply(200, expectedResponse); + + const commerceCode = "597055555537"; + const buyOrder = Math.floor(Math.random() * 1000).toString(); + 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 generateJsonResponse(): any { + return { + 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" + }; + } +}); diff --git a/tests/webpay/transaccion_completa/transaction.test.ts b/tests/webpay/transaccion_completa/transaction.test.ts new file mode 100644 index 0000000..44fed01 --- /dev/null +++ b/tests/webpay/transaccion_completa/transaction.test.ts @@ -0,0 +1,157 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta } from '../../../lib'; +import { randomInt } from 'crypto'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; + +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 = generateJsonResponse(); + nock(apiUrl) + .put(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new TransaccionCompleta.Transaction(option).commit(testToken); + 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); + }); + + 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 = generateJsonResponse(); + nock(apiUrl) + .get(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new TransaccionCompleta.Transaction(option).status(testToken); + 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); + }); + + test('capture', async () => { + const expectedResponse = { + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 + }; + + nock(apiUrl) + .put(`/transactions/${testToken}/capture`) + .reply(200, expectedResponse); + + + const buyOrder = Math.floor(Math.random() * 1000).toString(); + 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 generateJsonResponse(): any { + return { + vci: "TSY", + amount: 1000.0, + status: "AUTHORIZED", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { card_number: "6623" }, // Asegúrate de que mapResponseDetail esté definido previamente + 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 + }; + } +}); + + From 82ffb08309ca39ff2e68caf5ccc82ff2f9da4e16 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 22 Aug 2024 20:03:38 -0500 Subject: [PATCH 23/44] test: add webpay plus tests --- .../webpay_plus/mall_transaction.test.ts | 215 ++++++++++++++++++ tests/webpay/webpay_plus/transaction.test.ts | 145 ++++++++++++ 2 files changed, 360 insertions(+) create mode 100644 tests/webpay/webpay_plus/mall_transaction.test.ts create mode 100644 tests/webpay/webpay_plus/transaction.test.ts 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..52c4236 --- /dev/null +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -0,0 +1,215 @@ +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'; + +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 = 'http://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 = generateJsonResponse(); + + nock(apiUrl) + .put(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new WebpayPlus.MallTransaction(option) + .commit(testToken); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + test('refund', async () => { + const url = `/${apiUrl}/transactions/${testToken}/refunds`; + 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); + }); + + test('status', async () => { + const expectedResponse = generateJsonResponse(); + + nock(apiUrl) + .get(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await new WebpayPlus.MallTransaction(option) + .status(testToken); + + 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); + + let expectedDetail = expectedResponse.details[0]; + let detail = response.details[0]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + + expectedDetail = expectedResponse.details[1]; + detail = response.details[1]; + expect(detail.amount).toBe(expectedDetail.amount); + expect(detail.status).toBe(expectedDetail.status); + expect(detail.authorization_code).toBe(expectedDetail.authorization_code); + expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); + expect(detail.response_code).toBe(expectedDetail.response_code); + expect(detail.installments_number).toBe(expectedDetail.installments_number); + expect(detail.commerce_code).toBe(expectedDetail.commerce_code); + expect(detail.buy_order).toBe(expectedDetail.buy_order); + }); + + test('capture', async () => { + const expectedResponse = { + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 + }; + + nock(apiUrl) + .put(`/transactions/${testToken}/capture`) + .reply(200, expectedResponse); + + const commerceCode = "597055555537"; + const buyOrder = Math.floor(Math.random() * 1000).toString(); + 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 generateJsonResponse(): any { + return { + 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" + } + ] + }; + } +}); diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts new file mode 100644 index 0000000..74c36ae --- /dev/null +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -0,0 +1,145 @@ +import nock from 'nock'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, WebpayPlus } from '../../../lib'; +import ApiConstants from '../../../lib/transbank/common/api_constants'; + +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 = String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); + const sessionId = String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); + const amount = 1000; + const returnUrl = "http://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 = generateJsonResponse(); + nock(apiUrl) + .put(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .commit(testToken); + 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); + }); + + 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 = generateJsonResponse(); + nock(apiUrl) + .get(`/transactions/${testToken}`) + .reply(200, expectedResponse); + + const response = await WebpayPlus.Transaction + .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) + .status(testToken); + 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); + }); + + test('capture', async () => { + const expectedResponse = { + authorization_code: "1213", + authorization_date: "2021-07-31T23:31:14.249Z", + captured_amount: 1000, + response_code: 0 + }; + + nock(apiUrl) + .put(`/transactions/${testToken}/capture`) + .reply(200, expectedResponse); + + + const buyOrder = Math.floor(Math.random() * 1000).toString(); + 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 generateJsonResponse(): any { + return { + vci: "TSY", + amount: 1000.0, + status: "AUTHORIZED", + buy_order: "1643997337", + session_id: "1134425622", + card_detail: { card_number: "6623" }, // Asegúrate de que mapResponseDetail esté definido previamente + accounting_date: "0731", + transaction_date: "2021-07-31T23:31:14.249Z", + authorization_code: "1213", + payment_type_code: "VD", + response_code: 0, + installments_number: 0 + }; + } +}); + + From 5243e82927ab395823e8ca6633f2d88e1884978f Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 26 Aug 2024 11:16:58 -0500 Subject: [PATCH 24/44] feat: remove other initialization methods for patpass --- lib/transbank/patpass/index.ts | 44 ---------------------------------- 1 file changed, 44 deletions(-) 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; From b9f7fec4680723413b675f9f17ef86c085633382 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 26 Aug 2024 11:18:31 -0500 Subject: [PATCH 25/44] feat: remove default initialization for patpass --- lib/transbank/patpass/inscription.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/transbank/patpass/inscription.ts b/lib/transbank/patpass/inscription.ts index b5d5b86..43fbbd6 100644 --- a/lib/transbank/patpass/inscription.ts +++ b/lib/transbank/patpass/inscription.ts @@ -1,20 +1,16 @@ 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); } From 3a06df6f861384c020f77c6e914a5711ecc19804 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 26 Aug 2024 11:18:49 -0500 Subject: [PATCH 26/44] test: add patpass tests --- tests/patpass/inscription.test.ts | 86 +++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/patpass/inscription.test.ts diff --git a/tests/patpass/inscription.test.ts b/tests/patpass/inscription.test.ts new file mode 100644 index 0000000..6a0a264 --- /dev/null +++ b/tests/patpass/inscription.test.ts @@ -0,0 +1,86 @@ +import { IntegrationApiKeys, IntegrationCommerceCodes, PatpassComercio } from "../../lib"; +import ApiConstants from "../../lib/transbank/common/api_constants"; +import Environment from "../../lib/transbank/patpass/common/environment"; + +const nock = require('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 = 'http://localhost:8081/patpass-comercio/commit'; + const name = 'nombre'; + const firstLastName = 'apellido'; + const secondLastName = 'sapellido'; + const rut = '14140066-5'; + const serviceId = String(Math.floor(Math.random() * Math.pow(2, 31))); + const finalUrl = 'http://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); + }); +}); From a137738db4682b7e3852162efc14a9950242d79f Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 26 Aug 2024 11:27:36 -0500 Subject: [PATCH 27/44] docs: remove optional --- lib/transbank/webpay/oneclick/mall_inscription.ts | 2 +- lib/transbank/webpay/oneclick/mall_transaction.ts | 2 +- lib/transbank/webpay/transaccion_completa/mall_transaction.ts | 2 +- lib/transbank/webpay/transaccion_completa/transaction.ts | 2 +- lib/transbank/webpay/webpay_plus/mall_transaction.ts | 2 +- lib/transbank/webpay/webpay_plus/transaction.ts | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/transbank/webpay/oneclick/mall_inscription.ts b/lib/transbank/webpay/oneclick/mall_inscription.ts index ccac30e..83cc627 100644 --- a/lib/transbank/webpay/oneclick/mall_inscription.ts +++ b/lib/transbank/webpay/oneclick/mall_inscription.ts @@ -10,7 +10,7 @@ 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) { super(options); diff --git a/lib/transbank/webpay/oneclick/mall_transaction.ts b/lib/transbank/webpay/oneclick/mall_transaction.ts index 796fce1..902e4d2 100644 --- a/lib/transbank/webpay/oneclick/mall_transaction.ts +++ b/lib/transbank/webpay/oneclick/mall_transaction.ts @@ -11,7 +11,7 @@ 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) { super(options); diff --git a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts index 36add50..4dad4a5 100644 --- a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts +++ b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts @@ -17,7 +17,7 @@ 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) { super(options); diff --git a/lib/transbank/webpay/transaccion_completa/transaction.ts b/lib/transbank/webpay/transaccion_completa/transaction.ts index 6c391b6..e1a4be7 100644 --- a/lib/transbank/webpay/transaccion_completa/transaction.ts +++ b/lib/transbank/webpay/transaccion_completa/transaction.ts @@ -10,7 +10,7 @@ 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) { super(options); diff --git a/lib/transbank/webpay/webpay_plus/mall_transaction.ts b/lib/transbank/webpay/webpay_plus/mall_transaction.ts index 551a091..a5eb171 100644 --- a/lib/transbank/webpay/webpay_plus/mall_transaction.ts +++ b/lib/transbank/webpay/webpay_plus/mall_transaction.ts @@ -12,7 +12,7 @@ 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) { super(options); diff --git a/lib/transbank/webpay/webpay_plus/transaction.ts b/lib/transbank/webpay/webpay_plus/transaction.ts index e2a6999..c8f152d 100644 --- a/lib/transbank/webpay/webpay_plus/transaction.ts +++ b/lib/transbank/webpay/webpay_plus/transaction.ts @@ -13,7 +13,7 @@ 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) { super(options); From d33d03ff75feac75873cd157279f1f962907e8ef Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 26 Aug 2024 11:29:30 -0500 Subject: [PATCH 28/44] refactor: remove unused variable --- lib/transbank/webpay/transaccion_completa/mall_transaction.ts | 3 --- tests/webpay/webpay_plus/transaction.test.ts | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/transbank/webpay/transaccion_completa/mall_transaction.ts b/lib/transbank/webpay/transaccion_completa/mall_transaction.ts index 4dad4a5..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'; diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts index 74c36ae..29e9427 100644 --- a/tests/webpay/webpay_plus/transaction.test.ts +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -1,5 +1,5 @@ import nock from 'nock'; -import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, WebpayPlus } from '../../../lib'; +import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, WebpayPlus } from '../../../lib'; import ApiConstants from '../../../lib/transbank/common/api_constants'; describe('WebpayPlusTest', () => { From 0cff1ad8017d7c1c48726621d31454bd2199febd Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Wed, 28 Aug 2024 10:36:59 -0500 Subject: [PATCH 29/44] tests: remove random --- tests/patpass/inscription.test.ts | 2 +- tests/webpay/transaccion_completa/mall_transaction.test.ts | 2 +- tests/webpay/transaccion_completa/transaction.test.ts | 2 +- tests/webpay/webpay_plus/mall_transaction.test.ts | 2 +- tests/webpay/webpay_plus/transaction.test.ts | 6 +++--- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/patpass/inscription.test.ts b/tests/patpass/inscription.test.ts index 6a0a264..8e9d7c0 100644 --- a/tests/patpass/inscription.test.ts +++ b/tests/patpass/inscription.test.ts @@ -29,7 +29,7 @@ describe('PatpassComercioTest', () => { const firstLastName = 'apellido'; const secondLastName = 'sapellido'; const rut = '14140066-5'; - const serviceId = String(Math.floor(Math.random() * Math.pow(2, 31))); + const serviceId = 'service123'; const finalUrl = 'http://localhost:8081/patpass-comercio/final'; const maxAmount = 0; const phoneNumber = '123456734'; diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index 24a5cae..03af061 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -195,7 +195,7 @@ describe('MallFullTransactionTest', () => { .reply(200, expectedResponse); const commerceCode = "597055555537"; - const buyOrder = Math.floor(Math.random() * 1000).toString(); + const buyOrder = 'order_123'; const authorization = '1213'; const amount = 1000; diff --git a/tests/webpay/transaccion_completa/transaction.test.ts b/tests/webpay/transaccion_completa/transaction.test.ts index 44fed01..d2e4512 100644 --- a/tests/webpay/transaccion_completa/transaction.test.ts +++ b/tests/webpay/transaccion_completa/transaction.test.ts @@ -107,7 +107,7 @@ describe('FullTransactionTest', () => { .reply(200, expectedResponse); - const buyOrder = Math.floor(Math.random() * 1000).toString(); + const buyOrder = 'order_123'; const authorization = '1213'; const amount = 1000; diff --git a/tests/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index 52c4236..6c5a981 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -165,7 +165,7 @@ describe('WebpayPlusMallTest', () => { .reply(200, expectedResponse); const commerceCode = "597055555537"; - const buyOrder = Math.floor(Math.random() * 1000).toString(); + const buyOrder = 'order_123'; const authorization = '1213'; const amount = 1000; diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts index 29e9427..8eb9d6b 100644 --- a/tests/webpay/webpay_plus/transaction.test.ts +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -21,8 +21,8 @@ describe('WebpayPlusTest', () => { .post('/transactions') .reply(200, mockResponse); - const buyOrder = String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); - const sessionId = String(Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)); + const buyOrder = 'order_123'; + const sessionId = 'session_123'; const amount = 1000; const returnUrl = "http://www.google.com"; @@ -109,7 +109,7 @@ describe('WebpayPlusTest', () => { .reply(200, expectedResponse); - const buyOrder = Math.floor(Math.random() * 1000).toString(); + const buyOrder = 'order_123'; const authorization = '1213'; const amount = 1000; From 0659a740cf0ca167c4ae1ed92e4798d44b8a4d28 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Wed, 28 Aug 2024 10:53:58 -0500 Subject: [PATCH 30/44] tests: update url --- tests/webpay/oneclick/mall_inscription.test.ts | 2 +- tests/webpay/webpay_plus/transaction.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/webpay/oneclick/mall_inscription.test.ts b/tests/webpay/oneclick/mall_inscription.test.ts index 609bc0a..2e3495a 100644 --- a/tests/webpay/oneclick/mall_inscription.test.ts +++ b/tests/webpay/oneclick/mall_inscription.test.ts @@ -17,7 +17,7 @@ describe('InscriptionOneclickMallTest', () => { }; const username = "steve"; const email = "steve.rogers@continuum.cl"; - const returnUrl = "http://localhost:8081/oneclick-mall/finish"; + const returnUrl = "https://localhost:8081/oneclick-mall/finish"; nock(apiUrl) .post(`/inscriptions`) diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts index 8eb9d6b..dd51fbc 100644 --- a/tests/webpay/webpay_plus/transaction.test.ts +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -24,7 +24,7 @@ describe('WebpayPlusTest', () => { const buyOrder = 'order_123'; const sessionId = 'session_123'; const amount = 1000; - const returnUrl = "http://www.google.com"; + const returnUrl = "https://www.google.com"; const response = await WebpayPlus.Transaction .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) From 13775891dc8d80b2053bcf4adc7d3968506edf7d Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Wed, 28 Aug 2024 11:11:11 -0500 Subject: [PATCH 31/44] test: update url to https --- tests/patpass/inscription.test.ts | 4 ++-- tests/webpay/webpay_plus/mall_transaction.test.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/patpass/inscription.test.ts b/tests/patpass/inscription.test.ts index 8e9d7c0..3bda927 100644 --- a/tests/patpass/inscription.test.ts +++ b/tests/patpass/inscription.test.ts @@ -24,13 +24,13 @@ describe('PatpassComercioTest', () => { .post('/patInscription') .reply(200, mockResponse); - const urlRequest = 'http://localhost:8081/patpass-comercio/commit'; + 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 = 'http://localhost:8081/patpass-comercio/final'; + const finalUrl = 'https://localhost:8081/patpass-comercio/final'; const maxAmount = 0; const phoneNumber = '123456734'; const mobileNumber = '123456723'; diff --git a/tests/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index 6c5a981..bf86dc1 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -21,7 +21,7 @@ describe('WebpayPlusMallTest', () => { url: urlResponse }); - const returnUrl = 'http://www.google.com'; + const returnUrl = 'https://www.google.com'; const buyOrder = randomInt(0, 1000).toString(); const sessionId = randomInt(0, 1000).toString(); From c9fc0f0f90c524bc1aef70ec6806c6e89c5c185d Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Wed, 28 Aug 2024 14:18:22 -0500 Subject: [PATCH 32/44] feat: remove webpay modal --- lib/index.ts | 3 +- lib/transbank/webpay/modal/index.ts | 55 ------------- lib/transbank/webpay/modal/requests/index.ts | 5 -- .../modal/requests/modal_create_request.ts | 26 ------- lib/transbank/webpay/modal/transaction.ts | 77 ------------------- 5 files changed, 1 insertion(+), 165 deletions(-) delete mode 100644 lib/transbank/webpay/modal/index.ts delete mode 100644 lib/transbank/webpay/modal/requests/index.ts delete mode 100644 lib/transbank/webpay/modal/requests/modal_create_request.ts delete mode 100644 lib/transbank/webpay/modal/transaction.ts 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/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; From 108bdd9da7a22e0bcbea55e8bdda965236217adc Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 10 Oct 2024 15:07:42 -0500 Subject: [PATCH 33/44] test: remove commas --- tests/webpay/oneclick/mall_transaction.test.ts | 2 +- tests/webpay/transaccion_completa/mall_transaction.test.ts | 4 ++-- tests/webpay/webpay_plus/mall_transaction.test.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/webpay/oneclick/mall_transaction.test.ts b/tests/webpay/oneclick/mall_transaction.test.ts index 11bdf7d..2c71e3a 100644 --- a/tests/webpay/oneclick/mall_transaction.test.ts +++ b/tests/webpay/oneclick/mall_transaction.test.ts @@ -36,7 +36,7 @@ describe('OneclickMallTest', () => { amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo - ), + ) ]; nock(apiUrl) diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index 03af061..ef98007 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -40,7 +40,7 @@ describe('MallFullTransactionTest', () => { amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo - ), + ) ]; nock(apiUrl) @@ -84,7 +84,7 @@ describe('MallFullTransactionTest', () => { idQueryInstallmentsTwo, deferredPeriodIndexTwo, false - ), + ) ]; const expectedResponse = generateJsonResponse(); diff --git a/tests/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index bf86dc1..96b39ec 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -44,7 +44,7 @@ describe('WebpayPlusMallTest', () => { amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo - ), + ) ]; const response = await new WebpayPlus.MallTransaction(option) From 21df7a4eebae99d85143e7b2d3d73e9d4454465c Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 10 Oct 2024 15:10:02 -0500 Subject: [PATCH 34/44] test: remove unused variable --- tests/webpay/transaccion_completa/mall_transaction.test.ts | 2 -- tests/webpay/webpay_plus/mall_transaction.test.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index ef98007..8f9d4fb 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -16,7 +16,6 @@ describe('MallFullTransactionTest', () => { 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'; @@ -165,7 +164,6 @@ describe('MallFullTransactionTest', () => { }); test('refund', async () => { - const url = `/${apiUrl}/transactions/${testToken}/refunds`; const type = 'REVERSED'; nock(apiUrl) diff --git a/tests/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index 96b39ec..41dfcd3 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -95,7 +95,6 @@ describe('WebpayPlusMallTest', () => { }); test('refund', async () => { - const url = `/${apiUrl}/transactions/${testToken}/refunds`; const type = 'REVERSED'; nock(apiUrl) From 2e1cfdfadff78ec514db9d3c059102cc50503979 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 10 Oct 2024 15:10:16 -0500 Subject: [PATCH 35/44] test: remove unused param --- tests/common/options.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/common/options.test.ts b/tests/common/options.test.ts index 0324bbb..9d99236 100644 --- a/tests/common/options.test.ts +++ b/tests/common/options.test.ts @@ -30,7 +30,6 @@ test('the timeout parameter is set successfully', async () => { ); const handleTimeout = ( - resolve: (value?: unknown) => void, reject: (reason?: any) => void ): void => { setTimeout(() => { From 00d83fb6ac2c1c3e76d78755c03efcfeac2374d2 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Thu, 10 Oct 2024 15:12:45 -0500 Subject: [PATCH 36/44] test: fix import --- tests/patpass/inscription.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/patpass/inscription.test.ts b/tests/patpass/inscription.test.ts index 3bda927..0b89471 100644 --- a/tests/patpass/inscription.test.ts +++ b/tests/patpass/inscription.test.ts @@ -1,8 +1,7 @@ import { IntegrationApiKeys, IntegrationCommerceCodes, PatpassComercio } from "../../lib"; import ApiConstants from "../../lib/transbank/common/api_constants"; import Environment from "../../lib/transbank/patpass/common/environment"; - -const nock = require('nock'); +import nock from 'nock'; describe('PatpassComercioTest', () => { const apiUrl = `${Environment.Integration}${ApiConstants.PATPASS_COMERCIO_ENDPOINT}`; From 03820f8a57d5327b928464d08e48f53bfc63e4bb Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 10:19:10 -0500 Subject: [PATCH 37/44] test: refactor data --- tests/mocks/oneclick_data.ts | 42 +++++ .../webpay/oneclick/mall_transaction.test.ts | 165 ++++-------------- 2 files changed, 76 insertions(+), 131 deletions(-) create mode 100644 tests/mocks/oneclick_data.ts 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/webpay/oneclick/mall_transaction.test.ts b/tests/webpay/oneclick/mall_transaction.test.ts index 2c71e3a..d4bf235 100644 --- a/tests/webpay/oneclick/mall_transaction.test.ts +++ b/tests/webpay/oneclick/mall_transaction.test.ts @@ -1,7 +1,8 @@ import nock from 'nock'; import { randomInt } from 'crypto'; -import { Environment, IntegrationApiKeys, IntegrationCommerceCodes, Oneclick, Options, TransactionDetail, WebpayPlus } from '../../../lib'; +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}`; @@ -11,183 +12,85 @@ describe('OneclickMallTest', () => { nock.cleanAll(); }); - test('authorize', async () => { - const expectedResponse = generateJsonResponse(); + 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); + 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); - - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); test('refund', async () => { const type = 'REVERSED'; const buyOrder = '500894028'; - - nock(apiUrl) - .post(`/transactions/${buyOrder}/refunds`) - .reply(200, { type }); - + 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 = generateJsonResponse(); - nock(apiUrl) - .get(`/transactions/${expectedResponse.buy_order}`) - .reply(200, expectedResponse); + 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); - - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); - test('capture', async () => { - const expectedResponse = { - authorization_code: "1213", - authorization_date: "2021-07-31T23:31:14.249Z", - captured_amount: 1000, - response_code: 0 - }; - - nock(apiUrl) - .put(`/transactions/capture`) - .reply(200, expectedResponse); - + 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 generateJsonResponse(): any { - return { - 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" - } - ] - }; + 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); + } + }); From e7156f568b6a6cd292e176daf955c6140f7eaddc Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:19:44 -0500 Subject: [PATCH 38/44] test: refactor webpay mall --- tests/mocks/webpay_data.ts | 42 +++++ .../webpay_plus/mall_transaction.test.ts | 144 ++++-------------- 2 files changed, 71 insertions(+), 115 deletions(-) create mode 100644 tests/mocks/webpay_data.ts diff --git a/tests/mocks/webpay_data.ts b/tests/mocks/webpay_data.ts new file mode 100644 index 0000000..52e42c0 --- /dev/null +++ b/tests/mocks/webpay_data.ts @@ -0,0 +1,42 @@ +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/webpay/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index 41dfcd3..9073e8e 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -2,6 +2,7 @@ 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}`; @@ -22,29 +23,18 @@ describe('WebpayPlusMallTest', () => { }); 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 - ) + new TransactionDetail(amountMallOne, mallOneCommerceCode, buyOrderMallOne), + new TransactionDetail(amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo) ]; const response = await new WebpayPlus.MallTransaction(option) @@ -55,43 +45,14 @@ describe('WebpayPlusMallTest', () => { }); test('commit', async () => { - const expectedResponse = generateJsonResponse(); - + 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); - - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); test('refund', async () => { @@ -112,53 +73,18 @@ describe('WebpayPlusMallTest', () => { }); test('status', async () => { - const expectedResponse = generateJsonResponse(); - + 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); - - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); test('capture', async () => { - const expectedResponse = { - authorization_code: "1213", - authorization_date: "2021-07-31T23:31:14.249Z", - captured_amount: 1000, - response_code: 0 - }; - + const expectedResponse = WEBPAY_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK; nock(apiUrl) .put(`/transactions/${testToken}/capture`) .reply(200, expectedResponse); @@ -177,38 +103,26 @@ describe('WebpayPlusMallTest', () => { expect(response.response_code).toBe(expectedResponse.response_code); }); - function generateJsonResponse(): any { - return { - 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" - } - ] - }; + 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); + } + }); From 39dabe6f1718e73c2972465553342d421b307424 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:23:51 -0500 Subject: [PATCH 39/44] test: refactor full tx mall test --- tests/mocks/transaccion_completa_data.ts | 41 ++++++ .../mall_transaction.test.ts | 126 ++++-------------- 2 files changed, 68 insertions(+), 99 deletions(-) create mode 100644 tests/mocks/transaccion_completa_data.ts diff --git a/tests/mocks/transaccion_completa_data.ts b/tests/mocks/transaccion_completa_data.ts new file mode 100644 index 0000000..6d18793 --- /dev/null +++ b/tests/mocks/transaccion_completa_data.ts @@ -0,0 +1,41 @@ +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/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index 8f9d4fb..da6072a 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -1,7 +1,8 @@ import nock from 'nock'; import { randomInt } from 'crypto'; -import { CommitDetail, Environment, InstallmentDetail, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta, TransactionDetail } from '../../../lib'; +import { CommitDetail, Environment, IntegrationApiKeys, IntegrationCommerceCodes, Options, TransaccionCompleta, TransactionDetail } 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}`; @@ -85,8 +86,7 @@ describe('MallFullTransactionTest', () => { false ) ]; - const expectedResponse = generateJsonResponse(); - + const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; nock(apiUrl) .put(`/transactions/${testToken}`) .reply(200, expectedResponse); @@ -94,38 +94,11 @@ describe('MallFullTransactionTest', () => { const response = await new TransaccionCompleta.MallTransaction(option) .commit(testToken, mallDetails); - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); test('status', async () => { - const expectedResponse = generateJsonResponse(); - + const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; nock(apiUrl) .get(`/transactions/${testToken}`) .reply(200, expectedResponse); @@ -133,34 +106,7 @@ describe('MallFullTransactionTest', () => { const response = await new TransaccionCompleta.MallTransaction(option) .status(testToken); - 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); - - let expectedDetail = expectedResponse.details[0]; - let detail = response.details[0]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); - - expectedDetail = expectedResponse.details[1]; - detail = response.details[1]; - expect(detail.amount).toBe(expectedDetail.amount); - expect(detail.status).toBe(expectedDetail.status); - expect(detail.authorization_code).toBe(expectedDetail.authorization_code); - expect(detail.payment_type_code).toBe(expectedDetail.payment_type_code); - expect(detail.response_code).toBe(expectedDetail.response_code); - expect(detail.installments_number).toBe(expectedDetail.installments_number); - expect(detail.commerce_code).toBe(expectedDetail.commerce_code); - expect(detail.buy_order).toBe(expectedDetail.buy_order); + testResponse(response, expectedResponse); }); test('refund', async () => { @@ -181,13 +127,7 @@ describe('MallFullTransactionTest', () => { }); test('capture', async () => { - const expectedResponse = { - authorization_code: "1213", - authorization_date: "2021-07-31T23:31:14.249Z", - captured_amount: 1000, - response_code: 0 - }; - + const expectedResponse = FULL_TX_MALL_TRANSACTION_CAPTURE_RESPONSE_MOCK; nock(apiUrl) .put(`/transactions/${testToken}/capture`) .reply(200, expectedResponse); @@ -206,37 +146,25 @@ describe('MallFullTransactionTest', () => { expect(response.response_code).toBe(expectedResponse.response_code); }); - function generateJsonResponse(): any { - return { - 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" - }; + 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); } }); From 9a4b835fcd243b55d34d469d43985101ec9ac805 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:29:02 -0500 Subject: [PATCH 40/44] test: delete comment --- tests/webpay/webpay_plus/transaction.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts index dd51fbc..74c7578 100644 --- a/tests/webpay/webpay_plus/transaction.test.ts +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -131,7 +131,7 @@ describe('WebpayPlusTest', () => { status: "AUTHORIZED", buy_order: "1643997337", session_id: "1134425622", - card_detail: { card_number: "6623" }, // Asegúrate de que mapResponseDetail esté definido previamente + card_detail: { card_number: "6623" }, accounting_date: "0731", transaction_date: "2021-07-31T23:31:14.249Z", authorization_code: "1213", From 3b8b719054412268413134a673ec48c0ef6ea4a2 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:37:32 -0500 Subject: [PATCH 41/44] test: refactor webpay test --- tests/mocks/webpay_data.ts | 23 +++++ tests/webpay/webpay_plus/transaction.test.ts | 93 +++++--------------- 2 files changed, 47 insertions(+), 69 deletions(-) diff --git a/tests/mocks/webpay_data.ts b/tests/mocks/webpay_data.ts index 52e42c0..ae28599 100644 --- a/tests/mocks/webpay_data.ts +++ b/tests/mocks/webpay_data.ts @@ -1,3 +1,26 @@ +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", diff --git a/tests/webpay/webpay_plus/transaction.test.ts b/tests/webpay/webpay_plus/transaction.test.ts index 74c7578..4c80e43 100644 --- a/tests/webpay/webpay_plus/transaction.test.ts +++ b/tests/webpay/webpay_plus/transaction.test.ts @@ -1,6 +1,7 @@ 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}`; @@ -16,10 +17,7 @@ describe('WebpayPlusTest', () => { token: testToken, url: urlResponse }; - - nock(apiUrl) - .post('/transactions') - .reply(200, mockResponse); + nock(apiUrl).post('/transactions').reply(200, mockResponse); const buyOrder = 'order_123'; const sessionId = 'session_123'; @@ -35,37 +33,20 @@ describe('WebpayPlusTest', () => { test('commit', async () => { - const expectedResponse = generateJsonResponse(); - nock(apiUrl) - .put(`/transactions/${testToken}`) - .reply(200, expectedResponse); + 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); - 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); + testResponse(response, expectedResponse); }); test('refund', async () => { const amount = 1000; const type = "REVERSED"; - const mockResponse = { type }; - - nock(apiUrl) - .post(`/transactions/${testToken}/refunds`) - .reply(200, mockResponse); + nock(apiUrl).post(`/transactions/${testToken}/refunds`).reply(200, mockResponse); const response = await WebpayPlus.Transaction .buildForIntegration(IntegrationCommerceCodes.WEBPAY_PLUS, IntegrationApiKeys.WEBPAY) @@ -74,41 +55,18 @@ describe('WebpayPlusTest', () => { }); test('status', async () => { - const expectedResponse = generateJsonResponse(); - nock(apiUrl) - .get(`/transactions/${testToken}`) - .reply(200, expectedResponse); + 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); - 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); + testResponse(response, expectedResponse); }); test('capture', async () => { - const expectedResponse = { - authorization_code: "1213", - authorization_date: "2021-07-31T23:31:14.249Z", - captured_amount: 1000, - response_code: 0 - }; - - nock(apiUrl) - .put(`/transactions/${testToken}/capture`) - .reply(200, expectedResponse); - - + 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; @@ -123,22 +81,19 @@ describe('WebpayPlusTest', () => { expect(response.response_code).toBe(expectedResponse.response_code); }); - - function generateJsonResponse(): any { - return { - 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 - }; + 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); } }); From ddcacd74628bbf5323f203f205eaa1861d40d803 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:44:50 -0500 Subject: [PATCH 42/44] test: refactor full tx tests --- tests/mocks/transaccion_completa_data.ts | 24 ++++ .../mall_transaction.test.ts | 35 ++---- .../transaccion_completa/transaction.test.ts | 112 +++++------------- 3 files changed, 58 insertions(+), 113 deletions(-) diff --git a/tests/mocks/transaccion_completa_data.ts b/tests/mocks/transaccion_completa_data.ts index 6d18793..36de165 100644 --- a/tests/mocks/transaccion_completa_data.ts +++ b/tests/mocks/transaccion_completa_data.ts @@ -1,3 +1,27 @@ +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" }, // Asegúrate de que mapResponseDetail esté definido previamente + 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: [ { diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index da6072a..19a82ba 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -21,31 +21,19 @@ describe('MallFullTransactionTest', () => { 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 - ) + new TransactionDetail(amountMallOne, mallOneCommerceCode, buyOrderMallOne), + new TransactionDetail(amountMallTwo, mallTwoCommerceCode, buyOrderMallTwo) ]; - nock(apiUrl) - .post('/transactions') - .reply(200, { token }); + nock(apiUrl).post('/transactions').reply(200, { token }); const response = await new TransaccionCompleta.MallTransaction(option).create( buyOrder, @@ -87,9 +75,7 @@ describe('MallFullTransactionTest', () => { ) ]; const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; - nock(apiUrl) - .put(`/transactions/${testToken}`) - .reply(200, expectedResponse); + nock(apiUrl).put(`/transactions/${testToken}`).reply(200, expectedResponse); const response = await new TransaccionCompleta.MallTransaction(option) .commit(testToken, mallDetails); @@ -99,14 +85,11 @@ describe('MallFullTransactionTest', () => { test('status', async () => { const expectedResponse = FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK; - nock(apiUrl) - .get(`/transactions/${testToken}`) - .reply(200, expectedResponse); + nock(apiUrl).get(`/transactions/${testToken}`).reply(200, expectedResponse); const response = await new TransaccionCompleta.MallTransaction(option) .status(testToken); - - testResponse(response, expectedResponse); + testResponse(response, expectedResponse); }); test('refund', async () => { @@ -122,15 +105,12 @@ describe('MallFullTransactionTest', () => { 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); + nock(apiUrl).put(`/transactions/${testToken}/capture`).reply(200, expectedResponse); const commerceCode = "597055555537"; const buyOrder = 'order_123'; @@ -139,7 +119,6 @@ describe('MallFullTransactionTest', () => { 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); diff --git a/tests/webpay/transaccion_completa/transaction.test.ts b/tests/webpay/transaccion_completa/transaction.test.ts index d2e4512..1c5f2b2 100644 --- a/tests/webpay/transaccion_completa/transaction.test.ts +++ b/tests/webpay/transaccion_completa/transaction.test.ts @@ -2,6 +2,7 @@ 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}`; @@ -21,11 +22,7 @@ describe('FullTransactionTest', () => { const cardNumber = 'XXXXXXXXXXXX6623'; const month = '12'; const year = '28'; - - nock(apiUrl) - .post('/transactions') - .reply(200, { token }); - + nock(apiUrl).post('/transactions').reply(200, { token }); const response = await new TransaccionCompleta.Transaction(option).create( buyOrder, sessionId, @@ -34,85 +31,40 @@ describe('FullTransactionTest', () => { cardNumber, year + "/" + month ); - expect(response.token).toBe(testToken); }); test('commit', async () => { - const expectedResponse = generateJsonResponse(); - nock(apiUrl) - .put(`/transactions/${testToken}`) - .reply(200, expectedResponse); - + 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); - 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); + testResponse(response, expectedResponse); }); test('refund', async () => { const amount = 1000; const type = "REVERSED"; const expectedResponse = { type }; - - nock(apiUrl) - .post(`/transactions/${testToken}/refunds`) - .reply(200, expectedResponse); - + 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 = generateJsonResponse(); - nock(apiUrl) - .get(`/transactions/${testToken}`) - .reply(200, expectedResponse); - + 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); - 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); + testResponse(response, expectedResponse); }); test('capture', async () => { - const expectedResponse = { - authorization_code: "1213", - authorization_date: "2021-07-31T23:31:14.249Z", - captured_amount: 1000, - response_code: 0 - }; - - nock(apiUrl) - .put(`/transactions/${testToken}/capture`) - .reply(200, expectedResponse); - - + 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); @@ -121,36 +73,26 @@ describe('FullTransactionTest', () => { 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 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 generateJsonResponse(): any { - return { - vci: "TSY", - amount: 1000.0, - status: "AUTHORIZED", - buy_order: "1643997337", - session_id: "1134425622", - card_detail: { card_number: "6623" }, // Asegúrate de que mapResponseDetail esté definido previamente - 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 - }; + + 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); } }); From 567346def05fe280776551cfe059e635347c2596 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 11:55:57 -0500 Subject: [PATCH 43/44] test: divided at the request of kiuwan --- .../mall_transaction.test.ts | 56 +------------ .../mall_transaction_2.test.ts | 78 +++++++++++++++++++ .../webpay_plus/mall_transaction.test.ts | 31 -------- .../webpay_plus/mall_transaction_2.test.ts | 68 ++++++++++++++++ 4 files changed, 149 insertions(+), 84 deletions(-) create mode 100644 tests/webpay/transaccion_completa/mall_transaction_2.test.ts create mode 100644 tests/webpay/webpay_plus/mall_transaction_2.test.ts diff --git a/tests/webpay/transaccion_completa/mall_transaction.test.ts b/tests/webpay/transaccion_completa/mall_transaction.test.ts index 19a82ba..b8d595e 100644 --- a/tests/webpay/transaccion_completa/mall_transaction.test.ts +++ b/tests/webpay/transaccion_completa/mall_transaction.test.ts @@ -2,7 +2,7 @@ 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_CAPTURE_RESPONSE_MOCK, FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/transaccion_completa_data'; +import { FULL_TX_MALL_TRANSACTION_STATUS_RESPONSE_MOCK } from '../../mocks/transaccion_completa_data'; describe('MallFullTransactionTest', () => { const apiUrl = `${Environment.Integration}${ApiConstants.WEBPAY_ENDPOINT}`; @@ -27,12 +27,10 @@ describe('MallFullTransactionTest', () => { 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( @@ -43,7 +41,6 @@ describe('MallFullTransactionTest', () => { mallDetails, cvv ); - expect(response.token).toBe(testToken); }); @@ -52,22 +49,19 @@ describe('MallFullTransactionTest', () => { 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, + new CommitDetail(mallOneCommerceCode, buyOrderMallOne, idQueryInstallmentsOne, deferredPeriodIndexOne, false ), - new CommitDetail( - buyOrderMallTwo, + new CommitDetail(buyOrderMallTwo, mallTwoCommerceCode, idQueryInstallmentsTwo, deferredPeriodIndexTwo, @@ -76,55 +70,11 @@ describe('MallFullTransactionTest', () => { ]; 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); - }); - - 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); 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/webpay_plus/mall_transaction.test.ts b/tests/webpay/webpay_plus/mall_transaction.test.ts index 9073e8e..9a317bc 100644 --- a/tests/webpay/webpay_plus/mall_transaction.test.ts +++ b/tests/webpay/webpay_plus/mall_transaction.test.ts @@ -72,37 +72,6 @@ describe('WebpayPlusMallTest', () => { expect(response.type).toBe(type); }); - 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); 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); + } + +}); From 78535fc54e6670dd37fe0827f870d0a58014e3f3 Mon Sep 17 00:00:00 2001 From: Miguel Vargas Date: Mon, 21 Oct 2024 15:37:05 -0500 Subject: [PATCH 44/44] test: remove comment --- tests/mocks/transaccion_completa_data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mocks/transaccion_completa_data.ts b/tests/mocks/transaccion_completa_data.ts index 36de165..4f38c01 100644 --- a/tests/mocks/transaccion_completa_data.ts +++ b/tests/mocks/transaccion_completa_data.ts @@ -4,7 +4,7 @@ export const FULL_TX_TRANSACTION_STATUS_RESPONSE_MOCK = { status: "AUTHORIZED", buy_order: "1643997337", session_id: "1134425622", - card_detail: { card_number: "6623" }, // Asegúrate de que mapResponseDetail esté definido previamente + card_detail: { card_number: "6623" }, accounting_date: "0731", transaction_date: "2021-07-31T23:31:14.249Z", authorization_code: "1213",