Order.php
7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
// +----------------------------------------------------------------------
// | WeChatDeveloper
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
// | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------
namespace WePayV3;
use WeChat\Contracts\Tools;
use WeChat\Exceptions\InvalidArgumentException;
use WeChat\Exceptions\InvalidResponseException;
use WePayV3\Contracts\BasicWePay;
use WePayV3\Contracts\DecryptAes;
/**
* 直连商户 | 订单支付接口
* Class Order
* @package WePayV3
*/
class Order extends BasicWePay
{
const WXPAY_H5 = 'h5';
const WXPAY_APP = 'app';
const WXPAY_JSAPI = 'jsapi';
const WXPAY_NATIVE = 'native';
/**
* 创建支付订单
* @param string $type 支付类型
* @param array $data 支付参数
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml
*/
public function create($type, $data)
{
$types = [
'h5' => '/v3/pay/transactions/h5',
'app' => '/v3/pay/transactions/app',
'jsapi' => '/v3/pay/transactions/jsapi',
'native' => '/v3/pay/transactions/native',
];
if (empty($types[$type])) {
throw new InvalidArgumentException("Payment {$type} not defined.");
} else {
// 创建预支付码
$result = $this->doRequest('POST', $types[$type], json_encode($data, JSON_UNESCAPED_UNICODE), true);
if (empty($result['h5_url']) && empty($result['code_url']) && empty($result['prepay_id'])) {
$message = isset($result['code']) ? "[ {$result['code']} ] " : '';
$message .= isset($result['message']) ? $result['message'] : json_encode($result, JSON_UNESCAPED_UNICODE);
throw new InvalidResponseException($message);
}
// 支付参数签名
$time = strval(time());
$appid = $this->config['appid'];
$nonceStr = Tools::createNoncestr();
if ($type === 'app') {
$sign = $this->signBuild(join("\n", [$appid, $time, $nonceStr, $result['prepay_id'], '']));
return ['partnerId' => $this->config['mch_id'], 'prepayId' => $result['prepay_id'], 'package' => 'Sign=WXPay', 'nonceStr' => $nonceStr, 'timeStamp' => $time, 'sign' => $sign];
} elseif ($type === 'jsapi') {
$sign = $this->signBuild(join("\n", [$appid, $time, $nonceStr, "prepay_id={$result['prepay_id']}", '']));
return ['appId' => $appid, 'timestamp' => $time, 'timeStamp' => $time, 'nonceStr' => $nonceStr, 'package' => "prepay_id={$result['prepay_id']}", 'signType' => 'RSA', 'paySign' => $sign];
} else {
return $result;
}
}
}
/**
* 支付订单查询
* @param string $tradeNo 订单单号
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
*/
public function query($tradeNo)
{
$pathinfo = "/v3/pay/transactions/out-trade-no/{$tradeNo}";
return $this->doRequest('GET', "{$pathinfo}?mchid={$this->config['mch_id']}", '', true);
}
/**
* 关闭支付订单
* @param string $tradeNo 订单单号
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
*/
public function close($tradeNo)
{
$data = ['mchid' => $this->config['mch_id']];
$path = "/v3/pay/transactions/out-trade-no/{$tradeNo}/close";
return $this->doRequest('POST', $path, json_encode($data, JSON_UNESCAPED_UNICODE), true);
}
/**
* 支付通知解析
* @param array $data
* @return array
* @throws \WeChat\Exceptions\InvalidDecryptException
*/
public function notify(array $data = [])
{
if (empty($data)) {
$data = json_decode(Tools::getRawInput(), true);
}
if (isset($data['resource'])) {
$aes = new DecryptAes($this->config['mch_v3_key']);
$data['result'] = $aes->decryptToString(
$data['resource']['associated_data'],
$data['resource']['nonce'],
$data['resource']['ciphertext']
);
}
return $data;
}
/**
* 创建退款订单
* @param array $data 退款参数
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml
*/
public function createRefund($data)
{
$path = '/v3/refund/domestic/refunds';
return $this->doRequest('POST', $path, json_encode($data, JSON_UNESCAPED_UNICODE), true);
}
/**
* 退款订单查询
* @param string $refundNo 退款单号
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_10.shtml
*/
public function queryRefund($refundNo)
{
$path = "/v3/refund/domestic/refunds/{$refundNo}";
return $this->doRequest('GET', $path, '', true);
}
/**
* 获取退款通知
* @param mixed $data
* @return array
* @throws \WeChat\Exceptions\InvalidDecryptException
* @deprecated 直接使用 Notify 方法
*/
public function notifyRefund($data = [])
{
return $this->notify($data);
}
/**
* 申请交易账单
* @param array|string $params
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_6.shtml
*/
public function tradeBill($params)
{
$path = '/v3/bill/tradebill?' . is_array($params) ? http_build_query($params) : $params;
return $this->doRequest('GET', $path, '', true);
}
/**
* 申请资金账单
* @param array|string $params
* @return array
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_7.shtml
*/
public function fundflowBill($params)
{
$path = '/v3/bill/fundflowbill?' . is_array($params) ? http_build_query($params) : $params;
return $this->doRequest('GET', $path, '', true);
}
/**
* 下载账单文件
* @param string $fileurl
* @return string 二进制 Excel 内容
* @throws \WeChat\Exceptions\InvalidResponseException
* @document https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_1.shtml
*/
public function downloadBill($fileurl)
{
return $this->doRequest('GET', $fileurl, '', false, false);
}
}