Flask-Mercadopago is a collection of methods for the implementation of Mercado Pago OAuth in Flask.
Python 3.8+
- Flask(>=2.0.1) for build the backend.
- Requests for build the backend.
- mercadopago for build the backend.
You can install via pip:
$> pip install Flask-Mercadopago
Register the extension:
from datetime import datetime
from flask import Flask, jsonify
# To follow the naming rule of Flask extension, although
# this project's name is Flask-Mercadopago, the actual package
# installed is named `flask_mercadopago`.
from flask_mercadopago import Mercadopago
app = Flask(__name__)
app.config["APP_ACCESS_TOKEN"]="APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966"
mercadopago = Mercadopago(app)
@app.route("/")
def index():
card_token_object = {
"card_number": "4074090000000004",
"security_code": "123",
"expiration_year": datetime.now().strftime("%Y"),
"expiration_month": "12",
"cardholder": {
"name": "APRO",
"identification": {"CPF": "19119119100"},
},
}
card_token_created = mercadopago.card_token().create(card_token_object)
payment_data = {
"transaction_amount": 100,
"token": card_token_created["response"]["id"],
"description": "Payment description",
"payment_method_id": 'visa',
"installments": 1,
"payer": {
"email": '[email protected]'
}
}
result = mercadopago.payment().create(payment_data)
payment = result["response"]
return jsonify(payment)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=True)
-
Create a virtual environment:
$> python3 -m venv venv
-
Activate the newly created environment:
On macOS and Linux:
$> source venv/bin/activate
On Windows
c:\> .\env\Scripts\activate
-
Install dependencies:
$> (venv) python -m pip install -r requirements/dev.txt
-
Start the sample app on server locally:
$> (venv) python sample_app/app.py
You will get a form like this:
When the validation, the response data will be rendered with proper style:
- Ferreira, Juan David
Please submit bug reports, suggestions for improvements and patches via the (E-mail: [email protected]).
Credits goes to these peoples:
Flask-Mercadopago
is free software you can redistribute it and/or modify it
under the terms of the MIT License. For more information, you can see the
LICENSE file
for details.