MiniSubController.php 74 KB

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