评论

微信支付商户免充值代金券接口升级验收脚本 用例组合1001+1002+1003+1004+1005(强迫症专用)

微信支付商户免充值代金券接口升级验收脚本 用例组合1001+1002+1003+1004+1005,强迫症可以不用再纠结验收哪个组合了,一次执行可以全部验收

为什么要进行接口升级验收

  • 商户开通免充值类产品功能后,微信支付接口和账单格式会有调整,商户内部系统需要适配升级后的接口参数。
  • 注:本文所提供脚本代码需要电脑安装python环境才可以运行。
  • 注意:一键验收脚本虽好,自己应用层代码也需要改哦

如何进行接口升级

准备事项

1) 微信支付商户号
-2)微信支付商户号对应32位秘钥

import time
from xml.dom import minidom
 
import hashlib
from heapq import heappush, heappop
from collections import OrderedDict

import requests

SandBox_Url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey'
MicroPay_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/micropay"
UnifiedOrder_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder"
OrderQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery"
ReFund_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refund"
RefundQuery_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery"
DownloadBill_Url = "https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill"
nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"

if __name__ == '__main__':
    mch_id = ""
    wxpay_key = ""
 
def get_sign_key(mch_id, key):
    template = "<xml><mch_id><![CDATA[{0}]]></mch_id>" \
               "<nonce_str><![CDATA[{1}]]></nonce_str>" \
               "<sign><![CDATA[{2}]]></sign></xml>"
    nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"
    encrypted_str = "mch_id=" + mch_id + "&nonce_str=" + nonce_str + "&key=" + key
    m = hashlib.md5()
    m.update(encrypted_str.encode('utf-8'))
    sign_key_request_data = template.format(mch_id, nonce_str, m.hexdigest().upper())
    result = requests.post(SandBox_Url, sign_key_request_data)
    dom = minidom.parseString(result.content)
    root = dom.documentElement
    sandbox_signkey = ''
    if root.getElementsByTagName("return_code")[0].childNodes[0].nodeValue == "FAIL":
        retmsg = root.getElementsByTagName("return_msg")[0].childNodes[0].nodeValue
        raise RuntimeError("请求出了点小错误:" + retmsg)
    else:
        sandbox_signkey = root.getElementsByTagName("sandbox_signkey")[0].childNodes[0].nodeValue
    print("亲,这就是你的沙箱密钥了哦: \n" + sandbox_signkey)
    return sandbox_signkey

def to_tree_map(param_map):
    keys = param_map.keys()
    heap = []
    for item in keys:
        heappush(heap, item)
 
    sort = []
    while heap:
        sort.append(heappop(heap))
 
    res_map = OrderedDict()
    for key in sort:
        res_map[key] = param_map.get(key)
 
    return res_map
 
 
def build_xml(param, wxpay_key):
    tree_map = to_tree_map(param)
    encrypted_str = ""
    for k in tree_map:
        encrypted_str += "{}={}&".format(k, tree_map[k])
    encrypted_str = encrypted_str + "key=" + wxpay_key
    m = hashlib.md5()
    m.update(encrypted_str.encode('utf-8'))
    sign = m.hexdigest().upper()
    param.update(sign=sign)
 
    complete_tree_map = to_tree_map(param)
    xml = "<xml>"
    for k in complete_tree_map:
        xml += "<{}><![CDATA[{}]]></{}>".format(k, complete_tree_map[k], k)
    xml += "</xml>"
    return xml
 
 
def request_handler(url, xml, desc):
    result = requests.post(url, xml)
    print(desc + "我才不是请求结果呢:\n" + result.content.decode("utf-8"))
 

def upgrade(mch_id, wxpay_key):
    if mch_id == "":
        raise RuntimeError("出差错了哦,亲,你的商户号在哪呢?不填写商户号亲亲是要给空气去验收吗?")
 
    if wxpay_key == "":
        raise RuntimeError("出差错了哦,亲,你不填写商户密钥怎么继续呢,是用爱吗?")
    key = get_sign_key(mch_id, wxpay_key)
    nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"
    out_trade_no = round(time.time())
    MicroPay_param = {
                 'appid': "wxd678efh567hg6787",
                 'mch_id': mch_id,
                 'nonce_str': nonce_str, 
                 'body': "check",
                 'out_trade_no': out_trade_no, 
                 'total_fee': "501", 
                 'spbill_create_ip': "8.8.8.8",
                 'auth_code': "120061098828009406", 
                }
    MicroPay_xml = build_xml(MicroPay_param, key)
    request_handler(MicroPay_Url, MicroPay_xml, "亲,用例编号1001刷卡正常支付有结果了,快来看呀 \n")
    time.sleep(1)
    
    OrderQuery_param = {
                       'appid': "wxd678efh567hg6787",
                       'mch_id': mch_id, 
                       'nonce_str': nonce_str,
                       'out_trade_no': out_trade_no,
                      }
    OrderQuery_xml = build_xml(OrderQuery_param, key)
    request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1001刷卡正常支付查询出结果了,快来看呀 \n")
    time.sleep(1)
    
    out_trade_no_2nd = round(time.time())
    print("我是1002下单的订单号:",+ out_trade_no_2nd)
    MicroPay_param = {
                 'appid': "wxd678efh567hg6787",
                 'mch_id': mch_id,
                 'nonce_str': nonce_str, 
                 'body': "check",
                 'out_trade_no': out_trade_no_2nd, 
                 'total_fee': "502", 
                 'spbill_create_ip': "8.8.8.8",
                 'auth_code': "120061098828009406",
                 }
    MicroPay_xml = build_xml(MicroPay_param, key)
    request_handler(MicroPay_Url, MicroPay_xml, "亲,用例编号1002刷卡正常支付结果来了,你还抓紧不来看 \n")
    time.sleep(1)
    
    OrderQuery_param = {
                       'appid': "wxd678efh567hg6787",
                       'mch_id': mch_id, 
                       'nonce_str': nonce_str,
                       'out_trade_no': out_trade_no_2nd,
                      }
    OrderQuery_xml = build_xml(OrderQuery_param, key)
    request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1002刷卡正常支付查询结果,结果好像有点不太对呢 \n")
    time.sleep(1)
 
    ReFund_param = {
                    'appid': "wxd678efh567hg6787", 
                    'mch_id': mch_id, 
                    'nonce_str':nonce_str,
                    'out_refund_no': out_trade_no,
                    'total_fee': "502",
                    'refund_fee': "501",  
                    'out_trade_no': out_trade_no_2nd,
                    }
 
    ReFund_xml = build_xml(ReFund_param, key)
    request_handler(ReFund_Url, ReFund_xml, "亲,下面展示的是用例编号1002刷卡支付退款的结果,你猜对不对 \n")
    time.sleep(1)
    
    RefundQuery_param = {
                          'appid': "wxd678efh567hg6787", 
                          'mch_id': mch_id,
                          'nonce_str':nonce_str,
                          'out_trade_no': out_trade_no_2nd, 
                          }
 
    RefundQuery_xml = build_xml(RefundQuery_param, key)
    request_handler(RefundQuery_Url, RefundQuery_xml, "亲,用例编号1002刷卡支付退款查询结果返回中,加载不出来长按电源键或Ait+F4重试哦 \n")
    time.sleep(1)
    
    nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS"
    out_trade_no = round(time.time())
    UnifiedOrder_param = {
                 'appid': "wxd678efh567hg6787",
                 'mch_id': mch_id,
                 'nonce_str': nonce_str, 
                 'body': "check",
                 'out_trade_no': out_trade_no, 
                 'total_fee': "551", 
                 'notify_url':"https://www.weixin.qq.com/wxpay/pay.php",
                 'spbill_create_ip': "8.8.8.8",
                 'trade_type': "JSAPI", 
                }
    UnifiedOrder_xml = build_xml(UnifiedOrder_param, key)
    request_handler(UnifiedOrder_Url, UnifiedOrder_xml, "亲,用例编号1003-公众号/APP/扫码正常支付有结果了,快来看呀 \n")
    time.sleep(1)
    OrderQuery_param = {
                       'appid': "wxd678efh567hg6787",
                       'mch_id': mch_id, 
                       'nonce_str': nonce_str,
                       'out_trade_no': out_trade_no,
                      }
    OrderQuery_xml = build_xml(OrderQuery_param, key)
    request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1003-公众号/APP/扫码正常支付查询出结果了,快来看呀 \n")
    time.sleep(1)
    
    out_trade_no_2nd = round(time.time() * 1000)
    print("我是1002下单的订单号:",+ out_trade_no_2nd)
    UnifiedOrder_param = {
                 'appid': "wxd678efh567hg6787",
                 'mch_id': mch_id,
                 'nonce_str': nonce_str, 
                 'body': "check",
                 'out_trade_no': out_trade_no_2nd, 
                 'total_fee': "552", 
                 'notify_url':"https://www.weixin.qq.com/wxpay/pay.php",
                 'spbill_create_ip': "8.8.8.8",
                 'trade_type': "JSAPI", 
                 }
    UnifiedOrder_xml = build_xml(UnifiedOrder_param, key)
    request_handler(UnifiedOrder_Url, UnifiedOrder_xml, "亲,用例编号1004-公众号/APP/扫码支付退款结果来了,你还抓紧不来看 \n")
    time.sleep(1)
    
    OrderQuery_param = {
                       'appid': "wxd678efh567hg6787",
                       'mch_id': mch_id, 
                       'nonce_str': nonce_str,
                       'out_trade_no': out_trade_no_2nd,
                      }
    OrderQuery_xml = build_xml(OrderQuery_param, key)
    request_handler(OrderQuery_Url, OrderQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果,结果好像有点不太对呢 \n")
    time.sleep(1)
 
    ReFund_param = {
                    'appid': "wxd678efh567hg6787", 
                    'mch_id': mch_id, 
                    'nonce_str':nonce_str,
                    'out_refund_no': out_trade_no,
                    'total_fee': "552",
                    'refund_fee': "551",  
                    'out_trade_no': out_trade_no_2nd,
                    }
 
    ReFund_xml = build_xml(ReFund_param, key)
    request_handler(ReFund_Url, ReFund_xml, "亲,下面展示的是用例编号1004-公众号/APP/扫码支付退款的结果,你猜对不对 \n")
    time.sleep(1)
    
    RefundQuery_param = {
                          'appid': "wxd678efh567hg6787", 
                          'mch_id': mch_id,
                          'nonce_str':nonce_str,
                          'out_trade_no': out_trade_no_2nd, 
                          }
 
    RefundQuery_xml = build_xml(RefundQuery_param, key)
    request_handler(RefundQuery_Url, RefundQuery_xml, "亲,用例编号1004-公众号/APP/扫码支付退款查询结果返回中,加载不出来长按电源键或Ait+F4重试哦 \n")
    time.sleep(1)
    
    DownloadBill_param = {
                           'appid': "wxd678efh567hg6787", 
                           'mch_id': mch_id, 
                           'nonce_str': nonce_str,
                           'bill_date': "2021-04-01", 
                           'bill_type': "ALL"
                          }
 
    DownloadBill_xml = build_xml(DownloadBill_param, key)
    request_handler(DownloadBill_Url, DownloadBill_xml, "亲,你要下载交易的对账单来了,加载中······,加载不出来长按电源键或Ait+F4重试哦 \n")
 
upgrade(mch_id, wxpay_key)   
  • 没有写注释的习惯,遇到错误可以私信我,拒绝手摸手教学
最后一次编辑于  2021-05-10  
点赞 11
收藏
评论

13 个评论

  • 奇异果
    奇异果
    2021-04-28

    小白,有谁能指导一下吗?

    2021-04-28
    赞同
    回复 1
    • JQ_Tsang🏀
      JQ_Tsang🏀
      2021-05-20
      改编码格式,或者把中文全改成英文
      2021-05-20
      回复
  • 为了爱1号
    为了爱1号
    2021-04-22

    前面验证组合二,等了一天,还在审核中,索性一步到位,👍,看还要审核几天。

    2021-04-22
    赞同
    回复 1
    • Memory
      Memory
      2021-04-22
      目前差不多要7个工作日哈
      2021-04-22
      1
      回复
  • 蔡婷
    蔡婷
    发表于移动端
    2021-04-19
    非常温柔的提示哈哈哈🤙🤙🤙
    2021-04-19
    赞同
    回复

正在加载...

登录 后发表内容