From 0072c5e1d6c009a1e1e1887e2f5c37d7909d832a Mon Sep 17 00:00:00 2001 From: Joaquin de Francesca Date: Thu, 26 Aug 2021 16:53:21 -0300 Subject: [PATCH] Example for Python --- server/python/.gitignore | 0 server/python/README.md | 14 +++++++++ server/python/requirements.txt | 2 ++ server/python/server.py | 53 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 server/python/.gitignore create mode 100644 server/python/README.md create mode 100644 server/python/requirements.txt create mode 100644 server/python/server.py diff --git a/server/python/.gitignore b/server/python/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/server/python/README.md b/server/python/README.md new file mode 100644 index 0000000..2400e18 --- /dev/null +++ b/server/python/README.md @@ -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 diff --git a/server/python/requirements.txt b/server/python/requirements.txt new file mode 100644 index 0000000..93c3b3d --- /dev/null +++ b/server/python/requirements.txt @@ -0,0 +1,2 @@ +Flask==2.0.1 +mercadopago==2.0.7 \ No newline at end of file diff --git a/server/python/server.py b/server/python/server.py new file mode 100644 index 0000000..2d25b8e --- /dev/null +++ b/server/python/server.py @@ -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)