Hello everyone. When I run this code I get the following return:
TypeError: list indices must be integers or slices, not str
How can I fix this?
import pagarme
import psycopg2
pagarme.authentication_key("API_KEY")
transaction = pagarme.transaction.find_all()
#print(transaction)
conn = psycopg2.connect("dbname='DB' user='USER' host='HOST'
password='PASSWORD'")
insert = "INSERT INTO TABELA (object,status) VALUES"
gravar=[]
for infos in transaction:
gravar.append((
transaction['object'],
transaction['status']
))
if len(gravar) >0 :
cur = conn.cursor()
y = b','.join(cur.mogrify("(%s,%s)", x) for x in gravar)
comando = insert + y.decode()
try:
cur.execute(comando)
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
cur.close()
print('Carga Completa')
else:
conn.close()
print('Nada a Inserir')
This is the return of JSON:
[
{
"object": "transaction",
"status": "refused",
"refuse_reason": "acquirer",
"status_reason": "acquirer",
"acquirer_response_code": "1011",
"acquirer_name": "pagarme",
"acquirer_id": "5b1eb16fd15fc06256543da4",
"authorization_code": null,
"soft_descriptor": null,
"tid": 59869003,
"nsu": 59869003,
"date_created": "2018-10-30T20:57:01.883Z",
"date_updated": "2018-10-30T20:57:03.655Z",
"amount": 20520,
"authorized_amount": 0,
"paid_amount": 0,
"refunded_amount": 0,
"installments": 1,
"id": 59869003,
"cost": 0,
"card_holder_name": "roberto v raymundo",
"card_last_digits": "5116",
"card_first_digits": "544731",
"card_brand": "mastercard",
"card_pin_mode": null,
"postback_url": null,
"payment_method": "credit_card",
"capture_method": "ecommerce",
"antifraud_score": null,
"boleto_url": null,
"boleto_barcode": null,
"boleto_expiration_date": null,
"referer": "api_key",
"ip": "104.41.14.184",
"subscription_id": null,
"phone": null,
"address": null,
"customer": {
"object": "customer",
"id": 25744747,
"external_id": "3804",
"type": "individual",
"country": "br",
"document_number": null,
"document_type": "cpf",
"name": "Roberto",
"email": "[email protected]",
"phone_numbers": [
"+5511999999999"
],