Skip to content

充值接口

类说明

Charge 类提供账户充值功能,支持CDK充值和在线支付(虎皮椒微信支付)。

充值方式:

  1. CDK充值:使用卡密直接充值到账户余额
  2. 在线充值:通过虎皮椒支付接口进行微信支付充值

使用说明:

  • 所有充值接口都需要登录
  • 充值金额以分为单位存储(1元=100分)
  • 在线支付采用异步回调机制,支付成功后自动到账

CDK充值

Method : POST

URL : /wp-json/vtheme/v1/charge/cdk

Auth required : True

Headers :

  • Authorization: Bearer
  • Content-Type: application/json

Body :

json
{
  "cdk": "ABCD1234EFGH"
}

参数说明:

  • cdk : [string] CDK卡密,长度5-20位

Success Response

Code : 200 OK

Content example :

json
{
  "message": "充值成功",
  "balance": "100.00",
  "amount": "50.00"
}

返回字段说明:

  • message : 提示信息
  • balance : 充值后的账户余额(元)
  • amount : 本次充值金额(元)

Error Response

Code : 400 Bad Request

Content examples :

json
{
  "code": "missing_cdk",
  "message": "请输入CDK卡密",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_cdk_length",
  "message": "CDK长度不正确",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_cdk",
  "message": "CDK不存在或已使用",
  "data": {
    "status": 400
  }
}
json
{
  "code": "cdk_expired",
  "message": "CDK已过期",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "请先登录",
  "data": {
    "status": 401
  }
}

Code : 500 Internal Server Error

Content example :

json
{
  "code": "recharge_failed",
  "message": "具体错误信息",
  "data": {
    "status": 500
  }
}

创建在线充值订单

Method : POST

URL : /wp-json/vtheme/v1/charge/create

Auth required : True

Headers :

  • Authorization: Bearer
  • Content-Type: application/json

Body :

json
{
  "amount": 100
}

参数说明:

  • amount : [number] 充值金额(元),最低0.01元

Success Response

Code : 200 OK

Content example :

json
{
  "order_id": 123,
  "order_trade_no": "CHG202301011200001234",
  "pay_url": "https://api.xunhupay.com/payment/xxx",
  "qrcode": "weixin://wxpay/bizpayurl?pr=xxx",
  "qrcode_type": "text",
  "amount": "100.00"
}

返回字段说明:

  • order_id : 订单ID
  • order_trade_no : 订单号
  • pay_url : 支付页面URL
  • qrcode : 二维码内容(可能是URL或图片URL)
  • qrcode_type : 二维码类型,image=图片URL,text=文本内容
  • amount : 充值金额(元)

注意:

  • PC端会返回可直接扫描的支付二维码
  • 前端可以使用 qrcode 生成二维码供用户扫描支付

Error Response

Code : 400 Bad Request

Content examples :

json
{
  "code": "invalid_amount",
  "message": "请输入有效的充值金额",
  "data": {
    "status": 400
  }
}
json
{
  "code": "amount_too_low",
  "message": "最低充值金额为0.01元",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "请先登录",
  "data": {
    "status": 401
  }
}

Code : 500 Internal Server Error

Content examples :

json
{
  "code": "config_error",
  "message": "虎皮椒配置不完整",
  "data": {
    "status": 500
  }
}
json
{
  "code": "create_order_failed",
  "message": "具体错误信息",
  "data": {
    "status": 500
  }
}

查询充值订单状态

Method : GET

URL : /wp-json/vtheme/v1/charge/query/{id}

Auth required : True

URL Parameters :

  • id : [integer] 订单ID

Headers :

  • Authorization: Bearer

Success Response

Code : 200 OK

Content example :

json
{
  "order_id": 123,
  "order_trade_no": "CHG202301011200001234",
  "amount": "100.00",
  "status": "completed",
  "created_at": "2023-01-01 12:00:00"
}

status 可能的值:

  • wait-pay : 待支付
  • completed : 已完成

Error Response

Code : 400 Bad Request

Content example :

json
{
  "code": "missing_order_id",
  "message": "缺少订单ID",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "请先登录",
  "data": {
    "status": 401
  }
}

Code : 404 Not Found

Content example :

json
{
  "code": "order_not_found",
  "message": "订单不存在",
  "data": {
    "status": 404
  }
}

虎皮椒支付回调(系统内部使用)

Method : POST

URL : /wp-json/vtheme/v1/charge/hpj-notify

Auth required : False (回调接口不需要认证)

说明:

  • 此接口由虎皮椒支付平台调用,用户无需手动调用
  • 用于处理支付成功后的异步通知
  • 系统会自动验证签名、更新订单状态、增加用户余额

使用示例

CDK充值

javascript
// CDK充值
fetch('/wp-json/vtheme/v1/charge/cdk', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + token
  },
  body: JSON.stringify({
    cdk: 'ABCD1234EFGH'
  })
})
.then(response => response.json())
.then(data => {
  console.log('充值成功');
  console.log('当前余额:', data.balance);
  console.log('充值金额:', data.amount);
})
.catch(error => {
  console.error('充值失败:', error);
});

在线充值

javascript
// 创建充值订单
fetch('/wp-json/vtheme/v1/charge/create', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + token
  },
  body: JSON.stringify({
    amount: 100
  })
})
.then(response => response.json())
.then(data => {
  console.log('订单创建成功');
  console.log('订单号:', data.order_trade_no);
  
  // 显示二维码
  if (data.qrcode_type === 'image') {
    // qrcode是图片URL
    document.getElementById('qrcode').src = data.qrcode;
  } else {
    // qrcode是文本,需要生成二维码
    QRCode.toCanvas(document.getElementById('qrcode'), data.qrcode);
  }
  
  // 轮询查询订单状态
  const checkInterval = setInterval(() => {
    fetch(`/wp-json/vtheme/v1/charge/query/${data.order_id}`, {
      headers: {
        'Authorization': 'Bearer ' + token
      }
    })
    .then(response => response.json())
    .then(orderData => {
      if (orderData.status === 'completed') {
        clearInterval(checkInterval);
        console.log('支付成功!');
        alert('充值成功!');
      }
    });
  }, 3000); // 每3秒查询一次
});

注意事项

  1. CDK充值

    • CDK只能使用一次
    • CDK可能有过期时间
    • 充值后立即到账
  2. 在线充值

    • 最低充值金额为0.01元
    • 支付成功后通过异步回调自动到账
    • 建议前端轮询查询订单状态
    • 回调接口具有幂等性,重复回调不会重复充值
  3. 安全性

    • 所有操作都需要登录
    • 使用事务保证数据一致性
    • 回调接口会验证签名防止伪造