preApproval data is required

Asked

Viewed 564 times

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>

1 answer

3


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

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

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

As for the data sent, you are sending more fields than you requested. Shipping data to create a prepaid plan with monthly charges of R $ 100,00 for 2 years must follow the following model:

{
  "preApproval": {
    "name": "Assinatura da Revista Fictícia",
    "charge": "AUTO",
    "period": "MONTHLY",
    "amountPerPayment": 100.00,
    "expiration": {
      "value": 2,
      "unit": "YEARS"
    }
  },
  "receiver": {
    "email": "[email protected]"
  }
}
  • I tried this, but simply Instead returns to error code 415. I updated the issue of showing what I tried

  • just update, please check

  • Which returns the same error Past

  • and I did, this updated below the original

Browser other questions tagged

You are not signed in. Login or sign up in order to post.