10-支付功能
一、支付功能
1、Pycharm远程代码调试
通过设置Pycharm来调试远端的服务器,为啥要用Pycharm调试远端服务器的代码?是因为关于第三方支付以及第三方登录,都是有一个回调的URL,这个URL一般指向的是服务器的IP地址,如何将本地代码上传到服务器中?
在windows下如何连接远程服务器呢?下载一个轻量级的连接远程服务器的客户端secureCRT。(下载注册机注册)
在这里面指明远程服务器的Hsotname与Username就可以连接远程服务器啦。连接远程服务器,过一段时间会自动退出来,怎么防止它退出来呢?安装一个工具可以防止它退出来:
yum install tmux #centos 7 屏幕管理里面很好用的
直接命令tmux就可以进入屏幕管理,这样客户端就不会关闭掉。创建一个文件夹mkdir projects,新建其他用户,最好不要用root用户。进入新建的目录继续新建目录mkdir MxShop,建好之后就可以去Pycharm配置啦
tmux
这样做的目的,就是在本地写的代码会映射到阿里云服务器上。接下来就可以将本地代码上传到云服务器啦。仔细看也可以将服务器的代码下载下来,甚至也可以同步。
上传之后,不代表可以调试远程服务器的代码,必须还需要在远程服务器上创建虚拟环境,还需要在虚拟服务器上安装python版本与项目python版本一致。同时在本地将数据库传输到阿里云服务器的数据库上,需要进行相关设置,否则数据库端口不对外开放。
2、支付宝公钥、私钥和沙箱环境的配置
为了测试支付环境,支付宝必须需要企业账号,我们个人开发者测试没有企业账号,因此支付宝提供了沙箱环境供给个人测试支付。
在接口文档中,有很多支付接口。
这样就完成沙箱环境的配置。
3、支付宝支付源码解读
虚拟环境安装支付宝源码所需要的包:然后utils下面建立alipay.py文件,将下面代码复制进去
pip3 install -i https//pypi.douban.com/simple 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 unsigned_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__": return_url = 'http://127.0.0.1:8000/?total_amount=100.00×tamp=2017-08-15+23%3A53%3A34&sign=e9E9UE0AxR84NK8TP1CicX6aZL8VQj68ylugWGHnM79zA7BKTIuxxkf%2FvhdDYz4XOLzNf9pTJxTDt8tTAAx%2FfUAJln4WAeZbacf1Gp4IzodcqU%2FsIc4z93xlfIZ7OLBoWW0kpKQ8AdOxrWBMXZck%2F1cffy4Ya2dWOYM6Pcdpd94CLNRPlH6kFsMCJCbhqvyJTflxdpVQ9kpH%2B%2Fhpqrqvm678vLwM%2B29LgqsLq0lojFWLe5ZGS1iFBdKiQI6wZiisBff%2BdAKT9Wcao3XeBUGigzUmVyEoVIcWJBH0Q8KTwz6IRC0S74FtfDWTafplUHlL%2Fnf6j%2FQd1y6Wcr2A5Kl6BQ%3D%3D&trade_no=2017081521001004340200204115&sign_type=RSA2&auth_app_id=2016080600180695&charset=utf-8&seller_id=2088102170208070&method=alipay.trade.page.pay.return&app_id=2016080600180695&out_trade_no=20170202185&version=1.0' o = urlparse(return_url) query = parse_qs(o.query) processed_query = {} ali_sign = query.pop("sign")[0] alipay = AliPay( appid="xxxxxxxxx", #支付宝沙盒环境中的appID app_notify_url="http://127.0.0.1:8000/alipay/return/", app_private_key_path="../trade/keys/private_2048.txt", alipay_public_key_path="../trade/keys/alipay_key_2048.txt", # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, 路径是相对路径 debug=True, # 默认False, return_url="http://127.0.0.1:8000/alipay/return/" ) for key, value in query.items(): processed_query[key] = value[0] print (alipay.verify(processed_query, ali_sign)) url = alipay.direct_pay( subject="测试订单2", out_trade_no="20170202sss", total_amount=100, return_url="http://127.0.0.1:8000/alipay/return/" ) re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url) print(re_url)
将打印的路由拿到浏览器中访问。利用沙盒环境中测试账号登录支付查看是否正常。
4、django集成支付宝notify_url和return_url接口
MxShop/settings.py:
#支付宝相关设置 private_key_path = os.path.join(BASE_DIR, 'apps/trade/keys/private_2048.txt') ali_pub_key_path = os.path.join(BASE_DIR, 'apps/trade/keys/alipay_key_2048.txt')
trade/views.py:
将noyify_url以及return_url都改成服务器地址,如果是远程服务就改成远程服务器地址。
from django.shortcuts import redirect from rest_framework.views import APIView from utils.alipay import AliPay from MxShop.settings import ali_pub_key_path, private_key_path from rest_framework.response import Response class AliPayview(APIView): def get(self, request): """ 处理支付宝的return_url返回 :param request: :return: """ processed_dict = {} for key, value in request.GET.items(): processed_dict[key] = value sign = processed_dict.pop("sign", None) alipay = AliPay( appid="xxxxxxxxx", app_notify_url="http://127.0.0.1:8000/alipay/return/", app_private_key_path=private_key_path, alipay_public_key_path=ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://127.0.0.1:8000/alipay/return/" ) verify_re = alipay.verify(processed_dict, sign) if verify_re is True: order_sn = processed_dict.get('out_trade_no', None) trade_no = processed_dict.get('trade_no', None) trade_status = processed_dict.get('trade_status', None) existed_orders = OrderInfo.objects.filter(order_sn=order_sn) for existed_order in existed_orders: existed_order.pay_status = trade_status existed_order.trade_no = trade_no existed_order.pay_time = datetime.now() existed_order.save() response = redirect("index") response.set_cookie("nextPath", "pay", max_age=3) return response else: response = redirect("index") return response def post(self, request): """ 处理支付宝的notify_url :param request: :return: """ processed_dict = {} for key, value in request.POST.items(): processed_dict[key] = value sign = processed_dict.pop("sign", None) alipay = AliPay( appid="", app_notify_url="http://127.0.0.1:8000/alipay/return/", app_private_key_path=private_key_path, alipay_public_key_path=ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://127.0.0.1:8000/alipay/return/" ) verify_re = alipay.verify(processed_dict, sign) if verify_re is True: order_sn = processed_dict.get('out_trade_no', None) trade_no = processed_dict.get('trade_no', None) trade_status = processed_dict.get('trade_status', None) existed_orders = OrderInfo.objects.filter(order_sn=order_sn) for existed_order in existed_orders: order_goods = existed_order.goods.all() for order_good in order_goods: goods = order_good.goods goods.sold_num += order_good.goods_num goods.save() existed_order.pay_status = trade_status existed_order.trade_no = trade_no existed_order.pay_time = datetime.now() existed_order.save() return Response("success")
MxShop/settings.py:
from trade.views import AliPayview urlpatterns = [ path('alipay/return/',AliPayview.as_view(),name="alipay"), ]
trade/serializers.py:
class OrderDetailSerializer(serializers.ModelSerializer): #OrderInfo中的外键order需要设置relate_name goods = OrderGoodsSerializer(many=True) alipay_url = serializers.SerializerMethodField(read_only=True) def get_alipay_url(self, obj): alipay = AliPay( appid="xxxxxx", app_notify_url="http://127.0.0.1:8000/alipay/return/", app_private_key_path=private_key_path, alipay_public_key_path=ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://127.0.0.1:8000/alipay/return/" ) url = alipay.direct_pay( subject=obj.order_sn, out_trade_no=obj.order_sn, total_amount=obj.order_amount, return_url="http://127.0.0.1:8000/alipay/return/" ) re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url) return re_url class Meta: model = OrderInfo fields = "__all__" class OrderSerializer(serializers.ModelSerializer): user = serializers.HiddenField( default=serializers.CurrentUserDefault() ) pay_status = serializers.CharField(read_only=True) order_sn = serializers.CharField(read_only=True) trade_no = serializers.CharField(read_only=True) pay_time = serializers.CharField(read_only=True) alipay_url = serializers.SerializerMethodField(read_only=True) def get_alipay_url(self,obj): alipay = AliPay( appid="xxxxxxxx", app_notify_url="http://127.0.0.1:8000/alipay/return/", app_private_key_path=private_key_path, alipay_public_key_path=ali_pub_key_path, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥, debug=True, # 默认False, return_url="http://127.0.0.1:8000/alipay/return/" ) url = alipay.direct_pay( subject=obj.order_sn, out_trade_no=obj.order_sn, total_amount=obj.order_amount, return_url="http://127.0.0.1:8000/alipay/return/" ) re_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url) return re_url def generate_order_sn(self): #当前时间+userID+随机数 from random import Random random_int = Random() order_sn = "{time_str}{userid}{ranstr}".format(time_str=time.strftime("%Y%m%d%H%M%S"), userid=self.context["request"].user.id,ranstr=random_int.randint(10,99)) return order_sn def validate(self, attrs): attrs["order_sn"] = self.generate_order_sn() return attrs class Meta: model = OrderInfo fields = "__all__"
为了让支付宝支付后能将网页跳转到网购页面,因此需要将前端vue的用django控制,django控制需要将前端的静态文件以及首页放在django项目文件下,在dos命令中的前端项目路径下运行这个,将vue的打包成静态文件。
cnpm run build
文件中将index.html放在template下面,其他文件全部放在static文件下,然后去项目设置里面配置静态文件路径。
#将静态文件添加到全局路径 STATICFILES_DIR = ( os.path.join(BASE_DIR,"static"), )
配置index.html的路由:
from django.views.generic import TemplateView urlpatterns = [ path('index/', TemplateView.as_view(template_name="index.html"),name="index"), ]
用项目路由访问,收藏购物车,下单支付。如下图:
支付成功后会自动跳转到首页。