WechatService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. *
  4. * @author: lyh
  5. * @date: 2019/3/19
  6. */
  7. namespace App\Service;
  8. use App\Modes\ErrorLog;
  9. use App\Modes\Order;
  10. use Yansongda\Pay\Log;
  11. use Yansongda\Pay\Pay;
  12. class WechatService
  13. {
  14. protected $pay;
  15. public function __construct()
  16. {
  17. $this->pay = Pay::wechat(config('pay.wechat'));
  18. }
  19. public function getPayInfo($outTradeNo, $price, $subject = '')
  20. {
  21. $order = [
  22. 'out_trade_no' => $outTradeNo,
  23. 'total_fee' => $price * 100,
  24. 'body' => $subject
  25. ];
  26. $pay = $this->pay->app($order);
  27. //ErrorLog::saveMsg('wxpay请求支付参数回调', $pay->getContent(), 2);
  28. // app json 解码:
  29. // return json_decode($pay->getContent(), true);
  30. $paydata=json_decode($pay->getContent(),true);
  31. $paydata['order_no']=$outTradeNo;
  32. return $paydata;
  33. }
  34. public function getPayInfoh5($outTradeNo, $price, $subject = '')
  35. {
  36. $order = [
  37. 'out_trade_no' => $outTradeNo,
  38. 'total_fee' => $price * 100,
  39. 'body' => $subject
  40. ];
  41. $pay = $this->pay->wap($order);
  42. //ErrorLog::saveMsg('wxpay请求支付参数回调', $pay->getContent(), 2);
  43. // var_dump($pay);
  44. // app json 解码:
  45. // return json_decode($pay->getContent(), true);
  46. // $paydata=json_decode($pay->getContent(),true);
  47. // $paydata['order_no']=$outTradeNo;
  48. return $pay;
  49. }
  50. //扫码支付 addbuwsl 20190701
  51. public function getPcpayinfo($outTradeNo, $price, $subject = ''){
  52. $order = [
  53. 'out_trade_no' => $outTradeNo,
  54. 'total_fee' => $price * 100,
  55. 'body' => $subject
  56. ];
  57. $pay = $this->pay->scan($order);
  58. $pay['order_no']=$outTradeNo;
  59. return $pay;
  60. }
  61. //退款 editbywsl 20190628
  62. public function getReturnInfo($outTradeNo, $trade_no,$price,$refund_fee)
  63. {
  64. $order = [
  65. 'out_trade_no' => $outTradeNo,
  66. 'total_fee' => $price*100,
  67. 'out_refund_no' => $trade_no,
  68. 'refund_fee' => $refund_fee*100,
  69. //'notify_url' => $refund_fee,
  70. ];
  71. $wxrefund=$this->pay->refund($order);
  72. return $wxrefund;
  73. }
  74. /*
  75. * <xml><appid><![CDATA[wx8f83830966c66e91]]></appid>
  76. <bank_type><![CDATA[CFT]]></bank_type>
  77. <cash_fee><![CDATA[1]]></cash_fee>
  78. <fee_type><![CDATA[CNY]]></fee_type>
  79. <is_subscribe><![CDATA[N]]></is_subscribe>
  80. <mch_id><![CDATA[1531734301]]></mch_id>
  81. <nonce_str><![CDATA[raVSwwkeMI1qzT6W]]></nonce_str>
  82. <openid><![CDATA[oTo0h1K3GdoLInWt4ZpvBGxfDanM]]></openid>
  83. <out_trade_no><![CDATA[1561560731166279]]></out_trade_no>
  84. <result_code><![CDATA[SUCCESS]]></result_code>
  85. <return_code><![CDATA[SUCCESS]]></return_code>
  86. <sign><![CDATA[B9FFF31AE806D797C39BE40344F76955]]></sign>
  87. <time_end><![CDATA[20190626225217]]></time_end>
  88. <total_fee>1</total_fee>
  89. <trade_type><![CDATA[APP]]></trade_type>
  90. <transaction_id><![CDATA[4200000297201906262151954923]]></transaction_id>
  91. </xml>
  92. Array
  93. (
  94. [appid] => wx8f83830966c66e91
  95. [bank_type] => CFT
  96. [cash_fee] => 1
  97. [fee_type] => CNY
  98. [is_subscribe] => N
  99. [mch_id] => 1531734301
  100. [nonce_str] => FkQmKj3TZUXBACKp
  101. [openid] => oTo0h1K3GdoLInWt4ZpvBGxfDanM
  102. [out_trade_no] => 1561560634510459
  103. [result_code] => SUCCESS
  104. [return_code] => SUCCESS
  105. [sign] => DDEF3D6A5AD77E855FF34DB86F2EA19B
  106. [time_end] => 20190626225041
  107. [total_fee] => 1
  108. [trade_type] => APP
  109. [transaction_id] => 4200000307201906266583434239
  110. )
  111. *
  112. *
  113. * */
  114. public function notify($data)
  115. {
  116. try {
  117. //ErrorLog::saveMsg('Wechatdata', $data, 2);
  118. $res = $this->pay->verify($data); // 是的,验签就这么简单!
  119. //$this->logResult('微信res',$res,'wxres');
  120. $dataarr=$this->xml_to_array($data);
  121. if ($res && $dataarr['return_code']=='SUCCESS') {
  122. Order::verifyOrder($dataarr['out_trade_no'], $dataarr['total_fee']/100, 3,$dataarr['transaction_id']);
  123. } else {
  124. ErrorLog::saveMsg('Wechat后台异步通知yanqianshibai', $data, 3);
  125. }
  126. } catch (\Exception $e) {
  127. ErrorLog::saveMsg('wxpay后台异步通知异常', [
  128. 'msg' => $e->getMessage(),
  129. 'file' => $e->getFile(),
  130. 'code' => $e->getCode(),
  131. 'data' => $data
  132. ], 2);
  133. }
  134. }
  135. public function xml_to_array($xml){
  136. if(!$xml){
  137. return false;
  138. }
  139. //将XML转为array
  140. //禁止引用外部xml实体
  141. libxml_disable_entity_loader(true);
  142. $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  143. return $data;
  144. }
  145. function logResult($word = '',$var=array(),$filenames) {
  146. define('MROOT',dirname(__FILE__).'/');
  147. $output= strftime("%Y%m%d %H:%M:%S", time()) . "\n" ;
  148. $output .= $word."\n" ;
  149. if(!empty($var) && is_array($var)){
  150. $output .= print_r($var, true)."\n";
  151. }else $output.=$var;
  152. $output.="\n";
  153. $log_path=MROOT . "/paylog/";
  154. if(!is_dir($log_path)){
  155. @mkdir($log_path, 0777, true);
  156. }
  157. file_put_contents($log_path."alipay".date("Ymd-His").$filenames.".txt", $output, FILE_APPEND | LOCK_EX);
  158. }
  159. }