Django实现支付宝付款和微信支付的示例代码

支付宝支付和微信支付是当今互联网产品常用的功能,我使用Django Rest Framework实现了网页上支付宝支付和微信支付的一个通用服务,提供rpc接口给其他服务,包括获取支付宝支付页面url的rpc接口、支付宝支付成功异步回调http接口、获取微信支付二维码rpc接口、主动查询微信订单是否支付的rpc接口等。

支付宝网站支付需要蚂蚁金服开放平台账号,创建应用、配置秘钥等步骤请参考:蚂蚁金服支付宝电脑网站支付快速接入

微信网站支付需要到微信支付官网注册服务商账号,

目录结构如下:

1、models.py

from django.db import models
from django.contrib.postgres.fields import ArrayField

# Create your models here.
class BaseModel(models.Model):
 """
  基础模型
 """
 created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
 updated_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")
 created_by = models.IntegerField(verbose_name="创建人ID")
 updated_by = models.IntegerField(verbose_name="修改人ID")
 is_active = models.BooleanField(default=True, verbose_name='是否正常')

 class Meta:
  abstract = True

class Alipay(BaseModel):
 """
  支付
 """

 subject = models.CharField(max_length=256, verbose_name="订单标题")
 out_trade_no = models.CharField(max_length=64, unique=True, verbose_name="唯一订单号")
 trade_no = models.CharField(default="", max_length=64, verbose_name="支付宝系统中的交易流水号")
 total_amount = models.DecimalField(max_digits=11, decimal_places=2, verbose_name="订单的资金总额")
 return_url = models.CharField(max_length=500, verbose_name="支付完成同步跳转地址")
 notify_url = models.CharField(max_length=500, verbose_name="支付完成异步通知rpc地址")
 pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间")
 pay_nos = ArrayField(models.CharField(max_length=100), default=[], verbose_name='同一订单的支付ID数组')

 class Meta:
  verbose_name = "阿里支付"
  verbose_name_plural = verbose_name
  ordering = ('-created_time',)

class Wxorder(BaseModel):
 """
  订单
 """
 body = models.CharField(max_length=256, verbose_name="商品描述")
 out_trade_no = models.CharField(max_length=64, unique=True, verbose_name="订单号")
 transaction_id = models.CharField(default="", max_length=64, verbose_name="微信支付订单号")
 total_fee = models.BigIntegerField(verbose_name="订单的资金总额,单位为分")
 product_id = models.CharField(max_length=16, verbose_name="商品ID")
 notify_url = models.CharField(max_length=500, verbose_name="支付完成通知url")
 pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付时间")

 class Meta:
  verbose_name = "微信订单"
  verbose_name_plural = verbose_name
  ordering = ('-created_time',)

class Wxpay(BaseModel):
 """
  微信支付
 """
 out_trade_no = models.CharField(null=True, blank=True, max_length=64, verbose_name="订单号")
 pay_no = models.CharField(null=True, blank=True, max_length=64, unique=True, verbose_name="支付唯一订单号")
 code_url = models.CharField(null=True, blank=True, max_length=100, verbose_name="二维码地址")
 nonce_str = models.CharField(null=True, blank=True, max_length=32, verbose_name="随机字符串")

 class Meta:
  verbose_name = "微信支付"
  verbose_name_plural = verbose_name
  ordering = ('-created_time',)

2、serializers.py:

from django.conf import settings
from rest_framework import serializers
from pay.models import Alipay, Wxpay

class BaseSerializer(serializers.ModelSerializer):
 created_time = serializers.DateTimeField(format=settings.DATETIME_FORMAT, read_only=True)
 updated_time = serializers.DateTimeField(format=settings.DATETIME_FORMAT, read_only=True)
 is_active = serializers.BooleanField(read_only=True)

 class Meta:
  model = None

class AlipaySerializer(BaseSerializer):
 """
  阿里支付序列化类
 """

 class Meta:
  model = Alipay
  fields = "__all__"

class WxpaySerializer(BaseSerializer):
 """
  阿里支付序列化类
 """

 class Meta:
  model = Wxpay
  fields = "__all__"

3、views.py:

# -*- coding=utf-8 -*-
# Create your views here.
import time
import dicttoxml
from jsonrpc import jsonrpc_method
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView
from rest_framework_xml.parsers import XMLParser
from rest_framework_xml.renderers import XMLRenderer
from tokenauth.decorators import is_login
from pay import utils
from pay.weixin_pay import WeiXinPay, UnifiedOrderPay, OrderQuery
from pay.UUIDTools import UUIDTools
from pay.models import Alipay, Wxpay, Wxorder
from pay.serializers import AlipaySerializer, WxpaySerializer
from pay.utils import UnActiveModelMixin
from pay.alipay import AliPay
from PAY_SERVICE.settings.base import APPID, PRIVATE_KEY_PATH, \
 ALI_PUB_KEY_PATH, ALIPAY_CALLBACK_URL, \
 WXAPPID, WX_PAY_KEY, WX_MCH_ID, WXPAY_CALLBACK_URL

NOTIFY_URL = ALIPAY_CALLBACK_URL + 'api/v1.0/pay/alipay/notify/'

class AlipayViewSet(ModelViewSet):
 queryset = Alipay.objects.filter(is_active=True)
 serializer_class = AlipaySerializer

 @list_route(methods=['post'])
 def notify(self, request):
  """
   处理支付宝的notify_url
  :param request:
  :return:
  """
  processed_dict = {}
  for k, v in request.data.items():
   processed_dict[k] = v
  app_id = processed_dict.get('app_id')
  pay_no = processed_dict.get('out_trade_no')
  trade_no = processed_dict.get('trade_no')
  total_amount = processed_dict.get('total_amount')
  pay_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

  alipay = Alipay.objects.filter(pay_nos__contains=[pay_no]).values().first()
  if alipay is None:
   return Response("failed")
  if str(alipay.get('total_amount')) != str(total_amount):
   return Response("failed")
  if app_id != APPID:
   return Response("failed")
  if alipay.get('trade_no') != "":
   return Response("failed")

  sign = processed_dict.pop('sign', None)

  ali_pay = AliPay(
   appid=APPID,
   app_notify_url=NOTIFY_URL,
   app_private_key_path=PRIVATE_KEY_PATH,
   alipay_public_key_path=ALI_PUB_KEY_PATH,
   debug=True, # 默认False,
   return_url=alipay.get('return_url')
  )

  is_verify = ali_pay.verify(processed_dict, sign)

  if is_verify is True:
   Alipay.objects.filter(pk=alipay.get('id')).update(pay_time=pay_time, trade_no=trade_no)
   ret = utils.request_thrift('TradingManager', 'notify',
    settings.TRADING_RPC_IP, int(settings.TRADING_RPC_PORT),
    alipay.get('out_trade_no'), str(pay_time))
   if ret == "success":
    return Response("success")

class WxpayViewSet(ModelViewSet):
 queryset = Wxpay.objects.filter(is_active=True)
 serializer_class = WxpaySerializer
 parser_classes = (XMLParser,)
 renderer_classes = (XMLRenderer,)

@jsonrpc_method('pay.get_alipay_url')
def get_alipay_url(request, subject, out_trade_no, total_amount, return_url, notify_url, user_id):
 recode = Alipay.objects.filter(out_trade_no=out_trade_no).values().first()
 if recode is not None:
  pay_no = UUIDTools.datetime_random()
  alipay = Alipay.objects.get(pk=recode.get('id'))
  alipay.pay_nos.append(pay_no)
  alipay.save()
 else:
  pay_no = out_trade_no
  Alipay.objects.create(subject=subject,
        out_trade_no=out_trade_no,
        total_amount=total_amount,
        return_url=return_url,
        notify_url=notify_url,
        pay_nos=[pay_no],
        created_by=user_id,
        updated_by=user_id
        )

 ali_pay = AliPay(
  appid=APPID,
  app_notify_url=NOTIFY_URL,
  app_private_key_path=PRIVATE_KEY_PATH,
  alipay_public_key_path=ALI_PUB_KEY_PATH,
  debug=True, # 默认False,
  return_url=return_url
 )

 total_amount = "%.2f" % float(total_amount)
 url = ali_pay.direct_pay(
  subject=subject,
  out_trade_no=pay_no,
  total_amount=total_amount
 )
 # 沙箱环境网关
 # alipay_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)
 # 正式环境网关
 alipay_url = "https://openapi.alipay.com/gateway.do?{data}".format(data=url)
 return alipay_url

@jsonrpc_method('pay.get_wxpay_url')
def get_wxpay_url(request, out_trade_no, body, total_fee, notify_url, product_id, user_id):
 recode = Wxorder.objects.filter(out_trade_no=out_trade_no).values().first()
 if recode is None:
  Wxorder.objects.create(
   out_trade_no=out_trade_no,
   body=body,
   total_fee=total_fee,
   notify_url=notify_url,
   product_id=product_id,
   created_by=user_id,
   updated_by=user_id
  )

 pay_no = UUIDTools.datetime_random()
 pay = UnifiedOrderPay(WXAPPID, WX_MCH_ID, WX_PAY_KEY)
 response = pay.post(body, pay_no, total_fee,
      WXPAY_CALLBACK_URL.split('://')[1].split(':')[0], WX_NOTIFY_URL)
 if response and response["return_code"] == "SUCCESS" and response["result_code"] == "SUCCESS":
  wxorder = Wxorder.objects.filter(out_trade_no=out_trade_no).values().first()
  Wxpay.objects.create(
   out_trade_no=out_trade_no,
   pay_no=pay_no,
   code_url=response.get('code_url'),
   nonce_str=response.get('nonce_str'),
   created_by=user_id,
   updated_by=user_id
  )
  return response.get('code_url')

@jsonrpc_method('pay.wx_order_query')
def wx_order_query(request, out_trade_no):
 wxpays = Wxpay.objects.filter(out_trade_no=out_trade_no).values()
 pay = OrderQuery(WXAPPID, WX_MCH_ID, WX_PAY_KEY)
 for wxpay in wxpays:
  response = pay.post(wxpay.get('pay_no'))
  if response and response["return_code"] == "SUCCESS" \
    and response["result_code"] == "SUCCESS":
   trade_state = response["trade_state"]
   if trade_state == "SUCCESS": # 支付成功
    pay_time = response["time_end"]
    transaction_id = response["transaction_id"]
    Wxorder.objects.filter(out_trade_no=out_trade_no).update(
     pay_time=time.strftime("%Y-%m-%d %H:%M:%S",
           time.strptime(pay_time, "%Y%m%d%H%M%S")),
     transaction_id=transaction_id
    )
    return {"success": True, "pay_time": pay_time}
 return {"success": False}

4、alipay.py:

# -*- coding: utf-8 -*-
# pip install pycryptodome

from datetime import datetime
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode, b64decode
from urllib.parse import quote_plus
from urllib.parse import urlparse, parse_qs
from urllib.request import urlopen
from base64 import decodebytes, encodebytes

import json

class AliPay(object):
 """
 支付宝支付接口
 """

 def __init__(self, appid, app_notify_url, app_private_key_path,
     alipay_public_key_path, return_url, debug=False):
  self.appid = appid
  self.app_notify_url = app_notify_url
  self.app_private_key_path = app_private_key_path
  self.app_private_key = None
  self.return_url = return_url
  with open(self.app_private_key_path) as fp:
   self.app_private_key = RSA.importKey(fp.read())

  self.alipay_public_key_path = alipay_public_key_path
  with open(self.alipay_public_key_path) as fp:
   self.alipay_public_key = RSA.import_key(fp.read())

  if debug is True:
   self.__gateway = "https://openapi.alipaydev.com/gateway.do"
  else:
   self.__gateway = "https://openapi.alipay.com/gateway.do"

 def direct_pay(self, subject, out_trade_no, total_amount, return_url=None, **kwargs):
  biz_content = {
   "subject": subject,
   "out_trade_no": out_trade_no,
   "total_amount": total_amount,
   "product_code": "FAST_INSTANT_TRADE_PAY",
   # "qr_pay_mode":4
  }

  biz_content.update(kwargs)
  data = self.build_body("alipay.trade.page.pay", biz_content, self.return_url)
  return self.sign_data(data)

 def build_body(self, method, biz_content, return_url=None):
  data = {
   "app_id": self.appid,
   "method": method,
   "charset": "utf-8",
   "sign_type": "RSA2",
   "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
   "version": "1.0",
   "biz_content": biz_content
  }

  if return_url is not None:
   data["notify_url"] = self.app_notify_url
   data["return_url"] = self.return_url

  return data

 def sign_data(self, data):
  data.pop("sign", None)
  # 排序后的字符串
  unsigned_items = self.ordered_data(data)
  unsigned_string = "&".join("{0}={1}".format(k, v) for k, v in unsigned_items)
  sign = self.sign(unsigned_string.encode("utf-8"))
  ordered_items = self.ordered_data(data)
  quoted_string = "&".join("{0}={1}".format(k, quote_plus(v)) for k, v in ordered_items)

  # 获得最终的订单信息字符串
  signed_string = quoted_string + "&sign=" + quote_plus(sign)
  return signed_string

 def ordered_data(self, data):
  complex_keys = []
  for key, value in data.items():
   if isinstance(value, dict):
    complex_keys.append(key)

  # 将字典类型的数据dump出来
  for key in complex_keys:
   data[key] = json.dumps(data[key], separators=(',', ':'))

  return sorted([(k, v) for k, v in data.items()])

 def sign(self, unsigned_string):
  # 开始计算签名
  key = self.app_private_key
  signer = PKCS1_v1_5.new(key)
  signature = signer.sign(SHA256.new(unsigned_string))
  # base64 编码,转换为unicode表示并移除回车
  sign = encodebytes(signature).decode("utf8").replace("\n", "")
  return sign

 def _verify(self, raw_content, signature):
  # 开始计算签名
  key = self.alipay_public_key
  signer = PKCS1_v1_5.new(key)
  digest = SHA256.new()
  digest.update(raw_content.encode("utf8"))
  if signer.verify(digest, decodebytes(signature.encode("utf8"))):
   return True
  return False

 def verify(self, data, signature):
  if "sign_type" in data:
   sign_type = data.pop("sign_type")
  # 排序后的字符串
  unsigned_items = self.ordered_data(data)
  message = "&".join(u"{}={}".format(k, v) for k, v in unsigned_items)
  return self._verify(message, signature)

if __name__ == "__main__":
 alipay = AliPay(
  appid="2016081500252338",
  app_notify_url="http://projectsedus.com/",
  app_private_key_path="keys/private_2048.txt",
  alipay_public_key_path="keys/alipay_key_2048.txt", # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
  debug=True, # 默认False,
  return_url="http://192.168.247.129:8000/"
 )

 url = alipay.direct_pay(
  subject="测试订单",
  out_trade_no="20170202126666",
  total_amount=1000
 )
 re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)
 print(re_url)

5、wxpay.py:

# -*- coding=utf-8 -*-

import time
import json
import hashlib
import requests

from pay.utils import (smart_str, dict_to_xml, calculate_sign, random_str,
      post_xml, xml_to_dict, validate_post_xml, format_url)

# from local_settings import appid, mch_id, api_key

OAUTH2_AUTHORIZE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?%s"
OAUTH2_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?%s"

class WeiXinPay(object):
 def __init__(self, appid, mch_id, api_key):
  self.appid = appid # 微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
  self.mch_id = mch_id # 受理商ID,身份标识
  self.api_key = api_key # 商户支付密钥Key。审核通过后,在微信发送的邮件中查看
  self.common_params = {
   "appid": self.appid,
   "mch_id": self.mch_id,
  }
  self.params = {}
  self.url = ""
  self.trade_type = ""

 def set_params(self, **kwargs):
  self.params = {}
  for (k, v) in kwargs.items():
   self.params[k] = smart_str(v)

  self.params["nonce_str"] = random_str(32)
  if self.trade_type:
   self.params["trade_type"] = self.trade_type
  self.params.update(self.common_params)

 def post_xml(self):
  sign = calculate_sign(self.params, self.api_key)
  xml = dict_to_xml(self.params, sign)
  response = post_xml(self.url, xml)
  return xml_to_dict(response.text)

 def valiate_xml(self, xml):
  return validate_post_xml(xml, self.appid, self.mch_id, self.api_key)

 def get_error_code_desc(self, error_code):
  error_desc = {
   "SYSTEMERROR": u"接口后台错误",
   "INVALID_TRANSACTIONID": u"无效 transaction_id",
   "PARAM_ERROR": u"提交参数错误",
   "ORDERPAID": u"订单已支付",
   "OUT_TRADE_NO_USED": u"商户订单号重复",
   "NOAUTH": u"商户无权限",
   "NOTENOUGH": u"余额丌足",
   "NOTSUPORTCARD": u"不支持卡类型",
   "ORDERCLOSED": u"订单已关闭",
   "BANKERROR": u"银行系统异常",
   "REFUND_FEE_INVALID": u"退款金额大亍支付金额",
   "ORDERNOTEXIST": u"订单不存在",
  }
  return error_desc.get(error_code.strip().upper(), u"未知错误")

class UnifiedOrderPay(WeiXinPay):
 """发送预支付单"""

 def __init__(self, appid, mch_id, api_key):
  super(UnifiedOrderPay, self).__init__(appid, mch_id, api_key)
  self.url = "https://api.mch.weixin.qq.com/pay/unifiedorder"
  self.trade_type = "NATIVE"

 def post(self, body, out_trade_no, total_fee, spbill_create_ip, notify_url, **kwargs):
  tmp_kwargs = {
   "body": body,
   "out_trade_no": out_trade_no,
   "total_fee": total_fee,
   "spbill_create_ip": spbill_create_ip,
   "notify_url": notify_url,
  }
  tmp_kwargs.update(**kwargs)
  self.set_params(**tmp_kwargs)
  return self.post_xml()[1]

class OrderQuery(WeiXinPay):
 """订单状态查询"""

 def __init__(self, appid, mch_id, api_key):
  super(OrderQuery, self).__init__(appid, mch_id, api_key)
  self.url = "https://api.mch.weixin.qq.com/pay/orderquery"

 def post(self, out_trade_no):
  self.set_params(out_trade_no=out_trade_no)
  return self.post_xml()[1]

class JsAPIOrderPay(UnifiedOrderPay):
 """H5页面的Js调用类"""

 def __init__(self, appid, mch_id, api_key, app_secret):
  super(JsAPIOrderPay, self).__init__(appid, mch_id, api_key)
  self.app_secret = app_secret
  self.trade_type = "JSAPI"

 def create_oauth_url_for_code(self, redirect_uri):
  url_params = {
   "appid": self.appid,
   "redirect_uri": redirect_uri, # 一般是回调当前页面
   "response_type": "code",
   "scope": "snsapi_base",
   "state": "STATE#wechat_redirect"
  }
  url = format_url(url_params)
  return OAUTH2_AUTHORIZE_URL % url

 def _create_oauth_url_for_openid(self, code):
  url_params = {
   "appid": self.appid,
   "secret": self.app_secret,
   "code": code,
   "grant_type": "authorization_code",
  }
  url = format_url(url_params)
  return OAUTH2_ACCESS_TOKEN_URL % url

 def _get_oauth_info(self, code):
  """
  获取OAuth2的信息:access_token、expires_in、refresh_token、openid、scope
  返回结果为字典,可使用["xxx"]或.get("xxx", None)的方式进行读取
  """
  url = self._create_oauth_url_for_openid(code)
  response = requests.get(url)
  return response.json() if response else None

 def _get_openid(self, code):
  oauth_info = self._get_oauth_info(code)
  if oauth_info:
   return oauth_info.get("openid", None)
  return None

 def _get_json_js_api_params(self, prepay_id):
  js_params = {
   "appId": self.appid,
   "timeStamp": "%d" % time.time(),
   "nonceStr": random_str(32),
   "package": "prepay_id=%s" % prepay_id,
   "signType": "MD5",
  }
  js_params["paySign"] = calculate_sign(js_params, self.api_key)
  return js_params

 def post(self, body, out_trade_no, total_fee, spbill_create_ip, notify_url, code):
  if code:
   open_id = self._get_openid(code)
   if open_id:
    # 直接调用基类的post方法查询prepay_id,如果成功,返回一个字典
    unified_order = super(JsAPIOrderPay, self).post(body, out_trade_no, total_fee, spbill_create_ip,
                notify_url, open_id=open_id)
    if unified_order:
     prepay_id = unified_order.get("prepay_id", None)
     if prepay_id:
      return self._get_json_js_api_params(prepay_id)
  return None

6、utils.py:

# -*- coding=utf-8 -*-
import hashlib
import re
import types
from random import Random
import requests
import thriftpy

from django.conf import settings
from django.core.exceptions import FieldDoesNotExist
from django.db import models
from django.db.models.fields.reverse_related import ForeignObjectRel
from rest_framework.pagination import PageNumberPagination
from thriftpy.rpc import make_client

from pay.exception_handler import ForeignObjectRelDeleteError, ModelDontHaveIsActiveFiled, logger

def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
 """
 Returns a bytestring version of 's', encoded as specified in 'encoding'.
 If strings_only is True, don't convert (some) non-string-like objects.
 """
 if strings_only and isinstance(s, (types.NoneType, int)):
  return s
 if not isinstance(s, str):
  try:
   return str(s)
  except UnicodeEncodeError:
   if isinstance(s, Exception):
    # An Exception subclass containing non-ASCII data that doesn't
    # know how to print itself properly. We shouldn't raise a
    # further exception.
    return ' '.join([smart_str(arg, encoding, strings_only,
           errors) for arg in s])
   return unicode(s).encode(encoding, errors)
 elif s and encoding != 'utf-8':
  return s.decode('utf-8', errors).encode(encoding, errors)
 else:
  return s

def format_url(params, api_key=None):
 url = "&".join(['%s=%s' % (key, smart_str(params[key])) for key in sorted(params)])
 if api_key:
  url = '%s&key=%s' % (url, api_key)
 return url

def calculate_sign(params, api_key):
 # 签名步骤一:按字典序排序参数, 在string后加入KEY
 url = format_url(params, api_key)
 # 签名步骤二:MD5加密, 所有字符转为大写
 return hashlib.md5(url.encode('utf-8')).hexdigest().upper()

def dict_to_xml(params, sign):
 xml = ["<xml>", ]
 for (k, v) in params.items():
  if (v.isdigit()):
   xml.append('<%s>%s</%s>' % (k, v, k))
  else:
   xml.append('<%s><![CDATA[%s]]></%s>' % (k, v, k))
 xml.append('<sign><![CDATA[%s]]></sign></xml>' % sign)
 return ''.join(xml)

def xml_to_dict(xml):
 if xml[0:5].upper() != "<XML>" and xml[-6].upper() != "</XML>":
  return None, None

 result = {}
 sign = None
 content = ''.join(xml[5:-6].strip().split('\n'))

 pattern = re.compile(r"<(?P<key>.+)>(?P<value>.+)</(?P=key)>")
 m = pattern.match(content)
 while (m):
  key = m.group("key").strip()
  value = m.group("value").strip()
  if value != "<![CDATA[]]>":
   pattern_inner = re.compile(r"<!\[CDATA\[(?P<inner_val>.+)\]\]>")
   inner_m = pattern_inner.match(value)
   if inner_m:
    value = inner_m.group("inner_val").strip()
   if key == "sign":
    sign = value
   else:
    result[key] = value

  next_index = m.end("value") + len(key) + 3
  if next_index >= len(content):
   break
  content = content[next_index:]
  m = pattern.match(content)

 return sign, result

def validate_post_xml(xml, appid, mch_id, api_key):
 sign, params = xml_to_dict(xml)
 if (not sign) or (not params):
  return None

 remote_sign = calculate_sign(params, api_key)
 if sign != remote_sign:
  return None

 if params["appid"] != appid or params["mch_id"] != mch_id:
  return None

 return params

def random_str(randomlength=8):
 chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
 random = Random()
 return "".join([chars[random.randint(0, len(chars) - 1)] for i in range(randomlength)])

def post_xml(url, xml):
 return requests.post(url, data=xml.encode('utf-8'), verify=False)

class UnActiveModelMixin(object):
 """
 删除一个对象,并不真删除,级联将对应外键对象的is_active设置为false,需要外键对象都有is_active字段.
 """

 def perform_destroy(self, instance):
  rel_fileds = [f for f in instance._meta.get_fields() if isinstance(f, ForeignObjectRel)]

  links = [f.get_accessor_name() for f in rel_fileds]

  for link in links:
   manager = getattr(instance, link, None)
   if not manager:
    continue
   if isinstance(manager, models.Model):
    if hasattr(manager, 'is_active') and manager.is_active:
     manager.is_active = False
     manager.save()
     raise ForeignObjectRelDeleteError(u'{} 上有关联数据'.format(link))
   else:
    if not manager.count():
     continue
    try:
     manager.model._meta.get_field('is_active')
     manager.filter(is_active=True).update(is_active=False)
    except FieldDoesNotExist as ex:
     # 理论上,级联删除的model上面应该也有is_active字段,否则代码逻辑应该有问题
     logger.warn(ex)
     raise ModelDontHaveIsActiveFiled(
      '{}.{} 没有is_active字段, 请检查程序逻辑'.format(
       manager.model.__module__,
       manager.model.__class__.__name__
      ))
  instance.is_active = False
  instance.save()

 def get_queryset(self):
  return self.queryset.filter(is_active=True)

class StandardResultsSetPagination(PageNumberPagination):
 page_size_query_param = 'size'

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • django 实现电子支付功能的示例代码

    思路:调用第三方支付 API 接口实现支付功能.本来想用支付宝来实现第三方网站的支付功能的,但是在实际操作中发现支付宝没有 Python 接口,网上虽然有他人二次封装的的 Python 接口,但是对我这个小白白来说上手还是有点难度,后来发现 PayPal 有现成的 Django 模块,想着以学习的目的来实现这一功能(其实还是自己辣鸡),就决定以 PayPal 的电子支付功能来练手. 首先,安装 PayPal 的 Django 模块:django-paypal,具体介绍可以参考 GitHub上说明

  • python采用django框架实现支付宝即时到帐接口

    因工作需要研究了支付宝即时到帐接口,并成功应用到网站上,把过程拿出来分享. 即时到帐只是支付宝众多商家服务中的一个,表示客户付款,客户用支付宝付款,支付宝收到款项后,马上通知你,并且此笔款项与交易脱离关系,商家可以马上使用. 即时到帐只对企业客户服务,注册成功企业账号以后,申请签约即时到帐产品,大约3-5个工作日后,签约成功,可以马上进入集成产品阶段. 这个是支付宝提供的接口,有asp,c#,java,php四种语言的,每种语言提供GBK和UTF-8两种方案.另带一份支付宝的文档,这份文档我感觉

  • Django实现支付宝付款和微信支付的示例代码

    支付宝支付和微信支付是当今互联网产品常用的功能,我使用Django Rest Framework实现了网页上支付宝支付和微信支付的一个通用服务,提供rpc接口给其他服务,包括获取支付宝支付页面url的rpc接口.支付宝支付成功异步回调http接口.获取微信支付二维码rpc接口.主动查询微信订单是否支付的rpc接口等. 支付宝网站支付需要蚂蚁金服开放平台账号,创建应用.配置秘钥等步骤请参考:蚂蚁金服支付宝电脑网站支付快速接入 微信网站支付需要到微信支付官网注册服务商账号, 目录结构如下: 1.mo

  • SpringMvc微信支付回调示例代码

    介绍 大家都知道微信支付的回调链接要求不能跟参数,但又要接收返回的xml数据.我开始使用@RequestBody注解在参数上,希望能获取xml数据,测试失败.最后使用HttpServletRequest去获取数据成功了. 示例代码 @RequestMapping("/weixinpay/callback") public String callBack(HttpServletRequest request){ InputStream is = request.getInputStrea

  • Spring MVC 使用支付宝接口完成在线支付的示例代码

    项目中要使用到在线支付功能 目前常用的在线支付手段主要是 支付宝 和微信. 这里我使用的是支付宝支付,支付宝有个好处就是他有一个沙箱模式 即使没有申请渠道的资格也可以体验一把在线支付. 第一步:完善沙箱信息 进入支付宝的开发者中心 就可以看到有个沙箱环境 使用支付宝提供的秘钥生成工具 生成对应的秘钥 一定要保存好.支付宝推荐使用RSA2(SHA256)秘钥 把自己的公钥填上去就可以了. 第二步 阅读在线支付开发文档 里面需要的配置信息 对应的使用沙箱环境的信息 第三步 环境搭建 下载服务端SDK

  • php实现微信和支付宝支付的示例代码

    php实现微信支付 微信支付文档地址:https://pay.weixin.qq.com/wiki/doc/api/index.html 在php下实现微信支付,这里我使用了EasyWeChat 这里我是在Yii框架实现的,安装EasyWeChat插件 composer require jianyan74/yii2-easy-wechat 一:配置EasyWeChat 1:在config/main.php 的 component中添加EasyWeChat的SDK 'components' =>

  • PHP实现一个二维码同时支持支付宝和微信支付的示例

    实现思路 生成一个二维码,加入要处理的url连接 在用户扫完码后,在对应的脚本中,判断扫码终端,调用相应的支付 若能够扫码之后能唤起相应app,支付宝要用手机网站支付方式,微信要使用jsapi支付方式 效果展示 提示: 因为项目即将上线,所以上面的支付二维码连接被我替换了(注意在生成二维码时加入的连接,要带上http协议) 实现 步骤生成二维码 //我的url指向了checkTerrace方法 $url = self::ADMIN_URL . 'params=' . $params; //ADM

  • Laravel使用支付宝进行支付的示例代码

    本文介绍了Laravel使用支付宝进行支付的示例代码,分享给大家,也给自己留个笔记 /* 支付 */ public function pay() { header("content-type:text/html;charset=utf8"); //echo 1;die; // ******************************************************配置 start********************************************

  • springboot 集成支付宝支付的示例代码

    最简单的springboot集成支付宝 1 注册沙箱 沙箱是一个模拟环境登录,百度蚂蚁金服开放平台,支付宝扫码登录如下 然后沙箱需要注册一下,非常之简单,注册好以后进入到如下页面,选沙箱工具,然后下载一个生成密钥的工具.然后解压按照里面的readme生成公私密钥, 选择沙箱应用 上传公钥即可..沙箱到这里就基本完成了,里面还有沙箱版本的的android app可以下载下来. java 程序 1 新建一个springboot项目 因为我们创建的是一个web工程,所以,仅仅演示支付宝的demo,只需

  • Vue+SpringBoot实现支付宝沙箱支付的示例代码

    首先去下载支付宝沙箱的一系列东西,具体的配置什么的我就不说了,有很多博客都讲了,还有蚂蚁金服官方也说的很详细,我就直接说怎么样把后端的支付页面显示到Vue前端来: 在你配置好AlipayConfig这个文件后,就可以写前端的逻辑了,前端是采用支付宝的页面如下: 下面展示一些 内联代码片. /* 以下是支付确认html */ <div style="text=#000000 bgColor=#ffffff leftMargin=0 topMargin=4"> <head

  • Django后端发送小程序微信模板消息示例(服务通知)

    模板消息 官方文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.send.html 模板消息如下图所示 Django中获取access_token 根据文档描述,获取access_token文档,后端必须获取一个access_token才能够发送模板消息,文档中说明该token有效期为两小时,需要后端定时去获取.我们这里使用Django

  • python实现网站微信登录的示例代码

    最近微信登录开放公测,为了方便微信用户使用,我们的产品也决定加上微信登录功能,然后就有了这篇笔记. 根据需求选择相应的登录方式 python实现网站微信登录的示例代码 微信现在提供两种登录接入方式 移动应用微信登录 网站应用微信登录 这里我们使用的是网站应用微信登录 按照 官方流程 1 注册并通过开放平台开发者资质认证 注册微信开放平台帐号后,在帐号中心中填写开发者资质认证申请,并等待认证通过. 2 创建网站应用 通过填写网站应用名称.简介和图标,以及各平台下载地址等资料,创建网站应用 3 接入

随机推荐