帮助中心 / 开发者文档 / 微信支付接入

概述

1Pass 支持微信 Native 支付(PC 扫码付),使用与登录相同的 AK/SK 签名体系。你的后端创建订单后,用户在 1Pass 提供的支付页面扫码付款,付款成功后 1Pass 通知你的后端。

支付流程

sequenceDiagram participant B as 你的后端 participant P as 1Pass participant W as 微信支付 participant U as 用户 B->>P: ① POST /pay/wechat/native(AK/SK 签名) P->>W: ② 调用微信下单 API W-->>P: 返回 code_url P-->>B: ③ 返回 checkout_url + code_url B->>U: ④ 重定向到 checkout_url U->>W: ⑤ 扫码支付 W->>P: ⑥ 支付回调通知 P->>B: ⑦ 通知你的后端(client_notify_url) P-->>U: ⑧ 页面显示支付成功

第一步:创建支付订单

使用与 /token 相同的 AK/SK 签名方式调用:

POST https://1pass.top/pay/wechat/native
Content-Type: application/json
X-1Pass-AK: your_ak
X-1Pass-Ts: 1706601234
X-1Pass-Nonce: random-uuid
X-1Pass-Sign: hmac_signature

{
    "out_trade_no": "order_20240101_001",
    "description": "VIP月卡",
    "amount_total": 2990,
    "currency": "CNY",
    "expire_minutes": 15
}

请求参数

字段 类型 必填 说明
out_trade_no string 商户订单号(6~64 字符,字母数字下划线横杠)
description string 商品描述(≤127 字符)
amount_total integer 金额(单位:分,如 2990 = ¥29.90)
currency string 币种,默认 CNY
expire_minutes integer 过期时间(分钟),默认 15

成功响应

{
    "out_trade_no": "order_20240101_001",
    "code_url": "weixin://wxpay/bizpayurl?pr=...",
    "prepay_id": "wx2024...",
    "status": "pending",
    "checkout_url": "https://1pass.top/pay/wechat/checkout?token=ck_..."
}

第二步:展示支付页面

方式一(推荐):将用户重定向到 checkout_url。1Pass 提供完整的支付 UI,包含二维码、倒计时和自动状态轮询。

方式二:使用 code_url 自行渲染二维码,并通过以下接口轮询支付状态:

GET https://1pass.top/pay/wechat/status?token=ck_...

第三步:接收支付结果

方式一:回调通知(推荐)

在管理后台配置 client_notify_url,支付成功后 1Pass 会 POST 通知到你的后端:

{
    "site_id": "site_123",
    "out_trade_no": "order_20240101_001",
    "transaction_id": "420000000000000000000",
    "trade_state": "SUCCESS",
    "amount": { "total": 2990, "currency": "CNY" },
    "event_type": "TRANSACTION.SUCCESS"
}

方式二:主动轮询

GET https://1pass.top/pay/wechat/status?token=ck_...

返回 status 字段值:

状态 说明
created已创建
pending等待支付
paid已支付
failed支付失败
closed已关闭
refunded已退款

自动对账:1Pass 内置补偿机制,每 5 分钟检查卡在待支付状态的订单,主动向微信查询实际状态。即使因网络问题漏掉回调,你也可以放心。

代码示例(Node.js)

const crypto = require('crypto');

async function createPaymentOrder(ak, sk, orderData) {
    const ts = Math.floor(Date.now() / 1000).toString();
    const nonce = crypto.randomBytes(16).toString('hex');
    const body = JSON.stringify(orderData);

    // 签名方式与 /token 完全一致
    const bodyHash = crypto.createHash('sha256').update(body).digest('hex');
    const canonical = `${ts}\n${nonce}\nPOST\n/pay/wechat/native\n${bodyHash}`;
    const sign = crypto.createHmac('sha256', sk)
        .update(canonical).digest('base64url');

    const resp = await fetch('https://1pass.top/pay/wechat/native', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-1Pass-AK': ak, 'X-1Pass-Ts': ts,
            'X-1Pass-Nonce': nonce, 'X-1Pass-Sign': sign,
        },
        body,
    });
    return await resp.json();
}

// 使用:
const result = await createPaymentOrder('ak_xxx', 'sk_xxx', {
    out_trade_no: 'order_' + Date.now(),
    description: 'VIP月卡',
    amount_total: 2990,
});
// 重定向用户到 result.checkout_url

签名注意:支付接口的签名方式与 /token 完全一致,只是路径变为 /pay/wechat/native。详见 签名算法详解

支付错误码

错误码 HTTP 说明
missing_fields 400 缺少必填字段(out_trade_no / description / amount_total)
invalid_out_trade_no 400 商户订单号格式不正确
invalid_amount 400 金额无效(须为正整数,单位分)
invalid_currency 400 不支持的币种(仅支持 CNY)
description_too_long 400 描述超过 127 字符
payment_not_configured 400 站点未配置微信支付
out_trade_no_conflict 409 商户订单号已被其他站点使用
wechat_create_failed 502 微信下单失败(请查看错误详情)