支付与收款商户号分离:支付改读 pay_* 独立配置与证书目录,收款侧不动。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import requests
|
||||
from django.db import transaction, IntegrityError
|
||||
from django.conf import settings
|
||||
from utils.xcx_sys_config import wx_cfg
|
||||
from utils.pay_mch_config import pay_cfg, get_pay_wx_cfg
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
@@ -9211,7 +9212,8 @@ class KefuPlatformRefundView(APIView):
|
||||
refund_result = self.call_wechat_refund(
|
||||
dingdan_id=order.dingdan_id,
|
||||
jine=order.jine,
|
||||
transaction_id=wechat_transaction_id
|
||||
transaction_id=wechat_transaction_id,
|
||||
pay_mch_id=getattr(order, 'pay_mch_id', '') or '',
|
||||
)
|
||||
|
||||
if refund_result['code'] != 0:
|
||||
@@ -9337,16 +9339,17 @@ class KefuPlatformRefundView(APIView):
|
||||
return Response({'code': 500, 'msg': '服务器内部错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
# ==================== 商户微信支付退款 ====================
|
||||
def call_wechat_refund(self, dingdan_id, jine, transaction_id=None):
|
||||
def call_wechat_refund(self, dingdan_id, jine, transaction_id=None, pay_mch_id=None):
|
||||
"""
|
||||
调用微信支付退款接口 secapi/pay/refund
|
||||
:return: {'code': 0, 'msg': '成功', 'refund_id': 'xxx'} 或 {'code': 非0, 'msg': '错误信息'}
|
||||
"""
|
||||
appid = wx_cfg.WEIXIN_APPID
|
||||
mch_id = wx_cfg.WEIXIN_MCHID
|
||||
key = wx_cfg.WEIXIN_SHANGHUMIYAO
|
||||
cert_path = wx_cfg.WEIXIN_CERT_PATH
|
||||
key_path = wx_cfg.WEIXIN_KEY_PATH
|
||||
pcfg = get_pay_wx_cfg(pay_mch_id)
|
||||
mch_id = pcfg.get('MCHID') or ''
|
||||
key = pcfg.get('KEY') or ''
|
||||
cert_path = pcfg.get('CERT_PATH') or ''
|
||||
key_path = pcfg.get('KEY_PATH') or ''
|
||||
|
||||
if not all([appid, mch_id, key, cert_path, key_path]):
|
||||
missing = [
|
||||
@@ -9596,7 +9599,8 @@ class KefuPlatformRefundView(APIView):
|
||||
refund_result = self.call_wechat_refund(
|
||||
dingdan_id=order.dingdan_id,
|
||||
jine=order.jine,
|
||||
transaction_id=wechat_transaction_id
|
||||
transaction_id=wechat_transaction_id,
|
||||
pay_mch_id=getattr(order, 'pay_mch_id', '') or '',
|
||||
)
|
||||
|
||||
if refund_result['code'] != 0:
|
||||
@@ -9672,20 +9676,22 @@ class KefuPlatformRefundView(APIView):
|
||||
logger.error(f"平台退款接口异常: {str(e)}", exc_info=True)
|
||||
return Response({'code': 500, 'msg': '服务器内部错误'})
|
||||
|
||||
def call_wechat_refund(self, dingdan_id, jine, transaction_id=None):
|
||||
def call_wechat_refund(self, dingdan_id, jine, transaction_id=None, pay_mch_id=None):
|
||||
"""
|
||||
调用微信支付退款接口
|
||||
:param dingdan_id: 商户订单号(我们的订单ID)
|
||||
:param jine: 退款金额
|
||||
:param transaction_id: 微信支付订单号(优先使用)
|
||||
:param pay_mch_id: 下单支付商户号
|
||||
:return: {'code': 0, 'msg': '成功', 'refund_id': 'xxx'} 或 {'code': 非0, 'msg': '错误信息'}
|
||||
"""
|
||||
# 获取配置
|
||||
appid = wx_cfg.WEIXIN_APPID
|
||||
mch_id = wx_cfg.WEIXIN_MCHID
|
||||
key = wx_cfg.WEIXIN_SHANGHUMIYAO
|
||||
cert_path = wx_cfg.WEIXIN_CERT_PATH
|
||||
key_path = wx_cfg.WEIXIN_KEY_PATH
|
||||
pcfg = get_pay_wx_cfg(pay_mch_id)
|
||||
mch_id = pcfg.get('MCHID') or ''
|
||||
key = pcfg.get('KEY') or ''
|
||||
cert_path = pcfg.get('CERT_PATH') or ''
|
||||
key_path = pcfg.get('KEY_PATH') or ''
|
||||
|
||||
if not all([appid, mch_id, key, cert_path, key_path]):
|
||||
missing = [k for k, v in {'appid': appid, 'mch_id': mch_id, 'key': key, 'cert_path': cert_path, 'key_path': key_path}.items() if not v]
|
||||
@@ -14837,8 +14843,8 @@ class FaKuanPayView(APIView):
|
||||
|
||||
def generate_wechat_pay_params(self, dingdanid, jine, openid, pay_type):
|
||||
APPID = wx_cfg.WEIXIN_APPID
|
||||
MCHID = wx_cfg.WEIXIN_MCHID
|
||||
KEY = wx_cfg.WEIXIN_SHANGHUMIYAO
|
||||
MCHID = pay_cfg.WEIXIN_MCHID
|
||||
KEY = pay_cfg.WEIXIN_SHANGHUMIYAO
|
||||
|
||||
if pay_type == 'fakuan':
|
||||
PAY_DESCRIPTION = settings.FAKUAN_PAY_DESCRIPTION
|
||||
@@ -15098,7 +15104,7 @@ class FaKuanHuitiaoView(View):
|
||||
|
||||
# 拼接字符串
|
||||
sign_string = '&'.join([f"{key}={value}" for key, value in sorted_params if value])
|
||||
sign_string += f'&key={wx_cfg.WEIXIN_SHANGHUMIYAO}'
|
||||
sign_string += f'&key={pay_cfg.WEIXIN_SHANGHUMIYAO}'
|
||||
|
||||
# 计算签名
|
||||
calculated_sign = hashlib.md5(sign_string.encode('utf-8')).hexdigest().upper()
|
||||
@@ -15360,8 +15366,8 @@ class KaohePayView(APIView):
|
||||
def generate_wechat_pay_params(self, dingdanid, jine, openid, attach, pay_type):
|
||||
"""生成微信JSAPI支付参数"""
|
||||
APPID = wx_cfg.WEIXIN_APPID
|
||||
MCHID = wx_cfg.WEIXIN_MCHID
|
||||
KEY = wx_cfg.WEIXIN_SHANGHUMIYAO
|
||||
MCHID = pay_cfg.WEIXIN_MCHID
|
||||
KEY = pay_cfg.WEIXIN_SHANGHUMIYAO
|
||||
|
||||
if pay_type == 'kaohe':
|
||||
PAY_DESCRIPTION = settings.KAOHE_PAY_DESCRIPTION
|
||||
@@ -15679,7 +15685,7 @@ class KaohePayCallbackView(View):
|
||||
|
||||
sorted_params = sorted(params.items(), key=lambda x: x[0])
|
||||
sign_string = '&'.join([f"{key}={value}" for key, value in sorted_params if value])
|
||||
sign_string += f'&key={wx_cfg.WEIXIN_SHANGHUMIYAO}'
|
||||
sign_string += f'&key={pay_cfg.WEIXIN_SHANGHUMIYAO}'
|
||||
calculated_sign = hashlib.md5(sign_string.encode('utf-8')).hexdigest().upper()
|
||||
|
||||
result = sign == calculated_sign
|
||||
|
||||
Reference in New Issue
Block a user