jdopen.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. import simplejson as json
  5. from typing import TYPE_CHECKING, Optional
  6. from apilib.monetary import RMB, Percent
  7. from apps.web.constant import AppPlatformType, PARTITION_ROLE
  8. from apps.web.core import JDOpenPayMixin
  9. from apps.web.core.payment.base import PaymentGateway
  10. from apps.web.exceptions import UserServerException
  11. from apps.web.user.conf import REFUND_NOTIFY_URL
  12. from library.jdopen.pay import JDOpenPay
  13. if TYPE_CHECKING:
  14. from apps.web.core.models import JDOpenPayApp
  15. logger = logging.getLogger(__name__)
  16. class JDOpenPaymentGateway(PaymentGateway, JDOpenPayMixin):
  17. def __init__(self, app, gateway_type): # type: (JDOpenPayApp, AppPlatformType)->None
  18. super(JDOpenPaymentGateway, self).__init__(app)
  19. self.__gateway_type__ = gateway_type
  20. def __repr__(self):
  21. return '<JDOpenPaymentGateway(occupantId = %s, customerNum = %s, debug = %s)>' % (
  22. str(self.occupantId), self.customerNum, self.debug)
  23. @property
  24. def __client__(self): # type: ()->JDOpenPay
  25. agent = self.app.agent.fetch()
  26. return JDOpenPay(agent.agentNo, agent.accessKey, agent.secretKey, self.app.customerNum, self.app.shopNum)
  27. def create_pay_url(self, order_no, money, notify_url, attach=None, **kwargs):
  28. # type: (str, RMB, str, str, dict)->str
  29. return self.client.create_pay_url(
  30. out_trade_no=order_no,
  31. total_fee=str(money),
  32. notify_url=notify_url,
  33. extraInfo=json.dumps(attach, separators=(',', ':')) if attach else '',
  34. **kwargs)
  35. def unified_order(self, out_trade_no, money, notify_url, subject=u'充值', attach=None,
  36. openId=None, **kwargs):
  37. return self.client.unified_order(authId=openId, bankType=self.bank_type, requestNum=out_trade_no,
  38. amount=str(money), callbackUrl=notify_url, subject=subject,
  39. extraInfo=json.dumps(attach, separators=(',', ':')) if attach else '',
  40. **kwargs)
  41. def api_trade_query(self, out_trade_no=None, trade_no=None):
  42. # type:(Optional[str], Optional[str])->dict
  43. """
  44. 查询订单
  45. :param out_trade_no: 商户订单号
  46. :param trade_no: 微信订单号
  47. :return:
  48. """
  49. return self.client.api_trade_query(out_trade_no=out_trade_no, trade_no=trade_no)
  50. def refund_to_user(self, out_trade_no, out_refund_no, refund_fee, total_fee, refund_reason, **kwargs):
  51. # type:(str, str, RMB, RMB, str, dict)->dict
  52. """
  53. :param out_trade_no:
  54. :param out_refund_no:
  55. :param refund_fee:
  56. :param total_fee:
  57. :param refund_reason:
  58. :param kwargs callbackUrl,ledgerInfoList
  59. :return:
  60. """
  61. return self.client.api_trade_refund(outTradeNo=out_trade_no,
  62. outRefundNo=out_refund_no,
  63. amount=str(refund_fee),
  64. **kwargs)
  65. def api_refund_query(self, requestNum=None, orderNum=None):
  66. """
  67. 退款查询
  68. :param requestNum:
  69. :param orderNum:
  70. :return:
  71. """
  72. return self.client.api_refund_query(requestNum=requestNum, orderNum=orderNum)
  73. def add_wechat_auth_pay_dir(self, auth_pay_dir):
  74. return self.client.add_wechat_auth_pay_dir(auth_pay_dir)
  75. def query_wechat_auth_pay_dir(self, batch_num):
  76. return self.client.query_wechat_auth_pay_dir(batch_num)
  77. def api_close_order(self, requestNum):
  78. """
  79. 关闭订单接口提供给商户向支付服务发送关闭订单请求数据集合,支付服务会根据请求数据验证商户身份,以及验证支付信息是否被篡改。
  80. 验证通过后,支付服务会返回订单关闭信息。
  81. 商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付;
  82. 系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。
  83. 调用关单的时候,如果订单已经支付成功,会给用户退款
  84. :param requestNum:
  85. :return:
  86. """
  87. return self.client.api_close_order(requestNum)
  88. def api_cancel_order(self, requestNum):
  89. """
  90. 支付交易返回失败或支付系统超时,调用该接口撤销交易。
  91. 如果此订单用户支付失败,支付系统会将此订单关闭;
  92. 如果用户支付成功,支付系统会将此订单资金退还给用户。
  93. :param requestNum:
  94. :return:
  95. """
  96. return self.client.api_cancel_order(requestNum)
  97. def download_bill(self, bill_date):
  98. return self.client.download_bill(bill_date = bill_date)