Files
test/sg_wechat_enterprise/we_api/pay/api/order.py
2024-07-10 15:58:47 +08:00

139 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import time
import random
from datetime import datetime, timedelta
from wechatpy.utils import timezone
from wechatpy.pay.utils import get_external_ip
from wechatpy.pay.base import BaseWeChatPayAPI
from wechatpy.utils import random_string, to_text
from wechatpy.pay.utils import calculate_signature
class WeChatOrder(BaseWeChatPayAPI):
def create(self, trade_type, body, total_fee, notify_url, client_ip=None,
user_id=None, out_trade_no=None, detail=None, attach=None,
fee_type='CNY', time_start=None, time_expire=None,
goods_tag=None, product_id=None, device_info=None, limit_pay=None):
"""
统一下单接口
:param trade_type: 交易类型取值如下JSAPINATIVEAPPWAP
:param body: 商品描述
:param total_fee: 总金额,单位分
:param notify_url: 接收微信支付异步通知回调地址
:param client_ip: 可选APP和网页支付提交用户端ipNative支付填调用微信支付API的机器IP
:param user_id: 可选用户在商户appid下的唯一标识。trade_type=JSAPI此参数必传
:param out_trade_no: 可选,商户订单号,默认自动生成
:param detail: 可选,商品详情
:param attach: 可选附加数据在查询API和支付通知中原样返回该字段主要用于商户携带订单的自定义数据
:param fee_type: 可选符合ISO 4217标准的三位字母代码默认人民币CNY
:param time_start: 可选,订单生成时间,默认为当前时间
:param time_expire: 可选,订单失效时间,默认为订单生成时间后两小时
:param goods_tag: 可选,商品标记,代金券或立减优惠功能的参数
:param product_id: 可选trade_type=NATIVE此参数必传。此id为二维码中包含的商品ID商户自行定义
:param device_info: 可选,终端设备号(门店号或收银设备ID)注意PC网页或公众号内支付请传"WEB"
:param limit_pay: 可选指定支付方式no_credit--指定不能使用信用卡支付
:return: 返回的结果数据
"""
now = datetime.fromtimestamp(time.time(), tz=timezone('Asia/Shanghai'))
hours_later = now + timedelta(hours=2)
if time_start is None:
time_start = now
if time_expire is None:
time_expire = hours_later
if not out_trade_no:
out_trade_no = '{0}{1}{2}'.format(
self.mch_id,
now.strftime('%Y%m%d%H%M%S'),
random.randint(1000, 10000)
)
data = {
'appid': self.appid,
'device_info': device_info,
'body': body,
'detail': detail,
'attach': attach,
'out_trade_no': out_trade_no,
'fee_type': fee_type,
'total_fee': total_fee,
'spbill_create_ip': client_ip or get_external_ip(),
'time_start': time_start.strftime('%Y%m%d%H%M%S'),
'time_expire': time_expire.strftime('%Y%m%d%H%M%S'),
'goods_tag': goods_tag,
'notify_url': notify_url,
'trade_type': trade_type,
'limit_pay': limit_pay,
'product_id': product_id,
'openid': user_id,
}
return self._post('pay/unifiedorder', data=data)
def query(self, transaction_id=None, out_trade_no=None):
"""
查询订单
:param transaction_id: 微信的订单号,优先使用
:param out_trade_no: 商户系统内部的订单号当没提供transaction_id时需要传这个。
:return: 返回的结果数据
"""
data = {
'appid': self.appid,
'transaction_id': transaction_id,
'out_trade_no': out_trade_no,
}
return self._post('pay/orderquery', data=data)
def close(self, out_trade_no):
"""
关闭订单
:param out_trade_no: 商户系统内部的订单号
:return: 返回的结果数据
"""
data = {
'appid': self.appid,
'out_trade_no': out_trade_no,
}
return self._post('pay/closeorder', data=data)
def get_appapi_params(self, prepay_id, timestamp=None, nonce_str=None):
"""
获取 APP 支付参数
:param prepay_id: 统一下单接口返回的 prepay_id 参数值
:param timestamp: 可选,时间戳,默认为当前时间戳
:param nonce_str: 可选,随机字符串,默认自动生成
:return: 签名
"""
data = {
'appid': self.appid,
'partnerid': self.mch_id,
'prepayid': prepay_id,
'package': 'Sign=WXPay',
'timestamp': timestamp or to_text(int(time.time())),
'noncestr': nonce_str or random_string(32)
}
sign = calculate_signature(data, self._client.api_key)
data['sign'] = sign
return data
def reverse(self, transaction_id=None, out_trade_no=None):
"""
撤销订单
:param transaction_id: 可选,微信的订单号,优先使用
:param out_trade_no: 可选,商户系统内部的订单号,
transaction_id、out_trade_no二选一
如果同时存在优先级transaction_id> out_trade_no
:return: 返回的结果数据
"""
data = {
'appid': self.appid,
'transaction_id': transaction_id,
'out_trade_no': out_trade_no,
}
return self._post('secapi/pay/reverse', data=data)