Files
server-core/wechatpayv3/media.py
T
Pine f1147dced3 feat(pay): 微信支付V3子应用与算力充值
- app/pay 独立支付子应用(config/wxpay/models/repository/service/routers),便于后期拆分微服务
- 充值订单表 0026_compute_recharge;套餐支持折扣(discount)与上架开关(enabled),DB system_configs 优先、env 兜底
- 统一小程序支付:桌面端出小程序码→扫码进小程序确认页按 openid 发起 JSAPI→回调幂等到账(引擎 adjust_user_quota+镜像回写)
- 内嵌 wechatpayv3(含 async_),响应验签失败降级告警;新增 aiofiles 依赖

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-30 22:29:44 +08:00

55 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
import os.path
from .type import RequestType
from .utils import sha256
def _media_upload(self, filepath, filename, path):
if not (filepath and os.path.exists(filepath) and os.path.isfile(filepath) and path):
raise Exception('filepath is not assigned or not exists')
with open(filepath, mode='rb') as f:
content = f.read()
if not filename:
filename = os.path.basename(filepath)
params = {}
params.update({'meta': '{"filename":"%s","sha256":"%s"}' % (filename, sha256(content))})
mimes = {
'.bmp': 'image/bmp',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.avi': 'video/x-msvideo',
'.wmv': 'video/x-ms-wmv',
'.mpeg': 'video/mpeg',
'.mp4': 'video/mp4',
'.mov': 'video/quicktime',
'.mkv': 'video/x-matroska',
'.flv': 'video/x-flv',
'.f4v': 'video/x-f4v',
'.m4v': 'video/x-m4v',
'.rmvb': 'application/vnd.rn-realmedia-vbr'
}
media_type = os.path.splitext(filename)[-1]
if media_type not in mimes:
raise Exception(f'wechatpayv3 does not support this media type: {media_type}')
files = [('file', (filename, content, mimes[media_type]))]
return self._core.request(path, method=RequestType.POST, data=params, sign_data=params.get('meta'), files=files)
def image_upload(self, filepath, filename=None):
"""图片上传
:param filepath: 图片文件路径
:param filename: 文件名称,未指定则从filepath参数中截取
"""
return _media_upload(self, filepath, filename, path='/v3/merchant/media/upload')
def video_upload(self, filepath, filename=None):
"""视频上传
:param filepath: 视频文件路径
:param filename: 文件名称,未指定则从filepath参数中截取
"""
return _media_upload(self, filepath, filename, path='/v3/merchant/media/video_upload')