# -*- coding: utf-8 -*- # !/usr/bin/env python import logging from typing import TYPE_CHECKING, Optional, Dict, Union import simplejson as json from apilib.monetary import RMB, Percent from apps.web.constant import AppPlatformType, PARTITION_ROLE from apps.web.core.payment.base import PaymentGateway from apps.web.utils import testcase_point from library.jd import JDAggrePay from library.jd.pay import GatewayMethod if TYPE_CHECKING: from apps.web.core.models import JDAggrePayApp logger = logging.getLogger(__name__) class JDAggrePaymentGateway(PaymentGateway, JDAggrePayMixin): def __init__(self, app, gateway_type): # type: (JDAggrePayApp, AppPlatformType)->None super(JDAggrePaymentGateway, self).__init__(app) self.__gateway_type__ = gateway_type def __repr__(self): return '' % ( str(self.occupantId), self.merchant_no, self.debug) @property def __client__(self): # type: ()->JDAggrePay return JDAggrePay(self.merchant_no, self.desKey, self.saltMd5Key, self.systemId, self.debug) def _to_fen(self, rmb): # type: (RMB)->long return int(rmb.amount * 100) def create_pay_url(self, order_no, money, notify_url, pi_type, subject, attach = None): # type: (str, RMB, str, str, str, Optional[dict])->str return self.client.create_pay_url( out_trade_no = order_no, total_fee = self._to_fen(money), notify_url = notify_url, piType = pi_type, tradeName = subject, returnParams = json.dumps(attach, separators = (',', ':')) if attach else '') def decrypt_response(self, payload): return self.client.decrypt_response(payload) def unified_order(self, out_trade_no, money, notify_url, subject = u'充值', expire = 300, attach = None, openId = None, gatewayMethod = GatewayMethod.SUBSCRIPTION, **kwargs): # type: (str, RMB, str, basestring, int, Optional[Dict], Optional[str], str, Dict)->Union[str, Dict] return self.client.unified_order(piType = self.pi_type, out_trade_no = out_trade_no, total_fee = str(self._to_fen(money)), notify_url = notify_url, productName = subject, expire = expire, returnParams = json.dumps(attach, separators = (',', ':')) if attach else '', client_ip = '127.0.0.1', openId = openId, gatewayMethod = gatewayMethod, **kwargs) def api_trade_query(self, out_trade_no = None, trade_no = None): # type:(Optional[str], Optional[str])->dict """ 查询订单 :param out_trade_no: 商户订单号 :param trade_no: 微信订单号 :return: """ return self.client.api_trade_query(out_trade_no = out_trade_no, trade_no = trade_no) @testcase_point() def refund_to_user(self, out_trade_no, out_refund_no, refund_fee, total_fee, refund_reason, **kwargs): # type:(str, str, RMB, RMB, str, dict)->dict """ :param out_trade_no: :param out_refund_no: :param refund_fee: :param total_fee: :param refund_reason: :return: """ return self.client.api_trade_refund(outTradeNo = out_trade_no, outRefundNo = out_refund_no, amount = self._to_fen(refund_fee), **kwargs) def api_refund_query(self, out_refund_no = None, refund_no = None): return self.client.api_refund_query(out_refund_no=out_refund_no)