Ejemplo n.º 1
0
 def get():
     token = request.GET.get('token')
     openid = pay_util.confirm_validate_token(token)
     store_id_timeout = 60 * 60 * 24
     cache.set(openid, int(sid), timeout=store_id_timeout)
     user = models.User.objects.get(openid=openid)
     maps, created = models.UserStoreMap.objects.get_or_create(
         store_id=int(sid), user_id=user.id)
     if created:
         maps.residual_amount = 0
     maps.save()
Ejemplo n.º 2
0
 def post():
     barcode = params.get('barcode')
     token = params.get('token')
     openid = pay_util.confirm_validate_token(token)
     sid = cache.get(openid)
     user = models.User.objects.get(openid=openid)
     item = models.Item.objects.get(barcode=barcode, store_id=sid)
     cart = models.Cart.objects.get_or_create(user=user, store_id=sid)[0]
     cart.save()
     cart_item = models.CartItem.objects.create(item=item, num=1, cart=cart)
     cart_item.save()
     return {'data': model_to_dict(item)}
Ejemplo n.º 3
0
 def get():
     token = request.GET.get('token')
     openid = pay_util.confirm_validate_token(token)
     sid = cache.get(openid)
     result = {}
     store = models.Store.objects.get(id=sid)
     cart = models.Cart.objects.get(user__openid=openid, store=store)
     cart_items = cart.get_cart_items()
     amount = 0
     result, amount = pay_util.bundling(cart_items, store)
     # for cart_item in cart_items:
     #     cart_item['price'] = float('%.2f' % cart_item['price'])
     #     cart_item['sum'] = float(cart_item.get('price')) * \
     #                                 float(cart_item.get('num'))
     #     amount += cart_item['sum']
     return {'items': cart_items, 'amount': amount}
Ejemplo n.º 4
0
 def post():
     params = QueryDict(request.body)
     phone = params.get('phone')
     code = params.get('code')
     token = params.get('token')
     openid = pay_util.confirm_validate_token(token)
     if int(cache.get(str(phone))) == int(code):
         try:
             store = models.Store.objects.get(boss__phone=phone)
             return {'data': store.boss_id}
         except:
             user = models.User.objects.get_or_create(openid=openid)[0]
             user.phone = phone
             user.save()
             return {'data': 'ok'}
     else:
         return JsonResponse({'code': 1, 'data': 'error'})
Ejemplo n.º 5
0
 def get():
     token = request.GET.get('token')
     openid = pay_util.confirm_validate_token(token)
     user = models.User.objects.get(openid=openid)
     year = timezone.now().timetuple().tm_year
     month = timezone.now().timetuple().tm_mon
     logs = models.RechargeLog.objects.filter(user_id=user.id,
                                              time__year=year,
                                              time__month=month).count()
     if logs > 0:
         raise Exception("done")
     us_map = models.UserStoreMap.objects.get(user_id=user.id,
                                              store_id=int(sid))
     us_map.residual_amount += float(num)
     us_map.save()
     log = models.RechargeLog.objects.create(user=user,
                                             integral=float(num),
                                             time=timezone.now())
     log.save()
Ejemplo n.º 6
0
 def get():
     token = request.GET.get('token')
     openid = pay_util.confirm_validate_token(token)
     user = models.User.objects.get(openid=openid)
     return pay_util.get_integrals(user)
Ejemplo n.º 7
0
 def get():
     token = request.GET.get('token')
     openid = pay_util.confirm_validate_token(token)
     user = models.User.objects.get(openid=openid)
     print user
     return pay_util.get_order_history(user)
Ejemplo n.º 8
0
    def post():
        if request.META.has_key('HTTP_X_FORWARDED_FOR'):
            ip = request.META.get('HTTP_X_FORWARDED_FOR')
        else:
            ip = request.META.get('REMOTE_ADDR')
        if ip in getattr(settings, 'BLOCKED_IPS', []):
            return HttpResponseForbidden('<h1>Forbbidden</h1>')
        token = params.get('token')
        order_id = params.get('order_id')
        openid = pay_util.confirm_validate_token(token)
        sid = int(cache.get(openid))
        store = models.Store.objects.get(id=sid)
        if hasattr(store, 'cvsconfig'):
            config = store.cvsconfig
        else:
            raise Exception('No config')
        user = models.User.objects.get(openid=openid)
        helper = CartHelper(openid)
        cart_items = helper.get()
        amount = cart_items.get('amount')
        randuuid = uuid.uuid4()
        nonce_str = str(randuuid).replace('-', '')
        out_trade_no = pay_util.create_mch_billno(str(order_id))
        cache.set(out_trade_no, order_id, timeout=24 * 60 * 60)
        url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'
        data = {}
        data['body'] = u'超便利'
        data['mch_id'] = config.mch_id
        data['nonce_str'] = nonce_str
        # data['device_info'] = 'WEB'
        data['total_fee'] = str(int(amount * 100))
        data['spbill_create_ip'] = ip
        # data['fee_type'] = 'CNY'
        data['openid'] = openid
        data['out_trade_no'] = out_trade_no
        data['notify_url'] = 'https://%s/api/v1.0/pay/notify' % (
            request.get_host())
        data['appid'] = config.appid
        data['trade_type'] = 'JSAPI'
        data['sign'] = pay_util.sign(data, config.pay_api_key)
        template = """
                    <xml>
                    <appid>{appid}</appid>
                    <body>{body}</body>
                    <mch_id>{mch_id}</mch_id>
                    <nonce_str>{nonce_str}</nonce_str>
                    <notify_url>{notify_url}</notify_url>
                    <openid>{openid}</openid>
                    <out_trade_no>{out_trade_no}</out_trade_no>
                    <spbill_create_ip>{spbill_create_ip}</spbill_create_ip>
                    <total_fee>{total_fee}</total_fee>
                    <trade_type>{trade_type}</trade_type>
                    <sign>{sign}</sign>
                    </xml>
                """
        content = template.format(**data)
        headers = {'Content-Type': 'application/xml'}
        raw = requests.post(url, data=content, headers=headers)
        rdict = pay_util.xml_response_to_dict(raw)

        return_data = {}
        if rdict['return_code'] == 'SUCCESS' and rdict[
                'result_code'] == 'SUCCESS':
            randuuid = uuid.uuid4()
            nonce_str = str(randuuid).replace('-', '')
            time_stamp = str(int(time.time()))
            sign_data = {}
            sign_data['appId'] = rdict['appid']
            sign_data['nonceStr'] = nonce_str
            sign_data['package'] = 'prepay_id=%s' % rdict['prepay_id']
            sign_data['signType'] = 'MD5'
            sign_data['timeStamp'] = time_stamp
            paySign = pay_util.sign(sign_data, config.pay_api_key)
            return_data['appId'] = rdict['appid']
            return_data['paySign'] = paySign
            return_data['nonceStr'] = nonce_str
            return_data['timeStamp'] = time_stamp
            return_data['package'] = 'prepay_id=%s' % rdict['prepay_id']
            return_data['signType'] = 'MD5'
            return {'data': return_data}
        else:
            return JsonResponse({'code': 1, 'data': u'支付失败'})
Ejemplo n.º 9
0
 def post():
     token = params.get('token')
     openid = pay_util.confirm_validate_token(token)
     helper = OrderHelper(openid)
     result = helper.order()
     return JsonResponse({'code': 0, 'data': result})
Ejemplo n.º 10
0
 def delete():
     token = params.get('token')
     cart_item_id = params.get('id')
     openid = pay_util.confirm_validate_token(token)
     user = models.User.objects.get(openid=openid)
     models.CartItem.objects.filter(id=cart_item_id).delete()