MiniSubController.php 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\FinishAccount;
  4. use App\Handlers\SignHandler;
  5. use App\Models\AddressW;
  6. use App\Models\CityW;
  7. use App\Models\IntegralW;
  8. use App\Models\TradeBillW;
  9. use App\Models\EcommerceBillW;
  10. use App\Models\ProfitSharingW;
  11. use App\Models\OrderRefundMoneyW;
  12. use App\Models\OrderW;
  13. use App\Models\Store;
  14. use App\Models\Apply;
  15. use App\Models\Bank;
  16. use App\Models\Amount;
  17. use App\Models\User;
  18. use GuzzleHttp\Client;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\DB;
  21. use Storage;
  22. use Illuminate\Support\Facades\Auth;
  23. use Illuminate\Support\Facades\Log;
  24. class MiniSubController extends Controller
  25. {
  26. //申请小微商户
  27. public function apply_wx(Request $request){
  28. $store=Store::where('user_id',Auth::user()->id)->first();
  29. if(empty($store)){
  30. return $this->error('450001','该店铺不能申请二级商户');
  31. }
  32. $out_request_no=$store->out_request_no??date('ymdHis').mt_rand(10000,99999);
  33. $contact_name=$request->input('contact_name');
  34. $contact_phone=$request->input('contact_phone');
  35. $contact_id_card=$request->input('contact_id_card');//"412326199612220311"
  36. $store_name=$request->input('store_name')??$store->name;
  37. if(preg_match('/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z]+/u',$store_name)){
  38. return $this->error('450002','店铺名称中不能使用特殊符号,请前往公众号—代理个人中心—我的,点击“名片”,修改昵称。');
  39. }
  40. if(mb_strlen($store_name)>10){
  41. return $this->error('450002','店铺名称不能超过10个字符,请前往公众号—代理个人中心—我的,点击“名片”,修改昵称。');
  42. }
  43. // $store_name=$this->filterEmoji($request->input('store_name'))??$this->filterEmoji($store->name);
  44. // $store_name=preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z]+/u", '', $store_name);//^0-9^A-Z^a-z
  45. // $store_name=mb_substr($store_name,0,16);
  46. // if(mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z]+/u", '', $store_name))==0){
  47. // return $this->error('450001','该店铺名称格式不符需重置');
  48. // }
  49. $store_url=$request->input('store_url')??"http://api.app.jiuweiyun.cn/api/gzh";
  50. $id_card_copy=$request->input('id_card_copy');
  51. $id_card_national=$request->input('id_card_national');
  52. $id_card_name=$request->input('id_card_name');
  53. $id_card_number=$request->input('id_card_number');
  54. $id_card_valid_time_begin=$request->input('id_card_valid_time_begin');//$request->input('id_card_valid_time');//"2027-09-07"
  55. $id_card_valid_time=$request->input('id_card_valid_time');//$request->input('id_card_valid_time');//"2027-09-07"
  56. $account_name=$request->input('account_name');//开户人姓名
  57. $account_bank=$request->input('account_bank');//银行
  58. $bank_address_code=str_pad($request->input('bank_address_code'),6,"0",STR_PAD_RIGHT);//开户城市编码
  59. // $bank_branch_id=$request->input('sub_branch_id');//开户银行联行号
  60. // $bank_name=$request->input('bank_name');//开户行(支行)
  61. $account_number=$request->input('account_number');//银行卡号
  62. $url="https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";//这个是微信的申请地址
  63. $param=array(
  64. "out_request_no" => $out_request_no,//申请编号,用户自定义就可以
  65. "organization_type" => "2401",//申请类型 2401代表申请的是小微商户(就是个人,没有营业执照的人),其它类型可参考微信v3文档
  66. // "need_account_info" => false,//true,//是否填写结算银行账户,提现会提到这个账户上,设置true需要添加银行卡信息等。这里我就设置false了。因为绑定银行卡可以通过微信中的小程序“商家助手”自己去绑定。也可以通过微信的绑卡接口在写一个绑定银行卡的功能。
  67. "account_info"=> [
  68. "bank_account_type" => "75",
  69. "account_name"=> $this->getEncrypts($id_card_name),//开户名称
  70. "account_bank"=> $account_bank,//开户银行
  71. "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
  72. // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
  73. // "bank_name"=> $bank_name,//开户银行全称(含支行)
  74. "account_number"=> $this->getEncrypts($account_number),//银行账户'6214833787989683'
  75. ],
  76. "contact_info" => array(//填写一个店铺的管理员信息
  77. "contact_type" => "65",//管理员类型 65是经营者/法人 其它类型看微信文档
  78. "contact_name" => $this->getEncrypts($contact_name),//管理员姓名getEncrypts是加密字符串的方法,见下文。
  79. "mobile_phone" => $this->getEncrypts($contact_phone),//管理员电话getEncrypts是加密字符串的方法
  80. "contact_id_card_number"=> $this->getEncrypts($contact_id_card),//管理员身份证号码
  81. ),
  82. "sales_scene_info" =>array(//店铺信息
  83. "store_name" =>$store_name,//店铺全称
  84. "store_url" =>$store_url //店铺的地址
  85. ),
  86. "merchant_shortname" =>$store_name,//商户简称
  87. "id_card_info" =>array(//经营者/法人的身份证信息
  88. "id_card_copy" =>$this->uploadmedia($id_card_copy),//身份证人像面uploadmedia()是上传图片到微信的方法,见下文
  89. "id_card_national" =>$this->uploadmedia($id_card_national),//身份证国徽面
  90. "id_card_name" =>$this->getEncrypts($id_card_name),//身份证上的姓名 getEncrypts()字符串加密方法 见下文
  91. "id_card_number" =>$this->getEncrypts($id_card_number),//身份证号,此身份证号可以和管理员是一个人
  92. "id_card_valid_time_begin"=>$id_card_valid_time_begin, //身份证国徽面的开始日期
  93. "id_card_valid_time"=>$id_card_valid_time //身份证国徽面的到期日期
  94. )
  95. );
  96. Log::info('监测提交认证数据');
  97. Log::info(json_encode($param,JSON_UNESCAPED_UNICODE));
  98. $merchant_id = config('wechat.payment.default.mch_id'); //"1604585534"; //服务商的商户号
  99. $serial_no = config('wechat.payment.default.serial_no'); //"73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书的序列号,可在微信商户平台API安全 中获取
  100. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  101. $timestamp =time();//时间戳
  102. $nonce =$this->nonce_str();//获取随机字符串
  103. $sign =$this->sign($url,'POST',$timestamp,$nonce,json_encode($param),$mch_private_key,$merchant_id,$serial_no);//进行签名操作
  104. $header =[//设置发送的头信息
  105. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,//签名
  106. 'Accept:application/json',
  107. 'User-Agent:' . $merchant_id,//服务商的商户号
  108. 'Content-Type:application/json',
  109. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书的序列号,注意:是平台证书,不是上文的商户证书序列号
  110. ];
  111. $result=$this->curl($url,json_encode($param),$header);//curl方法见下文
  112. $result=json_decode($result,true);
  113. if(isset($result['code']) && !empty($result['code'])){
  114. return $this->error('450001',$result['message']??'未知错误');
  115. }
  116. if(isset($result['applyment_id']) && isset($result['out_request_no'])){
  117. DB::connection('mysql_w')->beginTransaction();
  118. try{
  119. Apply::create([
  120. 'store_id'=>$store->id,
  121. 'contact_name'=>$contact_name,
  122. 'contact_phone'=>$contact_phone,
  123. 'contact_id_card'=>$contact_id_card,
  124. 'store_name'=>$store_name,
  125. 'store_url'=>$store_url,
  126. 'id_card_copy'=>$id_card_copy,
  127. 'id_card_national'=>$id_card_national,
  128. 'id_card_name'=> $id_card_name,
  129. 'id_card_number'=>$id_card_number,
  130. 'id_card_valid_time_begin'=>$id_card_valid_time_begin,
  131. 'id_card_valid_time'=>$id_card_valid_time,
  132. "account_name"=> $account_name,//开户名称
  133. "account_bank"=> $account_bank,//开户银行
  134. "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
  135. // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
  136. // "bank_name"=> $bank_name,//开户银行全称(含支行)
  137. "account_number"=> $account_number,//银行账户'6214833787989683'
  138. 'applyment_id'=>$result['applyment_id'],
  139. 'out_request_no'=>$result['out_request_no'],
  140. ]);
  141. Store::where('user_id',Auth::user()->id)
  142. ->update([
  143. 'username'=>$id_card_name,
  144. 'idCard'=>$id_card_number,
  145. 'cardImg1'=>$id_card_copy,
  146. 'cardImg2'=>$id_card_national,
  147. 'applyment_id'=>$result['applyment_id'],
  148. 'out_request_no'=>$result['out_request_no'],
  149. 'is_apply'=>1,//1已申请待审核 2 审核通过,待签约,3 已签约,已完成
  150. 'province'=>$request->input('province'),
  151. 'city'=>$request->input('city'),
  152. 'area'=>$request->input('area'),
  153. 'address'=>$request->input('address'),
  154. 'account_number'=> $account_number,
  155. 'account_bank'=> $account_bank,
  156. ]);
  157. // AddressW::create([
  158. // 'name'=>$contact_name,
  159. // 'phone'=>$contact_phone,
  160. // 'store_id'=>$store->id,
  161. // 'province'=>$request->input('province'),
  162. // 'city'=>$request->input('city'),
  163. // 'area'=>$request->input('area'),
  164. // 'address'=>$request->input('address'),
  165. // 'is_default'=>0
  166. // ]);
  167. Log::info('applyment_id:'.$result['applyment_id'].',&&out_request_no:'.$result['out_request_no']);
  168. DB::connection('mysql_w')->commit();
  169. return $this->success($result);
  170. }catch(\Exception $e){
  171. DB::connection('mysql_w')->rollBack();
  172. return $this->error('450001',$e->getMessage());
  173. }
  174. }else{
  175. return $this->error('450001','提交失败');
  176. }
  177. }
  178. //过滤微信表情符
  179. function filterEmoji($str)
  180. {
  181. $str = preg_replace_callback(
  182. '/./u',
  183. function (array $match) {
  184. return strlen($match[0]) >= 4 ? '' : $match[0];
  185. },
  186. $str);
  187. return $str;
  188. }
  189. //获取所有银行
  190. public function getAllBank(){
  191. return $this->success(Bank::whereNotNUll('bank_id')->where('status',1)->groupBy('bank_name')->pluck('bank_name','bank_id'));
  192. }
  193. //APP获取所有银行
  194. public function getAppAllBank(){
  195. return $this->success(['data'=>Bank::whereNotNUll('bank_id')->where('status',1)->groupBy('bank_name')->select('bank_name','bank_id')->get()]);
  196. }
  197. //获取省
  198. public function getAllProvince(Request $request){
  199. return $this->success(['list'=>Bank::where('status',1)->groupBy('province')->select('province','province_id')->get()]);
  200. }
  201. //获取省对应的市
  202. public function getAllCity(Request $request){
  203. return $this->success(['list'=>Bank::where('status',1)->where('province_id',$request->input('province_id'))->groupBy('city')->select('city','city_id')->get()]);
  204. }
  205. //获取支行
  206. public function getBankBranch(Request $request){
  207. $city_code=$request->input('city_code');
  208. $bank_id=$request->input('bank_id');
  209. // if($city_code=='1101' || $city_code=='1201' || $city_code=='3101' || $city_code=='5001'){
  210. // $city_code-=$city_code;
  211. // }
  212. $city_code.='00';
  213. Log::info($city_code.'//'.$bank_id);
  214. $bank=Bank::where('city_id',$city_code)->where('bank_id',$bank_id)->where('status',1)->get();
  215. return $this->success($bank);
  216. }
  217. //查询进件状态
  218. public function query(Request $request){
  219. $store=Store::where('user_id',Auth::user()->id)->first();
  220. $applyment_id=$store->applyment_id??$request->input('applyment_id');//'20200708093101909876';//申请编号
  221. Log:info('iii---------iii'.$applyment_id);
  222. if(empty($applyment_id)){
  223. return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
  224. }
  225. $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
  226. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  227. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  228. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  229. $timestamp=time();//时间戳
  230. $nonce=$this->nonce_str();//随机字符串
  231. $body="";
  232. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  233. $header=[
  234. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  235. 'Accept:application/json',
  236. 'User-Agent:' . $merchant_id,
  237. 'Content-Type:application/json',
  238. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  239. ];
  240. $result=$this->curl($url,'',$header,'GET');
  241. Log::info('------'.$result);
  242. $result=json_decode($result,true);
  243. $apply=Apply::where('store_id',$store->id)->where('applyment_id',$applyment_id)->OrderBy('id','desc')->first();
  244. if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
  245. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['legal_validation_url']]);
  246. }
  247. if($result['applyment_state']=="NEED_SIGN"){
  248. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
  249. Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
  250. }
  251. if($result['applyment_state']=="FINISH"){
  252. Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
  253. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  254. Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid']]);
  255. }
  256. if($result['applyment_state']=="REJECTED"){//已驳回
  257. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
  258. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  259. }
  260. if($result['applyment_state']=="FROZEN"){
  261. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
  262. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  263. }
  264. Apply::where('id',$apply->id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
  265. return $this->success($result);
  266. // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
  267. }
  268. //查询进件状态
  269. public function query123(Request $request){
  270. // $store=Store::where('user_id',Auth::user()->id)->first();
  271. // if(Auth::user()->id==32531){
  272. // $store=Store::where('user_id',9900)->first();
  273. // }
  274. $applyment_id=$request->input('applyment_id');//'20200708093101909876';//申请编号$store->applyment_id??
  275. Log:info('iii---------iii'.$applyment_id);
  276. if(empty($applyment_id)){
  277. return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
  278. }
  279. $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
  280. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  281. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  282. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  283. $timestamp=time();//时间戳
  284. $nonce=$this->nonce_str();//随机字符串
  285. $body="";
  286. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  287. $header=[
  288. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  289. 'Accept:application/json',
  290. 'User-Agent:' . $merchant_id,
  291. 'Content-Type:application/json',
  292. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  293. ];
  294. $result=$this->curl($url,'',$header,'GET');
  295. Log::info('------'.$result);
  296. $result=json_decode($result,true);
  297. return $this->success($result);
  298. if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
  299. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['legal_validation_url']]);
  300. }
  301. if($result['applyment_state']=="NEED_SIGN"){
  302. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
  303. Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
  304. }
  305. if($result['applyment_state']=="FINISH"){
  306. Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
  307. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  308. Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
  309. }
  310. // if($result['applyment_state']=="AUDITING"){
  311. // Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  312. // Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
  313. // }
  314. if($result['applyment_state']=="REJECTED"){//已驳回
  315. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
  316. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  317. }
  318. if($result['applyment_state']=="FROZEN"){
  319. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
  320. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  321. }
  322. Apply::where('applyment_id',$applyment_id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
  323. return $this->success($result);
  324. // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
  325. }
  326. //加密敏感字符串
  327. public function getEncrypts($str="张三"){
  328. // $public_key_path='D:\phpstudy_pro\WWW\weidian-api/cert/cert.pem';//获取的文件临时保存到服务器
  329. $public_key_path=config('wechat.payment.default.cert_pem');//这是获取平台证书临时存起来的平台公钥,通过getzhengshu()方法获取,见下文。。。注意是平台证书,不是商户证书
  330. $public_key=file_get_contents($public_key_path);
  331. $encrypted='';
  332. if (openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)) {
  333. //base64编码
  334. $sign = base64_encode($encrypted);
  335. } else {
  336. throw new Exception('encrypt failed');
  337. }
  338. return $sign;
  339. // if(openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)){
  340. // $sign=base64_encode($encrypted);
  341. // }else{
  342. // echo "error";exit;
  343. // }
  344. // return $sign;//返回加密的敏感字符串
  345. }
  346. //上传证件图片
  347. public function uploadmedia($paths){//$path="uploads/header/z.jpg"
  348. Log::info($paths);
  349. if(substr($paths,0,5)=='/cert'){
  350. $path='/www/wwwroot/test.woaidakele.cn/app-api/public'.$paths;
  351. }else{
  352. $path='https://qny.d0w.cc'.$paths;
  353. }
  354. // $path='/www/wwwroot/api.app.jiuweiyun.cn/app-api/public'.$path;
  355. Log::info($path);
  356. $url="https://api.mch.weixin.qq.com/v3/merchant/media/upload";//微信的上传地址
  357. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  358. $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号,注意是商户证书,不是平台证书序列号
  359. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法
  360. $timestamp=time();//时间戳
  361. $nonce=$this->nonce_str();//随机字符串
  362. // $fi=new \finfo(FILEINFO_MIME_TYPE);
  363. // $mime_type=$fi->file($path);
  364. if(substr($paths,0,5)=='/cert'){
  365. $fi=new \finfo(FILEINFO_MIME_TYPE);
  366. $mime_type=$fi->file($path);
  367. }else{
  368. $info = pathinfo($path);
  369. if($info['extension']=='jpg'){
  370. $info['extension']='jpeg';
  371. }
  372. $mime_type='image/'.$info['extension'];
  373. }
  374. Log::info($mime_type);
  375. if(substr($paths,0,5)=='/cert'){
  376. $pathss=$path;
  377. }else{
  378. $pathss=$path;
  379. $res = @get_headers($url,true);
  380. if($res){
  381. if(array_key_exists('Content-Length',$res) && $res['Content-Length'] > 1024*1024){
  382. Log::info('-----七牛云压缩图片-----');
  383. $pathss=$path.'?imageView2/0/w/500/q/80';
  384. }
  385. }
  386. }
  387. $meta=[
  388. 'filename'=>pathinfo($path,2),
  389. 'sha256'=>hash_file('sha256',$pathss)
  390. ];
  391. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($meta),$mch_private_key,$merchant_id,$serial_no);
  392. $boundary=uniqid();
  393. $header=[
  394. 'Content-Type:multipart/form-data;boundary=' . $boundary,
  395. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  396. 'Accept:application/json',
  397. 'User-Agent:' . $merchant_id
  398. ];
  399. Log::info($path);
  400. Log::info($pathss);
  401. $body='--' . $boundary . "\r\n";
  402. $body.='Content-Disposition:form-data; name="meta"' . "\r\n";
  403. $body.='Content-Type:application/json' . "\r\n\r\n";
  404. $body.=json_encode($meta)."\r\n";
  405. $body.='--' . $boundary . "\r\n";
  406. $body.='Content-Disposition:form-data;name="file";filename="' . $meta['filename'] . '"' . "\r\n";
  407. $body.='Content-Type:' .$mime_type."\r\n\r\n";
  408. $body.=file_get_contents($pathss)."\r\n";
  409. $body.='--'.$boundary .'--'."\r\n";
  410. $result=$this->curl($url,$body,$header);
  411. $result=json_decode($result,true);
  412. Log::info($result['media_id']);
  413. return $result['media_id'];//返回微信返回来的图片标识
  414. }
  415. public function getImgToMediaId(Request $request)
  416. {
  417. $app = app('wechat.official_account');
  418. $accessTokens = $app->access_token;
  419. $access_token = $accessTokens->getToken();
  420. $token = $access_token['access_token'];
  421. $img = $request->input('img');
  422. $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=" . $token . "&media_id=" . $img;
  423. return $url;
  424. }
  425. //读取商户api证书公钥
  426. public function getPublicKey(){
  427. return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
  428. }
  429. //生成随机字符串
  430. public function nonce_str($length=32){
  431. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  432. $str ="";
  433. for ( $i = 0; $i < $length; $i++ ) {
  434. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  435. }
  436. return $str;
  437. }
  438. //签名
  439. public function sign($url,$http_method,$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no){
  440. $url_parts=parse_url($url);
  441. $canonical_url=($url_parts['path'].(!empty($url_parts['query'])?"?${url_parts['query']}":""));
  442. $message=
  443. $http_method . "\n".
  444. $canonical_url . "\n".
  445. $timestamp . "\n".
  446. $nonce . "\n".
  447. $body . "\n";
  448. openssl_sign($message,$raw_sign,$mch_private_key,'sha256WithRSAEncryption');
  449. $sign=base64_encode($raw_sign);
  450. $schema='WECHATPAY2-SHA256-RSA2048';
  451. $token=sprintf(
  452. 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
  453. $merchant_id,
  454. $nonce,
  455. $sign,
  456. $timestamp,
  457. $serial_no
  458. );
  459. return $token;
  460. }
  461. //curl提交
  462. public function curl($url,$data=[],$header,$method='POST'){
  463. $curl=curl_init();
  464. curl_setopt($curl,CURLOPT_URL,$url);
  465. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  466. // curl_setopt($curl,CURLINFO_HEADER_OUT,true);
  467. // curl_setopt($curl,CURLOPT_HEADER,true);
  468. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  469. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  470. if($method=="POST"){
  471. curl_setopt($curl,CURLOPT_POST,TRUE);
  472. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  473. }
  474. $result=curl_exec($curl);
  475. // $headerStr=curl_getinfo($curl,CURLINFO_HEADER_OUT);
  476. // list($responseStr,$contentStr)=explode("\r\n\r\n",$result,2);
  477. // log::info("result:".$result);
  478. // log::info("request header:".$headerStr);
  479. // log::info('response header:'.$responseStr);
  480. // log::info('response content:'.$contentStr);
  481. curl_close($curl);
  482. return $result;
  483. }
  484. public function curls($url,$data=[],$header,$method='POST'){
  485. $curl=curl_init();
  486. curl_setopt($curl,CURLOPT_URL,$url);
  487. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  488. curl_setopt($curl,CURLOPT_HEADER,false);
  489. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  490. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  491. if($method=="POST"){
  492. curl_setopt($curl,CURLOPT_POST,TRUE);
  493. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  494. }
  495. $result=curl_exec($curl);
  496. $status=curl_getinfo($curl,CURLINFO_HTTP_CODE);
  497. curl_close($curl);
  498. return [$result,$status];
  499. }
  500. //获取平台证书
  501. public function getzhengshu(){
  502. $url="https://api.mch.weixin.qq.com/v3/certificates";//获取地址
  503. $timestamp=time();//时间戳
  504. $nonce=$this->nonce_str();//获取一个随机字符串
  505. $body="";
  506. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥
  507. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  508. $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号
  509. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);
  510. $header=[
  511. 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
  512. 'Accept:application/json',
  513. 'User-Agent:'.$merchant_id
  514. ];
  515. $result=$this->curl($url,'',$header,'GET');
  516. $result=json_decode($result,true);
  517. $serial_no=$result['data'][0]['serial_no'];//获取的平台证书序列号
  518. $encrypt_certificate=$result['data'][0]['encrypt_certificate'];
  519. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  520. $result=$this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key);
  521. // return null;//file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem');
  522. // if(empty(file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem'))){
  523. // file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem',$result);
  524. file_put_contents(config('wechat.payment.default.cert_pem'),$result);//获取的文件临时保存到服务器
  525. // }
  526. return $serial_no;//返回平台证书序列号
  527. }
  528. //解密加密密钥
  529. public function getDecrypt($ciphertext, $associatedData, $nonceStr){
  530. $mch_private_key= file_get_contents(config('wechat.payment.default.key_path'));
  531. // Log::info($mch_private_key);
  532. $str = base64_decode($ciphertext);
  533. openssl_private_decrypt($str,$encrypted,$mch_private_key,OPENSSL_PKCS1_OAEP_PADDING);
  534. return trim($encrypted);
  535. }
  536. //解密返回的信息
  537. public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){
  538. if (strlen($aesKey) != 32) {
  539. Log::info('1--');
  540. throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
  541. }
  542. $ciphertext=\base64_decode($ciphertext);
  543. if (strlen($ciphertext) <= 16) {
  544. Log::info('2--');
  545. return false;
  546. }
  547. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
  548. \sodium_crypto_aead_aes256gcm_is_available()) {
  549. Log::info('3--');
  550. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  551. }
  552. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
  553. \Sodium\crypto_aead_aes256gcm_is_available()) {
  554. Log::info('4--');
  555. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  556. }
  557. if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){
  558. $ctext=substr($ciphertext,0,-16);
  559. $authTag=substr($ciphertext,-16);
  560. Log::info('5--');
  561. return \openssl_decrypt(
  562. $ctext,
  563. 'aes-256-gcm',
  564. $aesKey,
  565. \OPENSSL_RAW_DATA,
  566. $nonceStr,
  567. $authTag,
  568. $associatedData
  569. );
  570. }
  571. Log::info('6--');
  572. throw new \RuntimeException('php7.1');
  573. }
  574. //设置银行账户信息
  575. public function setBankInfo(Request $request){
  576. Log::error($request->all());
  577. // if (Auth::user()->id==32531){
  578. // $user_id='13878';
  579. // }else{
  580. // $user_id=Auth::user()->id;
  581. // }
  582. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  583. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
  584. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  585. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  586. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  587. $timestamp=time();//时间戳
  588. $nonce=$this->nonce_str();//随机字符串
  589. $bank_address_code= $request->input('bank_address_code');
  590. if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
  591. $bank_address_code=substr($bank_address_code,0,2).'00';
  592. }
  593. Log::info('开户行所在城市代码:'.$bank_address_code);
  594. $data=[
  595. 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
  596. 'account_bank' =>$request->input('account_bank'),
  597. 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
  598. // 'bank_name'=>$request->input('bank_name'),
  599. // 'bank_branch_id'=>$request->input('bank_branch_id'),
  600. 'account_number'=>$this->getEncrypts($request->input('account_number'))
  601. ];
  602. Log::info($data);
  603. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  604. $header=[
  605. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  606. 'Accept:application/json',
  607. 'User-Agent:' . $merchant_id,
  608. // 'Content-Type:multipart/form-data',
  609. 'Content-Type:application/json',
  610. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  611. ];
  612. $result=$this->curls($url,json_encode($data),$header);
  613. Log::error('修改结算账户返回结果。'.json_encode($result));
  614. $http_status=$result[1];
  615. $result=json_decode($result[0],true);
  616. if($http_status==204){
  617. Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
  618. Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)
  619. ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
  620. return $this->success($result);
  621. }else{
  622. Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].',商户id为:'.$sub_mchid);
  623. return $this->error('450001',$result['message']);
  624. }
  625. }
  626. //获取银行账户信息
  627. public function getBankInfo(){
  628. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  629. // if($sub_mchid=='1607429904'){
  630. // $sub_mchid='1609015697';
  631. // }
  632. // if($sub_mchid=='1607448291'){
  633. // $sub_mchid='1609035479';
  634. // }
  635. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/settlement';//'https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//查询地址
  636. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  637. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  638. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  639. $timestamp=time();//时间戳
  640. $nonce=$this->nonce_str();//随机字符串
  641. $body="";
  642. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  643. $header=[
  644. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  645. 'Accept:application/json',
  646. 'User-Agent:' . $merchant_id,
  647. 'Content-Type:application/json',
  648. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  649. ];
  650. $result=$this->curl($url,'',$header,'GET');
  651. Log::error(Auth::user()->id.'//'.$result);
  652. $result=json_decode($result,true);
  653. return $this->success($result);
  654. }
  655. public function uploadImg(Request $request)
  656. {
  657. $path_url = 'public/wechat/' . date("ymd");
  658. $path = $request->file('img')->store($path_url);
  659. if ($path) {
  660. $url = Storage::url($path);
  661. return $this->success($url);
  662. } else {
  663. return $this->error();
  664. }
  665. }
  666. //获取账户实时余额
  667. public function getAccountInfo(){
  668. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  669. $result=$this->getAccount($sub_mchid);
  670. if($result['sub_mchid']){
  671. Store::withTrashed()->where('is_failure',0)->where('sub_mchid',$result['sub_mchid'])
  672. ->update([
  673. 'available_amount'=>round($result['available_amount']/100,2),
  674. 'pending_amount'=>round($result['pending_amount']/100,2)
  675. ]);
  676. }
  677. return $this->success($result,$sub_mchid);
  678. }
  679. //获取账户信息
  680. public function getAccount($sub_mchid){
  681. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/'.$sub_mchid;//查询地址
  682. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  683. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  684. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  685. $timestamp=time();//时间戳
  686. $nonce=$this->nonce_str();//随机字符串
  687. $body="";
  688. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  689. $header=[
  690. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  691. 'Accept:application/json',
  692. 'User-Agent:' . $merchant_id,
  693. 'Content-Type:application/json',
  694. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  695. ];
  696. $result=$this->curl($url,'',$header,'GET');
  697. Log::info($result);
  698. $result=json_decode($result,true);
  699. Log::info($result);
  700. return $result;
  701. }
  702. //二级商户提现
  703. public function withdrawal(Request $request){
  704. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  705. Log::info('提现金额:'.$request->input('amount'));
  706. $withdrawal_amount=str_replace( ',', '',$request->input('amount'));
  707. if(!is_numeric($withdrawal_amount)){
  708. return $this->error('450001','提现金额为非数值');
  709. }
  710. $amount=floor($withdrawal_amount * 100);
  711. if($amount < 1){
  712. return $this->error('450001','提现金额有误请重新输入');
  713. }
  714. $result=$this->getAccount($sub_mchid);
  715. if(!$result['sub_mchid']){
  716. return $this->error('450001','结算账户信息有误,暂不能提现');
  717. }
  718. // $phone=[];
  719. // $agent_phone=Store::where('user_id',Auth::user()->id)->value('phone');
  720. // if(!in_array($agent_phone,$phone)){
  721. // if(!$result['sub_mchid']){
  722. // return $this->error('450001','可提现金额小于200元,暂不能提现');
  723. // }
  724. // if($result['available_amount'] < 20000){
  725. // return $this->error('450001','可提现金额小于200元,暂不能提现');
  726. // }
  727. // }
  728. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw';//查询地址
  729. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  730. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  731. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  732. $timestamp=time();//时间戳
  733. $nonce=$this->nonce_str();//随机字符串
  734. $out_request_no='TX'.date('ymdHis') . substr(microtime(), 2, 6).mt_rand(10000,99999);
  735. $data=[
  736. 'sub_mchid'=>$sub_mchid,
  737. 'out_request_no'=> $out_request_no,
  738. 'amount'=> (int) $amount,
  739. 'remark'=> '二级商户提现',
  740. 'bank_memo'=> '微信二级商户提现'
  741. ];
  742. // Log::error(json_encode($data));
  743. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  744. $header=[
  745. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  746. 'Accept:application/json',
  747. 'User-Agent:' . $merchant_id,
  748. 'Content-Type:application/json',
  749. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  750. ];
  751. $result=$this->curl($url,json_encode($data),$header,'POST');
  752. $result=json_decode($result,true);
  753. if(isset($result['withdraw_id']) && !empty($result['withdraw_id'])){
  754. $store=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->first();
  755. Amount::create([
  756. 'transaction_id'=>$result['withdraw_id'],
  757. 'order_no'=>$result['out_request_no'],
  758. 'store_id'=>$store->id,
  759. 'money'=>$amount/100,
  760. 'type'=>3,//提现
  761. 'status'=>0,
  762. 'status_code'=>'CREATE_SUCCESS',
  763. 'service_fee'=>0,
  764. 'remark'=>'提现',
  765. 'remark_amount'=>$store->account_bank.'-'.$store->account_number,
  766. ]);
  767. }else{
  768. return $this->error('450001',$result['message']);
  769. }
  770. return $this->success($result);
  771. }
  772. //商户提现状态查询
  773. public function getWithdrawalStates(Request $request){
  774. $withdraw_id=$request->input('withdraw_id');
  775. $store_id=$request->input('user_id');
  776. // $user_id='32531';
  777. // $index=$request->input('index');
  778. // $size=$request->input('size');
  779. // $refunds=Amount::where('type',3)->orderBy('id')->skip($index)->take($size)->get();
  780. //// return $refunds;
  781. // foreach($refunds as $key=>$val){
  782. $sub_mchid=Store::where('id',$store_id)->value('sub_mchid');
  783. // $withdraw_id=$val->order_no;
  784. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  785. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  786. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  787. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  788. $timestamp=time();//时间戳
  789. $nonce=$this->nonce_str();//随机字符串
  790. $body="";
  791. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  792. $header=[
  793. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  794. 'Accept:application/json',
  795. 'User-Agent:' . $merchant_id,
  796. 'Content-Type:application/json',
  797. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  798. ];
  799. $result=$this->curl($url,'',$header,'GET');
  800. $result=json_decode($result,true);
  801. // if($val->status_code<>$result['status']){
  802. // Log::info('提现状态有误:'.$val->store_id.'---'.$val->order_no.'---'.$result['status']);
  803. // }
  804. // Amount::where('id',$val->id)->update([
  805. // 'transaction_id'=>$result['withdraw_id'],
  806. // ]);
  807. // }
  808. // $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
  809. return $result;
  810. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  811. // $reason=$result['reason'];
  812. // }else{
  813. // $reason=null;
  814. // }
  815. // Amount::where('withdraw_id',$withdraw_id)
  816. // ->update([
  817. // 'status'=>0,
  818. // 'status_code'=>$result['status'],
  819. // 'reason'=>$reason,
  820. // ]);
  821. // return $this->success($result);
  822. }
  823. //商户提现状态查询
  824. public function getWithdrawalState(Request $request){
  825. $withdraw_id=$request->input('withdraw_id');
  826. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  827. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  828. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  829. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  830. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  831. $timestamp=time();//时间戳
  832. $nonce=$this->nonce_str();//随机字符串
  833. $body="";
  834. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  835. $header=[
  836. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  837. 'Accept:application/json',
  838. 'User-Agent:' . $merchant_id,
  839. 'Content-Type:application/json',
  840. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  841. ];
  842. $result=$this->curl($url,'',$header,'GET');
  843. $result=json_decode($result,true);
  844. if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  845. $reason=$result['reason'];
  846. }else{
  847. $reason=null;
  848. }
  849. Amount::where('withdraw_id',$withdraw_id)
  850. ->update([
  851. 'status'=>0,
  852. 'status_code'=>$result['status'],
  853. 'reason'=>$reason,
  854. ]);
  855. return $this->success($result);
  856. }
  857. //获取收款记录
  858. public function getAccountDetail(Request $request){
  859. $type=$request->input('type');
  860. $cycle=$request->input('cycle');
  861. $page_size=$request->input('page_size');
  862. $page_index=$request->input('page_index');
  863. $startime=$request->input('startime');
  864. $endtime=$request->input('endtime');
  865. $num=$page_size*($page_index-1);
  866. $store=Store::where('user_id',Auth::user()->id)->first();
  867. $data= Amount::where('store_id',$store->id);
  868. if($type!='all'){
  869. $data->where('type',$type);
  870. }
  871. if($startime && $endtime){
  872. $data->whereBetween('created_at',[date("Y-m-d 00:00:00",strtotime($startime)),date("Y-m-d 23:59:59",strtotime($endtime))]);
  873. }
  874. $cc=clone $data;
  875. $count=$data->groupBy('order_no','transaction_id','type')->get()->count();
  876. if($type==3){
  877. $ll['account']=$cc->where('status_code','SUCCESS')->sum('money');
  878. }else{
  879. $ll['account']=$cc->sum('money');
  880. }
  881. if($type==4){
  882. $data->with('refund_money:refund_no,order_no');
  883. }
  884. $data->groupBy('order_no','transaction_id','type');
  885. if($type=='all'){
  886. $data->OrderBy('id');
  887. }else{
  888. $data->OrderByDesc('id');
  889. }
  890. $list=$data->skip($num)->take($page_size)->get();
  891. if($type==1){
  892. foreach($list as $key=>$val){
  893. $order_no=$val->order_no;
  894. $refund_no='TK'.substr($order_no,2);
  895. $amount=Amount::where('store_id',$store->id)->where('order_no',$refund_no)->where('type',4)->first();
  896. if($amount){
  897. $list[$key]->refund_no=$refund_no;
  898. $list[$key]->is_refund=1;
  899. }else{
  900. $list[$key]->refund_no='';
  901. $list[$key]->is_refund=0;
  902. }
  903. if($val->type==4){
  904. }
  905. }
  906. }
  907. $ll['list']=$list;
  908. return $this->success_list($ll,'',$count);
  909. }
  910. //获取按日下载提现异常文件
  911. public function getDownWithdraw(Request $request){
  912. $bill_type=$request->input('bill_type');
  913. $bill_date=$request->input('bill_date');
  914. $tar_type=$request->input('tar_type');
  915. $url='https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date='.$bill_date;// .'&bill_type='.$bill_type.'&tar_type='.$tar_type
  916. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  917. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  918. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  919. $timestamp=time();//时间戳
  920. $nonce=$this->nonce_str();//随机字符串
  921. $body="";
  922. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  923. $header=[
  924. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  925. 'Accept:application/json',
  926. 'User-Agent:' . $merchant_id,
  927. 'Content-Type:application/json',
  928. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  929. ];
  930. $result=$this->curl($url,'',$header,'GET');
  931. $res=json_decode($result,true);
  932. $con= $this->down($res['download_url'],'withdraw');
  933. return $this->success($con);
  934. }
  935. //获取资金账单
  936. public function getfundflowbill(Request $request){
  937. $bill_date=$request->input('bill_date');
  938. $account_type=$request->input('account_type');
  939. $tar_type=$request->input('tar_type');
  940. $url='https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date='.$bill_date;// .'&account_type='.$bill_type.'&tar_type='.$tar_type
  941. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  942. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  943. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  944. $timestamp=time();//时间戳
  945. $nonce=$this->nonce_str();//随机字符串
  946. $body="";
  947. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  948. $header=[
  949. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  950. 'Accept:application/json',
  951. 'User-Agent:' . $merchant_id,
  952. 'Content-Type:application/json',
  953. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  954. ];
  955. $result=$this->curl($url,'',$header,'GET');
  956. return $this->success($result);
  957. $res=json_decode($result,true);
  958. $this->down($res['download_url']);
  959. return $this->success($res);
  960. }
  961. //获取分账结算账单
  962. public function getprofitsharing(Request $request){
  963. $bill_date=$request->input('bill_date');
  964. $sub_mchid=$request->input('sub_mchid');
  965. $tar_type=$request->input('tar_type');
  966. $url='https://api.mch.weixin.qq.com/v3/profitsharing/bills?bill_date='.$bill_date;// .'&account_type='.$bill_type.'&tar_type='.$tar_type
  967. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  968. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  969. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  970. $timestamp=time();//时间戳
  971. $nonce=$this->nonce_str();//随机字符串
  972. $body="";
  973. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  974. $header=[
  975. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  976. 'Accept:application/json',
  977. 'User-Agent:' . $merchant_id,
  978. 'Content-Type:application/json',
  979. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  980. ];
  981. $result=$this->curl($url,'',$header,'GET');
  982. $res=json_decode($result,true);
  983. $con= $this->down($res['download_url'],'sharing');
  984. return $this->success($con);
  985. }
  986. //获取二级商户资金账单
  987. public function getecommercebill(Request $request){
  988. $bill_date=$request->input('bill_date');
  989. $account_type=$request->input('account_type');
  990. $algorithm=$request->input('algorithm');
  991. $url='https://api.mch.weixin.qq.com/v3/ecommerce/bill/fundflowbill?bill_date='.$bill_date.'&account_type='.$account_type.'&algorithm='.$algorithm;
  992. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  993. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  994. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  995. $timestamp=time();//时间戳
  996. $nonce=$this->nonce_str();//随机字符串
  997. $body="";
  998. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  999. $header=[
  1000. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1001. 'Accept:application/json',
  1002. 'User-Agent:' . $merchant_id,
  1003. 'Content-Type:application/json',
  1004. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1005. ];
  1006. $result=$this->curl($url,'',$header,'GET');
  1007. $res=json_decode($result,true);
  1008. if(!array_key_exists('download_bill_list',$res)){
  1009. Log::info($res);
  1010. return $this->success($res);
  1011. }
  1012. Log::info($bill_date.'下载资金账单,页数为'.count($res['download_bill_list']));
  1013. foreach($res['download_bill_list'] as $key=>$val){
  1014. $ciphertext= $this->down($val['download_url']);
  1015. $key=$this->getDecrypt($val['encrypt_key'],'',$val['nonce']);
  1016. $result_str=$this->decryptToString('',$val['nonce'],base64_encode($ciphertext),$key);
  1017. $res_arr=$this->deal_ecommerce_bill($result_str);
  1018. $bill_day = $res_arr['bill'][0]['bill_date'];
  1019. $trade_info = EcommerceBillW::where('bill_date',$bill_day)->first();
  1020. if(!$trade_info){
  1021. if(count($res_arr['bill']) > 1000){
  1022. $arr = array_chunk($res_arr['bill'],1000);
  1023. foreach($arr as $k=>$v){
  1024. EcommerceBillW::insert($v);
  1025. }
  1026. }else{
  1027. EcommerceBillW::insert($res_arr['bill']);
  1028. }
  1029. }
  1030. }
  1031. // $ciphertext= $this->down($res['download_bill_list'][0]['download_url']);
  1032. // $key=$this->getDecrypt($res['download_bill_list'][0]['encrypt_key'],'',$res['download_bill_list'][0]['nonce']);
  1033. // $result=$this->decryptToString('',$res['download_bill_list'][0]['nonce'],base64_encode($ciphertext),$key);
  1034. // $res=$this->deal_ecommerce_bill($result);
  1035. // $bill_date = $res['bill'][0]['bill_date'];
  1036. // $trade_info = EcommerceBillW::where('bill_date',$bill_date)->first();
  1037. // if(!$trade_info){
  1038. // if(count($res['bill']) > 1000){
  1039. // $arr = array_chunk($res['bill'],1000);
  1040. // foreach($arr as $key=>$val){
  1041. // EcommerceBillW::insert($val);
  1042. // }
  1043. // }else{
  1044. // EcommerceBillW::insert($res['bill']);
  1045. // }
  1046. // }
  1047. return $this->success($res);
  1048. }
  1049. public function down($url){
  1050. // $url=$request->input('url');
  1051. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1052. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1053. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1054. $timestamp=time();//时间戳
  1055. $nonce=$this->nonce_str();//随机字符串
  1056. $body="";
  1057. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1058. $header=[
  1059. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1060. 'Accept:application/json',
  1061. 'User-Agent:' . $merchant_id,
  1062. 'Content-Type:application/json;charset=utf-8',
  1063. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1064. ];
  1065. $result=$this->curl($url,'',$header,'GET');
  1066. return $result;
  1067. if($t == 'withdraw'){
  1068. $res = $this->deal_WeChat_response($result);
  1069. $trade_date = $res['bill'][0]['trade_date'];
  1070. $trade_info = TradeBillW::where('trade_date',$trade_date)->first();
  1071. if(!$trade_info){
  1072. if(count($res['bill']) > 1000){
  1073. $arr = array_chunk($res['bill'],1000);
  1074. foreach($arr as $key=>$val){
  1075. TradeBillW::insert($val);
  1076. }
  1077. }else{
  1078. TradeBillW::insert($res['bill']);
  1079. }
  1080. }
  1081. }elseif($t == 'sharing'){
  1082. $res = $this->deal_profit_sharing($result);
  1083. $sharing_date = $res['sharing'][0]['sharing_date'];
  1084. $sharing_info = ProfitSharingW::where('sharing_date',$sharing_date)->first();
  1085. if(!$sharing_info){
  1086. if(count($res['sharing']) > 1000){
  1087. $arr = array_chunk($res['sharing'],1000);
  1088. foreach($arr as $key=>$val){
  1089. ProfitSharingW::insert($val);
  1090. }
  1091. }else{
  1092. ProfitSharingW::insert($res['sharing']);
  1093. }
  1094. }
  1095. }
  1096. return $res;
  1097. }
  1098. public function deal_WeChat_response($response){
  1099. $result = array();
  1100. $response = str_replace(","," ",$response);
  1101. $response = explode(PHP_EOL, $response);
  1102. foreach ($response as $key=>$val){
  1103. if(strpos($val, '`') !== false){
  1104. $data = explode('`', $val);
  1105. array_shift($data); // 删除第一个元素并下标从0开始
  1106. if(count($data) == 27){ // 处理账单数据
  1107. $result['bill'][] = array(
  1108. 'pay_time' => $data[0], // 支付时间
  1109. 'app_id' => $data[1], // app_id
  1110. 'mch_id' => $data[2], // 商户id
  1111. 'sub_mch_id' => $data[3], // 特约商户id
  1112. 'imei' => $data[4], // 设备号
  1113. 'order_sn_wx' => $data[5], // 微信订单号
  1114. 'order_sn_sh' => $data[6], // 商户订单号
  1115. 'user_tag' => $data[7], // 用户标识
  1116. 'pay_type' => $data[8], // 交易类型
  1117. 'pay_status' => $data[9], // 交易状态
  1118. 'bank' => $data[10], // 付款银行
  1119. 'money_type' => $data[11], // 货币种类
  1120. 'total_amount' => $data[12], // 应结订单金额
  1121. 'coupon_amount' => $data[13], // 代金券金额
  1122. 'refund_number_wx' => $data[14], // 微信退款单号
  1123. 'refund_number_sh' => $data[15], // 商户退款单号
  1124. 'refund_amount' => $data[16], // 退款金额
  1125. 'coupon_refund_amount' => $data[17], // 充值券退款退款金额
  1126. 'refund_type' => $data[18], // 退款类型
  1127. 'refund_status' => $data[19], // 退款状态
  1128. 'goods_name' => $data[20], // 商品名称
  1129. 'goods_data_bag' => $data[21], // 商户数据包
  1130. 'service_charge' => $data[22], // 手续费
  1131. 'rate' => $data[23], // 费率
  1132. 'order_account' => $data[24], // 订单金额
  1133. 'apply_refund_account' => $data[25], // 申请退款金额
  1134. 'rate_remark' => $data[26], // 费率备注
  1135. 'trade_date' => date("Y-m-d", strtotime($data[0]))
  1136. );
  1137. }
  1138. if(count($data) == 7){ // 统计数据
  1139. $result['summary'] = array(
  1140. 'order_num' => $data[0], // 总交易单数
  1141. 'turnover' => $data[1], // 应结订单总金额
  1142. 'refund_turnover' => $data[2], // 退款总金额
  1143. 'coupon_turnover' => $data[3], // 充值券退款总金额
  1144. 'rate_turnover' => $data[4], // 手续费总金额
  1145. 'order_turnover' => $data[5], // 订单总金额
  1146. 'apply_refund_turnover' => $data[6], // 申请退款总金额
  1147. );
  1148. }
  1149. }
  1150. }
  1151. return $result;
  1152. }
  1153. public function deal_profit_sharing($response)
  1154. {
  1155. $result = array();
  1156. $response = str_replace(",", " ", $response);
  1157. $response = explode(PHP_EOL, $response);
  1158. foreach ($response as $key => $val) {
  1159. if (strpos($val, '`') !== false) {
  1160. $data = explode('`', $val);
  1161. array_shift($data); // 删除第一个元素并下标从0开始
  1162. if (count($data) == 9) { // 处理账单数据
  1163. $result['sharing'][] = array(
  1164. 'sharing_time' => $data[0], // 分账时间
  1165. 'sharing_originator' => $data[1], // 分账发起方
  1166. 'sharing_user' => $data[2], // 分账方
  1167. 'wx_order_no' => $data[3], // 微信订单号
  1168. 'wx_refund_no' => $data[4], // 微信分账方/回退单号
  1169. 'sharing_detail_no' => $data[5], // 分账明细单号
  1170. 'mch_refund_no' => $data[6], // 商户分账/回退单号
  1171. 'order_amount' => $data[7], // 订单金额
  1172. 'sharing_recipient' => explode(' ',$data[8])[0], // 分账接收方
  1173. 'amount' => explode(' ',$data[8])[1], // 金额
  1174. 'sharing_type' => explode(' ',$data[8])[2], // 业务类型
  1175. 'sharing_status' => explode(' ',$data[8])[3], // 处理状态
  1176. 'sharing_desc' => explode(' ',$data[8])[4], // 分账描述
  1177. 'remark' => explode(' ',$data[8])[5], // 备注
  1178. 'sharing_date' => date("Y-m-d", strtotime($data[0])), // 分账日期
  1179. );
  1180. }
  1181. if(count($data) == 5){ // 统计数据
  1182. $result['summary'] = array(
  1183. 'sharing_num' => $data[0], // 总条数
  1184. 'sharing_success_amount' => $data[1], // 分账成功出资金
  1185. 'sharing_fail_turnover' => $data[2], // 分账失败已转回分账方
  1186. 'thaw_funds_amount' => $data[3], // 解冻资金给分账方
  1187. 'refund_amount' => $data[4], // 分账回退资金
  1188. );
  1189. }
  1190. }
  1191. }
  1192. return $result;
  1193. }
  1194. public function deal_ecommerce_bill($response){
  1195. $result = array();
  1196. $response = str_replace(","," ",$response);
  1197. $response = explode(PHP_EOL, $response);
  1198. foreach ($response as $key=>$val){
  1199. if(strpos($val, '`') !== false){
  1200. $data = explode('`', $val);
  1201. array_shift($data); // 删除第一个元素并下标从0开始
  1202. if(count($data) == 13){ // 处理账单数据
  1203. $result['bill'][] = array(
  1204. 'bill_time' => $data[0], // 记账时间
  1205. 'wx_order_no' => $data[1], // 微信支付业务单号
  1206. 'fee_order_no' => $data[2], // 资金流水单号
  1207. 'bill_name' => $data[3], // 业务名称
  1208. 'bill_type' => $data[4], // 业务类型
  1209. 'fee_type' => $data[5], // 收支类型
  1210. 'amount' => $data[6], // 收支金额(元)
  1211. 'account_fee' => $data[7], // 账户结余(元)
  1212. 'apply_name' => $data[8], // 资金变更提交申请人
  1213. 'remark' => $data[9], // 备注
  1214. 'bill_cre_no' => $data[10], // 业务凭证号
  1215. 'sub_mch_id' => $data[11], // 电商二级商户号
  1216. 'account_type' => $data[12], // 账户类型
  1217. 'bill_date' => date("Y-m-d", strtotime($data[0]))
  1218. );
  1219. }
  1220. if(count($data) == 1){ // 统计数据
  1221. $result['summary'] = array(
  1222. 'order_num' => $data[0], // 资金流水总笔数
  1223. );
  1224. }
  1225. }
  1226. }
  1227. return $result;
  1228. }
  1229. //开启点金计划
  1230. public function dianjin(Request $request){
  1231. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  1232. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changegoldplanstatus';//查询地址
  1233. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1234. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1235. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1236. $timestamp=time();//时间戳
  1237. $nonce=$this->nonce_str();//随机字符串
  1238. $data=[
  1239. 'sub_mchid'=>$sub_mchid,
  1240. 'operation_type'=>'OPEN'
  1241. ];
  1242. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1243. $header=[
  1244. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1245. 'Accept:application/json',
  1246. 'User-Agent:' . $merchant_id,
  1247. 'Content-Type:application/json',
  1248. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1249. ];
  1250. $result=$this->curl($url,json_encode($data),$header,'POST');
  1251. $result=json_decode($result,true);
  1252. if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
  1253. Store::where('sub_mchid',$sub_mchid)->update(['is_dianjin'=>1]);
  1254. return $this->success($result);
  1255. }else{
  1256. return $this->error('450001',$result['message']);
  1257. }
  1258. }
  1259. //开启商家小票
  1260. public function businessTips(){
  1261. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  1262. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changecustompagestatus';//点金地址
  1263. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1264. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1265. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1266. $timestamp=time();//时间戳
  1267. $nonce=$this->nonce_str();//随机字符串
  1268. $data=[
  1269. 'sub_mchid'=>$sub_mchid,
  1270. 'operation_type'=> 'OPEN'
  1271. ];
  1272. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1273. $header=[
  1274. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1275. 'Accept:application/json',
  1276. 'User-Agent:' . $merchant_id,
  1277. 'Content-Type:application/json',
  1278. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1279. ];
  1280. $result=$this->curl($url,json_encode($data),$header,'POST');
  1281. $result=json_decode($result,true);
  1282. if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
  1283. Store::where('sub_mchid',$sub_mchid)->update(['is_business'=>1]);
  1284. return $this->success($result);
  1285. }else{
  1286. return $this->error('450001',$result['message']);
  1287. }
  1288. }
  1289. //关闭广告
  1290. public function adverting(Request $request){
  1291. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  1292. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/close-advertising-show';//查询地址
  1293. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1294. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1295. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1296. $timestamp=time();//时间戳
  1297. $nonce=$this->nonce_str();//随机字符串
  1298. $data=[
  1299. 'sub_mchid'=>$sub_mchid
  1300. ];
  1301. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1302. $header=[
  1303. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1304. 'Accept:application/json',
  1305. 'User-Agent:' . $merchant_id,
  1306. 'Content-Type:application/json',
  1307. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1308. ];
  1309. $result=$this->curl($url,json_encode($data),$header,'POST');
  1310. $result=json_decode($result,true);
  1311. if(empty($result)){
  1312. Store::where('sub_mchid',$sub_mchid)->update(['is_adverting'=>1]);
  1313. return $this->success($result);
  1314. }else{
  1315. // Log::info(Auth::user()->nickname.'关闭广告失败:'.json_encode($result));
  1316. return $this->error('450001',$result['message']);
  1317. }
  1318. }
  1319. //手动退款
  1320. public function refundAmountManual(Request $request){
  1321. $order_no=$request->input('order_no');
  1322. $order=OrderW::where('order_no',$order_no)->first();
  1323. $sub_mchid=Store::where('id',$order->store_id)->value('sub_mchid');
  1324. if(empty($sub_mchid)){
  1325. return $this->error('450001','店铺信息有误,暂不能退款');
  1326. }
  1327. if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
  1328. $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
  1329. }else{
  1330. return $this->error('450001','订单状态有误,暂不能退款');
  1331. }
  1332. // $pay_money=$request->input('pay_money');
  1333. // $sub_mchid = $request->input('sub_mchid');
  1334. $wechat_pay_no = $order->wechat_pay_no;
  1335. // $out_refund_no = $request->input('out_refund_no');
  1336. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
  1337. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1338. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1339. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1340. $timestamp=time();//时间戳
  1341. $nonce=$this->nonce_str();//随机字符串
  1342. $data=[
  1343. 'sub_mchid'=>$sub_mchid,
  1344. 'sp_appid'=>config('wechat.payment.default.app_id'),
  1345. 'transaction_id'=>$wechat_pay_no,
  1346. // 'out_trade_no'=> $order_no,
  1347. 'out_refund_no'=> 'TK'.substr($order_no,2),
  1348. 'amount'=> $amount,
  1349. ];
  1350. // return $this->success($data);
  1351. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1352. $header=[
  1353. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1354. 'Accept:application/json',
  1355. 'User-Agent:' . $merchant_id,
  1356. 'Content-Type:application/json',
  1357. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1358. ];
  1359. $result=$this->curl($url,json_encode($data),$header,'POST');
  1360. $result=json_decode($result,true);
  1361. return $this->success($result);
  1362. if(isset($result['refund_id']) && !empty($result['refund_id'])){
  1363. return $this->success($result);
  1364. }
  1365. return $this->error();
  1366. }
  1367. //退款
  1368. public function refundAmount(Request $request){
  1369. $order_no=$request->input('order_no');
  1370. $order=OrderW::where('order_no',$order_no)->first();
  1371. $sub_mchid=Store::where('id',$order->store_id)->value('sub_mchid');
  1372. if(empty($sub_mchid)){
  1373. return $this->error('450001','店铺信息有误,暂不能退款');
  1374. }
  1375. if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
  1376. $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
  1377. }else{
  1378. return $this->error('450001','订单状态有误,暂不能退款');
  1379. }
  1380. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
  1381. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1382. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1383. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1384. $timestamp=time();//时间戳
  1385. $nonce=$this->nonce_str();//随机字符串
  1386. $data=[
  1387. 'sub_mchid'=>$sub_mchid,
  1388. 'sp_appid'=>config('wechat.payment.default.app_id'),
  1389. 'transaction_id'=>$order->wechat_pay_no,
  1390. // 'out_trade_no'=> $order_no,
  1391. 'out_refund_no'=> 'TK'.substr($order_no,2),
  1392. 'amount'=> $amount,
  1393. 'notify_url'=>url('api/miniSub/refund_notify')
  1394. ];
  1395. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1396. $header=[
  1397. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1398. 'Accept:application/json',
  1399. 'User-Agent:' . $merchant_id,
  1400. 'Content-Type:application/json',
  1401. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1402. ];
  1403. $result=$this->curl($url,json_encode($data),$header,'POST');
  1404. $result=json_decode($result,true);
  1405. if(isset($result['refund_id']) && !empty($result['refund_id'])){
  1406. // OrderW::where('order_no',$order_no)
  1407. // ->update(['refund_status'=>'START','refund_no'=>$result['refund_id'],'refund_time'=>$result['create_time']]);
  1408. OrderRefundMoneyW::create([
  1409. 'order_no'=>$order_no,
  1410. 'refund_status'=>'START',
  1411. 'refund_time'=>$result['create_time'],
  1412. 'refund_no'=>$result['refund_id'],
  1413. 'refund_account'=>null,
  1414. 'refund_amount'=>null
  1415. ]);
  1416. return $this->success($result);
  1417. }else{
  1418. return $this->error('450001',$result['message']);
  1419. }
  1420. }
  1421. //退款状态查询
  1422. public function getRefundState(Request $request){
  1423. $out_refund_no=$request->input('out_refund_no');
  1424. $sub_mchid=$request->input('sub_mchid');
  1425. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/out-refund-no/'.$out_refund_no.'?sub_mchid='.$sub_mchid;
  1426. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1427. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1428. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1429. $timestamp=time();//时间戳
  1430. $nonce=$this->nonce_str();//随机字符串
  1431. $body="";
  1432. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1433. $header=[
  1434. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1435. 'Accept:application/json',
  1436. 'User-Agent:' . $merchant_id,
  1437. 'Content-Type:application/json',
  1438. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1439. ];
  1440. $result=$this->curl($url,'',$header,'GET');
  1441. $result=json_decode($result,true);
  1442. return $result;
  1443. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  1444. // $reason=$result['reason'];
  1445. // }else{
  1446. // $reason=null;
  1447. // }
  1448. // Amount::where('withdraw_id',$withdraw_id)
  1449. // ->update([
  1450. // 'status'=>0,
  1451. // 'status_code'=>$result['status'],
  1452. // 'reason'=>$reason,
  1453. // ]);
  1454. // return $this->success($result);
  1455. }
  1456. //微信支付状态查询
  1457. public function getPayState(Request $request){
  1458. $out_refund_no=$request->input('out_refund_no');
  1459. $sub_mchid=$request->input('sub_mchid');
  1460. $sp_mchid=config('wechat.payment.default.mch_id');
  1461. // $url='https://api.mch.weixin.qq.com/v3/pay/partner/transactions/out-trade-no/'.$out_refund_no.'?sp_mchid='.$sp_mchid.'&sub_mchid='.$sub_mchid;
  1462. $url=' https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/'.$out_refund_no.'?sp_mchid='.$sp_mchid.'&sub_mchid='.$sub_mchid;
  1463. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1464. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1465. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1466. $timestamp=time();//时间戳
  1467. $nonce=$this->nonce_str();//随机字符串
  1468. $body="";
  1469. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1470. $header=[
  1471. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1472. 'Accept:application/json',
  1473. 'User-Agent:' . $merchant_id,
  1474. 'Content-Type:application/json',
  1475. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1476. ];
  1477. $result=$this->curl($url,'',$header,'GET');
  1478. $result=json_decode($result,true);
  1479. return $result;
  1480. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  1481. // $reason=$result['reason'];
  1482. // }else{
  1483. // $reason=null;
  1484. // }
  1485. // Amount::where('withdraw_id',$withdraw_id)
  1486. // ->update([
  1487. // 'status'=>0,
  1488. // 'status_code'=>$result['status'],
  1489. // 'reason'=>$reason,
  1490. // ]);
  1491. // return $this->success($result);
  1492. }
  1493. //退款通知
  1494. public function refundNotify(){
  1495. Log::info('开始退款通知');
  1496. defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
  1497. //接收微信返回的数据数据,返回的xml格式
  1498. $xmlData = file_get_contents('php://input');
  1499. //将xml格式转换为数组
  1500. $xmlData=json_decode($xmlData,true);
  1501. $data=$xmlData['resource'];
  1502. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  1503. $result=$this->decryptToString($data['associated_data'],$data['nonce'],$data['ciphertext'],$sign_key);
  1504. $result=json_decode($result,true);
  1505. log::info(json_encode($result));
  1506. if($result['refund_status']=='SUCCESS'){
  1507. if(strpos($result['out_trade_no'],'_')){
  1508. $result['out_trade_no']=explode('_',$result['out_trade_no'])['1'];
  1509. }
  1510. $order=OrderW::withTrashed()->where('order_no',$result['out_trade_no'])->first();
  1511. // $order->refund_status =$result['refund_status'];
  1512. // $order->refund_time =$result['success_time'];
  1513. // $order->refund_no =$result['refund_id'];
  1514. // $order->refund_account =$result['user_received_account'];
  1515. // $order->refund_amount =json_encode($result['amount']);
  1516. // $order->save();
  1517. DB::connection('mysql_w')->transaction(function()use($order,$result) {
  1518. OrderRefundMoneyW::where('order_no', $result['out_trade_no'])->where('refund_no', $result['refund_id'])
  1519. ->update([
  1520. 'refund_status' => $result['refund_status'],
  1521. 'refund_time' => $result['success_time'],
  1522. 'refund_account' => $result['user_received_account'],
  1523. 'refund_amount' => json_encode($result['amount']),
  1524. ]);
  1525. //学分变更
  1526. // $integral = $order->total * 2;
  1527. $integral = 0;
  1528. $inte = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 2)->first();
  1529. if (empty($inte)) {
  1530. $inte_add = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 1)->first();
  1531. if($inte_add){
  1532. // $integral_double= $inte_add->integral_double;
  1533. $integral= $inte_add->integral;
  1534. $integral_double= $inte_add->integral_double;
  1535. $is_zbs = $inte_add->is_zbs;
  1536. }else{
  1537. $integral_double= $integral;
  1538. $is_zbs = 0;
  1539. }
  1540. IntegralW::create([
  1541. 'store_id' => $order->store_id,
  1542. 'order_no' => $order->order_no,
  1543. 'num' => 0 - $order->total,
  1544. 'integral' => 0 - $integral,
  1545. 'integral_double' => 0 - $integral_double,
  1546. 'type' => 2,
  1547. 'remark' => '取消订单',
  1548. 'is_zbs' => $is_zbs,
  1549. ]);
  1550. }
  1551. //店铺信息q
  1552. $total_fee = round($result['amount']['refund'] / 100, 2);
  1553. $store = Store::where('id', $order->store_id)->lockForUpdate()->first();
  1554. $start_money = $store->pending_amount;
  1555. $store->pending_amount -= $total_fee;
  1556. $store->integral -= $integral;
  1557. $store->cycle_inte -= $integral;
  1558. $store->total -= $order->total;
  1559. $store->save();
  1560. //退款日志
  1561. Amount::create([
  1562. 'user_id' => $order->user_id,
  1563. 'store_id' => $order->store_id,
  1564. 'order_no' => $result['out_refund_no'],
  1565. 'transaction_id' => $result['refund_id'],
  1566. 'money' => $total_fee,
  1567. 'type' => 4, //退款
  1568. 'status' => 1, //成功
  1569. 'service_fee' => 0,
  1570. 'start_money' => $start_money,//支付前冻结金额
  1571. 'end_money' => $store->pending_amount,//支付后冻结金额
  1572. 'start_amount' => $store->available_amount,//支付前可用金额
  1573. 'end_amount' => $store->available_amount,//支付后可用金额
  1574. 'remark' => '退款',
  1575. ]);
  1576. },5);
  1577. Log::info('完成退款通知');
  1578. return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
  1579. }else{
  1580. OrderW::where('order_no',$result['out_trade_no'])->update([
  1581. 'refund_status'=> $result['refund_status'],
  1582. 'refund_no'=> $result['refund_id'],
  1583. ]);
  1584. Log::info('退款通知失败');
  1585. return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
  1586. }
  1587. //为了防止假数据,验证签名是否和返回的一样。
  1588. //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
  1589. // $sign = $data['sign'];
  1590. // unset($data['sign']);
  1591. // if($sign == self::getSign($data,config('wechat.payment.default.app_v3'))){
  1592. // //签名验证成功后,判断返回微信返回的
  1593. // if ($data['result_code'] == 'SUCCESS') {
  1594. // //根据返回的订单号做业务逻辑
  1595. // Log::info(json_encode($data));
  1596. // }else{//支付失败,输出错误信息
  1597. // fwrite($file,date("Y-m-d H:i:s")."错误信息:支付失败,".json_encode($data).PHP_EOL);
  1598. // }
  1599. // }else{
  1600. // fwrite($file,date("Y-m-d H:i:s")."错误信息:签名验证失败,".json_encode($data).PHP_EOL);
  1601. // }
  1602. }
  1603. //查询分账
  1604. public function fenzhang()
  1605. {
  1606. $sub_mchid = '1608091453';
  1607. $transaction_id = '4200000933202104068603011660';
  1608. $out_order_no = 'WJ21040615412878976147481';
  1609. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders?sub_mchid=' . $sub_mchid . '&transaction_id=' . $transaction_id . '&out_order_no=' . $out_order_no;//查询地址
  1610. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1611. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1612. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1613. $timestamp = time();//时间戳
  1614. $nonce = $this->nonce_str();//随机字符串
  1615. $body = "";
  1616. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  1617. $header = [
  1618. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1619. 'Accept:application/json',
  1620. 'User-Agent:' . $merchant_id,
  1621. 'Content-Type:application/json',
  1622. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1623. ];
  1624. $result = $this->curl($url, '', $header, 'GET');
  1625. $result = json_decode($result, true);
  1626. return $this->success($result);
  1627. }
  1628. //查询分账
  1629. public function searchFenzhang(Request $request)
  1630. {
  1631. $sub_mchid = $request->input('sub_mchid');
  1632. $transaction_id = $request->input('transaction_id');
  1633. $out_order_no = $request->input('out_order_no');
  1634. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/orders?sub_mchid=' . $sub_mchid . '&transaction_id=' . $transaction_id . '&out_order_no=' . $out_order_no;//查询地址
  1635. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1636. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1637. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1638. $timestamp = time();//时间戳
  1639. $nonce = $this->nonce_str();//随机字符串
  1640. $body = "";
  1641. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  1642. $header = [
  1643. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1644. 'Accept:application/json',
  1645. 'User-Agent:' . $merchant_id,
  1646. 'Content-Type:application/json',
  1647. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1648. ];
  1649. $result = $this->curl($url, '', $header, 'GET');
  1650. $result = json_decode($result, true);
  1651. return $this->success($result);
  1652. }
  1653. //完结分账信息
  1654. public function finish(Request $request)
  1655. {
  1656. $order_no=$request->input('order_no');
  1657. event(new FinishAccount($order_no));
  1658. return $this->success('完结结束');
  1659. }
  1660. //只完结不记录数据
  1661. public function simpleFinish(Request $request)
  1662. {
  1663. $wechat_pay_no= '4200001141202110183876870599';
  1664. $sub_mchid = '1609518357';
  1665. $order_no='6530_DX21101815293983868465657';
  1666. $url = 'https://api.mch.weixin.qq.com/v3/ecommerce/profitsharing/finish-order';//地址
  1667. $merchant_id = config('wechat.payment.default.mch_id');//商户号
  1668. $serial_no = config('wechat.payment.default.serial_no');//不是平台证书序列号
  1669. $handler = new SignHandler();
  1670. $mch_private_key = $handler->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1671. $timestamp = time();//时间戳
  1672. $nonce = $handler->nonce_str();//随机字符串
  1673. $data = [
  1674. 'sub_mchid' => $sub_mchid,
  1675. 'transaction_id' => $wechat_pay_no,//微信支付单号
  1676. 'out_order_no' => 'WJ' . $order_no,//订单号
  1677. 'description' => '分账完结',
  1678. ];
  1679. $sign = $handler->sign($url, 'POST', $timestamp, $nonce, json_encode($data), $mch_private_key, $merchant_id, $serial_no);//签名
  1680. $header = [
  1681. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1682. 'Accept:application/json',
  1683. 'User-Agent:' . $merchant_id,
  1684. 'Content-Type:application/json',
  1685. 'Wechatpay-Serial:' . $handler->getzhengshu()//获取平台证书序列号
  1686. ];
  1687. $result = $handler->curl($url, json_encode($data), $header);
  1688. Log::info($result);
  1689. $result = json_decode($result, true);
  1690. return $result;
  1691. }
  1692. public function setBankInfodemo(Request $request){
  1693. $id=Auth::user()->id;
  1694. $sub_mchid=Store::where('user_id',$id)->value('sub_mchid');
  1695. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
  1696. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1697. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1698. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1699. $timestamp=time();//时间戳
  1700. $nonce=$this->nonce_str();//随机字符串
  1701. $bank_address_code= $request->input('bank_address_code');
  1702. if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
  1703. $bank_address_code=substr($bank_address_code,0,2).'00';
  1704. }
  1705. $data=[
  1706. 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
  1707. 'account_bank' =>$request->input('account_bank'),
  1708. 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
  1709. 'bank_name'=>$request->input('bank_name'),
  1710. 'bank_branch_id'=>$request->input('bank_branch_id'),
  1711. 'account_number'=>$this->getEncrypts($request->input('account_number'))
  1712. ];
  1713. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1714. $header=[
  1715. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1716. 'Accept:application/json',
  1717. 'User-Agent:' . $merchant_id,
  1718. 'Content-Type:application/json',
  1719. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1720. ];
  1721. $result=$this->curls($url,json_encode($data),$header);
  1722. $http_status=$result[1];
  1723. $result=json_decode($result[0],true);
  1724. return $result;
  1725. if($http_status==204){
  1726. Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
  1727. Store::where('user_id',$id)
  1728. ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
  1729. return $this->success($result);
  1730. }else{
  1731. Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].'商户id为:'.$sub_mchid);
  1732. return $this->error('450001',$result['message']);
  1733. }
  1734. }
  1735. public function searchAccount(){
  1736. $user=User::find(13878);
  1737. $token = Auth::guard('api')->fromUser($user);
  1738. dd($token);
  1739. }
  1740. public function getBankInfodemo(Request $request){
  1741. $sub_mchid=Store::where('user_id',$request->id)->value('sub_mchid');
  1742. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/settlement';//'https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//查询地址
  1743. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1744. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1745. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1746. $timestamp=time();//时间戳
  1747. $nonce=$this->nonce_str();//随机字符串
  1748. $body="";
  1749. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1750. $header=[
  1751. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1752. 'Accept:application/json',
  1753. 'User-Agent:' . $merchant_id,
  1754. 'Content-Type:application/json',
  1755. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1756. ];
  1757. $result=$this->curl($url,'',$header,'GET');
  1758. dd($result);
  1759. $result=json_decode($result,true);
  1760. return $this->success($result);
  1761. }
  1762. //获取支持个人业务的银行列表
  1763. public function getPersonBankList(){
  1764. $offset=0;
  1765. $limit=100;
  1766. $url='https://api.mch.weixin.qq.com/v3/capital/capitallhh/banks/personal-banking?offset='.$offset.'&limit='.$limit;
  1767. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1768. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1769. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1770. $timestamp=time();//时间戳
  1771. $nonce=$this->nonce_str();//随机字符串
  1772. $body="";
  1773. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1774. $header=[
  1775. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1776. 'Accept:application/json',
  1777. 'User-Agent:' . $merchant_id,
  1778. 'Content-Type:application/json',
  1779. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1780. ];
  1781. $result=$this->curl($url,'',$header,'GET');
  1782. $result=json_decode($result,true);
  1783. return $this->success($result);
  1784. }
  1785. //获取省份列表
  1786. public function getProvinceList(){
  1787. $url='https://api.mch.weixin.qq.com/v3/capital/capitallhh/areas/provinces';
  1788. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1789. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1790. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1791. $timestamp=time();//时间戳
  1792. $nonce=$this->nonce_str();//随机字符串
  1793. $body="";
  1794. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1795. $header=[
  1796. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1797. 'Accept:application/json',
  1798. 'User-Agent:' . $merchant_id,
  1799. 'Content-Type:application/json',
  1800. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1801. ];
  1802. $result=$this->curl($url,'',$header,'GET');
  1803. $result=json_decode($result,true);
  1804. return $this->success($result);
  1805. }
  1806. //获取对应城市列表
  1807. public function getCityList(Request $request){
  1808. $province_code=$request->input('province_code');
  1809. $url='https://api.mch.weixin.qq.com/v3/capital/capitallhh/areas/provinces/'.$province_code.'/cities';
  1810. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1811. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1812. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1813. $timestamp=time();//时间戳
  1814. $nonce=$this->nonce_str();//随机字符串
  1815. $body="";
  1816. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1817. $header=[
  1818. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1819. 'Accept:application/json',
  1820. 'User-Agent:' . $merchant_id,
  1821. 'Content-Type:application/json',
  1822. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1823. ];
  1824. $result=$this->curl($url,'',$header,'GET');
  1825. $result=json_decode($result,true);
  1826. foreach($result['data'] as $key=>$val){
  1827. CityW::where('city',$val['city_name'])->update(['link_code'=>$val['city_code']]);
  1828. }
  1829. return $this->success($result);
  1830. }
  1831. //获取对应支行列表
  1832. public function getBankBranchList(Request $request){
  1833. $bank_alias_code=$request->input('bank_alias_code');
  1834. $city_code=$request->input('city_code');
  1835. $offset=$request->input('offset');
  1836. $limit=$request->input('limit');
  1837. $url='https://api.mch.weixin.qq.com/v3/capital/capitallhh/banks/'.$bank_alias_code.'/branches?city_code='.$city_code.'&offset='.$offset.'&limit='.$limit;
  1838. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1839. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1840. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1841. $timestamp=time();//时间戳
  1842. $nonce=$this->nonce_str();//随机字符串
  1843. $body="";
  1844. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1845. $header=[
  1846. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1847. 'Accept:application/json',
  1848. 'User-Agent:' . $merchant_id,
  1849. 'Content-Type:application/json',
  1850. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1851. ];
  1852. $result=$this->curl($url,'',$header,'GET');
  1853. $result=json_decode($result,true);
  1854. return $this->success($result);
  1855. }
  1856. //修改银行账户信息
  1857. public function setBankInfoTest(Request $request){
  1858. Log::error($request->all());
  1859. // if (Auth::user()->id==32531){
  1860. // $user_id='13878';
  1861. // }else{
  1862. // $user_id=Auth::user()->id;
  1863. // }
  1864. $sub_mchid=Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)->value('sub_mchid');
  1865. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
  1866. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1867. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1868. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1869. $timestamp=time();//时间戳
  1870. $nonce=$this->nonce_str();//随机字符串
  1871. $bank_address_code= $request->input('bank_address_code');
  1872. if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
  1873. $bank_address_code=substr($bank_address_code,0,2).'00';
  1874. }
  1875. Log::info($bank_address_code);
  1876. $data=[
  1877. 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
  1878. 'account_bank' =>$request->input('account_bank'),
  1879. 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
  1880. // 'bank_name'=>$request->input('bank_name'),
  1881. // 'bank_branch_id'=>$request->input('bank_branch_id'),
  1882. 'account_number'=>$this->getEncrypts($request->input('account_number'))
  1883. ];
  1884. return $data;
  1885. Log::info($data);
  1886. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1887. $header=[
  1888. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1889. 'Accept:application/json',
  1890. 'User-Agent:' . $merchant_id,
  1891. // 'Content-Type:multipart/form-data',
  1892. 'Content-Type:application/json',
  1893. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1894. ];
  1895. $result=$this->curls($url,json_encode($data),$header);
  1896. Log::error('修改结算账户返回结果。'.json_encode($result));
  1897. $http_status=$result[1];
  1898. $result=json_decode($result[0],true);
  1899. if($http_status==204){
  1900. Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
  1901. Store::withTrashed()->where('is_failure',0)->where('user_id',Auth::user()->id)
  1902. ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
  1903. return $this->success($result);
  1904. }else{
  1905. Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].',商户id为:'.$sub_mchid);
  1906. return $this->error('450001',$result['message']);
  1907. }
  1908. }
  1909. // public function pay(Request $request){
  1910. // $order_no=$request->input('order_no');
  1911. // $order=OrderW::where('order_no',$order_no)->first();
  1912. // $store=Store::where('id',$order->store_id)->first();
  1913. // $openid=User::where('id',Auth::user()->id)->value('openid');
  1914. // $info=array(//订单信息
  1915. // "sp_mchid" => config('wechat.payment.test.mch_id'),//服务商的商户号
  1916. // "sp_appid" => config('wechat.payment.test.app_id'),//服务商的appid
  1917. // "sub_mchid" => $store->sub_mchid,//二级商户的商户号
  1918. // "description" => "测试JSAPI下单API接口",//
  1919. // "out_trade_no" => $order_no,//商户订单号
  1920. // "amount" => array(//支付信息
  1921. // "total" =>1,//支付金额 单位为分 int类型
  1922. // "currency" =>"CNY"//币种 CNY人民币
  1923. // ),
  1924. // "payer" =>array(//支付用户的信息
  1925. // "sp_openid"=>$openid //用户在服务商appid下的唯一标识
  1926. // ),
  1927. // "notify_url" => url('api/payment/notifyUrl') //支付回调地址 支付成功后 微信通知你服务器的地址
  1928. // );
  1929. // Log::info($info);
  1930. // $json=json_encode($info);
  1931. // $url="https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi";
  1932. //
  1933. // $appid=config('wechat.payment.test.app_id');
  1934. // $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1935. // $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1936. // $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1937. // $timestamp=time();//时间戳
  1938. // $nonce=$this->nonce_str();//随机字符串
  1939. //
  1940. // $sign=$this->sign($url,'POST',$timestamp,$nonce,$json,$mch_private_key,$merchant_id,$serial_no);//签名
  1941. // $header=[
  1942. // 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
  1943. // 'Accept:application/json',
  1944. // 'User-Agent:'.$merchant_id,
  1945. // 'Content-Type:application/json',
  1946. // ];
  1947. // $result=$this->curl($url,$json,$header,'POST');
  1948. // $result=json_decode($result,true);
  1949. // Log::info($result);
  1950. // $prepayID="prepay_id=".$result['prepay_id'];//字符串拼接
  1951. // $key="微信证书v3 32位密钥字符串"; //微信证书v3密钥 在微信商户平台 API安全中设置的
  1952. // $qianming=$this->signJS($appid,$timestamp,$nonce,$prepayID,$mch_private_key);//二次签名
  1953. // //一下几个参数需要传到前端 js中使用
  1954. // $data['timestamp'] = $timestamp;//上文生成的时间戳
  1955. // $data['nonceStr'] = $nonce;//上文生成的随机字符串
  1956. // $data['package'] = $prepayID;//微信返回的预支付订单标识
  1957. // $data['signType'] = "RSA";//签名方式
  1958. // $data['paySign'] = $qianming;//二次签名
  1959. // $data['appId'] = $appid;//商户的appid
  1960. // Log::info($data);
  1961. // return response()->json([
  1962. // 'error_code'=>200,
  1963. // 'code'=>200,
  1964. // 'msg'=>'成功',
  1965. // 'data'=>$data
  1966. // ]);
  1967. // }
  1968. // 二次签名
  1969. // public function signJS($wxid,$timeStamp,$nonce,$package,$mch_private_key){
  1970. // $message=
  1971. // $wxid . "\n".
  1972. // $timeStamp . "\n".
  1973. // $nonce . "\n".
  1974. // $package . "\n";
  1975. // openssl_sign($message,$raw_sign,$mch_private_key,'sha256');
  1976. // $sign=base64_encode($raw_sign);
  1977. // return $sign;
  1978. // }
  1979. //回调地址
  1980. public function notifyUrl(){
  1981. $header = $this->getHeaders();//读取http头信息 见下文
  1982. $body = file_get_contents('php://input');//读取微信传过来的信息,是一个json字符串
  1983. Log::info($header);
  1984. Log::info($body);
  1985. if (empty($header)|| empty($body)) {
  1986. throw new Exception('通知参数为空', 2001);
  1987. }
  1988. $timestamp = $header['WECHATPAY-TIMESTAMP'];
  1989. $nonce = $header['WECHATPAY-NONCE'];
  1990. $signature = $header['WECHATPAY-SIGNATURE'];
  1991. $serialNo = $header['WECHATPAY-SERIAL'];
  1992. if (empty($timestamp) || empty($nonce) || empty($signature) || empty($serialNo)) {
  1993. Log::info('通知头参数为空');
  1994. return ;
  1995. }
  1996. $cert = $this->getzhengshu();
  1997. if($cert!=$serialNo){
  1998. Log::info('验签失败');
  1999. return ;
  2000. }
  2001. $decodeBody = json_decode($body,true);
  2002. if (empty($decodeBody) || !isset($decodeBody['resource'])) {
  2003. Log::info('通知参数内容为空');
  2004. return ;
  2005. }
  2006. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  2007. $decodeBodyResource = $decodeBody['resource'];
  2008. $decodeData = $this->decryptToString($decodeBodyResource['associated_data'], $decodeBodyResource['nonce'],$decodeBodyResource['ciphertext'], $sign_key);//解密resource
  2009. $decodeData = json_decode($decodeData, true);
  2010. Log::info($decodeData);
  2011. $arr=array("code"=>"SUCCESS","message"=>"");
  2012. return json_encode($arr);
  2013. }
  2014. //获取微信回调http头信息
  2015. public function getHeaders(){
  2016. $headers = array();
  2017. foreach ($_SERVER as $key => $value) {
  2018. if ('HTTP_' == substr($key, 0, 5)) {
  2019. $headers[str_replace('_', '-', substr($key, 5))] = $value;
  2020. }
  2021. if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
  2022. $header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST'];
  2023. } elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
  2024. $header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']);
  2025. }
  2026. if (isset($_SERVER['CONTENT_LENGTH'])) {
  2027. $header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];
  2028. }
  2029. if (isset($_SERVER['CONTENT_TYPE'])) {
  2030. $header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];
  2031. }
  2032. }
  2033. return $headers;
  2034. }
  2035. public function uploadAgentCard(Request $request)
  2036. {
  2037. //上传文件
  2038. if (!$request->hasFile('file')) {
  2039. return $this->error('450001','请上传文件');
  2040. }
  2041. //设置文件后缀白名单
  2042. $allowExt = ["jpg","png","jpeg"];
  2043. //获取文件
  2044. $file = $request->file('file');
  2045. // 获取七牛云配置信息
  2046. $config = config('filesystems.disks.qiniu');
  2047. // 构建鉴权对象
  2048. $auth = new Auths($config['access_key'], $config['secret_key']);
  2049. // 生成上传 Token
  2050. $token = $auth->uploadToken($config['bucket']);
  2051. //获取文件的绝对路径,但是获取到的在本地不能打开
  2052. $filePath = $file->getRealPath();
  2053. //获取文件的扩展名
  2054. $ext = $file->getClientOriginalExtension();
  2055. //获取上传文件的名称
  2056. $ext_name = $file->getClientOriginalName();
  2057. Log::info('上传文件的名称->'.$ext_name);
  2058. //判断是否是Excel
  2059. if (empty($ext) || !in_array(strtolower($ext), $allowExt)) {
  2060. return $this->error('450001','不允许的文件类型');
  2061. }
  2062. // 新文件名
  2063. $filename =date('ymdHis') . uniqid(mt_rand('100','999')) .'.'. $ext;
  2064. $dirname=$config['dirname'];
  2065. // 初始化 UploadManager 对象并进行文件的上传。
  2066. $uploadMgr = new UploadManager();
  2067. // 调用 UploadManager 的 putFile 方法进行文件的上传。
  2068. $uploadMgr->putFile($token, $dirname.'/'.$filename, $filePath);
  2069. // 获取文件完整路径
  2070. $filePath = $config['domain'].'/'.$dirname.'/'.$filename;
  2071. // 返回上传到云存储的文件名
  2072. return $this->success($filePath);
  2073. }
  2074. }