diff --git a/frontend/package-lock.json b/frontend/package-lock.json index f28ac29..5ba11ca 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "axios": "^1.7.9", "element-plus": "^2.9.4", + "jszip": "^3.10.2", "pinia": "^2.3.0", "qrcode": "^1.5.4", "vue": "^3.5.13", @@ -1744,6 +1745,12 @@ "proto-list": "~1.2.1" } }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmmirror.com/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", + "license": "MIT" + }, "node_modules/cssstyle": { "version": "4.6.0", "resolved": "https://registry.npmmirror.com/cssstyle/-/cssstyle-4.6.0.tgz", @@ -2393,6 +2400,18 @@ "node": ">=0.10.0" } }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmmirror.com/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==", + "license": "MIT" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, "node_modules/ini": { "version": "1.3.8", "resolved": "https://registry.npmmirror.com/ini/-/ini-1.3.8.tgz", @@ -2416,6 +2435,12 @@ "dev": true, "license": "MIT" }, + "node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", + "license": "MIT" + }, "node_modules/js-beautify": { "version": "2.0.3", "resolved": "https://registry.npmmirror.com/js-beautify/-/js-beautify-2.0.3.tgz", @@ -2516,6 +2541,27 @@ "node": ">= 14" } }, + "node_modules/jszip": { + "version": "3.10.2", + "resolved": "https://registry.npmmirror.com/jszip/-/jszip-3.10.2.tgz", + "integrity": "sha512-3l+rb15IOWtUhU0H5MFqES/T6Kh7abYwjosBey/vD6hDt8zoEffkSC5Ws5SGtgVw3gBx2NEbhTeSW1+kWkpyTQ==", + "license": "(MIT OR GPL-3.0-or-later)", + "dependencies": { + "lie": "~3.3.0", + "pako": "~1.0.2", + "readable-stream": "~2.3.6", + "setimmediate": "^1.0.5" + } + }, + "node_modules/lie": { + "version": "3.3.0", + "resolved": "https://registry.npmmirror.com/lie/-/lie-3.3.0.tgz", + "integrity": "sha512-UaiMJzeWRlEujzAuw5LokY1L5ecNQYZKfmyZ9L7wDHb/p5etKaxXhohBcrw0EYby+G/NA52vRSN4N39dxHAIwQ==", + "license": "MIT", + "dependencies": { + "immediate": "~3.0.5" + } + }, "node_modules/locate-path": { "version": "5.0.0", "resolved": "https://registry.npmmirror.com/locate-path/-/locate-path-5.0.0.tgz", @@ -2732,6 +2778,12 @@ "node": ">=6" } }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmmirror.com/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==", + "license": "(MIT AND Zlib)" + }, "node_modules/parse5": { "version": "7.3.0", "resolved": "https://registry.npmmirror.com/parse5/-/parse5-7.3.0.tgz", @@ -2896,6 +2948,12 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmmirror.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "license": "MIT" + }, "node_modules/proto-list": { "version": "1.2.4", "resolved": "https://registry.npmmirror.com/proto-list/-/proto-list-1.2.4.tgz", @@ -2939,6 +2997,21 @@ "node": ">=10.13.0" } }, + "node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmmirror.com/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmmirror.com/require-directory/-/require-directory-2.1.1.tgz", @@ -3007,6 +3080,12 @@ "dev": true, "license": "MIT" }, + "node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmmirror.com/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/safer-buffer": { "version": "2.1.2", "resolved": "https://registry.npmmirror.com/safer-buffer/-/safer-buffer-2.1.2.tgz", @@ -3046,6 +3125,12 @@ "integrity": "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==", "license": "ISC" }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmmirror.com/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", + "license": "MIT" + }, "node_modules/siginfo": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/siginfo/-/siginfo-2.0.0.tgz", @@ -3076,6 +3161,15 @@ "dev": true, "license": "MIT" }, + "node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmmirror.com/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmmirror.com/string-width/-/string-width-4.2.3.tgz", @@ -3250,6 +3344,12 @@ "dev": true, "license": "MIT" }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmmirror.com/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, "node_modules/vite": { "version": "6.4.3", "resolved": "https://registry.npmmirror.com/vite/-/vite-6.4.3.tgz", diff --git a/frontend/package.json b/frontend/package.json index ab4e971..58378d5 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -12,6 +12,7 @@ "dependencies": { "axios": "^1.7.9", "element-plus": "^2.9.4", + "jszip": "^3.10.2", "pinia": "^2.3.0", "qrcode": "^1.5.4", "vue": "^3.5.13", diff --git a/frontend/src/views/PaymentsPanel.test.ts b/frontend/src/views/PaymentsPanel.test.ts index 72bc17a..d5052f5 100644 --- a/frontend/src/views/PaymentsPanel.test.ts +++ b/frontend/src/views/PaymentsPanel.test.ts @@ -1,4 +1,5 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import JSZip from 'jszip' import { flushPromises, mount } from '@vue/test-utils' import { defineComponent, nextTick } from 'vue' import ElementPlus, { ElPagination } from 'element-plus' @@ -18,6 +19,35 @@ const options = {global:{plugins:[ElementPlus],stubs:{ }}} beforeEach(() => { vi.resetAllMocks(); calls.getPaymentChannel.mockImplementation((channel:string) => Promise.resolve({data:{data:configuration(channel)}})); calls.savePaymentChannel.mockImplementation((channel:string) => Promise.resolve({data:{data:configuration(channel)}})); calls.listPaymentOrders.mockResolvedValue({data:{data:[row,{...row,id:2,order_id:9,merchant_order_no:'merchant-9',channel:'wechat',status:'created',order_status:'pending_payment',last_error:null}]}}) }) describe('PaymentsPanel', () => { + it('stashes and restores the complete draft in page memory', async () => { + const wrapper = mount(PaymentsPanel,options); await flushPromises() + await wrapper.get('[aria-label="支付宝应用 ID"]').setValue('draft-app') + await wrapper.get('[aria-label="支付宝商户私钥"]').setValue('draft-secret') + const panel = wrapper.get('[data-channel="alipay"]') + await panel.findAll('button').find(button => button.text() === '暂存草稿')!.trigger('click') + await wrapper.get('[aria-label="支付宝应用 ID"]').setValue('changed') + await wrapper.get('[aria-label="支付宝商户私钥"]').setValue('changed') + await panel.findAll('button').find(button => button.text() === '恢复草稿')!.trigger('click') + expect(wrapper.get('[aria-label="支付宝应用 ID"]').element.value).toBe('draft-app') + expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('draft-secret') + wrapper.unmount() + }) + it('imports merchant private key locally without using merchant certificate as platform verifier', async () => { + const zip = new JSZip() + zip.file('apiclient_key.pem','-----BEGIN PRIVATE KEY-----\nTEST ONLY\n-----END PRIVATE KEY-----') + zip.file('apiclient_cert.pem','-----BEGIN CERTIFICATE-----\nTEST ONLY\n-----END CERTIFICATE-----') + const bytes = await zip.generateAsync({type:'uint8array'}) + const wrapper = mount(PaymentsPanel,options); await flushPromises() + await wrapper.findAll('button').find(button => button.text() === '导入微信证书 ZIP')!.trigger('click') + const input = wrapper.get('input[type="file"]') + Object.defineProperty(input.element,'files',{value:[new File([bytes as BlobPart],'cert.zip')]}) + await input.trigger('change') + await vi.waitFor(() => expect(wrapper.text()).toContain('已导入商户 API 私钥')) + expect(wrapper.get('[aria-label="微信支付商户私钥"]').element.value).toContain('TEST ONLY') + expect(wrapper.get('[aria-label="微信支付平台证书 / 验签公钥(兼容)"]').element.value).toBe('') + expect(calls.savePaymentChannel).not.toHaveBeenCalled() + wrapper.unmount() + }) it('collapses channel forms by default and expands them independently', async () => { const wrapper = mount(PaymentsPanel,{...options,attachTo:document.body}); await flushPromises() const alipayToggle = wrapper.get('[aria-label="支付宝渠道配置"]') @@ -103,30 +133,30 @@ describe('PaymentsPanel', () => { expect(wrapper.text()).toContain('配置保存成功'); expect(wrapper.text()).toContain('不代表真实交易联调通过') wrapper.unmount() }) - it('submits replacement credentials once, clears inputs, blocks duplicate save and refresh while saving', async () => { + it('submits replacement credentials once, clears inputs after success, blocks duplicate save and refresh while saving', async () => { let resolve!: (value:unknown) => void calls.savePaymentChannel.mockReturnValueOnce(new Promise(r => {resolve=r})) const wrapper = mount(PaymentsPanel,options); await flushPromises() await wrapper.get('[aria-label="支付宝商户私钥"]').setValue('replacement-secret') await wrapper.get('[data-channel="alipay"] form').trigger('submit') expect(calls.savePaymentChannel.mock.calls[0]![1].secrets).toEqual({private_key:'replacement-secret'}) - expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('') - expect(wrapper.html()).not.toContain('replacement-secret') + expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('replacement-secret') + expect(wrapper.text()).not.toContain('replacement-secret') await wrapper.get('[data-channel="alipay"] form').trigger('submit') expect(calls.savePaymentChannel).toHaveBeenCalledTimes(1) expect(wrapper.findAll('button').find(button => button.text().includes('刷新支付数据'))!.attributes('disabled')).toBeDefined() resolve({data:{data:configuration('alipay')}}); await flushPromises() expect(wrapper.text()).toContain('配置保存成功'); wrapper.unmount() }) - it('shows safe save failures, retains ordinary drafts and clears credentials for retry', async () => { + it('shows safe save failures and retains the complete draft for retry', async () => { calls.savePaymentChannel.mockRejectedValueOnce(new Error('replacement-secret raw PEM database-error')) const wrapper = mount(PaymentsPanel,options); await flushPromises() await wrapper.get('[aria-label="支付宝应用 ID"]').setValue('draft-app') await wrapper.get('[aria-label="支付宝商户私钥"]').setValue('replacement-secret') await wrapper.get('[data-channel="alipay"] form').trigger('submit'); await flushPromises() expect(wrapper.get('[role="alert"]').text()).toContain('服务端校验未通过') - expect(wrapper.text()).not.toContain('配置保存成功'); expect(wrapper.html()).not.toMatch(/replacement-secret|raw PEM|database-error/) - expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('') + expect(wrapper.text()).not.toContain('配置保存成功'); expect(wrapper.text()).not.toMatch(/replacement-secret|raw PEM|database-error/) + expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('replacement-secret') expect(wrapper.get('[aria-label="支付宝应用 ID"]').element.value).toBe('draft-app') await wrapper.get('[data-channel="alipay"] form').trigger('submit'); await flushPromises() expect(wrapper.find('[role="alert"]').exists()).toBe(false); expect(wrapper.text()).toContain('配置保存成功'); wrapper.unmount() @@ -210,8 +240,8 @@ describe('PaymentsPanel', () => { await wrapper.get('[aria-label="支付宝商户私钥"]').setValue('replacement-secret') await wrapper.get('[data-channel="alipay"] form').trigger('submit'); await flushPromises() expect(wrapper.get('[data-channel="alipay"] [role="alert"]').text()).toBe(message || '保存失败:服务端校验未通过或暂不可用,请核对配置与密钥格式后重试') - expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('') - expect(wrapper.html()).not.toMatch(/raw-server-secret|replacement-secret|PEM SQL token/) + expect(wrapper.get('[aria-label="支付宝商户私钥"]').element.value).toBe('replacement-secret') + expect(wrapper.html()).not.toMatch(/raw-server-secret|PEM SQL token/) expect(wrapper.text()).not.toContain('配置保存成功') wrapper.unmount() }) diff --git a/frontend/src/views/PaymentsPanel.vue b/frontend/src/views/PaymentsPanel.vue index a35abfc..f5db88f 100644 --- a/frontend/src/views/PaymentsPanel.vue +++ b/frontend/src/views/PaymentsPanel.vue @@ -1,5 +1,6 @@