Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for Python #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added server/python/.gitignore
Empty file.
14 changes: 14 additions & 0 deletions server/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Checkout payment processing with Checkout Pro

## Using a Python server with Flask

### Requirements
- Python 3.7 or higher
- Read our [testing instructions](https://www.mercadopago.com/developers/en/guides/online-payments/checkout-pro/test-integration)
- Setup your credentials:
- Private Access Token on server-side [`server.js`](https://github.com/mercadopago/checkout-payment-sample/tree/master/server/python/server.py)

### How to run it
- pip install -r requirements.txt
- python server.py
- Navigate to http://localhost:8080 on your browser
2 changes: 2 additions & 0 deletions server/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Flask==2.0.1
mercadopago==2.0.7
53 changes: 53 additions & 0 deletions server/python/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import mercadopago as mp
from flask import Flask, render_template, request

app = Flask(
__name__,
static_folder='../../client',
template_folder='../../client',
static_url_path='',
)
# REPLACE WITH YOUR ACCESS TOKEN AVAILABLE IN: https://developers.mercadopago.com/panel/credentials
mercadopago = mp.SDK("YOUR_ACCESS_TOKEN")


@app.route("/create_preference", methods=['POST'])
def create_preference():
payload = request.get_json()
preference = {
'items': [{
'title': payload.get('title'),
'unit_price': float(payload.get('price')),
'quantity': float(payload.get('quantity'))
}],
'back_urls': {
"success": "localhost:8080/feedback",
"failure": "localhost:8080/feedback",
"pending": "localhost:8080/feedback"
},
'auto_return': 'approved'
}
try:
response = mercadopago.preference().create(preference)
except Exception as error:
print(error)
return {'id': response['response']['id']}


@app.route("/feedback")
def feedback():
params = request.args
return {
'Payment': params.get('payment_id'),
'Status': params.get('status'),
'MechantOrder': params.get('merchant_order_id'),
}, 200


@app.route("/")
def home():
return render_template('index.html')


if "__main__" == __name__:
app.run(host="localhost", port=8080, debug=True)