3

i am trying to make this request for a checkout id in a payment process and this is my code so far. the code below works fine and i get a response from it, but as soon as I add some of the commented parts to the data dictionary it gives me an attribute error saying that int has no object get.

basically, i seem to only be able to add keys and string or number values to my data dict, but whenever i add a dict as a value it starts giving me the error.

i have no idea why this is happening, can anyone help? thanks in advance!

def request_checkout_id(request):

    user = request.user

    url = "https://xxxxxx"
    entity_id = 'xxxxx'
    auth_header = 'xxxxx'
    currency = 'USD'

    try:
        ref_code = request.session['ref_code']
        order = Order.objects.get(ref_code=ref_code)    
    except:
        ref_code = None
        return redirect(reverse('shop:cart'))

    amount = float(order.final_total)

    base_con_tarifa = order.cart.total
    valor_iva = order.cart.tax_total
    #total_con_iva = order.sub_total
    base_sin_tarifa = order.shipping_price
    custom_param = '0081004012'+ str(valor_iva).replace(".", "").zfill(12) + '052012' + str(base_sin_tarifa).replace(".", "").zfill(12) + '003007010391005100817913101053012' + str(base_con_tarifa).replace(".", "").zfill(12)
    print(custom_param)

    data = {}
    # cart = {}
    # items = []
    # i = 0
    # for item in order.cart.cartitem_set.all():
    #     items.append({})
    #     items[i]['name'] = item.product.name
    #     items[i]['description'] = item.product.description
    #     items[i]['price'] = item.product.price
    #     items[i]['quantity'] = str(item.quantity)
    #     i += 1
    # cart['items'] = items
    #customer = {}
    #customer['givenName'] = user.first_name
    #customer['surname'] = user.last_name
    # customer['merchantCustomerId'] = str(user.id)
    # #customer['phone'] = order.shipping_address.phone
    # customer['phone'] = '0987543493'
    # customer['identificationDocType'] = 'IDCARD'
    # customer['identificationDocId'] = '0926685432'
    #shipping = {}
    # shipping['street1'] = order.shipping_address.street_address
    # shipping['country'] = order.shipping_address.country
    #shipping['street1'] = 'aosidaoisd'
    #shipping['country'] = 'EC'
    #billing = {}
    # billing['street1'] = order.billing_address.street_address
    # billing['country'] = order.billing_address.country
    #billing['street1'] = 'aosidaoisASd'
    #billing['country'] = 'EC'
    data['entityId'] = entity_id
    data['amount'] = amount
    data['currency'] = currency
    data['paymentType'] = 'DB'
    data['testMode'] = 'EXTERNAL'
    data['merchantTransactionId'] = 'as09aSAS097'
    #data['customer'] = customer
    #data['cart'] = cart
    #data['shipping'] = shipping
    #data['billing'] = billing
    print(data)

    try:
        opener = urllib.request.build_opener(urllib.request.HTTPHandler)
        request_id = urllib.request.Request(url, data=urllib.parse.urlencode(data).encode('utf-8'))
        request_id.add_header('Authorization', auth_header)
        request_id.get_method = lambda: 'POST'
        response = opener.open(request_id)
        parsed_response = json.loads(response.read())
        checkout_id = parsed_response['id']
        print(checkout_id)
    except urllib.error.HTTPError as e:
        print(e.code)
        return e.code

    if checkout_id:
        payment = Pago()
        payment.order = order
        payment.ref_code = order.ref_code
        payment.user = request.user
        payment.checkout_id = checkout_id
        payment.save()
        return redirect(reverse('shop:checkout'))
    #else:
    #raise some error or alert

this is my error traceback:

Internal Server Error: /shop/checkout/request_checkout_id
Traceback (most recent call last):
  File "C:\Users\Roberto\radlab\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Roberto\radlab\lib\site-packages\django\utils\deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "C:\Users\Roberto\radlab\lib\site-packages\django\middleware\clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'int' object has no attribute 'get'
[31/Aug/2020 17:21:59] "GET /shop/checkout/request_checkout_id HTTP/1.1" 500 64423
4
  • 1
    add your complete error traceback Commented Aug 31, 2020 at 17:58
  • i edited the post so you can see the error traceback @ArakkalAbu Commented Aug 31, 2020 at 18:06
  • 1
    you are returning return e.code at some point from the view. I think that causes the error. You have to return a HTTP Response from a Django view. Commented Aug 31, 2020 at 18:09
  • the code as posted works, its only when i start adding the commented parameters that throws the exception. it only returns e.code if there is an exception before it gets to that. how would i implement the httpresponse? Commented Aug 31, 2020 at 18:17

3 Answers 3

1

To anyone reading this in the future, a simpler instance on where this error happens, and my solution.

# Python program for reading from file 

counts = dict() *# line that fixed it for me*

fname = input("Enter file name: ")

with open(fname) as fh:
    myfile = fh.read()
    words = list(myfile.split())

    for word in words:
        counts[word] = counts.get(word,0) + 1

print('counts', counts)

As it turns out, if you don't define the counts as a dict() type it will throw the same AttributeError: 'int' object has no attribute 'get' error. I am very new to Python so if this solution is too basic, its intent is towards someone who is also learning.

Sign up to request clarification or add additional context in comments.

Comments

0

As is pointed out in the comments, the problem is you're returning the status code as an int (HTTPError.code) in the HTTPError case, and Django needs it to be an HTTPResponse.

The simplest way to handle this would be to replace this logic:

except urllib.error.HTTPError as e:
    print(e.code)
    return e.code

with this:

except urllib.error.HTTPError as e:
    print(e.code)
    return HttpResponse(status=e.code)

Depending on your application, you may need more in your HTTPResponse, though. You can read more in the Django documentation for returning errors. There are also subclasses of HTTPResponse for specific status codes, so if you know that the error code is 500, you could return HttpResponseServerError.

Comments

0

Already solved it. I was just passing the data in the wrong format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.