Esempio n. 1
0
def sync_jd(db):
    goods = db.query(
        "select gds.id,gds.status,gds.distributor_goods_id,"
        "gds.goods_link_id,ds.taobao_api_info,ds.taobao_seller_id "
        "from goods_distributor_shop gds, distributor_shop ds "
        'where gds.distributor_shop_id=ds.id and gds.status <> "PREPARE" and gds.deleted=0 '
        'and gds.distributor_goods_id <> "" and ds.distributor_id=%s',
        options.distributor_id_jingdong,
    )
    now = datetime.now()
    for g in goods:
        api_info = json.loads(g.taobao_api_info, object_hook=json_hook)
        jingdong = Jingdong("queryTeamInfo", str(g.taobao_seller_id), api_info.vender_key, api_info.secret_key)
        response = jingdong.sync_fetch(vender_team_id=g.goods_link_id, jd_team_id=g.distributor_goods_id)
        jingdong.parse_response(response)
        if not jingdong.is_ok():
            if jingdong.result_code == "-538":
                db.execute('update goods_distributor_shop set deleted="1", deleted_at=NOW() ' "where id=%s", g.id)
                logging.info("sync goods status. delete jingdong item %s", g.distributor_goods_id)
            else:
                logging.error("sync goods status. jingdong request error %s", response)
            continue
        end_time = datetime.fromtimestamp(int(jingdong.message.findtext("EndTime")))
        if end_time < now and g.status != "OFF_SALE":
            db.execute(
                'update goods_distributor_shop set status="OFF_SALE", offsale_at=NOW(), onsale_at=NULL ' "where id=%s",
                g.id,
            )
            logging.info("sync goods status. offsale jingdong item %s", g.distributor_goods_id)
        elif end_time > now and g.status != "ON_SALE":
            db.execute(
                'update goods_distributor_shop set status="ON_SALE", onsale_at=NOW(), offsale_at=NULL ' "where id=%s",
                g.id,
            )
            logging.info("sync goods status. onsale jingdong item %s", g.distributor_goods_id)
Esempio n. 2
0
    def post(self):
        order_id = self.get_argument("jd_order_id", "")
        coupon = self.get_argument("jd_coupon_no", "")

        # 根据订单号查券
        if order_id:
            coupons = []
            i = 0
            while True:
                jd_coupons = Jingdong(
                    "getCouponsList",
                    options.jingdong_fx_vender_id,
                    options.jingdong_fx_vender_key,
                    options.jingdong_fx_secret_key,
                )
                response_coupons = jd_coupons.sync_fetch(order_id=order_id, start=i * 100, count=100)
                jd_coupons.parse_response(response_coupons)
                if jd_coupons.is_ok():
                    current_coupons = [
                        {
                            "coupon_sn": c.findtext("CouponId"),
                            "coupon_pwd": c.findtext("CouponPwd"),
                            "status": c.findtext("CouponStatus"),
                        }
                        for c in jd_coupons.message.findall("Coupons/Coupon")
                    ]
                    coupons.extend(current_coupons)
                    i += 1
                    if len(current_coupons) != 100:
                        break
                else:
                    logging.error("failed to query jd_coupons for jd_order_id: %s", order_id)
                    break

            self.render("coupon/imported_jd.html", coupons=coupons)
            return
        # 根据券号查
        if coupon:
            jd = Jingdong(
                "queryCouponStatus",
                options.jingdong_fx_vender_id,
                options.jingdong_fx_vender_key,
                options.jingdong_fx_secret_key,
            )
            resp = jd.sync_fetch(coupon=coupon)
            jd.parse_response(resp)
            if jd.is_ok():
                jd_coupons = [
                    {
                        "coupon_sn": c.findtext("CouponId"),
                        "coupon_pwd": c.findtext("CouponPwd"),
                        "status": str(c.findtext("CouponStatus")),
                    }
                    for c in jd.message.findall("Coupons/Coupon")
                ]
                self.render("coupon/imported_jd.html", coupons=jd_coupons)
                return

        self.render("coupon/imported_jd.html", coupons=None)
Esempio n. 3
0
def sync_jd(db):
    goods = db.query(
        'select gds.id,gds.status,gds.distributor_goods_id,'
        'gds.goods_link_id,ds.taobao_api_info,ds.taobao_seller_id '
        'from goods_distributor_shop gds, distributor_shop ds '
        'where gds.distributor_shop_id=ds.id and gds.status <> "PREPARE" and gds.deleted=0 '
        'and gds.distributor_goods_id <> "" and ds.distributor_id=%s',
        options.distributor_id_jingdong)
    now = datetime.now()
    for g in goods:
        api_info = json.loads(g.taobao_api_info, object_hook=json_hook)
        jingdong = Jingdong('queryTeamInfo', str(g.taobao_seller_id),
                            api_info.vender_key, api_info.secret_key)
        response = jingdong.sync_fetch(vender_team_id=g.goods_link_id,
                                       jd_team_id=g.distributor_goods_id)
        jingdong.parse_response(response)
        if not jingdong.is_ok():
            if jingdong.result_code == '-538':
                db.execute(
                    'update goods_distributor_shop set deleted="1", deleted_at=NOW() '
                    'where id=%s', g.id)
                logging.info('sync goods status. delete jingdong item %s',
                             g.distributor_goods_id)
            else:
                logging.error('sync goods status. jingdong request error %s',
                              response)
            continue
        end_time = datetime.fromtimestamp(
            int(jingdong.message.findtext('EndTime')))
        if end_time < now and g.status != 'OFF_SALE':
            db.execute(
                'update goods_distributor_shop set status="OFF_SALE", offsale_at=NOW(), onsale_at=NULL '
                'where id=%s', g.id)
            logging.info('sync goods status. offsale jingdong item %s',
                         g.distributor_goods_id)
        elif end_time > now and g.status != 'ON_SALE':
            db.execute(
                'update goods_distributor_shop set status="ON_SALE", onsale_at=NOW(), offsale_at=NULL '
                'where id=%s', g.id)
            logging.info('sync goods status. onsale jingdong item %s',
                         g.distributor_goods_id)
Esempio n. 4
0
    def post(self):
        order_id = self.get_argument('jd_order_id', '')
        coupon = self.get_argument('jd_coupon_no', '')

        # 根据订单号查券
        if order_id:
            coupons = []
            i = 0
            while True:
                jd_coupons = Jingdong('getCouponsList',
                                      options.jingdong_fx_vender_id,
                                      options.jingdong_fx_vender_key,
                                      options.jingdong_fx_secret_key)
                response_coupons = jd_coupons.sync_fetch(order_id=order_id,
                                                         start=i * 100,
                                                         count=100)
                jd_coupons.parse_response(response_coupons)
                if jd_coupons.is_ok():
                    current_coupons = [{
                        'coupon_sn': c.findtext('CouponId'),
                        'coupon_pwd': c.findtext('CouponPwd'),
                        'status': c.findtext('CouponStatus')
                    } for c in jd_coupons.message.findall('Coupons/Coupon')]
                    coupons.extend(current_coupons)
                    i += 1
                    if len(current_coupons) != 100:
                        break
                else:
                    logging.error(
                        'failed to query jd_coupons for jd_order_id: %s',
                        order_id)
                    break

            self.render('coupon/imported_jd.html', coupons=coupons)
            return
        # 根据券号查
        if coupon:
            jd = Jingdong('queryCouponStatus', options.jingdong_fx_vender_id,
                          options.jingdong_fx_vender_key,
                          options.jingdong_fx_secret_key)
            resp = jd.sync_fetch(coupon=coupon)
            jd.parse_response(resp)
            if jd.is_ok():
                jd_coupons = [{
                    'coupon_sn': c.findtext('CouponId'),
                    'coupon_pwd': c.findtext('CouponPwd'),
                    'status': str(c.findtext('CouponStatus'))
                } for c in jd.message.findall('Coupons/Coupon')]
                self.render('coupon/imported_jd.html', coupons=jd_coupons)
                return

        self.render('coupon/imported_jd.html', coupons=None)
Esempio n. 5
0
def partner_api_verify(db, coupon_sn):
    # 查找券信息
    coupon = db.get(
        'select i.*, c.*, c.id cid from item i, item_coupon c where c.sn=%s and i.id=c.item_id',
        coupon_sn)
    coupon_sn = coupon_sn.encode('utf-8')
    logging.info('被验证券sn:%s', coupon.sn)
    if not coupon:
        raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='券号不存在'))

    distributor_order = db.get(
        'select do.* from distributor_order do, orders o where do.id=o.distributor_order_id '
        'and o.id=%s', coupon.order_id)
    if coupon.distr_id == options.distributor_id_taobao:
        # 淘宝验证
        distributor_shop = db.get('select * from distributor_shop where id=%s',
                                  distributor_order.distributor_shop_id)

        order_message = json.loads(distributor_order.message,
                                   object_hook=json_hook)
        app_info = json.loads(distributor_shop.taobao_api_info,
                              object_hook=json_hook)

        taobao = Taobao('taobao.vmarket.eticket.consume')
        taobao.set_app_info(app_info.app_key, app_info.app_secret_key)
        if app_info.app_key == options.taobao_kunran_app_key:
            taobao.add_fields(codemerchant_id=options.taobao_kunran_id,
                              posid=options.taobao_coupon_posid)
            taobao.set_session(app_info.merchant_session)
        else:
            taobao.set_session(app_info.session)

        response = yield taobao(order_id=distributor_order.order_no,
                                verify_code=coupon_sn,
                                token=order_message.token,
                                consume_num=1)
        logging.info('taobao coupon verify. coupon_sn:%s, result:%s',
                     coupon_sn, response.body)

        taobao.parse_response(response.body)
        if taobao.is_ok():
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn,
                                      msg='验证成功'))
        else:
            raise gen.Return(
                PropDict(ok=False,
                         coupon_sn=coupon_sn,
                         msg=taobao.error.msg.encode('utf-8')))

    elif coupon.distr_id == options.distributor_id_jingdong:
        # 京东验证
        shop = db.get(
            'select taobao_seller_id, taobao_api_info from distributor_shop where id=%s',
            coupon.distr_shop_id)
        api_info = json.loads(shop.taobao_api_info, object_hook=json_hook)

        jingdong = Jingdong('verifyCode', str(shop.taobao_seller_id),
                            api_info.vender_key, api_info.secret_key)
        response = yield jingdong(jd_order_id=distributor_order.order_no,
                                  coupon_id=coupon.distr_sn,
                                  coupon_pwd=coupon.distr_pwd)
        logging.info('jingdong coupon verify. coupon_sn:%s, result:%s',
                     coupon_sn, response.body)

        jingdong.parse_response(response.body)
        if jingdong.is_ok():
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn,
                                      msg='验证成功'))
        else:
            raise gen.Return(
                PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败'))

    elif coupon.distr_shop_id == options.shop_id_wuba:
        # 58验证
        wuba = Wuba('order.ticketcheck')
        # todo 弥补之前的错误
        if 211796 < coupon.cid < 219863:
            oid = coupon.order_id
        else:
            oid = coupon.order_no
        response = yield wuba(ticketId=coupon.cid,
                              orderId=oid,
                              ticketIdIndex=0)
        logging.info('wuba coupon verify. coupon_sn:%s, result:%s', coupon_sn,
                     response.body)

        wuba.parse_response(response.body)
        if wuba.is_ok() and wuba.message.data.result == 1:
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn,
                                      msg='验证成功'))
        else:
            raise gen.Return(
                PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败'))
    else:
        logging.info('partner api verify skip. coupon_sn:%s', coupon_sn)
        raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='无需第三方验证'))
Esempio n. 6
0
def partner_api_verify(db, coupon_sn):
    # 查找券信息
    coupon = db.get('select i.*, c.*, c.id cid from item i, item_coupon c where c.sn=%s and i.id=c.item_id', coupon_sn)
    coupon_sn = coupon_sn.encode('utf-8')
    logging.info('被验证券sn:%s', coupon.sn)
    if not coupon:
        raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='券号不存在'))

    distributor_order = db.get('select do.* from distributor_order do, orders o where do.id=o.distributor_order_id '
                               'and o.id=%s', coupon.order_id)
    if coupon.distr_id == options.distributor_id_taobao:
        # 淘宝验证
        distributor_shop = db.get('select * from distributor_shop where id=%s', distributor_order.distributor_shop_id)

        order_message = json.loads(distributor_order.message, object_hook=json_hook)
        app_info = json.loads(distributor_shop.taobao_api_info, object_hook=json_hook)

        taobao = Taobao('taobao.vmarket.eticket.consume')
        taobao.set_app_info(app_info.app_key, app_info.app_secret_key)
        if app_info.app_key == options.taobao_kunran_app_key:
            taobao.add_fields(codemerchant_id=options.taobao_kunran_id, posid=options.taobao_coupon_posid)
            taobao.set_session(app_info.merchant_session)
        else:
            taobao.set_session(app_info.session)

        response = yield taobao(order_id=distributor_order.order_no, verify_code=coupon_sn, token=order_message.token,
                                consume_num=1)
        logging.info('taobao coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body)

        taobao.parse_response(response.body)
        if taobao.is_ok():
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功'))
        else:
            raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg=taobao.error.msg.encode('utf-8')))

    elif coupon.distr_id == options.distributor_id_jingdong:
        # 京东验证
        shop = db.get('select taobao_seller_id, taobao_api_info from distributor_shop where id=%s',
                      coupon.distr_shop_id)
        api_info = json.loads(shop.taobao_api_info, object_hook=json_hook)

        jingdong = Jingdong('verifyCode', str(shop.taobao_seller_id), api_info.vender_key, api_info.secret_key)
        response = yield jingdong(jd_order_id=distributor_order.order_no, coupon_id=coupon.distr_sn,
                                  coupon_pwd=coupon.distr_pwd)
        logging.info('jingdong coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body)

        jingdong.parse_response(response.body)
        if jingdong.is_ok():
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功'))
        else:
            raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败'))

    elif coupon.distr_shop_id == options.shop_id_wuba:
        # 58验证
        wuba = Wuba('order.ticketcheck')
        # todo 弥补之前的错误
        if 211796 < coupon.cid < 219863:
            oid = coupon.order_id
        else:
            oid = coupon.order_no
        response = yield wuba(ticketId=coupon.cid, orderId=oid, ticketIdIndex=0)
        logging.info('wuba coupon verify. coupon_sn:%s, result:%s', coupon_sn, response.body)

        wuba.parse_response(response.body)
        if wuba.is_ok() and wuba.message.data.result == 1:
            raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='验证成功'))
        else:
            raise gen.Return(PropDict(ok=False, coupon_sn=coupon_sn, msg='验证失败'))
    else:
        logging.info('partner api verify skip. coupon_sn:%s', coupon_sn)
        raise gen.Return(PropDict(ok=True, coupon_sn=coupon_sn, msg='无需第三方验证'))