MiniSubController.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Events\FinishAccount;
  4. use App\Models\AddressW;
  5. use App\Models\IntegralW;
  6. use App\Models\Order;
  7. use App\Models\OrderRefundMoneyW;
  8. use App\Models\OrderW;
  9. use App\Models\Store;
  10. use App\Models\Apply;
  11. use App\Models\Bank;
  12. use App\Models\Amount;
  13. use App\Models\User;
  14. use GuzzleHttp\Client;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\DB;
  17. use Storage;
  18. use Illuminate\Support\Facades\Auth;
  19. use Illuminate\Support\Facades\Log;
  20. class MiniSubController extends Controller
  21. {
  22. //申请小微商户
  23. public function apply_wx(Request $request){
  24. $store=Store::where('user_id',Auth::user()->id)->first();
  25. if(empty($store)){
  26. return $this->error('450001','该店铺不能申请二级商户');
  27. }
  28. $out_request_no=$store->out_request_no??date('ymdHis').mt_rand(10000,99999);
  29. $contact_name=$request->input('contact_name');
  30. $contact_phone=$request->input('contact_phone');
  31. $contact_id_card=$request->input('contact_id_card');//"412326199612220311"
  32. $store_name=$this->filterEmoji($request->input('store_name'))??$this->filterEmoji($store->name);
  33. $store_name=preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z]+/u", '', $store_name);//^0-9^A-Z^a-z
  34. $store_name=mb_substr($store_name,0,16);
  35. if(mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $store_name))==0){
  36. return $this->error('450001','该店铺名称格式不符需重置');
  37. }
  38. $store_url=$request->input('store_url')??"http://api.app.jiuweiyun.cn/api/gzh";//
  39. $id_card_copy=$request->input('id_card_copy');
  40. $id_card_national=$request->input('id_card_national');
  41. $id_card_name=$request->input('id_card_name');
  42. $id_card_number=$request->input('id_card_number');
  43. $id_card_valid_time=$request->input('id_card_valid_time');//$request->input('id_card_valid_time');//"2027-09-07"
  44. // $account_name=$request->input('account_name');//开户人姓名
  45. // $account_bank=$request->input('account_bank');//银行
  46. // $bank_address_code=str_pad($request->input('bank_address_code'),6,"0",STR_PAD_RIGHT);//开户城市编码
  47. // $bank_branch_id=$request->input('sub_branch_id');//开户银行联行号
  48. // $bank_name=$request->input('bank_name');//开户行(支行)
  49. // $account_number=$request->input('account_number');//银行卡号
  50. $url="https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";//这个是微信的申请地址
  51. $param=array(
  52. "out_request_no" => $out_request_no,//申请编号,用户自定义就可以
  53. "organization_type" => "2401",//申请类型 2401代表申请的是小微商户(就是个人,没有营业执照的人),其它类型可参考微信v3文档
  54. "need_account_info" => false,//true,//是否填写结算银行账户,提现会提到这个账户上,设置true需要添加银行卡信息等。这里我就设置false了。因为绑定银行卡可以通过微信中的小程序“商家助手”自己去绑定。也可以通过微信的绑卡接口在写一个绑定银行卡的功能。
  55. // "account_info"=> [
  56. // "bank_account_type" => "75",
  57. // "account_name"=> $this->getEncrypts($account_name),//开户名称
  58. // "account_bank"=> $account_bank,//开户银行
  59. // "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
  60. // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
  61. // "bank_name"=> $bank_name,//开户银行全称(含支行)
  62. // "account_number"=> $this->getEncrypts($account_number),//银行账户'6214833787989683'
  63. // ],
  64. "contact_info" => array(//填写一个店铺的管理员信息
  65. "contact_type" => "65",//管理员类型 65是经营者/法人 其它类型看微信文档
  66. "contact_name" => $this->getEncrypts($contact_name),//管理员姓名getEncrypts是加密字符串的方法,见下文。
  67. "mobile_phone" => $this->getEncrypts($contact_phone),//管理员电话getEncrypts是加密字符串的方法
  68. "contact_id_card_number"=> $this->getEncrypts($contact_id_card),//管理员身份证号码
  69. ),
  70. "sales_scene_info" =>array(//店铺信息
  71. "store_name" =>$store_name,//店铺全称
  72. "store_url" =>$store_url //店铺的地址
  73. ),
  74. "merchant_shortname" =>$store_name,//商户简称
  75. "id_card_info" =>array(//经营者/法人的身份证信息
  76. "id_card_copy" =>$this->uploadmedia($id_card_copy),//身份证人像面uploadmedia()是上传图片到微信的方法,见下文
  77. "id_card_national" =>$this->uploadmedia($id_card_national),//身份证国徽面
  78. "id_card_name" =>$this->getEncrypts($id_card_name),//身份证上的姓名 getEncrypts()字符串加密方法 见下文
  79. "id_card_number" =>$this->getEncrypts($id_card_number),//身份证号,此身份证号可以和管理员是一个人
  80. "id_card_valid_time"=>$id_card_valid_time //身份证国徽面的到期日期
  81. )
  82. );
  83. Log::info(json_encode($param,JSON_UNESCAPED_UNICODE));
  84. $merchant_id = config('wechat.payment.default.mch_id'); //"1604585534"; //服务商的商户号
  85. $serial_no = config('wechat.payment.default.serial_no'); //"73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书的序列号,可在微信商户平台API安全 中获取
  86. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  87. $timestamp =time();//时间戳
  88. $nonce =$this->nonce_str();//获取随机字符串
  89. $sign =$this->sign($url,'POST',$timestamp,$nonce,json_encode($param),$mch_private_key,$merchant_id,$serial_no);//进行签名操作
  90. $header =[//设置发送的头信息
  91. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,//签名
  92. 'Accept:application/json',
  93. 'User-Agent:' . $merchant_id,//服务商的商户号
  94. 'Content-Type:application/json',
  95. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书的序列号,注意:是平台证书,不是上文的商户证书序列号
  96. ];
  97. $result=$this->curl($url,json_encode($param),$header);//curl方法见下文
  98. $result=json_decode($result,true);
  99. if(isset($result['code']) && !empty($result['code'])){
  100. return $this->error('450001',$result['message']??'未知错误');
  101. }
  102. if(isset($result['applyment_id']) && isset($result['out_request_no'])){
  103. DB::connection('mysql_w')->beginTransaction();
  104. try{
  105. Apply::create([
  106. 'store_id'=>$store->id,
  107. 'contact_name'=>$contact_name,
  108. 'contact_phone'=>$contact_phone,
  109. 'contact_id_card'=>$contact_id_card,
  110. 'store_name'=>$store_name,
  111. 'store_url'=>$store_url,
  112. 'id_card_copy'=>$id_card_copy,
  113. 'id_card_national'=>$id_card_national,
  114. 'id_card_name'=> $id_card_name,
  115. 'id_card_number'=>$id_card_number,
  116. 'id_card_valid_time'=>$id_card_valid_time,
  117. // "account_name"=> $account_name,//开户名称
  118. // "account_bank"=> $account_bank,//开户银行
  119. // "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
  120. // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
  121. // "bank_name"=> $bank_name,//开户银行全称(含支行)
  122. // "account_number"=> $account_number,//银行账户'6214833787989683'
  123. 'applyment_id'=>$result['applyment_id'],
  124. 'out_request_no'=>$result['out_request_no'],
  125. ]);
  126. Store::where('user_id',Auth::user()->id)
  127. ->update([
  128. 'username'=>$id_card_name,
  129. 'idCard'=>$id_card_number,
  130. 'cardImg1'=>$id_card_copy,
  131. 'cardImg2'=>$id_card_national,
  132. 'applyment_id'=>$result['applyment_id'],
  133. 'out_request_no'=>$result['out_request_no'],
  134. 'is_apply'=>1,//1已申请待审核 2 审核通过,待签约,3 已签约,已完成
  135. 'province'=>$request->input('province'),
  136. 'city'=>$request->input('city'),
  137. 'area'=>$request->input('area'),
  138. 'address'=>$request->input('address'),
  139. ]);
  140. // AddressW::create([
  141. // 'name'=>$contact_name,
  142. // 'phone'=>$contact_phone,
  143. // 'store_id'=>$store->id,
  144. // 'province'=>$request->input('province'),
  145. // 'city'=>$request->input('city'),
  146. // 'area'=>$request->input('area'),
  147. // 'address'=>$request->input('address'),
  148. // 'is_default'=>0
  149. // ]);
  150. Log::info('applyment_id:'.$result['applyment_id'].',&&out_request_no:'.$result['out_request_no']);
  151. DB::connection('mysql_w')->commit();
  152. return $this->success($result);
  153. }catch(\Exception $e){
  154. DB::connection('mysql_w')->rollBack();
  155. return $this->error('450001',$e->getMessage());
  156. }
  157. }else{
  158. return $this->error('450001','提交失败');
  159. }
  160. }
  161. //过滤微信表情符
  162. function filterEmoji($str)
  163. {
  164. $str = preg_replace_callback(
  165. '/./u',
  166. function (array $match) {
  167. return strlen($match[0]) >= 4 ? '' : $match[0];
  168. },
  169. $str);
  170. return $str;
  171. }
  172. //获取所有银行
  173. public function getAllBank(){
  174. return $this->success(Bank::whereNotNUll('bank_id')->groupBy('bank_name')->pluck('bank_name','bank_id'));//
  175. }
  176. //获取支行
  177. public function getBankBranch(Request $request){
  178. $city_code=$request->input('city_code');
  179. $bank_id=$request->input('bank_id');
  180. // if($city_code=='1101' || $city_code=='1201' || $city_code=='3101' || $city_code=='5001'){
  181. // $city_code-=$city_code;
  182. // }
  183. $city_code.='00';
  184. Log::info($city_code.'//'.$bank_id);
  185. $bank=Bank::where('city_id',$city_code)->where('bank_id',$bank_id)->get();
  186. return $this->success($bank);
  187. }
  188. //查询进件状态
  189. public function query(Request $request){
  190. $store=Store::where('user_id',Auth::user()->id)->first();
  191. $applyment_id=$store->applyment_id??$request->input('applyment_id');//'20200708093101909876';//申请编号
  192. Log:info('iii---------iii'.$applyment_id);
  193. if(empty($applyment_id)){
  194. return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
  195. }
  196. $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
  197. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  198. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  199. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  200. $timestamp=time();//时间戳
  201. $nonce=$this->nonce_str();//随机字符串
  202. $body="";
  203. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  204. $header=[
  205. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  206. 'Accept:application/json',
  207. 'User-Agent:' . $merchant_id,
  208. 'Content-Type:application/json',
  209. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  210. ];
  211. $result=$this->curl($url,'',$header,'GET');
  212. Log::info('------'.$result);
  213. $result=json_decode($result,true);
  214. $apply=Apply::where('store_id',$store->id)->where('applyment_id',$applyment_id)->OrderBy('id','desc')->first();
  215. if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
  216. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['legal_validation_url']]);
  217. }
  218. if($result['applyment_state']=="NEED_SIGN"){
  219. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
  220. Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
  221. }
  222. if($result['applyment_state']=="FINISH"){
  223. Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
  224. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  225. Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid']]);
  226. }
  227. if($result['applyment_state']=="REJECTED"){//已驳回
  228. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
  229. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  230. }
  231. if($result['applyment_state']=="FROZEN"){
  232. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
  233. Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  234. }
  235. Apply::where('id',$apply->id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
  236. return $this->success($result);
  237. // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
  238. }
  239. //查询进件状态
  240. public function query123(Request $request){
  241. $store=Store::where('user_id',Auth::user()->id)->first();
  242. // if(Auth::user()->id==32531){
  243. // $store=Store::where('user_id',9900)->first();
  244. // }
  245. $applyment_id=$request->input('applyment_id');//'20200708093101909876';//申请编号$store->applyment_id??
  246. Log:info('iii---------iii'.$applyment_id);
  247. if(empty($applyment_id)){
  248. return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
  249. }
  250. $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
  251. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  252. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  253. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  254. $timestamp=time();//时间戳
  255. $nonce=$this->nonce_str();//随机字符串
  256. $body="";
  257. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  258. $header=[
  259. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  260. 'Accept:application/json',
  261. 'User-Agent:' . $merchant_id,
  262. 'Content-Type:application/json',
  263. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  264. ];
  265. $result=$this->curl($url,'',$header,'GET');
  266. Log::info('------'.$result);
  267. $result=json_decode($result,true);
  268. return $this->success($result);
  269. if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
  270. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['legal_validation_url']]);
  271. }
  272. if($result['applyment_state']=="NEED_SIGN"){
  273. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
  274. Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
  275. }
  276. if($result['applyment_state']=="FINISH"){
  277. Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
  278. Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  279. Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
  280. }
  281. // if($result['applyment_state']=="AUDITING"){
  282. // Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
  283. // Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
  284. // }
  285. if($result['applyment_state']=="REJECTED"){//已驳回
  286. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
  287. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  288. }
  289. if($result['applyment_state']=="FROZEN"){
  290. Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
  291. Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
  292. }
  293. Apply::where('applyment_id',$applyment_id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
  294. return $this->success($result);
  295. // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
  296. }
  297. //加密敏感字符串
  298. public function getEncrypts($str="张三"){
  299. // $public_key_path='D:\phpstudy_pro\WWW\weidian-api/cert/cert.pem';//获取的文件临时保存到服务器
  300. $public_key_path=config('wechat.payment.default.cert_pem');//这是获取平台证书临时存起来的平台公钥,通过getzhengshu()方法获取,见下文。。。注意是平台证书,不是商户证书
  301. $public_key=file_get_contents($public_key_path);
  302. $encrypted='';
  303. if (openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)) {
  304. //base64编码
  305. $sign = base64_encode($encrypted);
  306. } else {
  307. throw new Exception('encrypt failed');
  308. }
  309. return $sign;
  310. // if(openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)){
  311. // $sign=base64_encode($encrypted);
  312. // }else{
  313. // echo "error";exit;
  314. // }
  315. // return $sign;//返回加密的敏感字符串
  316. }
  317. //上传证件图片
  318. public function uploadmedia($path){//$path="uploads/header/z.jpg"
  319. $path='/www/wwwroot/api.app.jiuweiyun.cn/app-api/public'.$path;
  320. $url="https://api.mch.weixin.qq.com/v3/merchant/media/upload";//微信的上传地址
  321. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  322. $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号,注意是商户证书,不是平台证书序列号
  323. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法
  324. $timestamp=time();//时间戳
  325. $nonce=$this->nonce_str();//随机字符串
  326. $fi=new \finfo(FILEINFO_MIME_TYPE);
  327. $mime_type=$fi->file($path);
  328. $meta=[
  329. 'filename'=>pathinfo($path,2),
  330. 'sha256'=>hash_file('sha256',$path)
  331. ];
  332. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($meta),$mch_private_key,$merchant_id,$serial_no);
  333. $boundary=uniqid();
  334. $header=[
  335. 'Content-Type:multipart/form-data;boundary=' . $boundary,
  336. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  337. 'Accept:application/json',
  338. 'User-Agent:' . $merchant_id
  339. ];
  340. $body='--' . $boundary . "\r\n";
  341. $body.='Content-Disposition:form-data; name="meta"' . "\r\n";
  342. $body.='Content-Type:application/json' . "\r\n\r\n";
  343. $body.=json_encode($meta)."\r\n";
  344. $body.='--' . $boundary . "\r\n";
  345. $body.='Content-Disposition:form-data;name="file";filename="' . $meta['filename'] . '"' . "\r\n";
  346. $body.='Content-Type:' .$mime_type."\r\n\r\n";
  347. $body.=file_get_contents($path)."\r\n";
  348. $body.='--'.$boundary .'--'."\r\n";
  349. $result=$this->curl($url,$body,$header);
  350. $result=json_decode($result,true);
  351. return $result['media_id'];//返回微信返回来的图片标识
  352. }
  353. //读取商户api证书公钥
  354. public function getPublicKey(){
  355. return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
  356. }
  357. //生成随机字符串
  358. public function nonce_str($length=32){
  359. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  360. $str ="";
  361. for ( $i = 0; $i < $length; $i++ ) {
  362. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  363. }
  364. return $str;
  365. }
  366. //签名
  367. public function sign($url,$http_method,$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no){
  368. $url_parts=parse_url($url);
  369. $canonical_url=($url_parts['path'].(!empty($url_parts['query'])?"?${url_parts['query']}":""));
  370. $message=
  371. $http_method . "\n".
  372. $canonical_url . "\n".
  373. $timestamp . "\n".
  374. $nonce . "\n".
  375. $body . "\n";
  376. openssl_sign($message,$raw_sign,$mch_private_key,'sha256WithRSAEncryption');
  377. $sign=base64_encode($raw_sign);
  378. $schema='WECHATPAY2-SHA256-RSA2048';
  379. $token=sprintf(
  380. 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
  381. $merchant_id,
  382. $nonce,
  383. $sign,
  384. $timestamp,
  385. $serial_no
  386. );
  387. return $token;
  388. }
  389. //curl提交
  390. public function curl($url,$data=[],$header,$method='POST'){
  391. $curl=curl_init();
  392. curl_setopt($curl,CURLOPT_URL,$url);
  393. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  394. curl_setopt($curl,CURLOPT_HEADER,false);
  395. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  396. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  397. if($method=="POST"){
  398. curl_setopt($curl,CURLOPT_POST,TRUE);
  399. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  400. }
  401. $result=curl_exec($curl);
  402. curl_close($curl);
  403. return $result;
  404. }
  405. public function curls($url,$data=[],$header,$method='POST'){
  406. $curl=curl_init();
  407. curl_setopt($curl,CURLOPT_URL,$url);
  408. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  409. curl_setopt($curl,CURLOPT_HEADER,false);
  410. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  411. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  412. if($method=="POST"){
  413. curl_setopt($curl,CURLOPT_POST,TRUE);
  414. curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
  415. }
  416. $result=curl_exec($curl);
  417. $status=curl_getinfo($curl,CURLINFO_HTTP_CODE);
  418. curl_close($curl);
  419. return [$result,$status];
  420. }
  421. //获取平台证书
  422. public function getzhengshu(){
  423. $url="https://api.mch.weixin.qq.com/v3/certificates";//获取地址
  424. $timestamp=time();//时间戳
  425. $nonce=$this->nonce_str();//获取一个随机字符串
  426. $body="";
  427. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥
  428. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  429. $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号
  430. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);
  431. $header=[
  432. 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
  433. 'Accept:application/json',
  434. 'User-Agent:'.$merchant_id
  435. ];
  436. $result=$this->curl($url,'',$header,'GET');
  437. $result=json_decode($result,true);
  438. $serial_no=$result['data'][0]['serial_no'];//获取的平台证书序列号
  439. $encrypt_certificate=$result['data'][0]['encrypt_certificate'];
  440. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  441. $result=$this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key);
  442. // return null;//file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem');
  443. // if(empty(file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem'))){
  444. // file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem',$result);
  445. file_put_contents(config('wechat.payment.default.cert_pem'),$result);//获取的文件临时保存到服务器
  446. // }
  447. return $serial_no;//返回平台证书序列号
  448. }
  449. //解密返回的信息
  450. public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){
  451. if (strlen($aesKey) != 32) {
  452. throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
  453. }
  454. // $this->aesKey = $aesKey;
  455. $ciphertext=\base64_decode($ciphertext);
  456. if (strlen($ciphertext) <= 16) {
  457. return false;
  458. }
  459. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
  460. \sodium_crypto_aead_aes256gcm_is_available()) {
  461. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  462. return '1111';
  463. }
  464. // ext-libsodium (need install libsodium-php 1.x via pecl)
  465. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
  466. \Sodium\crypto_aead_aes256gcm_is_available()) {
  467. return '222';
  468. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  469. }
  470. // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){
  471. // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey);
  472. // }
  473. if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){
  474. $ctext=substr($ciphertext,0,-16);
  475. $authTag=substr($ciphertext,-16);
  476. return \openssl_decrypt(
  477. $ctext,
  478. 'aes-256-gcm',
  479. $aesKey,
  480. \OPENSSL_RAW_DATA,
  481. $nonceStr,
  482. $authTag,
  483. $associatedData
  484. );
  485. }
  486. throw new \RuntimeException('php7.1');
  487. }
  488. //设置银行账户信息
  489. public function setBankInfo(Request $request){
  490. Log::error($request->all());
  491. // if (Auth::user()->id==32531){
  492. // $user_id='13878';
  493. // }else{
  494. // $user_id=Auth::user()->id;
  495. // }
  496. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  497. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
  498. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  499. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  500. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  501. $timestamp=time();//时间戳
  502. $nonce=$this->nonce_str();//随机字符串
  503. $bank_address_code= $request->input('bank_address_code');
  504. if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
  505. $bank_address_code=substr($bank_address_code,0,2).'00';
  506. }
  507. Log::info($bank_address_code);
  508. $data=[
  509. 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
  510. 'account_bank' =>$request->input('account_bank'),
  511. 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
  512. 'bank_name'=>$request->input('bank_name'),
  513. 'bank_branch_id'=>$request->input('bank_branch_id'),
  514. 'account_number'=>$this->getEncrypts($request->input('account_number'))
  515. ];
  516. Log::info($data);
  517. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  518. $header=[
  519. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  520. 'Accept:application/json',
  521. 'User-Agent:' . $merchant_id,
  522. // 'Content-Type:multipart/form-data',
  523. 'Content-Type:application/json',
  524. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  525. ];
  526. $result=$this->curls($url,json_encode($data),$header);
  527. Log::error('修改结算账户返回结果。'.json_encode($result));
  528. $http_status=$result[1];
  529. $result=json_decode($result[0],true);
  530. if($http_status==204){
  531. Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
  532. Store::where('user_id',Auth::user()->id)
  533. ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
  534. return $this->success($result);
  535. }else{
  536. Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].',商户id为:'.$sub_mchid);
  537. return $this->error('450001',$result['message']);
  538. }
  539. }
  540. //获取银行账户信息
  541. public function getBankInfo(){
  542. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  543. // if($sub_mchid=='1607429904'){
  544. // $sub_mchid='1609015697';
  545. // }
  546. // if($sub_mchid=='1607448291'){
  547. // $sub_mchid='1609035479';
  548. // }
  549. $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';//查询地址
  550. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  551. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  552. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  553. $timestamp=time();//时间戳
  554. $nonce=$this->nonce_str();//随机字符串
  555. $body="";
  556. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  557. $header=[
  558. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  559. 'Accept:application/json',
  560. 'User-Agent:' . $merchant_id,
  561. 'Content-Type:application/json',
  562. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  563. ];
  564. $result=$this->curl($url,'',$header,'GET');
  565. Log::error(Auth::user()->id.'//'.$result);
  566. $result=json_decode($result,true);
  567. return $this->success($result);
  568. }
  569. public function uploadImg(Request $request)
  570. {
  571. $path_url = 'public/wechat/' . date("ymd");
  572. $path = $request->file('img')->store($path_url);
  573. if ($path) {
  574. $url = Storage::url($path);
  575. return $this->success($url);
  576. } else {
  577. return $this->error();
  578. }
  579. }
  580. //获取账户实时余额
  581. public function getAccountInfo(){
  582. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  583. $result=$this->getAccount($sub_mchid);
  584. if($result['sub_mchid']){
  585. Store::where('sub_mchid',$result['sub_mchid'])
  586. ->update([
  587. 'available_amount'=>round($result['available_amount']/100,2),
  588. 'pending_amount'=>round($result['pending_amount']/100,2)
  589. ]);
  590. }
  591. return $this->success($result);
  592. }
  593. //获取账户信息
  594. public function getAccount($sub_mchid){
  595. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/'.$sub_mchid;//查询地址
  596. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  597. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  598. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  599. $timestamp=time();//时间戳
  600. $nonce=$this->nonce_str();//随机字符串
  601. $body="";
  602. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  603. $header=[
  604. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  605. 'Accept:application/json',
  606. 'User-Agent:' . $merchant_id,
  607. 'Content-Type:application/json',
  608. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  609. ];
  610. $result=$this->curl($url,'',$header,'GET');
  611. $result=json_decode($result,true);
  612. return $result;
  613. }
  614. //二级商户提现
  615. public function withdrawal(Request $request){
  616. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  617. Log::info('提现金额:'.$request->input('amount'));
  618. $withdrawal_amount=str_replace( ',', '',$request->input('amount'));
  619. if(!is_numeric($withdrawal_amount)){
  620. return $this->error('450001','提现金额为非数值');
  621. }
  622. $amount=floor($withdrawal_amount * 100);
  623. if($amount < 1){
  624. return $this->error('450001','提现金额有误请重新输入');
  625. }
  626. $phone=[];
  627. $agent_phone=Store::where('user_id',Auth::user()->id)->value('phone');
  628. if(!in_array($agent_phone,$phone)){
  629. $result=$this->getAccount($sub_mchid);
  630. if(!$result['sub_mchid']){
  631. return $this->error('450001','可提现金额小于200元,暂不能提现');
  632. }
  633. if($result['available_amount'] < 20000){
  634. return $this->error('450001','可提现金额小于200元,暂不能提现');
  635. }
  636. }
  637. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw';//查询地址
  638. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  639. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  640. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  641. $timestamp=time();//时间戳
  642. $nonce=$this->nonce_str();//随机字符串
  643. $out_request_no='TX'.date('ymdHis') . substr(microtime(), 2, 6).mt_rand(10000,99999);
  644. $data=[
  645. 'sub_mchid'=>$sub_mchid,
  646. 'out_request_no'=> $out_request_no,
  647. 'amount'=> (int) $amount,
  648. 'remark'=> '二级商户提现',
  649. 'bank_memo'=> '微信二级商户提现'
  650. ];
  651. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  652. $header=[
  653. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  654. 'Accept:application/json',
  655. 'User-Agent:' . $merchant_id,
  656. 'Content-Type:application/json',
  657. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  658. ];
  659. $result=$this->curl($url,json_encode($data),$header,'POST');
  660. $result=json_decode($result,true);
  661. if(isset($result['withdraw_id']) && !empty($result['withdraw_id'])){
  662. $store=Store::where('user_id',Auth::user()->id)->first();
  663. Amount::create([
  664. 'transaction_id'=>$result['withdraw_id'],
  665. 'order_no'=>$result['out_request_no'],
  666. 'store_id'=>$store->id,
  667. 'money'=>$amount/100,
  668. 'type'=>3,//提现
  669. 'status'=>0,
  670. 'status_code'=>'CREATE_SUCCESS',
  671. 'service_fee'=>0,
  672. 'remark'=>'提现',
  673. 'remark_amount'=>$store->account_bank.'-'.$store->account_number,
  674. ]);
  675. }else{
  676. return $this->error('450001',$result['message']);
  677. }
  678. return $this->success($result);
  679. }
  680. //商户提现状态查询
  681. public function getWithdrawalStates(Request $request){
  682. // $withdraw_id=$request->input('withdraw_id');
  683. // $user_id=$request->input('user_id');
  684. // $user_id='32531';
  685. $index=$request->input('index');
  686. $size=$request->input('size');
  687. $refunds=Amount::where('type',3)->orderBy('id')->skip($index)->take($size)->get();
  688. // return $refunds;
  689. foreach($refunds as $key=>$val){
  690. $sub_mchid=Store::where('id',$val->store_id)->value('sub_mchid');
  691. $withdraw_id=$val->order_no;
  692. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  693. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  694. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  695. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  696. $timestamp=time();//时间戳
  697. $nonce=$this->nonce_str();//随机字符串
  698. $body="";
  699. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  700. $header=[
  701. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  702. 'Accept:application/json',
  703. 'User-Agent:' . $merchant_id,
  704. 'Content-Type:application/json',
  705. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  706. ];
  707. $result=$this->curl($url,'',$header,'GET');
  708. $result=json_decode($result,true);
  709. if($val->status_code<>$result['status']){
  710. Log::info('提现状态有误:'.$val->store_id.'---'.$val->order_no.'---'.$result['status']);
  711. }
  712. Amount::where('id',$val->id)->update([
  713. 'transaction_id'=>$result['withdraw_id'],
  714. ]);
  715. }
  716. // $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
  717. return $result;
  718. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  719. // $reason=$result['reason'];
  720. // }else{
  721. // $reason=null;
  722. // }
  723. // Amount::where('withdraw_id',$withdraw_id)
  724. // ->update([
  725. // 'status'=>0,
  726. // 'status_code'=>$result['status'],
  727. // 'reason'=>$reason,
  728. // ]);
  729. // return $this->success($result);
  730. }
  731. //商户提现状态查询
  732. public function getWithdrawalState(Request $request){
  733. $withdraw_id=$request->input('withdraw_id');
  734. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  735. $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
  736. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  737. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  738. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  739. $timestamp=time();//时间戳
  740. $nonce=$this->nonce_str();//随机字符串
  741. $body="";
  742. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  743. $header=[
  744. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  745. 'Accept:application/json',
  746. 'User-Agent:' . $merchant_id,
  747. 'Content-Type:application/json',
  748. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  749. ];
  750. $result=$this->curl($url,'',$header,'GET');
  751. $result=json_decode($result,true);
  752. if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  753. $reason=$result['reason'];
  754. }else{
  755. $reason=null;
  756. }
  757. Amount::where('withdraw_id',$withdraw_id)
  758. ->update([
  759. 'status'=>0,
  760. 'status_code'=>$result['status'],
  761. 'reason'=>$reason,
  762. ]);
  763. return $this->success($result);
  764. }
  765. //获取收款记录
  766. public function getAccountDetail(Request $request){
  767. $type=$request->input('type');
  768. $cycle=$request->input('cycle');
  769. $page_size=$request->input('page_size');
  770. $page_index=$request->input('page_index');
  771. $startime=$request->input('startime');
  772. $endtime=$request->input('endtime');
  773. $num=$page_size*($page_index-1);
  774. $store=Store::where('user_id',Auth::user()->id)->first();
  775. $data= Amount::where('store_id',$store->id)->where('type',$type);
  776. if($startime && $endtime){
  777. $data->whereBetween('created_at',[date("Y-m-d 00:00:00",strtotime($startime)),date("Y-m-d 23:59:59",strtotime($endtime))]);
  778. }
  779. $cc=clone $data;
  780. $count=$data->count();
  781. if($type==3){
  782. $ll['account']=$cc->where('status_code','SUCCESS')->sum('money');
  783. }else{
  784. $ll['account']=$data->sum('money');
  785. }
  786. if($type==4){
  787. $data->with('refund_money:refund_no,order_no');
  788. }
  789. $ll['list']=$data->OrderByDesc('id')->skip($num)->take($page_size)->get();
  790. return $this->success_list($ll,'',$count);
  791. }
  792. //获取按日下载提现异常文件
  793. public function getDownWithdraw(Request $request){
  794. $user_id='79143';
  795. $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
  796. $bill_type=$request->input('bill_type');
  797. $bill_date=$request->input('bill_date');
  798. // $bill_date='';
  799. $tar_type=$request->input('tar_type');
  800. // $url='https://api.mch.weixin.qq.com/v3/merchant/fund/withdraw/bill-type/'.$bill_type.'?bill_date='.$bill_date;//查询地址 .'?tar_type='.$tar_type
  801. $url='https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date='.$bill_date;
  802. // $url='https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date='.$bill_date;
  803. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  804. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  805. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  806. $timestamp=time();//时间戳
  807. $nonce=$this->nonce_str();//随机字符串
  808. $body="";
  809. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  810. $header=[
  811. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  812. 'Accept:application/json',
  813. 'User-Agent:' . $merchant_id,
  814. 'Content-Type:application/json',
  815. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  816. ];
  817. $result=$this->curl($url,'',$header,'GET');
  818. // $result=json_decode($result,true);
  819. return $result;
  820. }
  821. public function down(Request $request){
  822. $url=$request->input('url');
  823. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  824. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  825. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  826. $timestamp=time();//时间戳
  827. $nonce=$this->nonce_str();//随机字符串
  828. $body="";
  829. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  830. $header=[
  831. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  832. 'Accept:application/json',
  833. 'User-Agent:' . $merchant_id,
  834. 'Content-Type:application/json',
  835. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  836. ];
  837. $curl=curl_init();
  838. curl_setopt($curl,CURLOPT_URL,$url);
  839. curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
  840. curl_setopt($curl,CURLOPT_HEADER,false);
  841. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  842. curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  843. $result=curl_exec($curl);
  844. curl_close($curl);
  845. return $result;
  846. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  847. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  848. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  849. $timestamp=time();//时间戳
  850. $nonce=$this->nonce_str();//随机字符串
  851. $body="";
  852. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  853. $header=[
  854. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  855. 'Accept:application/json',
  856. 'User-Agent:' . $merchant_id,
  857. 'Content-Type:application/json',
  858. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  859. ];
  860. $result=$this->curl($url,'',$header,'GET');
  861. $result=json_decode($result,true);
  862. return $this->success($result);
  863. }
  864. //开启点金计划
  865. public function dianjin(Request $request){
  866. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  867. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changegoldplanstatus';//查询地址
  868. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  869. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  870. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  871. $timestamp=time();//时间戳
  872. $nonce=$this->nonce_str();//随机字符串
  873. $data=[
  874. 'sub_mchid'=>$sub_mchid,
  875. 'operation_type'=>'OPEN'
  876. ];
  877. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  878. $header=[
  879. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  880. 'Accept:application/json',
  881. 'User-Agent:' . $merchant_id,
  882. 'Content-Type:application/json',
  883. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  884. ];
  885. $result=$this->curl($url,json_encode($data),$header,'POST');
  886. $result=json_decode($result,true);
  887. if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
  888. Store::where('sub_mchid',$sub_mchid)->update(['is_dianjin'=>1]);
  889. return $this->success($result);
  890. }else{
  891. return $this->error('450001',$result['message']);
  892. }
  893. }
  894. //开启商家小票
  895. public function businessTips(){
  896. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  897. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changecustompagestatus';//点金地址
  898. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  899. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  900. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  901. $timestamp=time();//时间戳
  902. $nonce=$this->nonce_str();//随机字符串
  903. $data=[
  904. 'sub_mchid'=>$sub_mchid,
  905. 'operation_type'=> 'OPEN'
  906. ];
  907. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  908. $header=[
  909. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  910. 'Accept:application/json',
  911. 'User-Agent:' . $merchant_id,
  912. 'Content-Type:application/json',
  913. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  914. ];
  915. $result=$this->curl($url,json_encode($data),$header,'POST');
  916. $result=json_decode($result,true);
  917. if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
  918. Store::where('sub_mchid',$sub_mchid)->update(['is_business'=>1]);
  919. return $this->success($result);
  920. }else{
  921. return $this->error('450001',$result['message']);
  922. }
  923. }
  924. //关闭广告
  925. public function adverting(Request $request){
  926. $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
  927. $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/close-advertising-show';//查询地址
  928. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  929. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  930. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  931. $timestamp=time();//时间戳
  932. $nonce=$this->nonce_str();//随机字符串
  933. $data=[
  934. 'sub_mchid'=>$sub_mchid
  935. ];
  936. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  937. $header=[
  938. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  939. 'Accept:application/json',
  940. 'User-Agent:' . $merchant_id,
  941. 'Content-Type:application/json',
  942. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  943. ];
  944. $result=$this->curl($url,json_encode($data),$header,'POST');
  945. $result=json_decode($result,true);
  946. if(empty($result)){
  947. Store::where('sub_mchid',$sub_mchid)->update(['is_adverting'=>1]);
  948. return $this->success($result);
  949. }else{
  950. // Log::info(Auth::user()->nickname.'关闭广告失败:'.json_encode($result));
  951. return $this->error('450001',$result['message']);
  952. }
  953. }
  954. //退款
  955. public function refundAmount(Request $request){
  956. $order_no=$request->input('order_no');
  957. $order=OrderW::where('order_no',$order_no)->first();
  958. $sub_mchid=Store::where('id',$order->store_id)->value('sub_mchid');
  959. if(empty($sub_mchid)){
  960. return $this->error('450001','店铺信息有误,暂不能退款');
  961. }
  962. if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
  963. $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
  964. }else{
  965. return $this->error('450001','订单状态有误,暂不能退款');
  966. }
  967. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
  968. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  969. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  970. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  971. $timestamp=time();//时间戳
  972. $nonce=$this->nonce_str();//随机字符串
  973. $data=[
  974. 'sub_mchid'=>$sub_mchid,
  975. 'sp_appid'=>config('wechat.payment.default.app_id'),
  976. 'transaction_id'=>$order->wechat_pay_no,
  977. // 'out_trade_no'=> $order_no,
  978. 'out_refund_no'=> 'TK'.substr($order_no,2),
  979. 'amount'=> $amount,
  980. 'notify_url'=>url('api/miniSub/refund_notify')
  981. ];
  982. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  983. $header=[
  984. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  985. 'Accept:application/json',
  986. 'User-Agent:' . $merchant_id,
  987. 'Content-Type:application/json',
  988. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  989. ];
  990. $result=$this->curl($url,json_encode($data),$header,'POST');
  991. $result=json_decode($result,true);
  992. if(isset($result['refund_id']) && !empty($result['refund_id'])){
  993. // OrderW::where('order_no',$order_no)
  994. // ->update(['refund_status'=>'START','refund_no'=>$result['refund_id'],'refund_time'=>$result['create_time']]);
  995. OrderRefundMoneyW::create([
  996. 'order_no'=>$order_no,
  997. 'refund_status'=>'START',
  998. 'refund_time'=>$result['create_time'],
  999. 'refund_no'=>$result['refund_id'],
  1000. 'refund_account'=>null,
  1001. 'refund_amount'=>null
  1002. ]);
  1003. return $this->success($result);
  1004. }else{
  1005. return $this->error('450001',$result['message']);
  1006. }
  1007. }
  1008. //退款状态查询
  1009. public function getRefundState(Request $request){
  1010. $out_refund_no=$request->input('out_refund_no');
  1011. $sub_mchid=$request->input('sub_mchid');
  1012. $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/out-refund-no/'.$out_refund_no.'?sub_mchid='.$sub_mchid;
  1013. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1014. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1015. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1016. $timestamp=time();//时间戳
  1017. $nonce=$this->nonce_str();//随机字符串
  1018. $body="";
  1019. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1020. $header=[
  1021. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1022. 'Accept:application/json',
  1023. 'User-Agent:' . $merchant_id,
  1024. 'Content-Type:application/json',
  1025. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1026. ];
  1027. $result=$this->curl($url,'',$header,'GET');
  1028. $result=json_decode($result,true);
  1029. return $result;
  1030. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  1031. // $reason=$result['reason'];
  1032. // }else{
  1033. // $reason=null;
  1034. // }
  1035. // Amount::where('withdraw_id',$withdraw_id)
  1036. // ->update([
  1037. // 'status'=>0,
  1038. // 'status_code'=>$result['status'],
  1039. // 'reason'=>$reason,
  1040. // ]);
  1041. // return $this->success($result);
  1042. }
  1043. //微信支付状态查询
  1044. public function getPayState(Request $request){
  1045. $out_refund_no=$request->input('out_refund_no');
  1046. $sub_mchid=$request->input('sub_mchid');
  1047. $sp_mchid=config('wechat.payment.default.mch_id');
  1048. // $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;
  1049. $url=' https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/'.$out_refund_no.'?sp_mchid='.$sp_mchid.'&sub_mchid='.$sub_mchid;
  1050. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1051. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1052. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1053. $timestamp=time();//时间戳
  1054. $nonce=$this->nonce_str();//随机字符串
  1055. $body="";
  1056. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1057. $header=[
  1058. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1059. 'Accept:application/json',
  1060. 'User-Agent:' . $merchant_id,
  1061. 'Content-Type:application/json',
  1062. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1063. ];
  1064. $result=$this->curl($url,'',$header,'GET');
  1065. return $this->success($result);
  1066. $result=json_decode($result,true);
  1067. return $result;
  1068. // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
  1069. // $reason=$result['reason'];
  1070. // }else{
  1071. // $reason=null;
  1072. // }
  1073. // Amount::where('withdraw_id',$withdraw_id)
  1074. // ->update([
  1075. // 'status'=>0,
  1076. // 'status_code'=>$result['status'],
  1077. // 'reason'=>$reason,
  1078. // ]);
  1079. // return $this->success($result);
  1080. }
  1081. //退款通知
  1082. public function refundNotify(){
  1083. Log::info('开始退款通知');
  1084. defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
  1085. //接收微信返回的数据数据,返回的xml格式
  1086. $xmlData = file_get_contents('php://input');
  1087. //将xml格式转换为数组
  1088. $xmlData=json_decode($xmlData,true);
  1089. $data=$xmlData['resource'];
  1090. $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
  1091. $result=$this->decryptToString($data['associated_data'],$data['nonce'],$data['ciphertext'],$sign_key);
  1092. $result=json_decode($result,true);
  1093. log::info(json_encode($result));
  1094. if($result['refund_status']=='SUCCESS'){
  1095. if(strpos($result['out_trade_no'],'_')){
  1096. $result['out_trade_no']=explode('_',$result['out_trade_no'])['1'];
  1097. }
  1098. $order=OrderW::withTrashed()->where('order_no',$result['out_trade_no'])->first();
  1099. // $order->refund_status =$result['refund_status'];
  1100. // $order->refund_time =$result['success_time'];
  1101. // $order->refund_no =$result['refund_id'];
  1102. // $order->refund_account =$result['user_received_account'];
  1103. // $order->refund_amount =json_encode($result['amount']);
  1104. // $order->save();
  1105. DB::connection('mysql_w')->transaction(function()use($order,$result) {
  1106. OrderRefundMoneyW::where('order_no', $result['out_trade_no'])->where('refund_no', $result['refund_id'])
  1107. ->update([
  1108. 'refund_status' => $result['refund_status'],
  1109. 'refund_time' => $result['success_time'],
  1110. 'refund_account' => $result['user_received_account'],
  1111. 'refund_amount' => json_encode($result['amount']),
  1112. ]);
  1113. //学分变更
  1114. $integral = $order->total * 5;
  1115. $inte = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 2)->first();
  1116. if (empty($inte)) {
  1117. $inte_add = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 1)->first();
  1118. if($inte_add){
  1119. $integral_double= $inte_add->integral_double;
  1120. }else{
  1121. $integral_double= integral;
  1122. }
  1123. IntegralW::create([
  1124. 'store_id' => $order->store_id,
  1125. 'order_no' => $order->order_no,
  1126. 'num' => $order->total,
  1127. 'integral' => 0 - $integral,
  1128. 'integral_double' => 0 - $integral_double,
  1129. 'type' => 2,
  1130. 'remark' => '取消订单',
  1131. ]);
  1132. }
  1133. //店铺信息q
  1134. $total_fee = round($result['amount']['refund'] / 100, 2);
  1135. $store = Store::where('id', $order->store_id)->lockForUpdate()->first();
  1136. $start_money = $store->pending_amount;
  1137. $store->pending_amount -= $total_fee;
  1138. $store->integral -= $integral;
  1139. $store->save();
  1140. //退款日志
  1141. Amount::create([
  1142. 'user_id' => $order->user_id,
  1143. 'store_id' => $order->store_id,
  1144. 'order_no' => $result['out_refund_no'],
  1145. 'transaction_id' => $result['refund_id'],
  1146. 'money' => $total_fee,
  1147. 'type' => 4, //退款
  1148. 'status' => 1, //成功
  1149. 'service_fee' => 0,
  1150. 'start_money' => $start_money,//支付前冻结金额
  1151. 'end_money' => $store->pending_amount,//支付后冻结金额
  1152. 'start_amount' => $store->available_amount,//支付前可用金额
  1153. 'end_amount' => $store->available_amount,//支付后可用金额
  1154. 'remark' => '退款',
  1155. ]);
  1156. },5);
  1157. Log::info('完成退款通知');
  1158. return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
  1159. }else{
  1160. OrderW::where('order_no',$result['out_trade_no'])->update([
  1161. 'refund_status'=> $result['refund_status'],
  1162. 'refund_no'=> $result['refund_id'],
  1163. ]);
  1164. Log::info('退款通知失败');
  1165. return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
  1166. }
  1167. //为了防止假数据,验证签名是否和返回的一样。
  1168. //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
  1169. // $sign = $data['sign'];
  1170. // unset($data['sign']);
  1171. // if($sign == self::getSign($data,config('wechat.payment.default.app_v3'))){
  1172. // //签名验证成功后,判断返回微信返回的
  1173. // if ($data['result_code'] == 'SUCCESS') {
  1174. // //根据返回的订单号做业务逻辑
  1175. // Log::info(json_encode($data));
  1176. // }else{//支付失败,输出错误信息
  1177. // fwrite($file,date("Y-m-d H:i:s")."错误信息:支付失败,".json_encode($data).PHP_EOL);
  1178. // }
  1179. // }else{
  1180. // fwrite($file,date("Y-m-d H:i:s")."错误信息:签名验证失败,".json_encode($data).PHP_EOL);
  1181. // }
  1182. }
  1183. //查询分账
  1184. public function fenzhang()
  1185. {
  1186. $sub_mchid = '1608091453';
  1187. $transaction_id = '4200000933202104068603011660';
  1188. $out_order_no = 'WJ21040615412878976147481';
  1189. $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;//查询地址
  1190. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1191. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1192. $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1193. $timestamp = time();//时间戳
  1194. $nonce = $this->nonce_str();//随机字符串
  1195. $body = "";
  1196. $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
  1197. $header = [
  1198. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1199. 'Accept:application/json',
  1200. 'User-Agent:' . $merchant_id,
  1201. 'Content-Type:application/json',
  1202. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1203. ];
  1204. $result = $this->curl($url, '', $header, 'GET');
  1205. $result = json_decode($result, true);
  1206. return $this->success($result);
  1207. }
  1208. //完结分账信息
  1209. public function finish(Request $request)
  1210. {
  1211. $order_no=$request->input('order_no');
  1212. event(new FinishAccount($order_no));
  1213. return $this->success('完结结束');
  1214. }
  1215. public function setBankInfodemo(Request $request){
  1216. $id=$request->id;
  1217. $sub_mchid=Store::where('user_id',$id)->value('sub_mchid');
  1218. $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
  1219. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1220. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1221. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1222. $timestamp=time();//时间戳
  1223. $nonce=$this->nonce_str();//随机字符串
  1224. $bank_address_code= $request->input('bank_address_code');
  1225. if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
  1226. $bank_address_code=substr($bank_address_code,0,2).'00';
  1227. }
  1228. $data=[
  1229. 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
  1230. 'account_bank' =>$request->input('account_bank'),
  1231. 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
  1232. 'bank_name'=>$request->input('bank_name'),
  1233. 'bank_branch_id'=>$request->input('bank_branch_id'),
  1234. 'account_number'=>$this->getEncrypts($request->input('account_number'))
  1235. ];
  1236. $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
  1237. $header=[
  1238. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1239. 'Accept:application/json',
  1240. 'User-Agent:' . $merchant_id,
  1241. 'Content-Type:application/json',
  1242. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1243. ];
  1244. $result=$this->curls($url,json_encode($data),$header);
  1245. $http_status=$result[1];
  1246. $result=json_decode($result[0],true);
  1247. if($http_status==204){
  1248. Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
  1249. Store::where('user_id',$id)
  1250. ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
  1251. return $this->success($result);
  1252. }else{
  1253. Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].'商户id为:'.$sub_mchid);
  1254. return $this->error('450001',$result['message']);
  1255. }
  1256. }
  1257. public function searchAccount(){
  1258. $user=User::find(13878);
  1259. $token = Auth::guard('api')->fromUser($user);
  1260. dd($token);
  1261. }
  1262. public function getBankInfodemo(Request $request){
  1263. $sub_mchid=Store::where('user_id',$request->id)->value('sub_mchid');
  1264. $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';//查询地址
  1265. $merchant_id=config('wechat.payment.default.mch_id');//商户号
  1266. $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
  1267. $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
  1268. $timestamp=time();//时间戳
  1269. $nonce=$this->nonce_str();//随机字符串
  1270. $body="";
  1271. $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
  1272. $header=[
  1273. 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
  1274. 'Accept:application/json',
  1275. 'User-Agent:' . $merchant_id,
  1276. 'Content-Type:application/json',
  1277. 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
  1278. ];
  1279. $result=$this->curl($url,'',$header,'GET');
  1280. dd($result);
  1281. $result=json_decode($result,true);
  1282. return $this->success($result);
  1283. }
  1284. }