PayUtil.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : PayUtil.py
  4. @Time : 2022/6/29 11:41
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. from urllib.parse import quote
  10. from Ansjer.config import SERVER_DOMAIN_SSL
  11. from Object.AliPayObject import AliPayObject
  12. from Object.WechatPayObject import WechatPayObject
  13. """
  14. 支付服务
  15. """
  16. class PayService:
  17. @staticmethod
  18. def create_alipay_payment(lang, order_id, price, title, notify_url, content, response):
  19. aliPayObj = AliPayObject()
  20. alipay = aliPayObj.conf()
  21. subject = title + content
  22. order_string = alipay.api_alipay_trade_wap_pay(
  23. out_trade_no=order_id,
  24. total_amount=price,
  25. subject=subject,
  26. return_url="{}web/paid2/success.html".format(SERVER_DOMAIN_SSL),
  27. notify_url="{}{}".format(SERVER_DOMAIN_SSL, notify_url),
  28. quit_url="{}web/paid2/fail.html".format(SERVER_DOMAIN_SSL),
  29. passback_params=quote("lang=" + lang)
  30. )
  31. if not order_string:
  32. return response.json(10, '生成订单错误.')
  33. return aliPayObj.alipay_prefix + order_string
  34. @staticmethod
  35. def create_wechat_payment(lang, order_id, price, ip, notify_url, content, response):
  36. notify_url = "{}{}".format(SERVER_DOMAIN_SSL, notify_url)
  37. pay = WechatPayObject()
  38. # 统一调用接口
  39. pay.get_parameter(order_id, content, float(price) * 100, ip, notify_url, quote("lang=" + lang))
  40. sign_params = pay.re_finall(orderid=order_id)
  41. if not sign_params:
  42. return response.json(10, '生成订单错误.')
  43. return notify_url, sign_params
  44. @staticmethod
  45. def get_two_float(f_str, n):
  46. # f_str = '{}'.format(f_str) 也可以转换为字符串
  47. f_str = str(f_str)
  48. a, b, c = f_str.partition('.')
  49. # 如论传入的函数有几位小数,在字符串后面都添加n为小数0
  50. c = (c + "0" * n)[:n]
  51. return ".".join([a, c])