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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With