def _qrwxpay(self, shop_name): import chardet order_id = self.get_cookie("order_id") order = self.session.query(models.Order).filter_by(id=order_id).first() if not order: return self.send_fail('order not found') order.is_qrwxpay = 1 #表示该订单为扫码支付 order_num = order.num totalPrice = order.new_totalprice self.session.commit() # print("[WeixinQrPay]totalPrice:",totalPrice) # shop_name = re.compile(u'[\U00010000-\U0010ffff]').sub(u'',shop_name) wxPrice = int(totalPrice * 100) url = APP_OAUTH_CALLBACK_URL + '/customer/onlinewxpay' unifiedOrder = UnifiedOrder_pub() unifiedOrder.setParameter("body", 'Order No. ' + str(order_num)) unifiedOrder.setParameter("notify_url", url) unifiedOrder.setParameter("out_trade_no", str(order.num) + 'a') unifiedOrder.setParameter('total_fee', wxPrice) unifiedOrder.setParameter('trade_type', "NATIVE") res = unifiedOrder.postXml() # print(res) if isinstance(res, bytes): bianma = chardet.detect(res)['encoding'] res = res.decode(bianma) else: print("[WeixinQrPay]encoding error") res_dict = unifiedOrder.xmlToArray(res) if 'code_url' in res_dict: qr_url = res_dict['code_url'] else: qr_url = "" return qr_url
def get(self): print( "Shoudn't get into this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) order_id = self.get_cookie("order_id") order = self.session.query(models.Order).filter_by(id=order_id).first() if not order: return self.send_fail('order not found') totalPrice = order.new_totalprice wxPrice = int(totalPrice * 100) url = APP_OAUTH_CALLBACK_URL + '/customer/onlinewxpay' unifiedOrder = UnifiedOrder_pub() unifiedOrder.setParameter("body", str(order_num)) unifiedOrder.setParameter("notify_url", url) unifiedOrder.setParameter("out_trade_no", str(order.num)) unifiedOrder.setParameter('total_fee', wxPrice) unifiedOrder.setParameter('trade_type', "NATIVE") res = unifiedOrder.postXml().decode('utf-8') res_dict = unifiedOrder.xmlToArray(res) if 'code_url' in res_dict: qr_url = res_dict['code_url'] else: qr_url = "" return self.send_success(qr_url=qr_url)
def post(self): ############################################################## # 微信在线支付成功回调 # 修改订单状态 :支付订单刚生成时 状态为-1.完成支付后状态变为1 # 增加相应店铺 相应的余额 # 生成一条余额记录 # 给店铺管理员 和 顾客 发送微信消息 ############################################################## print( "[WeixinPay]handle WeixinPay Callback!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) data = self.request.body # print("[WeixinPay]request.body:",self.request.body) xml = data.decode('utf-8') UnifiedOrder = UnifiedOrder_pub() xmlArray = UnifiedOrder.xmlToArray(xml) status = xmlArray['result_code'] total_fee = float(int(xmlArray['total_fee']) / 100) order_num = str(xmlArray['out_trade_no']) order_num = order_num.split('a')[0] print("[WeixinPay]Callback order_num:", order_num) # result = orderId.split('a') # customer_id = int(result[0]) # shop_id = int(result[1]) # totalPrice = (float(result[2]))/100 transaction_id = str(xmlArray['transaction_id']) if status != 'SUCCESS': return False order = self.session.query( models.Order).filter_by(num=order_num).first() if not order: # return self.send_fail('order not found') #如果没找到订单,也要生成一条余额记录 #因为customer_id和shop_id 是外键,不能为空,所以给它们赋一个特定的值 balance_history = models.BalanceHistory( customer_id=0, shop_id=0, balance_value=total_fee, balance_record='在线支付(微信)异常:空订单' + order_num, balance_type=3, transaction_id=transaction_id) self.session.add(balance_history) self.session.commit() print( "[WeixinPay]No This Order!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) return self.write('success') customer_id = order.customer_id shop_id = order.shop_id totalPrice = order.new_totalprice create_date = order.create_date.timestamp() now = datetime.datetime.now().timestamp() time_difference = now - create_date if time_difference > 60 * 60 * 24 * 7: balance_history = models.BalanceHistory( customer_id=customer_id, shop_id=shop_id, balance_value=totalPrice, balance_record='在线支付(微信)异常:一星期以前的订单,很可能是线下测试回调到线上的', balance_type=3, transaction_id=transaction_id) self.session.add(balance_history) self.session.commit() print( "[WeixinPay]Order Time Wrong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) return self.write('success') order.status = 1 #修改订单状态 order.transaction_id = transaction_id self.session.flush() print("[WeixinPay]Callback order_num:", order_num, "change order.status to:", order.status) # 修改店铺总余额 # shop = self.session.query(models.Shop).filter_by(id = shop_id).first() # if not shop: # return self.send_fail('shop not found') shop = order.shop shop.shop_balance += totalPrice self.session.flush() #判断是否已经回调过,如果记录在表中,则不执行接下来操作 old_balance_history = self.session.query( models.BalanceHistory).filter_by( transaction_id=transaction_id).first() if old_balance_history: return self.write('success') customer = self.session.query( models.Customer).filter_by(id=customer_id).first() if customer: name = customer.accountinfo.nickname else: name = None shop_follow = self.session.query(models.CustomerShopFollow).filter_by(customer_id = customer_id,\ shop_id = shop_id).first() if not shop_follow: # return self.send_fail('shop_follow not found') #没有关注店铺也要生成余额记录 balance_history = models.BalanceHistory( customer_id=customer_id, shop_id=shop_id, balance_value=totalPrice, balance_record='在线支付(微信)异常:用户未关注,订单' + order.num, name=name, balance_type=3, shop_totalPrice=shop.shop_balance, transaction_id=transaction_id, shop_province=shop.shop_province, shop_name=shop.shop_name) self.session.add(balance_history) self.session.commit() print( "[WeixinPay]No CustomerShopFollow!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) # print("[WeixinPay]shop_balance:",shop.shop_balance) else: balance_history = models.BalanceHistory(customer_id =customer_id ,shop_id = shop_id,\ balance_value = totalPrice,balance_record = '在线支付(微信):订单'+ order.num, name = name , balance_type = 3,\ shop_totalPrice = shop.shop_balance,customer_totalPrice = shop_follow.shop_balance,transaction_id=transaction_id, shop_province=shop.shop_province,shop_name=shop.shop_name) self.session.add(balance_history) self.session.flush() # print("[WeixinPay]balance_history:",balance_history) #在线支付完成,CustomerSeckillGoods表对应的状态变为2,SeckillGoods表也做相应变化 fruits = eval(order.fruits) charge_type_list = list(fruits.keys()) seckill_goods = self.session.query(models.SeckillGoods).filter( models.SeckillGoods.seckill_charge_type_id.in_( charge_type_list)).with_lockmode('update').all() if seckill_goods: seckill_goods_id = [] for item in seckill_goods: seckill_goods_id.append(item.id) customer_seckill_goods = self.session.query(models.CustomerSeckillGoods).filter(models.CustomerSeckillGoods.shop_id == order.shop_id,models.CustomerSeckillGoods.customer_id == order.customer_id,\ models.CustomerSeckillGoods.seckill_goods_id.in_(seckill_goods_id)).with_lockmode('update').all() if customer_seckill_goods: for item in customer_seckill_goods: item.status = 2 self.session.flush() for item in seckill_goods: item.storage_piece -= 1 item.ordered += 1 self.session.flush() self.session.commit() print( "[WeixinPay]handle WeixinPay Callback SUCCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) # 发送订单模版消息给管理员/自动打印订单 if shop.admin.mp_name and shop.admin.mp_appid and shop.admin.mp_appsecret and shop.admin.has_mp: # print("[CustomerCart]cart_callback: shop.admin.mp_appsecret:",shop.admin.mp_appsecret,shop.admin.mp_appid) access_token = self.get_other_accessToken(self.session, shop.admin.id) # print(shop.admin.mp_name,shop.admin.mp_appid,shop.admin.mp_appsecret,access_token) else: access_token = None self.send_admin_message(self.session, order, access_token) return self.write('success')
def get(self): order_id = self.get_cookie("order_id") order = self.session.query(models.Order).filter_by(id=order_id).first() # print("[WeixinPay]Start Pay order.num:",order.num) if not order: return self.send_fail('order not found') totalPrice = order.new_totalprice wxPrice = int(totalPrice * 100) # print("[WeixinPay]full_url:",self.request.full_url()) path_url = self.request.full_url() shop_id = order.shop_id shop = self.session.query(models.Shop).filter_by(id=shop_id).first() if not shop: return self.send_fail('shop not found') shop_name = shop.shop_name shop_code = shop.shop_code shop_phone = shop.shop_phone jsApi = JsApi_pub() # order detail create_date = order.create_date receiver = order.receiver phone = order.phone address = order.address_text send_time = order.send_time remark = order.remark pay_type = order.pay_type online_type = order.online_type status = order.status order_num = order.num charge_types = self.session.query(models.ChargeType).filter( models.ChargeType.id.in_(eval(order.fruits).keys())).all() if order.type == 2: freight = order.shop.config.freight_on_time else: freight = order.shop.config.freight_now staff_id = order.SH2_id staff_info = self.session.query( models.Accountinfo).filter_by(id=staff_id).first() if staff_info is not None: sender_phone = staff_info.phone sender_img = staff_info.headimgurl_small else: sender_phone = None sender_img = None goods = [] f_d = eval(order.fruits) for f in f_d: goods.append([ f_d[f].get('fruit_name'), f_d[f].get('charge'), f_d[f].get('num') ]) qr_url = "" if not self.is_wexin_browser(): qr_url = self._qrwxpay(shop_name) # print("[WeixinPay]qr_url:",qr_url) return self.render('customer/online-qrwxpay.html',qr_url = qr_url,totalPrice = totalPrice,\ shop_name = shop_name,create_date=create_date,receiver=receiver,phone=phone,address=address,\ send_time = send_time,remark=remark,pay_type=pay_type,online_type=online_type,freight = freight,\ goods = goods,sender_phone=sender_phone,sender_img=sender_img,charge_types=charge_types,\ order=order,shop_code=shop_code,shop_phone = shop_phone) path = APP_OAUTH_CALLBACK_URL + self.reverse_url('onlineWxPay') code = self.args.get('code', None) if len(code) is 2: url = jsApi.createOauthUrlForCode(path) # print("[WeixinPay]code url:",url) return self.redirect(url) else: jsApi.setCode(code) openid = jsApi.getOpenid() # print("[WeixinPay]current code:",code) if not openid: print("[WeixinPay]OpenID not found") unifiedOrder = UnifiedOrder_pub() # totalPrice = self.args['totalPrice'] # totalPrice =float( self.get_cookie('money')) # print("[WeixinPay]totalPrice:",totalPrice) shop_name = re.compile(u'[\U00010000-\U0010ffff]').sub( u'', shop_name) unifiedOrder.setParameter("body", shop_name + '-订单号-' + str(order_num)) url = APP_OAUTH_CALLBACK_URL + '/customer/onlinewxpay' unifiedOrder.setParameter("notify_url", url) unifiedOrder.setParameter("openid", openid) unifiedOrder.setParameter("out_trade_no", order_num) # orderPriceSplite = (order.price) * 100 wxPrice = int(totalPrice * 100) # print("[WeixinPay]wxPrice:",wxPrice) unifiedOrder.setParameter('total_fee', wxPrice) unifiedOrder.setParameter('trade_type', "JSAPI") prepay_id = unifiedOrder.getPrepayId() # print("[WeixinPay]prepay_id:",prepay_id) if not prepay_id: return self.send_fail("微信支付失败,请稍后再试!") jsApi.setPrepayId(prepay_id) renderPayParams = jsApi.getParameters() # print("[WeixinPay]renderPayParams:",renderPayParams) noncestr = "".join( random.sample('zyxwvutsrqponmlkjihgfedcba0123456789', 10)) timestamp = datetime.datetime.now().timestamp() wxappid = 'wx0ed17cdc9020a96e' signature = self.signature(noncestr, timestamp, path_url) qr_url = self._qrwxpay(shop_name) return self.render("fruitzone/paywx.html",qr_url = qr_url ,renderPayParams = renderPayParams,wxappid = wxappid,\ noncestr = noncestr ,timestamp = timestamp,signature = signature,totalPrice = totalPrice,\ shop_name = shop_name,create_date=create_date,receiver=receiver,phone=phone,address=address,\ send_time = send_time,remark=remark,pay_type=pay_type,online_type=online_type,freight = freight,\ goods = goods,sender_phone=sender_phone,sender_img=sender_img,charge_types=charge_types,\ order=order,shop_code = shop_code,shop_phone = shop_phone)