PayController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-02-17
  10. // +—————————————————————————————————————————————————————————————————————
  11. namespace app\appapi\controller;
  12. use cmf\controller\HomeBaseController;
  13. use think\Db;
  14. /**
  15. * 支付回调
  16. */
  17. class PayController extends HomebaseController {
  18. //支付宝 回调
  19. public function notify_ali() {
  20. $configpri=getConfigPri();
  21. require_once(CMF_ROOT."sdk/alipay_app/alipay.config.php");
  22. $alipay_config['partner'] = $configpri['aliapp_partner'];
  23. require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_core.function.php");
  24. require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_rsa.function.php");
  25. require_once(CMF_ROOT."sdk/alipay_app/lib/alipay_notify.class.php");
  26. //计算得出通知验证结果
  27. $alipayNotify = new \AlipayNotify($alipay_config);
  28. $verify_result = $alipayNotify->verifyNotify();
  29. $this->logali("ali_data:".json_encode($_POST));
  30. if($verify_result) {//验证成功
  31. //商户订单号
  32. $out_trade_no = $_POST['out_trade_no'];
  33. //支付宝交易号
  34. $trade_no = $_POST['trade_no'];
  35. //交易状态
  36. $trade_status = $_POST['trade_status'];
  37. //交易金额
  38. $total_fee = $_POST['total_fee'];
  39. if($_POST['trade_status'] == 'TRADE_FINISHED') {
  40. }else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
  41. $where['orderno']=$out_trade_no;
  42. $where['money']=$total_fee;
  43. $where['type']=1;
  44. $data=[
  45. 'trade_no'=>$trade_no
  46. ];
  47. $this->logali("where:".json_encode($where));
  48. $res=handelCharge($where,$data);
  49. if($res==0){
  50. $this->logali("orderno:".$out_trade_no.' 订单信息不存在');
  51. echo "fail";
  52. exit;
  53. }
  54. $this->logali("成功");
  55. echo "success"; //请不要修改或删除
  56. exit;
  57. }
  58. echo "fail"; //请不要修改或删除
  59. exit;
  60. }else {
  61. $this->logali("验证失败");
  62. //验证失败
  63. echo "fail";
  64. exit;
  65. }
  66. }
  67. /* 微信支付 */
  68. private $wxDate = null;
  69. public function notify_wx(){
  70. $config=getConfigPri();
  71. //$xmlInfo = $GLOBALS['HTTP_RAW_POST_DATA'];
  72. $xmlInfo=file_get_contents("php://input");
  73. //解析xml
  74. $arrayInfo = $this -> xmlToArray($xmlInfo);
  75. $this -> wxDate = $arrayInfo;
  76. $this -> logwx("wx_data:".json_encode($arrayInfo));//log打印保存
  77. if($arrayInfo['return_code'] == "SUCCESS"){
  78. $wxSign = $arrayInfo['sign'];
  79. unset($arrayInfo['sign']);
  80. $arrayInfo['appid'] = $config['wx_appid'];
  81. $arrayInfo['mch_id'] = $config['wx_mchid'];
  82. $key = $config['wx_key'];
  83. ksort($arrayInfo);//按照字典排序参数数组
  84. $sign = $this -> sign($arrayInfo,$key);//生成签名
  85. $this -> logwx("数据打印测试签名signmy:".$sign.":::微信sign:".$wxSign);//log打印保存
  86. if($this -> checkSign($wxSign,$sign)){
  87. echo $this -> returnInfo("SUCCESS","OK");
  88. $this -> logwx("签名验证结果成功:".$sign);//log打印保存
  89. $this -> orderServer();//订单处理业务逻辑
  90. exit;
  91. }else{
  92. echo $this -> returnInfo("FAIL","签名失败");
  93. $this -> logwx("签名验证结果失败:本地加密:".$sign.':::::三方加密'.$wxSign);//log打印保存
  94. exit;
  95. }
  96. //}
  97. }else{
  98. echo $this -> returnInfo("FAIL","签名失败");
  99. $this -> logwx($arrayInfo['return_code']);//log打印保存
  100. exit;
  101. }
  102. }
  103. private function returnInfo($type,$msg){
  104. if($type == "SUCCESS"){
  105. return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code></xml>";
  106. }else{
  107. return $returnXml = "<xml><return_code><![CDATA[{$type}]]></return_code><return_msg><![CDATA[{$msg}]]></return_msg></xml>";
  108. }
  109. }
  110. //签名验证
  111. private function checkSign($sign1,$sign2){
  112. return trim($sign1) == trim($sign2);
  113. }
  114. /* 订单查询加值业务处理
  115. * @param orderNum 订单号
  116. */
  117. private function orderServer(){
  118. $info = $this -> wxDate;
  119. $this->logwx("info:".json_encode($info));
  120. $where['orderno']=$info['out_trade_no'];
  121. $where['type']=2;
  122. $trade_no=$info['transaction_id'];
  123. $data=[
  124. 'trade_no'=>$trade_no
  125. ];
  126. $this->logwx("where:".json_encode($where));
  127. $res=handelCharge($where,$data);
  128. if($res==0){
  129. $this->logwx("orderno:".$out_trade_no.' 订单信息不存在');
  130. return false;
  131. }
  132. $this->logwx("成功");
  133. return true;
  134. }
  135. /**
  136. * sign拼装获取
  137. */
  138. private function sign($param,$key){
  139. $sign = "";
  140. foreach($param as $k => $v){
  141. $sign .= $k."=".$v."&";
  142. }
  143. $sign .= "key=".$key;
  144. $sign = strtoupper(md5($sign));
  145. return $sign;
  146. }
  147. /**
  148. * xml转为数组
  149. */
  150. private function xmlToArray($xmlStr){
  151. $msg = array();
  152. $postStr = $xmlStr;
  153. $msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  154. return $msg;
  155. }
  156. /* 微信支付 */
  157. /* 打印log */
  158. public function logali($msg){
  159. file_put_contents(CMF_ROOT.'data/paylog/logali_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND);
  160. }
  161. /* 打印log */
  162. public function logwx($msg){
  163. file_put_contents(CMF_ROOT.'data/paylog/logwx_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').' msg:'.$msg."\r\n",FILE_APPEND);
  164. }
  165. }