Pay.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\library\AliPay3;
  16. use think\cache\driver\Redis;
  17. use EasyWeChat\Factory;
  18. use think\Controller;
  19. use think\Db;
  20. use think\Exception;
  21. use AlibabaCloud\Client\AlibabaCloud;
  22. use AlibabaCloud\Client\Exception\ClientException;
  23. use AlibabaCloud\Client\Exception\ServerException;
  24. use function AlibabaCloud\Client\value;
  25. /**
  26. * 支付管理类
  27. * Class Refund
  28. * @package app\api\controller\Refund
  29. */
  30. class Pay extends Controller
  31. {
  32. public function test(){
  33. $data = "{\"memberid\":220431411,\"orderid\":\"220431411OR29DAEDEEACBBF11820220602161239\",\"amount\":\"0.1000\",\"successdate\":\"2022-06-02 16:13:24\",\"bankcode\":\"953\",\"status\":\"success\",\"attach\":\"100000\",\"buyer_id\":\"2088012861214283\",\"out_trade_id\":\"202206021612394516457329\",\"notifydate\":\"2022-06-02 16:13:24\",\"num\":1,\"sign\":\"BC0568D0BA0E38DCB3246790C31570BA\"}";
  34. $result= json_decode($data,true);
  35. if ($result['status'] == 'success') {
  36. $result['out_trade_no'] = $result['out_trade_id'];
  37. $return = $this->dealData($result);
  38. echo $return;
  39. }
  40. }
  41. /**
  42. * 微信支付--商品支付成功回调订单
  43. */
  44. public function WxOrderNotify(){
  45. $payXml = file_get_contents("php://input");
  46. //将xml格式转化为json格式
  47. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  48. //将json格式转成数组格式 $result['out_trade_no']
  49. $result = json_decode($jsonXml, true);
  50. file_put_contents("wx_pay_return.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  51. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  52. $return = $this->dealData($result);
  53. if ($return){
  54. $arr = array(
  55. 'return_code' => 'SUCCESS',
  56. 'return_msg' => 'OK',
  57. );
  58. return $this->arrayToXml($arr);
  59. }else{
  60. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  61. }
  62. }
  63. }
  64. // /**
  65. // * 支付宝支付--支付成功回调订单
  66. // */
  67. // public function alipayOrderNotify(){
  68. // $result = input('post.');
  69. // file_put_contents("zfb_pay_return.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  70. // if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  71. // $return = $this->dealData($result);
  72. // if ($return){
  73. // echo 'success';
  74. // }else{
  75. // file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  76. // }
  77. // }
  78. // }
  79. /**
  80. * 支付宝支付--支付成功回调订单
  81. */
  82. public function alipayOrderNotify(){
  83. $results = input('post.');
  84. $zfb = new AliPay3();
  85. $result= json_decode($zfb->sslDeReturn($results['notifyData']),true);
  86. file_put_contents("zfb_pay_return.txt", json_encode($result,true) . "\n" . "\n", FILE_APPEND);
  87. if ($result['status'] == 'success')
  88. {
  89. $result['out_trade_no'] = $result['out_trade_id'];
  90. $return = $this->dealData($result);
  91. if ($return){
  92. echo 'success';
  93. }else{
  94. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  95. }
  96. }
  97. }
  98. /**
  99. * 汇聚支付支付宝h5,银联h5
  100. */
  101. public function ylOrderNotify(){
  102. $result = input();
  103. debug($result);
  104. if ($result['r6_Status'] == '100'){
  105. $result['out_trade_no'] = $result['r2_OrderNo'];
  106. $result['attach'] = $result['r5_Mp'];
  107. $return = $this->dealData($result);
  108. if ($return){
  109. echo 'success';
  110. }else{
  111. file_put_contents("order_alipay_error.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  112. }
  113. }
  114. }
  115. /**
  116. * 处理数据库信息
  117. * @param $result
  118. * @return bool
  119. */
  120. function dealData($result){
  121. Db::startTrans();
  122. $redis = new Redis();
  123. $key = 'order_not_pay_'.$result['attach'];
  124. $com = true;
  125. try {
  126. $order = $redis->hGet($key,$result['out_trade_no']);
  127. if (!$order) return false;
  128. $order = json_decode($order,true);
  129. $is_order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
  130. if (isset($is_order) && $is_order['status']==1) return false;
  131. $order['status'] = 1;
  132. $order['pay_at'] = date('Y-m-d H:i:s');
  133. $order['return_success_info'] = json_encode($result,true);
  134. $order_id = Db::name('store_order')->insertGetId($order);
  135. $array = [];
  136. $pro_info = json_decode($order['pro_info'],true);
  137. $is_nft = Db::name('hash2')->where('goods_id',$order['c_id'])->where('success',1)->find();
  138. for ($i=0;$i<$order['num'];$i++){
  139. //获取排名
  140. $rank = getRanking($order['c_id'])+1;
  141. $tag = getTag($order['c_id'],$rank,$order['inventory']);
  142. saveRanking($order['c_id']);
  143. $collectors_hash = '';
  144. $date = [
  145. 'order_id'=>$order_id,
  146. 'order_no'=>get_order_sn(),
  147. 'tag'=>$tag,
  148. 'mid'=>$order['mid'],
  149. 'c_id'=>$order['c_id'],
  150. 'name'=>$pro_info['name'],
  151. 'cover'=>$pro_info['cover'],
  152. 'pro_info'=>$order['pro_info'],
  153. 'type'=>$pro_info['type'],
  154. 'tokenid'=>$is_nft['class_id'],
  155. 'nfttype'=>$is_nft['operationId'],
  156. 'collectors_hash'=>$collectors_hash
  157. ];
  158. $array[] = $date;
  159. }
  160. Db::name('store_order_info')->insertAll($array);
  161. //送积分
  162. // $by_collection_integral = getConfigValue('by_collection_integral');
  163. // if ($by_collection_integral){
  164. // $by_collection_integral = bcmul($by_collection_integral,$order['num']);
  165. // memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']);
  166. // }
  167. Db::commit();
  168. } catch (\Exception $e){
  169. $com = false;
  170. Db::rollback();
  171. }
  172. if ($com){
  173. $redis->hdel($key,$result['out_trade_no']);
  174. return true;
  175. }else{
  176. return false;
  177. }
  178. }
  179. /**
  180. * 微信支付--充值盲盒成功回调订单
  181. */
  182. public function BlindRechargeNotify(){
  183. $payXml = file_get_contents("php://input");
  184. //将xml格式转化为json格式
  185. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  186. //将json格式转成数组格式 $result['out_trade_no']
  187. $result = json_decode($jsonXml, true);
  188. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  189. $return = $this->dealBox($result);
  190. if ($return){
  191. $arr = array(
  192. 'return_code' => 'SUCCESS',
  193. 'return_msg' => 'OK',
  194. );
  195. return $this->arrayToXml($arr);
  196. }else{
  197. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  198. }
  199. }
  200. }
  201. /**
  202. * 支付宝支付--充值盲盒回调订单
  203. */
  204. public function alipayBlindRechargeNotify(){
  205. $result = input('post.');
  206. if ($result['trade_status'] == 'TRADE_SUCCESS' || $result['trade_status'] == 'TRADE_FINISHED') {
  207. $return = $this->dealBox($result);
  208. if ($return){
  209. echo 'success';
  210. }else{
  211. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  212. }
  213. }
  214. }
  215. /**
  216. * 盲盒充值次数处理数据库信息
  217. * @param $result
  218. * @return bool
  219. */
  220. function dealBox($result){
  221. $com = true;
  222. Db::startTrans();
  223. try {
  224. $order = Db::name('store_blind_recharge')->where('order_no',$result['out_trade_no'])->find();
  225. if ($order['status']==1) return true;
  226. Db::name('store_blind_recharge')
  227. ->where('order_no',$result['out_trade_no'])
  228. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  229. //用户加次数
  230. Db::name('store_member')->where('id',$order['m_id'])->setInc('lottery_number',$order['num']);
  231. Db::commit();
  232. } catch (\Exception $e){
  233. $com = false;
  234. Db::rollback();
  235. }
  236. if ($com){
  237. setMemberInfoHash($order['m_id']);
  238. return true;
  239. }else{
  240. return false;
  241. }
  242. }
  243. /**
  244. * 微信支付--二级市场回调订单
  245. */
  246. public function SecondaryWxOrderNotify(){
  247. $payXml = file_get_contents("php://input");
  248. //将xml格式转化为json格式
  249. $jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
  250. //将json格式转成数组格式 $result['out_trade_no']
  251. $result = json_decode($jsonXml, true);
  252. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  253. $return = $this->dealSecondary($result);
  254. if ($return){
  255. $arr = array(
  256. 'return_code' => 'SUCCESS',
  257. 'return_msg' => 'OK',
  258. );
  259. return $this->arrayToXml($arr);
  260. }else{
  261. file_put_contents("order_pay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  262. }
  263. }
  264. }
  265. /**
  266. * 支付宝支付--二级市场回调订单
  267. */
  268. public function alipaySecondaryNotify(){
  269. $results = input('post.');
  270. $zfb = new AliPay3();
  271. $result= json_decode($zfb->sslDeReturn($results['notifyData']),true);
  272. file_put_contents("zfbse_pay_return.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  273. if ($result['status'] == 'success') {
  274. $result['out_trade_no'] = $result['out_trade_id'];
  275. $return = $this->dealSecondary($result);
  276. if ($return){
  277. echo 'success';
  278. }else{
  279. file_put_contents("order_alipay_error.txt", file_get_contents("php://input") . "\n" . json_encode($result) . "\n" . "\n", FILE_APPEND);
  280. }
  281. }
  282. }
  283. /**
  284. * 杉德支付--二级市场回调订单
  285. */
  286. public function shandeSecondaryNotify(){
  287. $data = stripslashes($_POST['data']); //支付数据
  288. file_put_contents("shandepay.txt", $data . "\n" , FILE_APPEND);
  289. $data = json_decode($data,true);
  290. if ($data['body']['orderStatus']==1){
  291. //$data['body']['orderCode']
  292. $return = $this->dealSecondary($data,'sd');
  293. if ($return){
  294. return 'respCode=000000';
  295. }else{
  296. file_put_contents("shandepay_error.txt", $data . "\n" . json_encode($data) . "\n" . "\n", FILE_APPEND);
  297. }
  298. }
  299. }
  300. /**
  301. * 二级市场-汇聚支付支付宝h5,银联h5
  302. */
  303. public function ylSecondaryNotify(){
  304. $result = input();
  305. debug($result);
  306. if ($result['r6_Status'] == '100'){
  307. $return = $this->dealSecondary($result,'huijuh5');
  308. if ($return){
  309. echo 'success';
  310. }else{
  311. file_put_contents("order_error.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  312. }
  313. }
  314. }
  315. /**
  316. * 二级市场处理数据库信息
  317. * @param $result
  318. * @return bool
  319. */
  320. function dealSecondary($result,$from=''){
  321. $com = true;
  322. Db::startTrans();
  323. try {
  324. if ($from=='sd'){
  325. $out_trade_no = $result['body']['orderCode'];
  326. }elseif ($from=='sd'){
  327. $out_trade_no = $result['out_trade_no'];
  328. } elseif($from=='huijuh5'){
  329. $out_trade_no = $result['r2_OrderNo'];;
  330. }
  331. $order = Db::name('store_order_info_order')->where('order_no',$out_trade_no)->find();
  332. if ($order['status']==1) return false;
  333. if (!$order) return false;
  334. $resale_status = Db::name('store_order_info')->where('id',$order['info_id'])->value('resale_status');
  335. if ($resale_status!=2){
  336. }else{
  337. Db::name('store_order_info_order')
  338. ->where('order_no',$out_trade_no)
  339. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  340. //藏品修改状态
  341. Db::name('store_order_info')->where('id',$order['info_id'])->update([
  342. 'status'=>2,
  343. 'resale_status'=>3,
  344. 'selling_time'=>date('Y-m-d H:i:s')
  345. ]);
  346. $info = Db::name('store_order_info')->where('id',$order['info_id'])->find();
  347. //增加一条记录
  348. $to_date = [
  349. 'order_id'=>$info['order_id'],
  350. 'order_no'=>get_order_sn(),
  351. 'tag'=>$info['tag'],
  352. 'mid'=>$order['mid'],
  353. 'c_id'=>$info['c_id'],
  354. 'name'=>$info['name'],
  355. 'cover'=>$info['cover'],
  356. 'pro_info'=>$info['pro_info'],
  357. 'type'=>$info['type'],
  358. 'status'=>3,
  359. 'from'=>2,
  360. 'to_mid'=>$info['mid'],
  361. 'over_time'=>date('Y-m-d H:i:s'),
  362. 'tokenid'=>$info['tokenid'],
  363. 'nfttype'=>$info['nfttype'],
  364. 'collectors_hash'=>'',
  365. 'collectors_hash_time'=>date('Y-m-d H:i:s')
  366. ];
  367. Db::name('store_order_info')->insert($to_date);
  368. //增加用户余额
  369. memberMoneyChange($order['to_account'],3,$info['mid'],'出售藏品',1,$order['id']);
  370. Db::commit();
  371. }
  372. } catch (\Exception $e){
  373. $com = false;
  374. Db::rollback();
  375. }
  376. if ($com){
  377. setMemberInfoHash($order['mid']);
  378. return true;
  379. }else{
  380. return false;
  381. }
  382. }
  383. /**
  384. * 余额充值--汇聚云闪付,支付宝h5支付回调
  385. */
  386. public function ylrechargeNotify(){
  387. $result = input();
  388. debug($result);
  389. if ($result['r6_Status'] == '100'){
  390. $return = $this->dealRecharge($result,'huijuh5');
  391. if ($return){
  392. echo 'success';
  393. }else{
  394. file_put_contents("recharge_error.txt", json_encode($result) . "\n" . "\n", FILE_APPEND);
  395. }
  396. }
  397. }
  398. //处理充值
  399. function dealRecharge($result,$from=''){
  400. $com = true;
  401. Db::startTrans();
  402. try {
  403. if($from=='huijuh5'){
  404. $out_trade_no = $result['r2_OrderNo'];;
  405. }
  406. $order = Db::name('store_member_recharge')->where('order_no',$out_trade_no)->find();
  407. if (!$order || $order['status']==1) return false;
  408. //增加用户余额
  409. $re = memberMoneyChange($order['pay_price'],3,$order['m_id'],'余额充值',1,$order['id']);
  410. if ($re){
  411. Db::name('store_member_recharge')
  412. ->where('order_no',$out_trade_no)
  413. ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
  414. Db::commit();
  415. }else{
  416. $com = false;
  417. Db::rollback();
  418. }
  419. } catch (\Exception $e){
  420. $com = false;
  421. Db::rollback();
  422. }
  423. if ($com){
  424. setMemberInfoHash($order['m_id']);
  425. return true;
  426. }else{
  427. return false;
  428. }
  429. }
  430. /**
  431. * 数组转xml
  432. * @ApiInternal
  433. */
  434. public function arrayToXml($arr)
  435. {
  436. $xml = "<xml>";
  437. foreach ($arr as $key => $val) {
  438. if (is_numeric($val)) {
  439. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  440. } else
  441. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  442. }
  443. $xml .= "</xml>";
  444. return $xml;
  445. }
  446. }