Files
hjc-web/server/api/tender/pay.post.ts
T
weicw-dshsa 31f2f07c5c feat: 收银台支持支付宝支付(微信/支付宝选择器)
PC 收银台此前硬编码「请使用微信扫码支付」,只能走微信 Native。

改动:
- 支付方式选择器(微信支付 / 支付宝),切换即重新发起支付并清掉上一张码——
  同一订单号换支付类型重复下单会被渠道拒(微信回 OUT_TRADE_NO_USED),
  两种支付信息同时留在页面上只会让用户困惑。
- 支付宝走电脑网站支付:后端返回自提交表单,用 document.write 写进新窗口。
  刻意不用 v-html——它插入的脚本标签不会执行(HTML 规范如此),
  表单永远不会自动提交。原收银台页留在原标签页继续轮询查单。
- 弹窗被拦截时给出可行动提示,并保留「前往支付宝付款」按钮兜底。
- 回到一张曾用支付宝发起过的待支付订单时,默认选中支付宝,
  避免又拿微信去撞一次「订单号已使用」。
- useHjcPayStatus 增加 channel 参数,仅供提示文案说对「微信侧/支付宝侧」。
  **状态词表不变**——后端已把两个渠道的状态映射成渠道无关的 PaymentStatus,
  前端再加一份支付宝词表等于把渠道知识泄漏进 UI。顺带把这层理由写进注释。
- 修正代码注释里对 ADR 0009 的引用:工作区 docs/adr 下并没有这一篇,
  相关决策现记录在 ADR 0008。

验证:eslint 0 error;npm run build:dev 构建通过。
2026-09-20 19:18:02 +08:00

41 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { $fetch } from 'ofetch'
import { createError, defineEventHandler, readBody, getHeader, getCookie } from 'h3'
import { useRuntimeConfig } from '#imports'
import { getTenantFromContext } from '../../utils/tenant'
/**
* 发起支付(微信 Native 扫码 / 支付宝电脑网站支付)
* POST /api/tender/pay { orderNo, payType? }
* 代理到 mp-api /api/hjc/order/pay;需登录态。
*
* `payType`:`wechat`(默认,与接入支付宝前行为一致)/ `alipay`。
* 请求体原样透传,因此新增支付方式**不需要改这个代理**。
*
* 约定:原样透传 ApiResult{code,message,data},由前端按 code 判定登录态与业务失败。
*/
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const ctx = getTenantFromContext(event, config)
const body = await readBody(event)
const cookieToken = getCookie(event, 'hjc_token')
const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null)
try {
return await $fetch('/hjc/order/pay', {
baseURL: config.public.modulesApiBase,
method: 'POST',
headers: {
TenantId: ctx.tenantId,
...(auth ? { Authorization: auth } : {})
},
query: { TenantId: ctx.tenantId },
body
})
} catch (error: any) {
throw createError({
statusCode: error?.statusCode || error?.response?.status || 502,
statusMessage: error?.statusMessage || 'Failed to pay'
})
}
})