Ejemplo n.º 1
0
def complete_transaction(request):
    try:
    # print request.body
    # data = json.loads(request.body)['json']
        if request.POST:
            consumer = request.POST.get('user_id')
            useracc = MainUser.objects.get(id=consumer)
            key = request.POST.get('key')
            prototype = Prototype.objects.get(key=key)
            type = request.POST.get('type')
            mode = request.POST.get('mode')
            ## type 1 is merchant and type 2 is personal and mode 2 is self
            if type == 1:
                products = Product.objects.filter(prototype=prototype)
                merchant = prototype.merchant
                total = Decimal(0)
                for p in products:
                    total += p.quantity*p.price
                merchant.balance += total
                merchant.save()
                ts = Transaction()
                ts.prototype = prototype
                ts.customer = useracc
                ts.status = 1
                ts.mode = mode
                ts.save()
                if mode == 2:
                    useracc.balance -= total
                    useracc.save()
            else:
                pt = PersonalTransfer.objects.filter(prototype=prototype)
                receiver = prototype.merchant
                sender = useracc
                total = Decimal(0)
                for p in pt:
                    total += p.amount
                receiver.balance += total
                receiver.save()
                ts = Transaction()
                ts.prototype = prototype
                ts.customer = useracc
                ts.status = 1
                ts.mode = mode
                ts.save()
                if mode == 2:
                    useracc.balance -= total
                    useracc.save()
                gcm_push_message("You've received funds in your paypaid account!","You have got funds!",receiver.gcm)
    except Exception,e:
        print e
        return HttpResponse("error",status=400)