【django创建支付对象】不限于任何第三方支付接入SDK

#  根据第三方支付平台的SKD开发文档进行,只是个示例(但是都差不多)

设置完第三方支付平台的公钥,与转换好的私钥后进行:

# 充值通道
class Alipay(APIView):

    # 返回支付链接
    def post(self,request):
        message = {}
        try:
            with transaction.atomic():
                # 充值金额
                recharge_money = float(request.data.get("money"))

                # 订单编号
                out_trade_no = "66" + str(datetime.datetime.fromtimestamp(time.time())).replace("-","").replace(" ","").replace(":","").replace(".","")

                # 建议在服务启动时初始化  以下的参数一定是你接入的SKD说明文档的示例代码 
                config_info = {
                    "api_key": cont.api_key,   
                    "mock_api_key": cont.mock_api_key,
                    "private_key": cont.private_key   # 一定会有个公钥转换成私钥VALUES 这个根据平台给定的规则进行转换后填入
                }
                adapay.mer_configs = {
                    "merchant1": config_info
                }

                # 创建订单请求  以下也是平台的SKD说明代码示例
                payment = adapay.Payment.create(
                    order_no=out_trade_no,
                    app_id='你平台账号的APPID',
                    pay_channel='alipay',    # 肯定会有个选择是H5支付,APP支付,扫码支付等
                    pay_amt=str(recharge_money),
                    goods_title=cont.RECHARGE_MONEY,
                    goods_desc=cont.RECHARGE_MONEY,
                    notify_url=cont.notify_url_link,  # 回调接口 用于接收第三方支付服务器对用户支付的后的请求告知 告知已经支付完成(根据SKD提示接入)
                    mer_key='merchant1'
                )

                if payment['status'] == 'succeeded':
                    # 数据库创建充值订单    数据库的操作自己对应着自己的写就好了
                    models.PayRecord.objects.create(title=cont.RECHARGE_MONEY,
                                                    order_id=out_trade_no,
                                                    manner_pay="支付宝",
                                                    action = "+{}".format(recharge_money),
                                                    user_pay_id=request.auth,       # 付款用户
                                                    user_receive_id= request.auth,  # 到款用用户
                                                    )

                    message['order'] = out_trade_no
                    message['pay_id'] = payment['id']
                    message['code'] =  200
                    message['pay_url'] = payment['expend']['pay_info']
return JsonResponse(message)

        except:
            # print(traceback.format_exc())
            msg = traceback.format_exc()
            logging_main.user_error.error(msg)
            message['code'] = 10014
            message['message'] = "请求异常"
            return JsonResponse(message)

 

posted @ 2020-12-25 15:48  PythonNew_Mr.Wang  Views(149)  Comments(0Edit  收藏  举报