preApproval data is required

3

Currently, I am trying to integrate a prepayment via v2 from api to 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 did not seem to be missing anything. Can anyone shed any 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 accepted headers, but I have a bad request headers accepted.

    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>
    
asked by anonymous 17.02.2017 / 02:12

1 answer

3

According to the documentation Transparent Recurring Payment you should use the following headers :

  • Content-Type application/json;charset=ISO-8859-1 to send data in JSON format. In the way that you are reporting ( application/x-www-form-urlencoded; charset=ISO-8859-1 ) the data should be sent in URL ;
  • Accept application/vnd.pagseguro.com.br.v3+json;charset=ISO-8859-1 to send JSON . In your example you are reporting application/vnd.pagseguro.com.br.v3+xml;charset=ISO-8859-1 which is relative to XML ;

Note also that you do not have /v2 in the URL described in the documentation, then for sandbox would be https://ws.sandbox.pagseguro.uol.com.br/pre-approvals/request?email=%s&token=%s‌ .

As for the uploaded data, you are sending more fields than you requested. The sending data to create a prepaid plan with monthly charges of $ 100.00 over 2 years should follow the following template:

{
  "preApproval": {
    "name": "Assinatura da Revista Fictícia",
    "charge": "AUTO",
    "period": "MONTHLY",
    "amountPerPayment": 100.00,
    "expiration": {
      "value": 2,
      "unit": "YEARS"
    }
  },
  "receiver": {
    "email": "[email protected]"
  }
}
    
27.02.2017 / 15:53