3
Currently, I am trying to integrate a prepayment via v2 of the api for pagseguro. For some reason I keep getting pre-approval data is required. I tried chaning the headers, I was also checking the example in the documentation and it doesn’t seem to be missing anything. Can someone shed some light on what I might be missing?
import requests
data = request.form
pagseguro_url = app_config.PAG_SEGURO['pageseguroUrl']
email = app_config.PAG_SEGURO['email']
token = app_config.PAG_SEGURO['token']
currency = app_config.PAG_SEGURO['currency']
item_id = data['itemId']
item_description = app_config.ITEMS[item_id]['itemDescription']
item_amount = app_config.ITEMS[item_id]['itemAmount']
item_quantity = app_config.ITEMS[item_id]['itemQuantity']
reference = data['reference']
sender_email = data['senderEmail']
redirect_url = app_config.PAG_SEGURO['redirectUrl']
if app_config.ITEMS[item_id]['preApproval']:
d = datetime.utcnow()
date = "%s-%02d-%02d" % (d.year, d.month, d.day)
headers = {'Content-Type':'application/json;charset=ISO-8859-1', 'Accept':'application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1'}
r = requests.post("https://ws.sandbox.pagseguro.uol.com.br/v2/pre-approvals/request?email=%s&token=%s" % (email, token), headers=headers, data={
'redirectUrl': redirect_url,
'reference': reference,
"preApproval": {
"name": item_description,
"charge": "AUTO",
"period": "MONTHLY",
"amountPerPayment": item_amount,
"expiration": {
"value": 24,
"unit": "MONTHS"
},
"details": "string",
"maxAmountPerPeriod": item_amount,
"maxAmountPerPayment": item_amount,
"maxTotalAmount": 360,
"maxPaymentsPerPeriod": 1,
# "initialDate": date,
"finalDate": date,
"cancelURL": "https://dev.domain.com/settings/premium"
},
"maxUses": 1000,
"receiver": {
'email': email
}
})
But when I run it I get this error.
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?><errors><error><code>11101</code><message>preApproval data is required.</message></error></errors>
Also I tried to change the encoding and accept headers, but I have a bad accepted request headers.
headers = {'Content-Type':'application/json;charset=ISO-8859-1', 'Accept':'application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1'}
r = requests.post("https://ws.sandbox.pagseguro.uol.com.br/v2/pre-approvals/request?email=%s&token=%s"
% (email, token), headers=headers, data={
'redirectUrl': redirect_url,
'reference': reference,
"preApproval": {
"name": "stuff",
"charge": "AUTO",
"period": "MONTHLY",
"amountPerPayment": item_amount,
"expiration": {
"value": 2,
"unit": "YEARS"
}
},
"receiver": {
"email": '[email protected]'
}
})
But this error code 415
returns
</head><body><h1>HTTP Status 415 - Cannot consume content type</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Cannot consume content type</u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Cannot consume content type).</u></p><HR size="1" noshade="noshade"><h3>JBossWeb/2.0.1.GA</h3></body></html>
I tried this, but simply Instead returns to error code 415. I updated the issue of showing what I tried
– nadermx
just update, please check
– nadermx
Which returns the same error Past
– nadermx
and I did, this updated below the original
– nadermx
Let’s go continue this discussion in chat.
– nadermx