Introducing WebPay from Flask

About webpay-python

On December 02, WebPay's Python package [webpay-python] 1 was released. webpay-python is a modern Python package with the following points:

--Supports multiple versions of Python (Python 2.6-3.3) by [tox] 2 -Data-driven test by [pytest] 3 -Supports PEP8 by [flake8] 4 --API access using [requests] 5

This package is important because many packages are not yet compatible with Python3. You can deploy WebPay with ** Python 3.3 ** without having to modify the package yourself.

Introducing WebPay from Flask

I love [Flask] 6 among Python web frameworks, so this time I will introduce WebPay to Flask. The environment used is MacOS 10.9.1 / Python3.3 / Flask 0.10.1.

About operation

Let's implement it while following [what @hmsk] 8.

  1. Enter the purchase information from ‘/’ and post to ‘/ purhcase’
  2. Billing processing and result output using WebPay with ‘/ purchase’

About file structure

Controller is placed under app.py and View is placed under templates. There is no Model this time.

webpay-flask-sample
├── app.py
└── templates
    ├── index.html
    ├── layout.html
    └── purchased.html

Actual code and execution

If you write the Controller roughly, it will look like the following. “\ # Noqa” is attached because it is through flake8.

app.py


import sys
from flask import Flask, render_template, request, url_for, redirect
from webpay import WebPay
from webpay.errors import InvalidRequestError

app = Flask(__name__)
app.config.from_object(__name__)
WEBPAY_PUB_KEY = 'test_public_19DdUs78k2lV8PO8ZCaYX3JT'
WEBPAY_PRI_KEY = 'test_secret_eHn4TTgsGguBcW764a2KA8Yd'
WEBDB_PRICE = 1554


@app.route('/')
def index():
    return render_template('index.html', webpay_pubkey=WEBPAY_PUB_KEY)


@app.route('/purchase', methods=['POST'])
def purchase():
    webpay = WebPay(WEBPAY_PRI_KEY)
    try:
        charge = webpay.charges.create(
            amount=WEBDB_PRICE,
            currency='jpy',
            card=request.form.get('webpay-token'))
        return render_template('purchased.html', charge=charge)
    except InvalidRequestError:
        e = sys.exc_info()[1]  # noqa
        return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(debug=True)

Next is View. Since [jinja2] 7 is available by default in flask, it is possible to inherit View. Create index.html and purchased.html based on layout.html.

layout.html


<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>WebPay flask sample</title>
  </head>
<body>
  {% block body %}{% endblock %}
</body>
</html>

index.html


{% extends "layout.html" %}
{% block body %}

<form action="/purchase" method="post">
  <script src="https://checkout.webpay.jp/v1/" class="webpay-button" data-key="{{ webpay_pubkey }}"></script>
</form>
{% endblock %}

purchased.html


{% extends "layout.html" %}
{% block body %}
<h3>Imagine purchased</h3>
{% if charge.id %}
<p>When making inquiries regarding shipping{{ charge.id }}Please let us know.</p>
{% endif %}
<ul>
  <li><a href='http://www.amazon.co.jp/dp/4774158747/' target='_blank'>Actually buy</a></li>
  <li><a href='/'>Buy again</a></li>
</ul>
{% endblock %}

After that, if you access [http: // localhost: 5000](http: // localhost: 8000) from the following command, you will find that you can process billing.

cd webpay-flask-sample
python app.py

About the JS part

CheckoutHelper is used for the JS part. See below for this.

-[Introduce WebPay with a little code] 8 -[Mastering WebPay's Checkout Helper] 9

At the end

This time I introduced webpay-python to flask. The code created this time is in [here] 10, so you can easily use webpay-python. webpay-python has just been released, but it is a package for WebPay that can also be used with Python 3.3. Please use it by all means. (Requests for webpay-python up to [Issues] 11!)

Recommended Posts

Introducing WebPay from Flask
(For myself) Flask_7 (Open database from Flask)
flask
flask
Connect to Docker's MySQL container from Flask
Get data from Cloudant with Bluemix flask
Flask tutorial (from installation to hello world)
Introducing Docker Engine to Linux From Scratch
Automatically map controllers from URLs in Flask