• 禾匠商城系统 企业转账到零钱 修改成 商家转账到零钱


    注意php 7以上的json_encode 溢出

    修改的路径:

    vendor/luweiss/wechat/src/Wechat/WechatPay.php

    1. namespace luweiss\Wechat;
    2. class WechatPay extends WechatBase
    3. {
    4. const SIGN_TYPE_MD5 = 'MD5';
    5. const TRADE_TYPE_JSAPI = 'JSAPI';
    6. const TRADE_TYPE_NATIVE = 'NATIVE';
    7. const TRADE_TYPE_APP = 'APP';
    8. const TRADE_TYPE_MWEB = 'MWEB';
    9. public $appId;
    10. public $mchId;
    11. public $key;
    12. public $certPemFile;
    13. public $keyPemFile;
    14. /**
    15. * WechatPay constructor.
    16. * @param array $config ['appId', 'mchId', 'key', 'certPemFile', 'keyPemFile']
    17. */
    18. public function __construct($config = [])
    19. {
    20. foreach ($config as $name => $value) {
    21. if (property_exists($this, $name)) {
    22. $this->$name = $value;
    23. }
    24. }
    25. }
    26. /**
    27. * @param array $result
    28. * @return array
    29. * @throws WechatException
    30. */
    31. protected function getClientResult($result)
    32. {
    33. if (!isset($result['return_code'])) {
    34. throw new WechatException(
    35. '返回数据格式不正确: ' . json_encode($result, JSON_UNESCAPED_UNICODE)
    36. );
    37. }
    38. if ($result['return_code'] !== 'SUCCESS') {
    39. $msg = 'returnCode: ' . $result['return_code'] . ', returnMsg: ' . $result['return_msg'];
    40. throw new WechatException($msg, 0, null, $result);
    41. }
    42. if (!isset($result['result_code'])) {
    43. throw new WechatException(
    44. '返回数据格式不正确: ' . json_encode($result, JSON_UNESCAPED_UNICODE)
    45. );
    46. }
    47. if ($result['result_code'] !== 'SUCCESS') {
    48. $msg = 'errCode: ' . $result['err_code'] . ', errCodeDes: ' . $result['err_code_des'];
    49. throw new WechatException($msg, 0, null, $result);
    50. }
    51. return $result;
    52. }
    53. /**
    54. * @param $api
    55. * @param $args
    56. * @return array
    57. * @throws WechatException
    58. */
    59. protected function send($api, $args)
    60. {
    61. $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
    62. $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
    63. $args['nonce_str'] = !empty($args['nonce_str']) ? $args['nonce_str'] : md5(uniqid());
    64. $args['sign'] = $this->makeSign($args);
    65. $xml = WechatHelper::arrayToXml($args);
    66. $res = $this->getClient()->setDataType(WechatHttpClient::DATA_TYPE_XML)->post($api, $xml);
    67. return $this->getClientResult($res);
    68. }
    69. /**
    70. * @param $api
    71. * @param $args
    72. * @return array
    73. * @throws WechatException
    74. */
    75. protected function sendWithPem($api, $args)
    76. {
    77. $args['nonce_str'] = !empty($args['nonce_str']) ? $args['nonce_str'] : md5(uniqid());
    78. $args['sign'] = $this->makeSign($args);
    79. $xml = WechatHelper::arrayToXml($args);
    80. $res = $this->getClient()
    81. ->setDataType(WechatHttpClient::DATA_TYPE_XML)
    82. ->setCertPemFile($this->certPemFile)
    83. ->setKeyPemFile($this->keyPemFile)
    84. ->post($api, $xml);
    85. return $this->getClientResult($res);
    86. }
    87. /**
    88. *
    89. * 统一下单,
    90. *
    91. * @param array $args ['body', 'out_trade_no', 'total_fee', 'notify_url', 'trade_type', 'openid']
    92. * @return array
    93. * @throws WechatException
    94. */
    95. public function unifiedOrder($args)
    96. {
    97. $args['spbill_create_ip'] = !empty($args['spbill_create_ip']) ? $args['spbill_create_ip'] : '127.0.0.1';
    98. $api = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
    99. return $this->send($api, $args);
    100. }
    101. /**
    102. *
    103. * 查询订单,
    104. *
    105. * @param array $args ['out_trade_no']
    106. * @return array
    107. * @throws WechatException
    108. */
    109. public function orderQuery($args)
    110. {
    111. $api = 'https://api.mch.weixin.qq.com/pay/orderquery';
    112. return $this->send($api, $args);
    113. }
    114. /**
    115. *
    116. * 关闭订单,
    117. *
    118. * @param array $args
    119. * @return array
    120. * @throws WechatException
    121. */
    122. public function closeOrder($args)
    123. {
    124. $api = 'https://api.mch.weixin.qq.com/pay/closeorder';
    125. return $this->send($api, $args);
    126. }
    127. /**
    128. *
    129. * 申请退款,
    130. *
    131. * @param array $args
    132. * @return array
    133. * @throws WechatException
    134. */
    135. public function refund($args)
    136. {
    137. $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
    138. $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
    139. $api = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
    140. return $this->sendWithPem($api, $args);
    141. }
    142. /**
    143. *
    144. * 查询退款,
    145. *
    146. * @param array $args
    147. * @return array
    148. * @throws WechatException
    149. */
    150. public function refundQuery($args)
    151. {
    152. $api = 'https://api.mch.weixin.qq.com/pay/refundquery';
    153. return $this->send($api, $args);
    154. }
    155. /**
    156. *
    157. * 企业付款,
    158. *
    159. * @param array $args ['partner_trade_no', 'openid', 'amount', 'desc']
    160. * @return array
    161. * @throws WechatException
    162. */
    163. public function transfers($args)
    164. {
    165. $arg = [];
    166. $arg['out_batch_no'] = $args['partner_trade_no'];
    167. $arg['batch_name'] = date("Y-m-d H:i:s").$args['transfer_remark'];
    168. $arg['appid'] = !empty($args['mch_appid']) ? $args['mch_appid'] : $this->appId;
    169. $arg['batch_remark'] = date("Y-m-d H:i:s").$args['transfer_remark'];
    170. $arg['total_amount'] = !empty($args['transfer_amount']) ? intval($args['transfer_amount']):$args['amount'];
    171. $arg['total_num'] = 1;
    172. $transfer_detail_list = array(
    173. 'out_detail_no'=>$args['partner_trade_no'],
    174. 'transfer_amount'=>!empty($args['transfer_amount']) ? intval($args['transfer_amount']):$args['amount'],
    175. 'transfer_remark'=>date("Y-m-d H:i:s").$args['desc'],
    176. 'openid'=>$args['openid'],
    177. );
    178. if(isset($args['user_name'])&&!empty($args['user_name'])){
    179. $transfer_detail_list['user_name'] = $args['user_name'];
    180. }
    181. $arg['transfer_detail_list'][]=$transfer_detail_list;
    182. // var_dump($arg);die();
    183. $api = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
    184. $token = $this->getToken($arg);//获取token
    185. return $this->https_request($api, json_encode($arg),$token);
    186. }
    187. protected function sendBatches($api, $arg)
    188. {
    189. $res = $this->getClient()
    190. ->setDataType(WechatHttpClient::DATA_TYPE_JSON)
    191. ->setCertPemFile($this->certPemFile)
    192. ->setKeyPemFile($this->keyPemFile)
    193. ->post($api, $arg);
    194. return $this->getClientResult($res);
    195. }
    196. public function https_request($url,$data = null,$token){
    197. try {
    198. $curl = curl_init();
    199. curl_setopt($curl, CURLOPT_URL, (string)$url);
    200. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    201. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    202. if (!empty($data)) {
    203. curl_setopt($curl, CURLOPT_POST, 1);
    204. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    205. }
    206. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    207. //添加请求头
    208. $headers = [
    209. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $token,
    210. 'Accept: application/json',
    211. 'Content-Type: application/json; charset=utf-8',
    212. 'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
    213. ];
    214. if (!empty($headers)) {
    215. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    216. }
    217. $output = curl_exec($curl);
    218. curl_close($curl);
    219. $data = json_decode($output,true);
    220. if($data['code']!=200&&isset($data['code'])){
    221. $data['err_code_des'] = $data['message'];
    222. throw new WechatException($data['message'], 0, null, $data);
    223. }
    224. return $data;
    225. }catch (WechatException $exception) {
    226. throw $exception;
    227. }
    228. }
    229. public function getToken($pars)
    230. {
    231. $url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
    232. $http_method = 'POST';//请求方法(GET,POST,PUT)
    233. $timestamp = time();//请求时间戳
    234. $url_parts = parse_url($url);//获取请求的绝对URL
    235. $nonce = $timestamp.rand('10000','99999');//请求随机串
    236. $body = json_encode((object)$pars);//请求报文主体
    237. $stream_opts = [
    238. "ssl" => [
    239. "verify_peer"=>false,
    240. "verify_peer_name"=>false,
    241. ]
    242. ];
    243. $apiclient_cert_path = $this->certPemFile;
    244. $apiclient_key_path = $this->keyPemFile;
    245. $apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path,false, stream_context_create($stream_opts)));
    246. $serial_no = $apiclient_cert_arr['serialNumberHex'];//证书序列号
    247. $mch_private_key = file_get_contents($apiclient_key_path,false, stream_context_create($stream_opts));//密钥
    248. $merchant_id = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;//商户id
    249. $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
    250. $message = $http_method."\n".
    251. $canonical_url."\n".
    252. $timestamp."\n".
    253. $nonce."\n".
    254. $body."\n";
    255. openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');
    256. $sign = base64_encode($raw_sign);//签名
    257. $schema = 'WECHATPAY2-SHA256-RSA2048';
    258. $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
    259. $merchant_id, $nonce, $timestamp, $serial_no, $sign);//微信返回token
    260. return $token;
    261. }
    262. // public function transfers($args)
    263. // {
    264. // $args['mch_appid'] = !empty($args['mch_appid']) ? $args['mch_appid'] : $this->appId;
    265. // $args['mchid'] = !empty($args['mchid']) ? $args['mchid'] : $this->mchId;
    266. // $args['spbill_create_ip'] = !empty($args['spbill_create_ip']) ? $args['spbill_create_ip'] : '127.0.0.1';
    267. // $args['check_name'] = !empty($args['check_name']) ? $args['check_name'] : 'NO_CHECK';
    268. // $api = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
    269. // return $this->sendWithPem($api, $args);
    270. // }
    271. /**
    272. *
    273. * 查询企业付款,
    274. *
    275. * @param array $args ['partner_trade_no']
    276. * @return array
    277. * @throws WechatException
    278. */
    279. public function getTransferInfo($args)
    280. {
    281. $args['appid'] = !empty($args['appid']) ? $args['appid'] : $this->appId;
    282. $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
    283. $api = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo';
    284. return $this->sendWithPem($api, $args);
    285. }
    286. /**
    287. *
    288. * 企业付款到银行卡,
    289. *
    290. * @param array $args
    291. * @return array
    292. * @throws WechatException
    293. */
    294. public function payBank($args)
    295. {
    296. $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
    297. $api = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank';
    298. return $this->sendWithPem($api, $args);
    299. }
    300. /**
    301. *
    302. * 查询企业付款到银行卡,
    303. *
    304. * @param array $args
    305. * @return array
    306. * @throws WechatException
    307. */
    308. public function queryBank($args)
    309. {
    310. $args['mch_id'] = !empty($args['mch_id']) ? $args['mch_id'] : $this->mchId;
    311. $api = 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank';
    312. return $this->sendWithPem($api, $args);
    313. }
    314. /**
    315. * 通过数组数据验证签名
    316. * @param array $array
    317. * @return bool
    318. */
    319. public function validateSignByArrayResult($array)
    320. {
    321. if (!isset($array['sign'])) {
    322. return false;
    323. }
    324. $inputSign = $array['sign'];
    325. $truthSign = $this->makeSign($array);
    326. return $inputSign === $truthSign;
    327. }
    328. /**
    329. * 通过XML数据验证签名
    330. * @param string $xml
    331. * @return bool
    332. */
    333. public function validateSignByXmlResult($xml)
    334. {
    335. $array = WechatHelper::xmlToArray($xml);
    336. return $this->validateSignByArrayResult($array);
    337. }
    338. /**
    339. * 数据签名
    340. * @param array $args
    341. * @param string $signType
    342. * @return string
    343. */
    344. public function makeSign($args, $signType = self::SIGN_TYPE_MD5)
    345. {
    346. if (isset($args['sign'])) {
    347. unset($args['sign']);
    348. }
    349. ksort($args);
    350. $string = '';
    351. foreach ($args as $i => $arg) {
    352. if ($arg === null || $arg === '') {
    353. continue;
    354. } else {
    355. $string .= ($i . '=' . $arg . '&');
    356. }
    357. }
    358. $string = $string . "key={$this->key}";
    359. $string = md5($string);
    360. $result = strtoupper($string);
    361. return $result;
    362. }
    363. }

  • 相关阅读:
    PodDisruptionBudget
    linux部署kafka并集成springboot
    网络编程常用的几种字符编码
    centos下Iptables的安装(离线)
    Python批量裁剪图片
    react-router v6使用createHashHistory进行history.push时,url改变页面不渲染
    `算法知识` 模意义下的乘法逆元
    【《On Java 8》学习之路——复用】知识点整理分享
    CH341/CH340Linux驱动使用教程
    常见锁策略与CAS介绍
  • 原文地址:https://blog.csdn.net/weixin_38984353/article/details/133738434