支付宝支付/退款/配置文件

# 支付----------

# 被调用---------------

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

def __init__(self):
self.config = settings
if not self.config.ALI_APPID:
raise Exception('You must define the APPID.')
if not self.config.ALI_PRIVATE_PEM_PATH:
raise Exception('You must define the ALI_PRIVATE_PEM_PATH path.')
if not self.config.ALI_PUBLIC_PEM_PATH:
raise Exception('You must define the ALI_PUBLIC_PEM_PATH path.')
public_path = self.config.ALI_PUBLIC_PEM_PATH
if os.path.exists(public_path):
with open(public_path) as fp:
public_key = fp.read()
else:
raise Exception('the ALI_PUBLIC_PEM_PATH path not exist.')
private_path = self.config.ALI_PRIVATE_PEM_PATH
if os.path.exists(private_path):
with open(private_path) as fp:
private_key = fp.read()
else:
raise Exception('the ALI_PRIVATE_PEM_PATH path not exist.')
self.alipay = Pay(
appid=self.config.ALI_APPID,
app_notify_url=self.config.ALI_NOTIFY_URL, # 默认回调url
app_private_key_string=private_key,
alipay_public_key_string=public_key, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
sign_type="RSA2", # RSA 或者 RSA2
debug=False # 默认False
)
self.isv_alipay = ISVAliPay(
appid=self.config.ALI_APPID,
app_notify_url=self.config.ALI_NOTIFY_URL, # 默认回调url
app_private_key_string=private_key,
alipay_public_key_string=public_key, # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
sign_type="RSA2", # RSA 或者 RSA2
debug=False, # 默认False
app_auth_code='7ed21ff5e5ae4e5db51a2cba4152dX76',
app_auth_token='201901BBd95e27f6b4da49e5bce752e788465A76'
)

def get_auth_token(self):
return self.isv_alipay.api_alipay_open_auth_token_app()

def order_query(self, out_trade_no, trade_no):
self.isv_alipay.api_alipay_trade_query(
out_trade_no=out_trade_no,
trade_no=trade_no
)

def auth_get(self):
data = {'redirect_uri': self.config.ALI_AUTH_RESULT_URL}
url = '{}?app_id={}&{}'.format(self.config.ALI_ORDER_QUERY_URL,
self.config.ALI_APPID,
urlencode(data))
print(url)
return url

def order_string(self, out_trade_no, total_amount, subject, passback_params=None):
if passback_params is None:
return self.alipay.api_alipay_trade_app_pay(
out_trade_no=out_trade_no, # 商户订单号
total_amount=total_amount,
subject=subject, # 商品简单描述
notify_url=self.config.ALI_NOTIFY_URL, # 可选, 不填则使用默认notify url
)
return self.alipay.api_alipay_trade_app_pay(
out_trade_no=out_trade_no, # 商户订单号
total_amount=total_amount,
subject=subject, # 商品简单描述
notify_url=self.config.ALI_NOTIFY_URL, # 可选, 不填则使用默认notify url
passback_params=passback_params
)

def verify(self, post_dict, sign):
return self.alipay.verify(post_dict, sign)


# 调用上面的---------------------
class AliPayView(View):
"""支付宝支付接口"""

@csrf_exempt
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)

@login_user
def post(self, _, json_data, user):
try:
uuid_o = json_data.get('code', None)
kind = json_data.get('kind', '0')
print(kind)
if kind is None:
kind = '0'
if uuid_o is None:
return HttpResponse(json.dumps({'state': -2, 'msg': 'code参数必须哟'}),
status=200,
content_type='application/json; charset=utf-8')
# 商城订单
if kind == '0':
print(222222222222)
total_order = TotalOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if total_order is None:
product_order = ProductOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if product_order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核对后再试'}),
status=200,
content_type='application/json; charset=utf-8')
else:
code = product_order.order_code
total_money = 0.0
count_product = 0
product_str = None
total_money += product_order.total_money
query = OrderRelated.objects.filter(is_effective=True, order=product_order)
if product_str is None:
rel = query.first()
product_str = rel.product.name
count_product += query.count()
if count_product > 1:
product_str += '等{}种商品'.format(count_product)
total_money = round(total_money, 2)
print(total_money)
else:
code = total_order.code
# if OrdersBelong.objects.filter(is_effective=True, code=code, order__state='0').count() != count:
# return HttpResponse(json.dumps({'state': -2, 'msg': '该订单无法再进行支付,请重新选择商品下单'}),
# status=200,
# content_type='application/json; charset=utf-8')
total_money = 0.0
count_product = 0
product_str = None
orders = ProductOrder.objects.filter(is_effective=True, total_order=total_order, user=user).all()
for o in orders:
total_money += o.total_money
query = OrderRelated.objects.filter(is_effective=True, order=o)
if product_str is None:
rel = query.first()
product_str = rel.product.name
count_product += query.count()
if count_product > 1:
product_str += '等{}种商品'.format(count_product)
# total_money = round(total_money, 2)
total_money = 0.01
print(total_money)
# e+商品订单
elif kind == '1':
order = EaddProductOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核对后再试'}),
status=200,
content_type='application/json; charset=utf-8')
query = EaddOrderRelated.objects.filter(is_effective=True, order=order)
rel = query.first()
code = order.order_code
total_money = 0.0
total_money += order.total_money
total_money = round(total_money, 2)
print(total_money)
product_str = rel.product.name
# E+服务
elif kind == '3':
print('----------E+服务---------')
order = EaddServeOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
total_money = 0.0
total_money += order.total_money
total_money = round(total_money, 2)
product_str = order.serve.name
print(total_money)
# 到家服务
elif kind == '4':
print('--------到家----------')
order = SServeOrder.objects.filter(is_effective=True, order_code=uuid_o, user=user).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
total_money = 0.0
total_money += order.total_money
# total_money = round(total_money, 2)
total_money = 0.01
product_str = order.serve.name
print(total_money)
elif kind == '5':
print("----------购买会员卡---------")
order = PurchaseCard.objects.filter(order_code=uuid_o, sort=0).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
total_money = float(order.card_price)
product_str = "会员卡"
print(total_money)
elif kind == "6":
print("----------会员 充值----------")
order = MemberCenterRecharge.objects.filter(order_code=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
# total_money = float(order.recharge)
product_str = "会员卡充值"
total_money = 0.01
print(total_money)
else:
return
order_string = AliPay().order_string(code, total_money, product_str, passback_params=kind)
print('wangmeili')
print(order_string)
print('guoxiaopeng')
return HttpResponse(json.dumps({'state': 0, 'msg': '请求成功', 'data': {'order_string': order_string}}),
status=200,
content_type='application/json; charset=utf-8')
except Exception as e:
print(str(e))
return HttpResponse(json.dumps({'state': -2, 'msg': str(e)}),
status=200,
content_type='application/json; charset=utf-8')




# 退款------=============
# 被调用--------==========
class AliPayRefund(object):
"""
支付宝退款接口
"""

def __init__(self):
self.config = settings
if not self.config.ALI_APPID:
raise Exception('You must define the APPID.')
if not self.config.ALI_PRIVATE_PEM_PATH:
raise Exception('You must define the ALI_PRIVATE_PEM_PATH path.')
if not self.config.ALI_PUBLIC_PEM_PATH:
raise Exception('You must define the ALI_PUBLIC_PEM_PATH path.')
public_path = self.config.ALI_PUBLIC_PEM_PATH
if os.path.exists(public_path):
with open(public_path) as fp:
public_key = fp.read()
else:
raise Exception('the ALI_PUBLIC_PEM_PATH path not exist.')
private_path = self.config.ALI_PRIVATE_PEM_PATH
if os.path.exists(private_path):
with open(private_path) as fp:
private_key = fp.read()
else:
raise Exception('the ALI_PRIVATE_PEM_PATH path not exist.')
self.alipay = Pay(
appid=self.config.ALI_APPID,
app_notify_url=self.config.ALI_NOTIFY_URL,
app_private_key_string=private_key,
alipay_public_key_string=public_key,
sign_type="RSA2",
debug=False
)
self.isv_alipay = ISVAliPay(
appid=self.config.ALI_APPID,
app_notify_url=self.config.ALI_NOTIFY_URL,
app_private_key_string=private_key,
alipay_public_key_string=public_key,
sign_type="RSA2",
debug=False,
app_auth_code='7ed21ff5e5ae4e5db51a2cba4152dX76',
app_auth_token='201901BBd95e27f6b4da49e5bce752e788465A76'
)

def get_auth_token(self):
return self.isv_alipay.api_alipay_open_auth_token_app()

def order_query(self, out_trade_no, trade_no):
self.isv_alipay.api_alipay_trade_query(
out_trade_no=out_trade_no,
trade_no=trade_no
)

def auth_get(self):
data = {'redirect_uri': self.config.ALI_AUTH_RESULT_URL}
url = '{}?app_id={}&{}'.format(self.config.ALI_ORDER_QUERY_URL,
self.config.ALI_APPID,
urlencode(data))
print(url)
return url

def order_string(self, refund_amount, out_trade_no=None, passback_params=None):
return self.alipay.api_alipay_trade_refund(
refund_amount=refund_amount, # 退款金额
out_trade_no=out_trade_no, # 订单编号
passback_params=passback_params
)

def verify(self, post_dict, sign):
return self.alipay.verify(post_dict, sign)


#退款接口-----调用上面的内容----------
class AliPayRefundView(View):
"""支付宝退款接口"""

@csrf_exempt
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)

@base_request
def post(self, _, json_data, ):
try:
uuid_o = json_data.get('code', None)
kind = json_data.get('kind', '3')
print(kind)
if kind is None:
kind = '0'
if uuid_o is None:
return HttpResponse(json.dumps({'state': -2, 'msg': 'code参数必须哟'}),
status=200,
content_type='application/json; charset=utf-8')
# 商品订单
if kind == '0':
total_order = TotalOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
print(total_order.uuid, 33333333333)
code = total_order.code
total_money = 0.0
count_product = 0
product_str = None
orders = ProductOrder.objects.filter(is_effective=True, total_order=total_order).all()
for o in orders:
total_money += o.total_money
query = OrderRelated.objects.filter(is_effective=True, order=o)
if product_str is None:
rel = query.first()
product_str = rel.product.name
count_product += query.count()
if count_product > 1:
product_str += '等{}种商品'.format(count_product)
# total_money = round(total_money, 2)
total_money = 0.01
print(total_money)

# E+服务
elif kind == '3':
print('----------E+服务---------')
order = EaddServeOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
total_money = 0.0
total_money += order.total_money
total_money = round(total_money, 2)
print(total_money)
# 到家服务
elif kind == '4':
print('--------到家----------')
order = SServeOrder.objects.filter(is_effective=True, uuid=uuid_o).first()
if order is None:
return HttpResponse(json.dumps({'state': -2, 'msg': '没有这个订单请核实后再试'}),
status=200,
content_type='application/json; charset=utf-8')
code = order.order_code
total_money = 0.0
total_money += order.total_money
total_money = round(total_money, 2)
print(total_money)
else:
return

order_string = AliPayRefund().order_string(0.01, code, passback_params=kind)
print(order_string)

return HttpResponse(json.dumps({'state': 0, 'msg': '请求成功', 'data': {'order_string': order_string}}),
status=200,
content_type='application/json; charset=utf-8')
except Exception as e:
print(str(e))
return HttpResponse(json.dumps({'state': -2, 'msg': str(e)}),
status=200,
content_type='application/json; charset=utf-8')


#---------------配置文件---------------
# -*- coding: utf-8 -*-
# ----------------------------------------------
# @Time : 19-1-5 下午12:43
# @Author : YYJ
# @File : global_settings.py
# @CopyRight: ZDWL
# ----------------------------------------------

import os


################
# media路径配置
################

# media路径
MEDIA_ROOT = '/home/zd_oa/beauty_new/Ebeauty/media/'

# 分类图标路径
MEDIA_ICON = os.path.join(MEDIA_ROOT, 'icon')
ICON = 'icon'

# 商城门店图片根路径
MEDIA_STORE = os.path.join(MEDIA_ROOT, 'store')
STORE = 'store'

# E+门店图片根路径
MEDIA_E_STORE = os.path.join(MEDIA_ROOT, 'e_store')
# E_STORE = 'e_store'
E_STORE = 'http://qiniu.hebzd.com'

# 到家门店图片路径
MEDIA_S_STORE = os.path.join(MEDIA_ROOT, 's_store')
# S_STORE = 's_store'
S_STORE = 'http://qiniu.hebzd.com'

# 技师图片路径
MEDIA_TECH = os.path.join(MEDIA_ROOT, 'tech')
TECH = 'tech'

# 用户图片根路径
MEDIA_USER = os.path.join(MEDIA_ROOT, 'user')
USER = 'user'

# apk文件路径
MEDIA_APK = os.path.join(MEDIA_ROOT, 'apk')
APK = 'apk'

# 专家说图片路径
MEDIA_ARTICLE = os.path.join(MEDIA_ROOT, 'article')
ARTICLE = 'article'


# 专家说视频路径
MEDIA_VIDEO = os.path.join(MEDIA_ROOT, 'video')
VIDEO = 'video'

# 每一页个数
PAGE_NUM = 10


################
# 支付宝支付相关配置
################

# 应用appid
# ALI_APPID = '2019010962821961'
ALI_APPID = '2021001170636445'

# 支付宝网关
ALI_GATEWAY = 'https://openapi.alipay.com/gateway.do'

# 支付宝沙箱网关
ALI_DEV_GATEWAY = 'https://openapi.alipaydev.com/gateway.do'

# 沙箱模式
ALI_DEBUG = False

# 应用私钥
ALI_APP_PRIVATE_KEY = ''

# 公钥文件路径
ALI_PUBLIC_PEM_PATH = '/home/zd_oa/beauty_new/Ebeauty/rsa/ali_rsa_public_key.pem'

# 私钥文件路径
ALI_PRIVATE_PEM_PATH = '/home/zd_oa/beauty_new/Ebeauty/rsa/self_rsa_private_key.pem'

ALI_SIGN_TYPE = 'RSA2'

ALI_PAY_URL = 'alipay.trade.app.pay'

ALI_AUTH_TOKEN_URL = 'alipay.open.auth.token.app'

ALI_NOTIFY_URL = 'http://beauty.hebzd.com/api/alipay/result'

ALI_ORDER_QUERY_URL = 'https://openauth.alipay.com/oauth2/appToAppAuth.htm'

ALI_AUTH_RESULT_URL = 'http://beauty.hebzd.com/api/alipay/auth/result'

ALI_AUTH_TOKEN = '201901BBd95e27f6b4da49e5bce752e788465A76'

################
# 微信支付相关配置
################

# 微信应用appid
WX_APPID = 'wxb41434428ef8c8e7'

# 微信app_Secret
WX_APP_SECRET = 'd6f319607acf167e982ebfa946e3d9c9'

# 微信商户id
WX_MCH_ID = '1600913544'

# 微信商户API秘钥
# WX_API_KEY = 'ZXCVBNMASDFGHJKLQWERTYUIOP789632'
WX_API_KEY = 'Heibeizhidaguangdian123456789000'
# WX_API_KEY = '92738f7e64b87118d9daabecbe5e91d0'

# 统一下单接口链接
WX_ORDER_URL = 'https://api.mch.weixin.qq.com/pay/unifiedorder'

WX_NOTIFY_URL = 'http://beauty.hebzd.com/api/wxpay/result'

# 微信退款接口链接
WX_REFUND_URL = 'https://api.mch.weixin.qq.com/secapi/pay/refund'


apiclient_cert = '/home/zd_oa/beauty_new/Ebeauty/rsa/cert/apiclient_cert.pem'
apiclient_key = '/home/zd_oa/beauty_new/Ebeauty/rsa/cert/apiclient_key.pem'
# wx_sign = 'bfb179cd0331439c94c0d9fc3f9b7102'
# wx_sign_new = 'c3043c5257dbefb833e547d585d19f01'
posted @ 2020-07-28 17:57  关美丽  阅读(478)  评论(0编辑  收藏  举报