Django Framework - Payment

I. Django Payment

WeChat

Request address

https://pay.weixin.qq.com/static/applyment_guide/applyment_index.shtml

Application conditions

Alipay

Alipay payment process:

  • Sellers issue purchase requests and services generate orders

  • When the order is submitted, the seller will send the request to Alipay.

  • Return payment page

  • The buyer completes the payment on the Payment Page page

  • Payment Processing Request for Payment

  • Return processing result (payment completed, payment failure, payment in progress)

Pay url

https://open.alipay.com/platform/home.htm

Developing, testing and using sandbox environment

Download An Zhuoduan

account information

Interface document address https://docs.open.alipay.com/








Configuration of public key and private key


Receiving money as a seller,

Alipay has written sdk, which can be used in modules.

Use requires installation

pip install pycryptodome    ## Ali sdk's dependency packages
pip install python-alipay-sdk --upgrade

Payment with python

Create files and test demo

from alipay import AliPay

# Public key
alipay_public_key_string = '''-----BEGIN PUBLIC KEY-----
//Public key
-----END PUBLIC KEY-----'''
# Private key
alipay_private_key_string='''-----BEGIN PRIVATE KEY-----
//Private key
-----END PRIVATE KEY-----'''
# Instantiate payment object
alipay = AliPay(
    appid='2016101300673951',
    app_notify_url=None,
    app_private_key_string=alipay_private_key_string,
    alipay_public_key_string=alipay_public_key_string,
    sign_type="RSA2",
)
# Instantiate orders
order_string = alipay.api_alipay_trade_page_pay(
    subject = 'Fresh beef and mutton',    # Transaction theme
    out_trade_no = '10000000009',    # Order number
    total_amount='400000',    # Total transaction amount
    return_url=None,    # An interface for timely callback after payment is requested
    notify_url=None    # Notification address
)
# Send Payment Request
# Request Address: Payment Gateway + Instantiated Order

result = 'https://openapi.alipaydev.com/gateway.do?'+order_string
print(result)

complete

  • When the order is submitted, the seller will send the request to Alipay.
  • Return payment page
  • The buyer completes the payment on the Payment Page page

settings.py

view

from alipay import AliPay
from Qshop.settings import alipay_private_key_string,alipay_public_key_string
def AlipayView(request):
    order_id=request.GET.get('order_id')    # Order ID
    payorder=PayOrder.objects.get(id=order_id)
    # Instantiate payment object
    alipay = AliPay(
        appid='2016101300673951',
        app_notify_url=None,
        app_private_key_string=alipay_private_key_string,
        alipay_public_key_string=alipay_public_key_string,
        sign_type="RSA2",
    )
    # Instantiate orders
    order_string = alipay.api_alipay_trade_page_pay(
        subject='Fresh every day',  # Transaction theme
        out_trade_no=payorder.order_number,  # Order number
        total_amount=str(payorder.order_total),  # Total transaction amount
        return_url=None,  # An interface for timely callback after payment is requested
        notify_url=None  # Notification address
    )
    # Send Payment Request
    # Request Address: Payment Gateway + Instantiated Order
    result = 'https://openapi.alipaydev.com/gateway.do?' + order_string
    return HttpResponseRedirect(result)

Route

Template

After payment submission, jump to the results page

Return processing results

view

def payresult(request):
    order_number = request.GET.get('out_trade_no')
    payorder = PayOrder.objects.get(order_number=order_number)
    payorder.order_status=1
    payorder.save()
    return render(request,'buyer/payresult.html',locals())

Route

Template

{% extends 'buyer/base.html' %}
{% block title %}
  Payment result
{% endblock %}
{% block content %}
  <div class="list_model">
    <h1>Payment result</h1>
    <table>
    {% for key,value in request.GET.items %}
      <tr>
        <th>{{ key }}:</th>
        <td>{{ value }}</td>
      </tr>
    {% endfor %}
    </table>
  </div>
{% endblock %}

Other payment methods

Rich, continuous, bank, bank and Minsheng Bank

Request mode

  • api interface
  • Form form (gateway interface)

Form form payment process

api payment process

Keywords: SDK pip Python Django

Added by Cerebral Cow on Mon, 30 Sep 2019 11:32:09 +0300