Example #1
0
 def create_order(self, orderItemObj, clientIp):
     weixinOrderObj = WeiXinOrder()
     weixinOrderObj.appid = settings.WEIXIN['app_id']
     weixinOrderObj.mch_id = settings.WEIXIN['mch_id']
     weixinOrderObj.body = orderItemObj.product_desc
     weixinOrderObj.attach = orderItemObj.attach
     weixinOrderObj.out_trade_no = orderItemObj.orderno
     weixinOrderObj.total_fee = orderItemObj.fee
     weixinOrderObj.spbill_create_ip = clientIp
     weixinOrderObj.time_start = orderItemObj.dt_start.strftime(
         "%Y%m%d%H%M%S")
     weixinOrderObj.time_expire = orderItemObj.dt_end.strftime(
         "%Y%m%d%H%M%S")
     weixinOrderObj.notify_url = settings.WEIXIN['mch_notify_url']
     weixinOrderObj.trade_type = 'APP'
     weixinOrderObj.save()
     prepayid = weixin_pay_lib.create_order(weixinOrderObj)
     data = {
         'appid': settings.WEIXIN['app_id'],
         'partnerid': settings.WEIXIN['mch_id'],
         'prepayid': prepayid,
         'package': 'Sign=WXPay',
         'noncestr': random_helper.generate_nonce_str(23),
         'timestamp': str(datetime.get_unix_timestamp())
     }
     data['sign'] = security.sign(data)
     return data
Example #2
0
 def to_url(self):
     # sign data
     valueDict = self._get_vlaue_dict()
     valueDict['nonce_str'] = random_helper.generate_nonce_str(23)
     valueDict['sign'] = sign(valueDict)
     urlparameer = parse.urlencode(valueDict)
     return 'weixin://wxpay/bizpayurl?{}'.format(urlparameer)
Example #3
0
def request_shorten_url(url):
    _logger.info("get shorten url")
    data = {
        'appid': settings.WEIXIN['app_id'],
        'mch_id': settings.WEIXIN['mch_id'],
        'long_url': url,
        'nonce_str': random_helper.generate_nonce_str(23),
    }
    data['sign'] = sign(data)
    requestBody = xml_helper.dict_to_xml(data)
    _logger.info("request data is:{}".format(requestBody))

    url = 'https://api.mch.weixin.qq.com/tools/shorturl'
    r = requests.post(url,
                      data=requestBody,
                      headers={'Content-Type': 'application/xml'},
                      verify=False)
    return __handle_weixin_api_xml_response(
        r, lambda responseData: responseData['short_url'])
Example #4
0
def query_order(orderNo):
    weixinOrderObj = WeiXinOrder.objects.get(out_trade_no=orderNo)

    result = _compose_pay_result(orderNo, weixinOrderObj.pay_result.tradestate)
    if result.Succ or result.Lapsed:
        return result

    valueDict = {
        'appid': weixinOrderObj.appid,
        'mch_id': weixinOrderObj.mch_id,
        'out_trade_no': weixinOrderObj.out_trade_no,
        'nonce_str': random_helper.generate_nonce_str(23)
    }
    valueDict['sign'] = sign(valueDict)
    data = xml_helper.dict_to_xml(valueDict)

    url = 'https://api.mch.weixin.qq.com/pay/orderquery'
    r = requests.post(url,
                      data=data,
                      headers={'Content-Type': 'application/xml'},
                      verify=False)

    return __handle_weixin_api_xml_response(r, _process_order_result)
Example #5
0
 def create_order(self, orderItemObj, clientIp):
     weixinOrderObj = WeiXinOrder()
     weixinOrderObj.appid = settings.WEIXIN['app_id']
     weixinOrderObj.mch_id = settings.WEIXIN['mch_id']
     weixinOrderObj.body = orderItemObj.product_desc
     weixinOrderObj.attach = orderItemObj.attach
     weixinOrderObj.out_trade_no = orderItemObj.orderno
     weixinOrderObj.total_fee = orderItemObj.fee
     weixinOrderObj.spbill_create_ip = clientIp
     weixinOrderObj.time_start = orderItemObj.dt_start.strftime("%Y%m%d%H%M%S")
     weixinOrderObj.time_expire = orderItemObj.dt_end.strftime("%Y%m%d%H%M%S")
     weixinOrderObj.notify_url = settings.WEIXIN['mch_notify_url']
     weixinOrderObj.trade_type = 'APP'
     weixinOrderObj.save()
     prepayid = weixin_pay_lib.create_order(weixinOrderObj)
     data = {'appid':settings.WEIXIN['app_id'],
             'partnerid':settings.WEIXIN['mch_id'],
             'prepayid':prepayid,
             'package':'Sign=WXPay',
             'noncestr':random_helper.generate_nonce_str(23),
             'timestamp':str(datetime.get_unix_timestamp())}
     data['sign'] = security.sign(data)
     return data
Example #6
0
def query_order(orderNo):
    weixinOrderObj = WeiXinOrder.objects.get(out_trade_no=orderNo)
    
    result = _compose_pay_result(orderNo, weixinOrderObj.pay_result.tradestate)
    if result.Succ or result.Lapsed:
        return result
    
    url = 'https://api.mch.weixin.qq.com/pay/orderquery'
    valueDict = {
          'appid':weixinOrderObj.appid,
          'mch_id':weixinOrderObj.mch_id,
          'out_trade_no':weixinOrderObj.out_trade_no,
          'nonce_str':random_helper.generate_nonce_str(23)
          }
    valueDict['sign'] = sign(valueDict)
    data = xml_helper.dict_to_xml(valueDict)
    r = requests.post(url, data=data, headers={'Content-Type':'application/xml'}, verify=False)
    r.encoding = 'utf-8'
    if r.status_code == 200:
        responseData = xml_helper.xml_to_dict(r.text)
        result = _process_order_result(responseData)
        return result
    else:
        raise exceptions.PayProcessError('request processed failed. body:{}'.format(r.content))       
Example #7
0
 def to_xml(self):
     # sign data
     valueDict = self._get_vlaue_dict()
     valueDict['nonce_str'] = random_helper.generate_nonce_str(23)
     valueDict['sign'] = sign(valueDict)
     return xml_helper.dict_to_xml(valueDict)
Example #8
0
 def to_raw_rul(self):
     valueDict = self._get_vlaue_dict()
     valueDict['nonce_str'] = random_helper.generate_nonce_str(23)
     valueDict['sign'] = sign(valueDict)
     temp = ['{}={}'.format(k, v) for k, v in valueDict.items()]
     return 'weixin://wxpay/bizpayurl?{}'.format('&'.join(temp))
Example #9
0
 def to_xml(self):
     # sign data
     valueDict = self._get_vlaue_dict()
     valueDict['nonce_str'] = random_helper.generate_nonce_str(23)
     valueDict['sign'] = sign(valueDict)
     return xml_helper.dict_to_xml(valueDict)