| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace App\Service;
- use App\Modes\ErrorLog;
- use App\Modes\Order;
- use App\Modes\PayLog;
- use Yansongda\Pay\Pay;
- use Yansongda\Pay\Log;
- use alipay\aop;
- use alipay\aop\request\AlipayTradeAppPayRequest;
- use alipay\aop\request\AlipayTradeRefundRequest;
- use ErrorException;
- use GuzzleHttp\Client;
- class AliPayService
- {
- protected $config = [];
- protected $pay;
- public function __construct()
- {
- $this->pay = Pay::alipay(config('pay.alipay'));
- }
- /**
- * 获取支付信息
- * @author lyh
- * @date 2019/3/22
- * @param string $outTradeNo 订单号
- * @param double $price 支付金额
- * @param string $subject 备注
- * @return \Symfony\Component\HttpFoundation\Response
- * @description
- */
- public function getPayInfo($outTradeNo, $price, $subject = '')
- {
- // $order = [
- // 'out_trade_no' => $outTradeNo,
- // 'total_amount' => $price,
- // 'subject' => $subject,
- // //'http_method' => 'GET'
- // ];
- // $alipay = $this->pay->wap($order);
- // $retData = $alipay->getContent();
- $client = new Client();
- $response = $client->request('GET',env('APP_URL').'/pay',[
- 'query'=>[
- 'op'=>'getPayInfo',
- 'outTradeNo'=>$outTradeNo,
- 'price'=>$price,
- 'subject'=>$subject,
- ]
- ]
- );
- $retData = $response->getBody()->getContents();
- ErrorLog::saveMsg('aliPay请求支付参数', $retData, 1);
- return $retData;
- }
- /*
- * pc支付信息
- * add bu wsl
- * 20190701
- * */
- public function getPcpayinfo($outTradeNo, $price, $subject = ''){
- $order = [
- 'out_trade_no' => $outTradeNo,
- 'total_amount' => $price,
- 'subject' => $subject,
- 'qr_pay_mode' => 4,
- 'qrcode_width' => 150,
- //'http_method' => 'GET'
- ];
- $alipay = $this->pay->web($order);
- //ErrorLog::saveMsg('aliPay请求支付参数', $alipay->getContent(), 1);
- return $alipay->getContent();
- }
- public function getReturnInfo($outTradeNo, $trade_no,$price)
- {
- // $order = [
- // 'out_trade_no' => $outTradeNo,
- // 'refund_amount' => $price,
- // 'trade_no' => $trade_no,
- // ];
- // $alirefund=$this->pay->refund($order);
- $client = new Client();
- $response = $client->request('GET',env('APP_URL').'/pay',[
- 'query'=>[
- 'op'=>'getReturnInfo',
- 'outTradeNo'=>$outTradeNo,
- 'price'=>$price,
- 'trade_no'=>$trade_no,
- ]
- ]
- );
- $alirefund = $response->getBody()->getContents();
- $resArr = json_decode($alirefund,true);
- if($resArr['code']!='10000'){
- throw new \ErrorException($resArr['msg']);
- }
- return json_decode($alirefund);
- }
- /**
- * [getPayInfo 获取支付信息,app原生支付]
- * @author lgs
- * @DateTime 2019-06-21T15:46:57+0800
- * @param [type] $outTradeNo [description]
- * @param [type] $price [description]
- * @param string $subject [description]
- * @return [type] [description]
- */
- public function getAppPayInfo($outTradeNo, $price, $subject = '')
- {
- // ErrorLog::saveMsg('aliPay请求支付参数222', $outTradeNo, 1);
- include(dirname(dirname(dirname(__FILE__))).'/public/alipay/AopSdk.php');
- include(dirname(dirname(dirname(__FILE__))).'/public/alipay/aop/AopClient.php');
- include(dirname(dirname(dirname(__FILE__))).'/public/alipay/aop/request/AlipayTradeAppPayRequest.php');
- //======================lzj 2021/4/23 注销========================//
- // $order = [
- // 'out_trade_no' => $outTradeNo,
- // 'total_amount' => $price,
- // 'subject' => $subject,
- // //'http_method' => 'GET'
- // ];
- // $alipay = $this->pay->app($order);
-
- // ErrorLog::saveMsg('aliPay请求支付参数', $alipay->getContent(), 1);
- //=================================================================//
- $config = config('pay.alipay');
- $aop = new aop\AopClient();
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $config["app_id"];
- $aop->rsaPrivateKey = $config['private_key'];
- $aop->format = "json";
- $aop->charset = "UTF-8";
- $aop->signType = "RSA2";
- $aop->alipayrsaPublicKey = $config['ali_public_key'];
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new AlipayTradeAppPayRequest();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = "{\"body\":\"点币购买支付\","
- . "\"subject\": \"{$subject}\","
- . "\"out_trade_no\": \"{$outTradeNo}\","
- . "\"timeout_express\": \"30m\","
- . "\"total_amount\": \"{$price}\","
- . "\"product_code\":\"QUICK_MSECURITY_PAY\""
- . "}";
- $request->setNotifyUrl($config['notify_url']);
- $request->setBizContent($bizcontent);
- //这里和普通的接口调用不同,使用的是sdkExecute
- $response = $aop->sdkExecute($request);
-
- //$this->logResult('alipayreplay:',$response,'response');
- ErrorLog::saveMsg('aliPay请求支付orderstring', $response, 1);
- //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
- //===============lzj 2021/4/23 注销 =================//
- // $alipay->response = htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
- // ===================================================//
- //echo $alipay->response ;exit;
- // return $alipay;
-
- return $response;
- }
- /**
- * h5 支付结果返回
- * 同步通知 示例:
- * http://nn19030510.vb.com/api/notify/return?charset=GBK&out_trade_no=1554970679424629&method=alipay.trade.wap.pay.return&total_amount=0.01&sign=LCrRfIJXlR2Zsg%2FCr3IlKiUFuLW3emGnLyQB4tjUTZn7zxKJ3sktsYT32EG%2BRG6xH1WrJvLRp%2FuNqvL5f%2Bwd6dG2QX2PgnyhSDS8F7lH5PG5MrK%2FkpD7vzVId4%2FdInZtLVn2EStWX7LRoXzrxAoweaGWL6HKun30Fk1yd80cYpd3N9N0%2Bf20kCSiekcsGozJgurvcUIAT%2BTfh2kYVRc0wXS8NDGJxOpqHowBTJm2gn0MdvsDPLoGYcfxyjBjKDtXFEA%2BpWHQfZgaS6vfyH97qeB4chG%2BmfxfYx1n1APSnTz2nZMyYo8B28uZE5IaWZrqjhbxtqitb4kw9gZdpAHLVg%3D%3D&trade_no=2019041122001444231025690858&auth_app_id=2019040263728819&version=1.0&app_id=2019040263728819&sign_type=RSA2&seller_id=2088431832595334×tamp=2019-04-11+16%3A18%3A07
- * $result 示例:
- * Collection {#582 ▼
- * #items: array:12 [▼
- * "charset" => "GBK"
- * "out_trade_no" => "1554970679424629"
- * "method" => "alipay.trade.wap.pay.return"
- * "total_amount" => "0.01"
- * "sign" => "LCrRfIJXlR2Zsg/Cr3IlKiUFuLW3emGnLyQB4tjUTZn7zxKJ3sktsYT32EG+RG6xH1WrJvLRp/uNqvL5f+wd6dG2QX2PgnyhSDS8F7lH5PG5MrK/kpD7vzVId4/dInZtLVn2EStWX7LRoXzrxAoweaGWL6HKun30 ▶"
- * "trade_no" => "2019041122001444231025690858"
- * "auth_app_id" => "2019040263728819"
- * "version" => "1.0"
- * "app_id" => "2019040263728819"
- * "sign_type" => "RSA2"
- * "seller_id" => "2088431832595334"
- * "timestamp" => "2019-04-11 16:18:07"
- * ]
- * }
- */
- public function return($data)
- {
- if (is_array($data)) {
- $insert = [];
- foreach ($data as $key => $item) {
- $insert[$key] = urldecode($item);
- }
- }
- $insert['type'] = 1;
- PayLog::insert($insert);
- try {
- ErrorLog::saveMsg('aliPay同步返回支付结果', $data, 1);
- $data = $this->pay->verify($data); // 是的,验签就这么简单!
- // dd($result->out_trade_no);
- // 订单号:$data->out_trade_no
- // 支付宝交易号:$data->trade_no
- // 订单总金额:$data->total_amount
- // 更具订单类型、跳转
- $order = Order::whereOrderNo($data->out_trade_no)->first();
- return $order;
- } catch (\Exception $e) {
- ErrorLog::saveMsg('aliPay后台同步通知异常', [
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'code' => $e->getCode(),
- 'data' => $data,
- ], 1);
- }
- }
- /**
- * 异步通知
- * @author lyh
- * @date 2019/3/22
- * @param array $data
- * {
- * "gmt_create": "2019-04-11+18%3A40%3A57",
- * "charset": "GBK",
- * "seller_email": "34696432%40qq.com",
- * "subject": "18978059931%B9%BA%C2%F2%B9%E3%B8%E6%CE%BB",
- * "sign": "arVZFl3Xxx%2F%2F3prTUu1ohBetSJNQtL0z5DEM6XqJbL4VYRlVjMabCeXTGbTfGUwMyjhIbRI6CNGrm4OBK7s41ss1N5FOElXk9yEKSEHXEIFAsFcGjKhBhsTexPuTudSNIUM%2F%2B%2B3TH9Ty9L%2FZPa2i8b%2FLbt3yDumcCnLyeAtmwAPMWmCcptSWGf24R5JTTKuJwnhC6Kw8GAyVA8HYpODajwYRCvedh0aDhIAdvyWGaGCaxHKbU%2FRz5NYr3J5ODXpFrhjMq4WS%2FD%2BK6wBBUQ9xHZiBI0FLEEt004vNYudkjPAQgioYmFscQuRPIEnlywaee3V%2BZCSxmR6N0G3dh1qgiQ%3D%3D",
- * "buyer_id": "2088702691344231",
- * "invoice_amount": "0.01",
- * "notify_id": "2019041100222184058044231003716600",
- * "fund_bill_list": "%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22ALIPAYACCOUNT%22%7D%5D",
- * "notify_type": "trade_status_sync",
- * "trade_status": "TRADE_SUCCESS",
- * "receipt_amount": "0.01",
- * "buyer_pay_amount": "0.01",
- * "app_id": "2019040263728819",
- * "sign_type": "RSA2",
- * "seller_id": "2088431832595334",
- * "gmt_payment": "2019-04-11+18%3A40%3A58",
- * "notify_time": "2019-04-11+18%3A43%3A18",
- * "version": "1.0",
- * "out_trade_no": "1554979252073513",
- * "total_amount": "0.01",
- * "trade_no": "2019041122001444231025848894",
- * "auth_app_id": "2019040263728819",
- * "buyer_logon_id": "151***%40139.com",
- * "point_amount": "0.00"
- * }
- * @return \Symfony\Component\HttpFoundation\Response
- * @description
- * 支付宝回复状态
- * 9000 订单支付成功
- * 8000 正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
- * 4000 订单支付失败
- * 5000 重复请求
- * 6001 用户中途取消
- * 6002 网络连接出错
- * 6004 支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态
- * 其它 其它支付错误
- *
- * // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
- * // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
- * // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
- * // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
- * // 4、验证app_id是否为该商户本身。
- * // 5、其它业务逻辑情况
- *{"msg":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'body' in 'field list' (SQL: insert into `zx_pay_log` (`gmt_create`, `charset`, `seller_email`, `subject`, `sign`, `body`, `buyer_id`, `invoice_amount`, `notify_id`, `fund_bill_list`, `notify_type`, `trade_status`, `receipt_amount`, `app_id`, `buyer_pay_amount`, `sign_type`, `seller_id`, `gmt_payment`, `notify_time`, `version`, `out_trade_no`, `total_amount`, `trade_no`, `auth_app_id`, `buyer_logon_id`, `point_amount`, `type`) values (2019-06-25 16:23:49, UTF-8, 34696432@qq.com, 18928798343SmztlVtqrZF+Fy9mT\/h4PN48a\/rv8BaGPlXnIxwmEGIJPRfVypVlpKuyB9MMWVjgc8xjljLmKqzB\/JEoM7yNaL5sdoopjZZEa2M0uCAoNuCJUq6Rl9D456VYfBDIMLthBlfw2C0FlOLI\/wEzXqgdHLOhHDXOlv1ssRklc+QUIGVCC343VEO5uqLHs+xREB42uWiYRDm5PHaIMied6HMfDfOcIm+twiyS24XOfpNb+LAN36ah02cV\/YRGwXHqBkH4fN66C\/DKsxYaxjMv5TRonAeQp5bv9u376aL+HhwoD07oPaTeRDDpG3ez3951Q2s1gQqLP+uOy+vuj9Jai34JAw==\u74702088802534445384\u74700.01\u74902019062500222162349045380558162099\u62f1[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]trade_status_syncTRADE_SUCCESS\u74900.01\u62f1, 2019040263728819, 0.01, RSA2, 2088431832595334, 2019-06-25 16:23:49, 2019-06-25 16:23:50, 1.0, 1561451016259457, 0.01, 2019062522001445380512216373, 2019040263728819, 189****8343, 0.00, 2, ?, ?, ?, ?, ?, ?, ?, ?, ?))","file":"\/www\/wwwroot\/10dsm\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":664,"code":"42S22","data":{"gmt_create":"2019-06-25 16:23:49","charset":"UTF-8","seller_email":"34696432@qq.com","subject":"18928798343?\u7470?\u7490?\u62f1","sign":"SmztlVtqrZF+Fy9mT\/h4PN48a\/rv8BaGPlXnIxwmEGIJPRfVypVlpKuyB9MMWVjgc8xjljLmKqzB\/JEoM7yNaL5sdoopjZZEa2M0uCAoNuCJUq6Rl9D456VYfBDIMLthBlfw2C0FlOLI\/wEzXqgdHLOhHDXOlv1ssRklc+QUIGVCC343VEO5uqLHs+xREB42uWiYRDm5PHaIMied6HMfDfOcIm+twiyS24XOfpNb+LAN36ah02cV\/YRGwXHqBkH4fN66C\/DKsxYaxjMv5TRonAeQp5bv9u376aL+HhwoD07oPaTeRDDpG3ez3951Q2s1gQqLP+uOy+vuj9Jai34JAw==","body":"?\u7470?\u7490?\u62f1???","buyer_id":"2088802534445384","invoice_amount":"0.01","notify_id":"2019062500222162349045380558162099","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2019040263728819","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088431832595334","gmt_payment":"2019-06-25 16:23:49","notify_time":"2019-06-25 16:23:50","version":"1.0","out_trade_no":"1561451016259457","total_amount":"0.01","trade_no":"2019062522001445380512216373","auth_app_id":"2019040263728819","buyer_logon_id":"189****8343","point_amount":"0.00"}}
- */
- public function notify($data)
- {
- try {
- $insert = $data;
- $insert['type'] = 2;
- if(isset($insert['body'])){
- unset($insert['body']);
- }
- PayLog::insert($insert);
- //$this->pay->verify($data); // 是的,验签就这么简单!先不验签吧
- $order = Order::whereOrderNo($data['out_trade_no'])->first();
- //$this->logResult('alipaynotify:',$data,'notify');
- if ($data['trade_status'] == 'TRADE_SUCCESS' && $data['total_amount']==$order['price'] && $order['is_pay']==0) {
- Order::verifyOrder($data['out_trade_no'], $data['total_amount'], 2,$data['trade_no']);
- }
- } catch (\Exception $e) {
- ErrorLog::saveMsg('aliPay后台异步通知异常', [
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'code' => $e->getCode(),
- 'data' => $data,
- ], 1);
- }
- return $this->pay->success();
- }
-
- 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)){
- $output .= print_r($var, true)."\n";
- }
- $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);
- }
- }
|