| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- *
- * @author: lyh
- * @date: 2019/3/19
- */
- namespace App\Service;
- use App\Modes\ErrorLog;
- use App\Modes\Order;
- use Yansongda\Pay\Log;
- use Yansongda\Pay\Pay;
- class WechatService
- {
- protected $pay;
- public function __construct()
- {
- $this->pay = Pay::wechat(config('pay.wechat'));
- }
- public function getPayInfo($outTradeNo, $price, $subject = '')
- {
- $order = [
- 'out_trade_no' => $outTradeNo,
- 'total_fee' => $price * 100,
- 'body' => $subject
- ];
- $pay = $this->pay->app($order);
- //ErrorLog::saveMsg('wxpay请求支付参数回调', $pay->getContent(), 2);
- // app json 解码:
- // return json_decode($pay->getContent(), true);
- $paydata=json_decode($pay->getContent(),true);
- $paydata['order_no']=$outTradeNo;
- return $paydata;
- }
- public function getPayInfoh5($outTradeNo, $price, $subject = '')
- {
- $order = [
- 'out_trade_no' => $outTradeNo,
- 'total_fee' => $price * 100,
- 'body' => $subject
- ];
- $pay = $this->pay->wap($order);
- //ErrorLog::saveMsg('wxpay请求支付参数回调', $pay->getContent(), 2);
- // var_dump($pay);
- // app json 解码:
- // return json_decode($pay->getContent(), true);
- // $paydata=json_decode($pay->getContent(),true);
- // $paydata['order_no']=$outTradeNo;
- return $pay;
- }
- //扫码支付 addbuwsl 20190701
- public function getPcpayinfo($outTradeNo, $price, $subject = ''){
- $order = [
- 'out_trade_no' => $outTradeNo,
- 'total_fee' => $price * 100,
- 'body' => $subject
- ];
- $pay = $this->pay->scan($order);
- $pay['order_no']=$outTradeNo;
- return $pay;
- }
- //退款 editbywsl 20190628
- public function getReturnInfo($outTradeNo, $trade_no,$price,$refund_fee)
- {
- $order = [
- 'out_trade_no' => $outTradeNo,
- 'total_fee' => $price*100,
- 'out_refund_no' => $trade_no,
- 'refund_fee' => $refund_fee*100,
- //'notify_url' => $refund_fee,
- ];
- $wxrefund=$this->pay->refund($order);
- return $wxrefund;
- }
- /*
- * <xml><appid><![CDATA[wx8f83830966c66e91]]></appid>
- <bank_type><![CDATA[CFT]]></bank_type>
- <cash_fee><![CDATA[1]]></cash_fee>
- <fee_type><![CDATA[CNY]]></fee_type>
- <is_subscribe><![CDATA[N]]></is_subscribe>
- <mch_id><![CDATA[1531734301]]></mch_id>
- <nonce_str><![CDATA[raVSwwkeMI1qzT6W]]></nonce_str>
- <openid><![CDATA[oTo0h1K3GdoLInWt4ZpvBGxfDanM]]></openid>
- <out_trade_no><![CDATA[1561560731166279]]></out_trade_no>
- <result_code><![CDATA[SUCCESS]]></result_code>
- <return_code><![CDATA[SUCCESS]]></return_code>
- <sign><![CDATA[B9FFF31AE806D797C39BE40344F76955]]></sign>
- <time_end><![CDATA[20190626225217]]></time_end>
- <total_fee>1</total_fee>
- <trade_type><![CDATA[APP]]></trade_type>
- <transaction_id><![CDATA[4200000297201906262151954923]]></transaction_id>
- </xml>
- Array
- (
- [appid] => wx8f83830966c66e91
- [bank_type] => CFT
- [cash_fee] => 1
- [fee_type] => CNY
- [is_subscribe] => N
- [mch_id] => 1531734301
- [nonce_str] => FkQmKj3TZUXBACKp
- [openid] => oTo0h1K3GdoLInWt4ZpvBGxfDanM
- [out_trade_no] => 1561560634510459
- [result_code] => SUCCESS
- [return_code] => SUCCESS
- [sign] => DDEF3D6A5AD77E855FF34DB86F2EA19B
- [time_end] => 20190626225041
- [total_fee] => 1
- [trade_type] => APP
- [transaction_id] => 4200000307201906266583434239
- )
- *
- *
- * */
- public function notify($data)
- {
- try {
- //ErrorLog::saveMsg('Wechatdata', $data, 2);
- $res = $this->pay->verify($data); // 是的,验签就这么简单!
- //$this->logResult('微信res',$res,'wxres');
- $dataarr=$this->xml_to_array($data);
- if ($res && $dataarr['return_code']=='SUCCESS') {
- Order::verifyOrder($dataarr['out_trade_no'], $dataarr['total_fee']/100, 3,$dataarr['transaction_id']);
- } else {
- ErrorLog::saveMsg('Wechat后台异步通知yanqianshibai', $data, 3);
- }
- } catch (\Exception $e) {
- ErrorLog::saveMsg('wxpay后台异步通知异常', [
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'code' => $e->getCode(),
- 'data' => $data
- ], 2);
- }
- }
- public function xml_to_array($xml){
- if(!$xml){
- return false;
- }
- //将XML转为array
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $data;
- }
- function logResult($word = '',$var=array(),$filenames) {
- define('MROOT',dirname(__FILE__).'/');
- $output= strftime("%Y%m%d %H:%M:%S", time()) . "\n" ;
- $output .= $word."\n" ;
- if(!empty($var) && is_array($var)){
- $output .= print_r($var, true)."\n";
- }else $output.=$var;
- $output.="\n";
- $log_path=MROOT . "/paylog/";
- if(!is_dir($log_path)){
- @mkdir($log_path, 0777, true);
- }
- file_put_contents($log_path."alipay".date("Ymd-His").$filenames.".txt", $output, FILE_APPEND | LOCK_EX);
- }
- }
|