コード例 #1
0
    def post(self):
        self.set_header('Content-Type', 'application/xml; charset=UTF-8')

        # 解析京东请求
        logging.info('jingdong api. send_sms %s', self.request.body)
        request = JdToUs(self.request.body)
        distr_shop = self.db.get('select id, taobao_api_info from distributor_shop '
                                 'where distributor_id=%s and taobao_seller_id=%s',
                                 options.distributor_id_jingdong, request.vender_id)

        api_info = json.loads(distr_shop.taobao_api_info, object_hook=json_hook)
        request.set_key(api_info.vender_key, api_info.secret_key)
        request.parse()

        logging.info('jingdong api. send_sms decrypted %s',
                     ElementTree.tostring(request.message, encoding='utf-8', method='xml'))

        mobile = request.message.findtext('Mobile')
        vender_coupon_id = request.message.findtext('VenderCouponId')
        jd_coupon_id = request.message.findtext('JdCouponId')
        coupon = self.db.get('select *, c.id as cid from item i, item_coupon c where i.id=c.item_id and sn=%s', vender_coupon_id)
        logging.info('JD-coupon:%s' % coupon)
        CouponSMSMessage(self.db, self.redis, coupon=coupon).mobile(mobile).remark('京东请求重发').send()

        self.write(request.response('send_sms', 200, 'ok',
                                    mobile=mobile, vender_coupon_id=vender_coupon_id, jd_coupon_id=jd_coupon_id))
コード例 #2
0
    def post(self):
        coupon_sn = self.get_argument('coupon_sn', '')
        if not coupon_sn:
            self.write({'error': '券号为空'})
            return

        coupon = self.db.get(
            'select i.*, c.* from item i, item_coupon c where i.id=c.item_id and c.sn=%s',
            coupon_sn)
        if not coupon:
            self.write({'error': '电子券不存在'})
            return

        if not datetime.now() < coupon.expire_at:
            self.write({'error': '过期电子券不能重发'})
            return

        if not coupon.mobile:
            self.write({'error': '手机号码为空,发送失败'})
            return

        if not coupon.sms_sent_count < 10:
            self.write({'error': '发送次数超过10次限制'})
            return

        if not can_send_by_operator(self.db, coupon):
            self.write({'error': '电子券当前状态不能执行重发操作'})
            return

        CouponSMSMessage(self.db, self.redis,
                         coupon=coupon).remark('运营后台重发短信').operator(
                             self.current_user.name).send()
コード例 #3
0
    def resend(self, params, order):
        """ 重新发码 """
        if not order:
            return self.write('{"code":504}')

        # 按order_item发送券短信
        all_order_items = self.db.query(
            'select * from order_item where order_id=%s', order.order_id)
        for item in all_order_items:
            CouponSMSMessage(self.db, self.redis,
                             order_item=item).remark('淘宝订单短信重发').send()

        logging.info('taobao coupon resend end')
        self.write('{"code":200}')
コード例 #4
0
def yihaodian_order(db, redis, distributor_order_id, message_raw):
    """
    一号店分销订单处理
    :param db:
    :param redis:
    :param distributor_order_id:
    :param message_raw:
    :type db:torndb.Connection
    :type redis:redis.client.StrictRedis
    :type distributor_order_id: int
    """
    distributor_order = db.get('select * from distributor_order where id=%s',
                               distributor_order_id)
    distributor_order_no = distributor_order.order_no

    # 分销订单号不存在,结束
    if not distributor_order:
        logging.error(
            'yihaodian job consume cannot find distributor order: order_no=%s',
            distributor_order_no)
        return

    # 重新刷新订单信息
    yihaodian = Yihaodian('yhd.order.detail.get')
    response = yihaodian.sync_fetch(orderCode=distributor_order_no)
    logging.info('yihaodian response for distributor_order_no %s : %s' %
                 (distributor_order_no, response))
    yihaodian.parse_response(response)

    # 一号店请求失败,结束
    if not yihaodian.is_ok():
        logging.error(
            'yihaodian job consume fetch order failed, distributor order_no=%s',
            distributor_order_no)
        return

    order_status = yihaodian.message.findtext(
        './orderInfo/orderDetail/orderStatus').strip()

    # 一号店订单已取消,从队列移除,结束
    if order_status == 'ORDER_CANCEL':
        # 队列移除
        redis.lrem(options.queue_distributor_order_processing, 0, message_raw)
        logging.info(' yihaodian order_no %s cancelled ', distributor_order_no)
        return
    # 一号店订单状态为‘可以发货’, 则处理该订单
    elif order_status == 'ORDER_TRUNED_TO_DO':
        # 更新分销订单message, 如果distributor_order表中message为空,则该分销订单已在分销商处取消
        db.execute('update distributor_order set message=%s where order_no=%s',
                   response, distributor_order_no)

        # 筛选电子券订单
        order_items = yihaodian.message.findall(
            './orderInfo/orderItemList/orderItem')
        coupon_items = []
        for item in order_items:
            goods_link_id = item.findtext('./outerId')
            if goods_link_id:
                goods = db.get(
                    'select g.* from goods as g, goods_distributor_shop as gds '
                    'where g.id = gds.goods_id and gds.distributor_shop_id=%s and gds.goods_link_id=%s',
                    options.shop_id_yihaodian, goods_link_id)
                if not goods and int(goods_link_id) < 20000:
                    goods = db.get(
                        'select g.* from goods g where g.type="E" and g.id=%s',
                        goods_link_id)
                # 当商品类型为电子券时,才处理
                if goods and goods.type == options.goods_type_electronic:
                    coupon_items.append((item, goods))
                else:
                    logging.warning(
                        'yihaodian job consume warning: goods_link_id=%s not found or goods type is real'
                        % goods_link_id)

        if len(coupon_items) == 0:
            # 分销订单没有电子券,从处理队列移除
            redis.lrem(options.queue_distributor_order_processing, 0,
                       message_raw)
            logging.info(
                'yihaodian job consume exits for no real goods, distributor order_no: %s'
                % distributor_order_no)
            return

        # 生成一百券订单
        if distributor_order.order_id == 0 and check_stock(coupon_items):
            mobile = yihaodian.message.findtext(
                './orderInfo/orderDetail/goodReceiverMoblie')
            order_amount = yihaodian.message.findtext(
                './orderInfo/orderDetail/orderAmount')
            payment = Decimal(
                yihaodian.message.findtext(
                    './orderInfo/orderDetail/realAmount'))
            order_id, order_no = new_distributor_order(
                db, options.shop_id_yihaodian, order_amount, payment, mobile)

            # 生成订单项
            for item in coupon_items:
                order_item = item[0]
                goods_info = item[1]
                sales_price = Decimal(order_item.findtext('./orderItemPrice'))
                count = int(order_item.findtext('./orderItemNum'))
                distributor_goods_id = order_item.findtext('./productId')
                result = new_distributor_item(db, order_id, order_no,
                                              sales_price, count, goods_info,
                                              mobile,
                                              options.shop_id_yihaodian,
                                              distributor_goods_id, [], False)
                if not result.ok:
                    logging.error(
                        'failed to generate order items for distributor_order_id=%s of goods_id=%s'
                        % (distributor_order_id, goods_info.id))
                    return
                logging.info(
                    'create new yibaiquan order id=%s for yihaodian distributor order id=%s'
                    % (order_id, distributor_order_id))

            # 相互更新分销订单和订单
            db.execute(
                'update orders set distributor_order_id=%s where id = %s',
                distributor_order_id, order_id)
            db.execute('update distributor_order set order_id=%s where id=%s',
                       order_id, distributor_order_id)

            # 按order_item发送券短信
            all_order_items = db.query(
                'select * from order_item where order_id=%s', order_id)
            for item in all_order_items:
                CouponSMSMessage(db, redis,
                                 order_item=item).remark('一号店订单发送券号短信').send()

            # 通知一号店,已发货
            yihaodian = Yihaodian('yhd.logistics.order.shipments.update')
            response = yihaodian.sync_fetch(
                orderCode=distributor_order_no,
                deliverySupplierId=options.yhd_delivery,
                expressNbr=distributor_order_no)
            yihaodian.parse_response(response)

            if yihaodian.is_ok():
                # 分销订单处理完成,从处理队列移除
                redis.lrem(options.queue_distributor_order_processing, 0,
                           message_raw)
                logging.info('yihaodian distributor order (id=%s) finish' %
                             distributor_order_id)
            else:
                logging.error(
                    'update yihaodian distributor order (id=%s) shipments failed'
                    % distributor_order_id)
コード例 #5
0
    def post(self):

        # 解密58请求参数
        json_text = decrypt(self.get_argument('param'), options.wuba_secret_key)  # '2a6f8f2c')#
        params = json.loads(json_text, object_hook=json_hook)
        logging.info('wuba new order request : %s', json_dumps(params))

        # 获得参数
        try:
            wuba_order_no = params.orderId                      # 58 订单号
            sales_price = Decimal(params.prodPrice)             # 商品售价
            product_count = int(params.prodCount)               # 购买数量
            mobile = params.mobile                              # 订单券号接受手机号
            goods_link_id = int(params.groupbuyIdThirdpart)     # 伪商品ID
        except AttributeError:
            logging.info('wuba request failed: wrong params')
            return self.write('{"status":"10201", "msg": "参数解析错误"}')

        # 检查参数格式
        if int(product_count) <= 0 or sales_price.compare(Decimal(0)) < 0 or not check_phone(mobile):
            logging.info('wuba request failed: invalid params')
            return self.write('{"status":"20210", "msg": "输入参数错误"}')

        # 查找分销订单
        wuba_order = self.db.get('select * from distributor_order where '
                                 'distributor_shop_id=%s and order_no=%s',
                                 options.shop_id_wuba, wuba_order_no)

        if wuba_order:
            # 分销订单重复
            logging.info('wuba request failed: duplicated wuba order no')
            return self.write('{"status":"10100", "msg": "订单已存在"}')
        else:
            # 分销订单不存在, 生成分销订单
            distributor_order_id = self.db.execute(
                'insert into distributor_order(order_no, message, distributor_shop_id, created_at) '
                'values (%s, %s, %s, NOW())',
                wuba_order_no, json_text, options.shop_id_wuba)
            #放入处理队列
            self.redis.lpush(options.queue_distributor_failed_order, "%s-%s" % (wuba_order_no, goods_link_id))

        # 根据伪商品找到关联的商品
        goods_info = self.db.get('select g.* from goods g, goods_distributor_shop gds '
                                 'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s',
                                 options.shop_id_wuba, goods_link_id)
        # 如果商品不存在,根据真实商品ID查找
        if not goods_info and goods_link_id < 20000:
            goods_info = self.db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id)
        # 商品不存在, 返回错误信息
        if not goods_info:
            logging.info('wuba request failed: can not find goods')
            return self.write('{"status":"10100", "msg": "未找到商品"}')
        
        # 生成订单
        order_id, order_no = new_distributor_order(self.db, options.shop_id_wuba,
                                                   sales_price*product_count, sales_price*product_count, mobile)
        result = new_distributor_item(self.db, order_id, order_no, sales_price, product_count, goods_info,
                                      mobile, options.shop_id_wuba, "", [], False)

        if not result.ok:
            # 生成订单失败,返回
            logging.info('wuba request failed: generate order failed msg %s' % result.msg)
            return self.write('{"status":"10100", "msg": %s}' % result.msg)
        else:
            # 生成订单成功,更新id
            self.db.execute('update orders set distributor_order_id=%s where id = %s',
                            distributor_order_id, result.order_id)
            self.db.execute('update distributor_order set order_id=%s where id = %s',
                            result.order_id, distributor_order_id)
            # 从处理队列移除
            self.redis.lrem(options.queue_distributor_failed_order, 0, "%s-%s" % (wuba_order_no, goods_link_id))

        # 发送短信
        all_order_items = self.db.query('select * from order_item where order_id=%s', order_id)
        for item in all_order_items:
            CouponSMSMessage(self.db, self.redis, order_item=item).remark('58订单短信发送').send()

        tickets = []
        # 添加券信息到需要返回的tickets
        for coupon in result.coupons:
            coupon_sn = coupon['coupon_sn']
            created_at = self.db.get('select * from item i, item_coupon c where i.id=c.item_id and c.sn=%s', coupon_sn).created_at
            ticket = {
                "ticketId": int(coupon['id']),
                "ticketCode": str(coupon_sn),
                "ticketCount": 1,
                "createTime": str(created_at),
                "endTime": str(goods_info.expire_at),
            }
            tickets.append(ticket)

        # 生成合法的json返回
        data = {'orderId': wuba_order_no, 'orderIdThirdpart': order_no, 'tickets': tickets}
        result_json = {'status': '10000', 'msg': 'ok', 'data': encrypt(str(data), options.wuba_secret_key)}  # '2a6f8f2c')
        logging.info('wuba request success: %s tickets generated' % product_count)
        return self.write(json_dumps(result_json))
コード例 #6
0
    def post(self):
        notify_data = unquote_plus(
            self.get_argument('notify_data').encode('utf-8'))
        notify_data_xml = ET.fromstring(notify_data)

        status_set = {
            'WAIT_BUYER_PAY': 0,
            'TRADE_CLOSED': 1,
            'TRADE_SUCCESS': 2,
            'TRADE_PENDING': 3,
            'TRADE_FINISHED': 4
        }

        order_id = notify_data_xml.findtext('out_trade_no')
        trade_status = notify_data_xml.findtext('trade_status')

        status = status_set.get(trade_status, -1)

        if status in [2, 4]:
            shipping = self.db.get(
                'select oi.shipping_info_id, oi.goods_id, o.distributor_shop_id, s.sales_id, g.*, '
                'o.id oid, oi.id oiid, o.payment, o.order_no, o.mobile, oi.num '
                'from orders o, order_item oi, goods g, supplier s '
                'where o.id = oi.order_id and oi.goods_id = g.id and g.supplier_id = s.id '
                'and o.status = 0 and o.id = %s', order_id)
            if shipping:
                self.db.execute(
                    'update orders set paid_at = NOW(), status = 1 where id = %s',
                    order_id)
                if shipping.shipping_info_id:
                    self.db.execute(
                        'update order_shipping_info set paid_at = NOW() where id = %s',
                        shipping.shipping_info_id)

                item_ids = []
                for i in range(shipping.num):
                    item_id = self.db.execute(
                        'insert into item(status, goods_name, goods_id, distr_id, distr_shop_id, '
                        'sp_id, sales_id, order_id, order_item_id, order_no, face_value, payment, '
                        'sales_price, purchase_price, created_at) '
                        'values(1, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())',
                        shipping.short_name, shipping.goods_id,
                        options.distributor_id_weixin,
                        shipping.distributor_shop_id, shipping.supplier_id,
                        shipping.sales_id, shipping.oid, shipping.oiid,
                        shipping.order_no, shipping.face_value,
                        shipping.payment, shipping.sales_price,
                        shipping.purchase_price)
                    item_ids.append(item_id)
                # 如果是电子券,生成item_coupon 并发货
                if shipping.type == 'E':
                    # 导入券,获得导入的券号,密码
                    if shipping.generate_type == 'IMPORT':
                        coupon_imported = self.db.query(
                            'select * from coupon_imported where goods_id=%s and used=0 '
                            'limit %s', shipping.goods_id, shipping.num)
                        # 导入券库存不足,直接返回
                        if len(coupon_imported) < shipping.num:
                            send_email(
                                redis=self.redis,
                                subject=
                                'weixin coupon generation failed:out of stock for imported coupon',
                                to_list='*****@*****.**',
                                html='order id=%s and goods id=%s' %
                                (shipping.oid, shipping.goods_id))
                            logging.error('imported goods id=%s out of stock' %
                                          shipping.goods_id)
                            self.write('success')
                            return

                        imported_ids = [c.id for c in coupon_imported]
                        self.db.execute(
                            'update coupon_imported set used=1 where id in (%s)'
                            % ','.join(['%s'] * len(imported_ids)),
                            *imported_ids)
                        coupon_sns = [c.coupon_sn for c in coupon_imported]
                        coupon_pwds = [c.coupon_pwd for c in coupon_imported]

                    # 生成电子券
                    for i in range(int(shipping.num)):
                        if shipping.generate_type == 'IMPORT':
                            coupon_sn = coupon_sns[i]
                            coupon_pwd = coupon_pwds[i]
                        else:
                            coupon_pwd = ''
                            while True:
                                coupon_sn = ''.join([
                                    random.choice(string.digits)
                                    for z in range(10)
                                ])
                                # 没有重复,停止
                                if not self.db.get(
                                        'select id from item_coupon where sn=%s',
                                        coupon_sn):
                                    break
                        item_coupon_field = {
                            'mobile': shipping.mobile,
                            'sn': coupon_sn,
                            'pwd': coupon_pwd,
                            'distr_sn': None,
                            'distr_pwd': None,
                            'sms_sent_count': 0,
                            'expire_at': shipping.expire_at,
                            'item_id': item_ids.pop()
                        }
                        self.db.execute(
                            'insert into item_coupon(%s) values (%s)' %
                            (','.join(item_coupon_field.keys()), ','.join(
                                ['%s'] * len(item_coupon_field))),
                            *item_coupon_field.values())

                    # 发送电子券
                    all_order_items = self.db.query(
                        'select * from order_item where order_id=%s', order_id)
                    for item in all_order_items:
                        CouponSMSMessage(
                            self.db, self.redis,
                            order_item=item).remark('微商城发送券号短信').send()

                # 删除redis对应的值
                self.redis.delete('o:alipay:%s' % order_id)
                logging.info('order: %s, paid success', order_id)
            else:
                logging.info('order: %s, can not find order', order_id)
        else:
            logging.info('order: %s, paid failed. status: %s', order_id,
                         trade_status)

        self.write('success')
コード例 #7
0
def taobao_order(db, redis, distributor_order_id, message_raw):
    """
    处理淘宝的分销订单队列
    :param db
    :param redis
    :param distributor_order_id: 分销订单id
    :param message_raw: redis 队列元素值
    :type db:torndb.Connection
    :type redis:redis.client.StrictRedis
    :type distributor_order_id: int
    """

    distributor_order = db.get('select * from distributor_order where id=%s', distributor_order_id)
    params = json.loads(distributor_order.message, object_hook=json_hook)
    shop = db.get('select * from distributor_shop where taobao_seller_id=%s', params.taobao_sid)
    api_info = json.loads(shop.taobao_api_info, object_hook=json_hook)
    if not distributor_order.order_id:
        # 如果还没生成一百券订单
        goods_link_id = int(params.outer_iid)
        # 找到关联的商品
        goods_info = db.get('select g.* from goods g, goods_distributor_shop gds '
                            'where g.id = gds.goods_id and distributor_shop_id=%s and goods_link_id=%s',
                            shop.id, goods_link_id)
        if not goods_info and goods_link_id < 20000:
            goods_info = db.get('select g.* from goods g where g.type="E" and g.id=%s', goods_link_id)

        if not goods_info:
            logging.error('taobao order consume failed: goods not found. link_id: %s', goods_link_id)
            return

        taobao = Taobao('taobao.trade.get')
        taobao.set_app_info(api_info.app_key, api_info.app_secret_key)
        taobao.set_session(api_info.session)
        response = taobao.sync_fetch(tid=distributor_order.order_no,
                                     fields='total_fee,payment,orders.payment,orders.num,'
                                            'orders.sku_properties_name,orders.price')
        taobao.parse_response(response)

        order_payment = Decimal(taobao.message.trade.payment)
        # 创建订单
        order_id, order_no = new_distributor_order(db, shop.id, Decimal(taobao.message.trade.total_fee),
                                                   order_payment, params.mobile)
        result = new_distributor_item(db, order_id, order_no, order_payment / int(params.num),
                                      int(params.num), goods_info, params.mobile, shop.id, params.num_iid, None, False)

        if not result.ok:
            logging.error('taobao order consume failed. %s', result.msg)
            return
        else:
            db.execute('update orders set distributor_order_id=%s where id = %s',
                       distributor_order_id, result.order_id)
            db.execute('update distributor_order set order_id=%s where id=%s',
                       result.order_id, distributor_order_id)
        ktv_order_result = create_ktv_order(db, order_id, params)
    else:
        order_id = distributor_order.order_id
        ktv_order_result = []

    coupons = db.query('select c.sn as coupon_sn from item i, item_coupon c where i.id=c.item_id and i.order_id=%s',
                       order_id)

    # 告诉淘宝我们已发货
    taobao = Taobao('taobao.vmarket.eticket.send')
    taobao.set_app_info(api_info.app_key, api_info.app_secret_key)
    if api_info.app_key == options.taobao_kunran_app_key:  # 如果是码商,要加上码商的信息
        taobao.add_field('codemerchant_id', options.taobao_kunran_id)
        taobao.set_session(api_info.merchant_session)
    else:
        taobao.set_session(api_info.session)
    response = taobao.sync_fetch(order_id=params.order_id, token=params.token,
                                 verify_codes=','.join(['%s:1' % item.coupon_sn for item in coupons]))
    logging.info('tell taobao coupon send response: %s', response)
    taobao.parse_response(response)

    if taobao.is_ok():
        logging.info('taobao order complete. distributor_order_id: %s', distributor_order_id)
        all_order_items = db.query('select * from order_item where order_id=%s', order_id)
        for item in all_order_items:
            CouponSMSMessage(db, redis, order_item=item).remark('淘宝订单短信发送').send()
        redis.lrem(options.queue_distributor_order_processing, 0, message_raw)
        #只要ktv预订时间有,就给门店经理发送ktv预订的包厢信息
        for ktv_info in ktv_order_result:
            sku = ktv_info['sku']
            phone_numbers = ktv_info['manager_mobile']
            for phone in (phone_numbers.split(',') if phone_numbers else []):
                content = str(sku.date) + ktv_info['shop_name'] + '预订【' + str(params.mobile) + \
                    sku.room_name + " (" + str(params.num) + "间)" + sku.human_time_range + "】"
                #给经理发送短信告知预订信息
                logging.info('send message to manager,phone:%s,content:%s', phone, content)
                SMSMessage(content, phone).send(redis)
    else:
        logging.error('tell taobao coupon send failed: %s', taobao.error)