Ejemplo n.º 1
0
def cancel_payment(request):
    # Verifico ApiKey
    cap = __check_apikey(request)
    if cap['status'] == 'error':
        return HttpResponse(status=http_UNAUTHORIZED)

    # Cargo el json
    try:
        data = json.loads(request.body)
    except Exception:
        message = "error decoding json"
        body = {'status': 'error', 'message': message}
        return HttpResponse(json.dumps(body),
                            content_type="application/json",
                            status=http_BAD_REQUEST)

    # Verifico las key mandatorios del json
    keys = ['user_id']
    json_loader = __validate_json(data, keys)
    if json_loader['status'] == 'error':
        return HttpResponse(json.dumps(json_loader),
                            content_type="application/json",
                            status=http_BAD_REQUEST)

    # Verifico que el usuario exista
    try:
        user = User.objects.get(user_id=data['user_id'])
    except ObjectDoesNotExist:
        message = "user_id %s does not exist" % data['user_id']
        body = {'status': 'error', 'message': message}
        return HttpResponse(json.dumps(body),
                            content_type="application/json",
                            status=http_BAD_REQUEST)

    # Obtengo el UserPayment activo y si no existe devulvo error
    up = UserPayment.get_active(user=user)
    if up is None:
        up = UserPayment.get_rebill_error(user=user)
        if up is None:
            message = "user_id %s has not enabled recurring payment" % data[
                'user_id']
            body = {'status': 'error', 'message': message}
            return HttpResponse(json.dumps(body),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

    # Si el integrador es CommerceGate ejecuto baja para ese integrador
    # ph = PaymentHistory.objects.filter(user_payment=up).order_by('-id')[0]
    #if ph.integrator.name == 'commerce_gate':
    if not up.internal:
        return cancel_commercegate(request)

    # Cancelo la recurrencia
    try:
        up.user_cancel()
    except:
        message = "could not disable recurring payment"
        body = {'status': 'error', 'message': message}
        return HttpResponse(json.dumps(body),
                            content_type="application/json",
                            status=http_INTERNAL_ERROR)

    # POST to promiscuus
    resp_promiscuus = post_to_promiscuus(up, 'cancel')
    if resp_promiscuus['status'] == 'error':
        up.message = "%s - Promiscuus error: %s" % (up.message,
                                                    resp_promiscuus['message'])
        up.save()

    return HttpResponse(status=http_POST_OK)
Ejemplo n.º 2
0
def payment_discount(request):
    # Verifico ApiKey
    cap = __check_apikey(request)
    dap = __check_discount_apikey(request)
    if cap['status'] == 'error' and dap['status'] == 'error':
        return HttpResponse(status=http_UNAUTHORIZED)

    if request.method == 'POST':
        # Cargo el json
        try:
            data = json.loads(request.body)
        except Exception:
            message = "error decoding json"
            body = {'status': 'error', 'message': message}
            return HttpResponse(json.dumps(body),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

        # Verifico las key mandatorios del json
        keys = ['user_id', 'discount', 'disc_counter']
        json_loader = __validate_json(data, keys)
        if json_loader['status'] == 'error':
            return HttpResponse(json.dumps(json_loader),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

        user_id = data['user_id']
        discount = data['discount']
        disc_counter = data['disc_counter']
        status = http_POST_OK

    else:
        try:
            user_id = request.GET['user_id']
            discount = int(request.GET['discount'])
            disc_counter = int(request.GET['disc_counter'])
            status = http_REQUEST_OK
        except:
            message = "missing parameter"
            body = {'status': 'error', 'message': message}
            return HttpResponse(json.dumps(body),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

        if discount < 1 or discount > 100 or disc_counter < 1:
            message = "invalid parameter value"
            body = {'status': 'error', 'message': message}
            return HttpResponse(json.dumps(body),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

    # Verifico que el usuario exista
    try:
        user = User.objects.get(user_id=user_id)
    except ObjectDoesNotExist:
        message = "user_id %s does not exist" % user_id
        body = {'status': 'error', 'message': message}
        return HttpResponse(json.dumps(body),
                            content_type="application/json",
                            status=http_BAD_REQUEST)

    # Obtengo el UserPayment y si no existe devulvo error
    up = UserPayment.get_active(user=user)
    if up is None:
        up = UserPayment.get_rebill_error(user=user)
        if up is None:
            message = "user_id %s has not enabled recurring payment" % user_id
            body = {'status': 'error', 'message': message}
            return HttpResponse(json.dumps(body),
                                content_type="application/json",
                                status=http_BAD_REQUEST)

    # Aplico el descuento
    try:
        up.discount(discount, disc_counter)
    except:
        message = "could not apply discount"
        body = {'status': 'error', 'message': message}
        return HttpResponse(json.dumps(body),
                            content_type="application/json",
                            status=http_INTERNAL_ERROR)

    return HttpResponse(status=status)