评论

微信支付商户免充值代金券接口升级验收脚本 用例组合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 个评论

  • 鹏宇zero
    鹏宇zero
    2021-04-20

    辛苦!配置好了,直接验证成功。。。

    需要配置本地python环境。。。

    楼主解答很耐心。。。辛苦啦!

    2021-04-20
    赞同 2
    回复 2
  • 青寒
    青寒
    2021-04-17

    1,2,3,4,5 齐活!

    2021-04-17
    赞同 2
    回复 1
    • Memory
      Memory
      2021-04-18
      让选择困难症的直接一步到位
      2021-04-18
      1
      回复
  • 星星之火
    星星之火
    2021-07-18

    Traceback (most recent call last):


      File "/Users/cg/Documents/微信支付.py", line 173, in <module>


        upgrade(mch_id, wechat_key)


      File "/Users/cg/Documents/微信支付.py", line 102, in upgrade


        key = get_sign_key(mch_id, wechat_key)


      File "/Users/cg/Documents/微信支付.py", line 39, in get_sign_key


        retmsg = root.getElementsByTagName("return_msg")[0].childNodes[0].nodeValue


    IndexError: list index out of range

    这个什么情况,麻烦帮忙看看!

    2021-07-18
    赞同 1
    回复 2
    • 星星之火
      星星之火
      2021-07-18回复Memory
      反复确认过,没有错的,不知何故,超出范围了
      2021-07-18
      回复
    • 星星之火
      星星之火
      2021-07-19回复Memory
      搞定,谢谢
      2021-07-19
      回复
  • 深圳乐天金融贷款公司
    深圳乐天金融贷款公司
    2021-06-30

    这个是查询的功能啊,怎么升级呢

    2021-06-30
    赞同 1
    回复 1
    • Memory
      Memory
      发表于小程序端
      2021-06-30

      ???

      2021-06-30
      1
      回复
  • 焰
    2021-06-22

    !!!!!神器啊,真一键就开通了,找了各种聚合支付的开源项目,想一个个去开通,浪费了四五天时间,这个倒好,10分钟搞完了,舒坦,必须点赞

    2021-06-22
    赞同 1
    回复 1
    • Memory
      Memory
      2021-06-22
      找那些东西有什么用
      2021-06-22
      回复
  • 香橙薄荷凉
    香橙薄荷凉
    发表于小程序端
    2021-09-20

    验收成功,好评

    2021-09-20
    赞同
    回复
  • 姗姗来迟📶⁵ᴳ
    姗姗来迟📶⁵ᴳ
    2021-08-23

    这个是py2.x还是3.x环境?

    2021-08-23
    赞同
    回复
  • 璐梨
    璐梨
    2021-08-17

    那个,我是苹果电脑,高了半天还是不会啊!大神,请赐教。。。

    2021-08-17
    赞同
    回复
  • 勤奋
    勤奋
    2021-08-10

    十分方便的使用简单明了🤙🤙🤙

    2021-08-10
    赞同
    回复
  • 星星之火
    星星之火
    2021-07-04

    这python本地环境总是无法成功



    2021-07-04
    赞同
    回复 1
    • Memory
      Memory
      发表于小程序端
      2021-07-04

      这是你自己配置的问题

      2021-07-04
      1
      回复

正在加载...

登录 后发表内容