Django docking Alipay

Django uses Alipay payment function

Alipay does not have a Python language interface, so we found the alipay package in Python, so we don't have to build our own wheels.

You need to set your own private   key   and public key in advance

  1. openssl  
  2. OpenSSL> genrsa -out app_private_key.pem   2048  # 私钥  
  3. OpenSSL >  rsa -in app_private_key.pem -pubout -out app_public_key.pem # Export public key  
  4. OpenSSL> exit  

You also need to set Alipay's public key in Alipay and write our public key 

# Alipay settings
ALIPAY_APPID = '2016091500520680'
# Private key
APP_PRIVATE_KEY_PATH = os.path.join(BASE_DIR, 'utils/app_private_key.pem')
# Public key
ALIPAY_PUBLIC_KEY_PATH = os.path.join(BASE_DIR, 'utils/alipay_public_key.pem')
# Interface
ALIPAY_URL = 'https://openapi.alipaydev.com/gateway.do?' 


Here we only use 2 interfaces, one is to generate an order, the other is to query the order status

# Import alipay package
from alipay import AliPay
# Import Alipay settings
from django.conf import settings


def create_alipay():
    # Create alipay object
    alipay = AliPay(
        appid=settings.ALIPAY_APPID,
        app_notify_url=None,
        app_private_key_path=settings.APP_PRIVATE_KEY_PATH,
        alipay_public_key_path=settings .ALIPAY_PUBLIC_KEY_PATH,
        sign_type="RSA2", # Password encryption method
        debug=True # True if sandbox is used, flase is used in official environment
    )
    return alipay


def get_alipay_page(order_id, total_amount):
    """Generate order"""
    alipay = create_alipay()
    print(5)
    # Call the interface
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=order_id,
        total_amount=str(total_amount),
        subject='支付付款',
        return_url=None,
        notify_url=None,
    )
    # 返回订单url
    print(order_string)
    return settings.ALIPAY_URL+order_string


def get_alipay_query(order_id):
    """查询订单状态"""
    alipay = create_alipay()
    result = alipay.api_alipay_trade_query(out_trade_no=order_id)
    print(result)


    if result.get("trade_status", "") == "TRADE_SUCCESS":
        return True
    else:

        return False


Then we only need to call these two functions where the order is generated and the order status is queried.


def alipay_pay(request):
    # 获得订单id
    order_id = request.GET.get('order_id')
    print(order_id)


    try:
        # 查询商品表
        order = OrderInfo.objects.get(pk=order_id)
    except:
        return JsonResponse({"result":"订单编号错误"})
    # 生成订单,返回订单url
    url = get_alipay_page(order_id,order.total_amount)


    return JsonResponse({"url":url})




def alipay_query(request):
    order_id = request.GET.get('order_id')
    result = get_alipay_query(order_id)
    if result:
        try:
            order=OrderInfo.objects.get(pk=order_id)
        except:
            return JsonResponse({'result':'失败'})
        # 支付成功就修改订单为代发货
        order.status = 2
        order.save()
        return JsonResponse({"result":1})
    else:
        return JsonResponse({"result":2})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326226228&siteId=291194637