123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import logging
- import simplejson as json
- from typing import TYPE_CHECKING, Optional
- from apilib.monetary import RMB, Percent
- from apps.web.constant import AppPlatformType, PARTITION_ROLE
- from apps.web.core import JDOpenPayMixin
- from apps.web.core.payment.base import PaymentGateway
- from apps.web.exceptions import UserServerException
- from apps.web.user.conf import REFUND_NOTIFY_URL
- from library.jdopen.pay import JDOpenPay
- if TYPE_CHECKING:
- from apps.web.core.models import JDOpenPayApp
- logger = logging.getLogger(__name__)
- class JDOpenPaymentGateway(PaymentGateway, JDOpenPayMixin):
- def __init__(self, app, gateway_type): # type: (JDOpenPayApp, AppPlatformType)->None
- super(JDOpenPaymentGateway, self).__init__(app)
- self.__gateway_type__ = gateway_type
- def __repr__(self):
- return '<JDOpenPaymentGateway(occupantId = %s, customerNum = %s, debug = %s)>' % (
- str(self.occupantId), self.customerNum, self.debug)
- @property
- def __client__(self): # type: ()->JDOpenPay
- agent = self.app.agent.fetch()
- return JDOpenPay(agent.agentNo, agent.accessKey, agent.secretKey, self.app.customerNum, self.app.shopNum)
- def create_pay_url(self, order_no, money, notify_url, attach=None, **kwargs):
- # type: (str, RMB, str, str, dict)->str
- return self.client.create_pay_url(
- out_trade_no=order_no,
- total_fee=str(money),
- notify_url=notify_url,
- extraInfo=json.dumps(attach, separators=(',', ':')) if attach else '',
- **kwargs)
- def unified_order(self, out_trade_no, money, notify_url, subject=u'充值', attach=None,
- openId=None, **kwargs):
- return self.client.unified_order(authId=openId, bankType=self.bank_type, requestNum=out_trade_no,
- amount=str(money), callbackUrl=notify_url, subject=subject,
- extraInfo=json.dumps(attach, separators=(',', ':')) if attach else '',
- **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)
- 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:
- :param kwargs callbackUrl,ledgerInfoList
- :return:
- """
- return self.client.api_trade_refund(outTradeNo=out_trade_no,
- outRefundNo=out_refund_no,
- amount=str(refund_fee),
- **kwargs)
- def api_refund_query(self, requestNum=None, orderNum=None):
- """
- 退款查询
- :param requestNum:
- :param orderNum:
- :return:
- """
- return self.client.api_refund_query(requestNum=requestNum, orderNum=orderNum)
- def add_wechat_auth_pay_dir(self, auth_pay_dir):
- return self.client.add_wechat_auth_pay_dir(auth_pay_dir)
- def query_wechat_auth_pay_dir(self, batch_num):
- return self.client.query_wechat_auth_pay_dir(batch_num)
- def api_close_order(self, requestNum):
- """
- 关闭订单接口提供给商户向支付服务发送关闭订单请求数据集合,支付服务会根据请求数据验证商户身份,以及验证支付信息是否被篡改。
- 验证通过后,支付服务会返回订单关闭信息。
- 商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付;
- 系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。
- 调用关单的时候,如果订单已经支付成功,会给用户退款
- :param requestNum:
- :return:
- """
- return self.client.api_close_order(requestNum)
- def api_cancel_order(self, requestNum):
- """
- 支付交易返回失败或支付系统超时,调用该接口撤销交易。
- 如果此订单用户支付失败,支付系统会将此订单关闭;
- 如果用户支付成功,支付系统会将此订单资金退还给用户。
- :param requestNum:
- :return:
- """
- return self.client.api_cancel_order(requestNum)
- def download_bill(self, bill_date):
- return self.client.download_bill(bill_date = bill_date)
|