12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343 |
- <?php
- namespace App\Http\Controllers;
- use App\Events\FinishAccount;
- use App\Models\AddressW;
- use App\Models\IntegralW;
- use App\Models\Order;
- use App\Models\OrderRefundMoneyW;
- use App\Models\OrderW;
- use App\Models\Store;
- use App\Models\Apply;
- use App\Models\Bank;
- use App\Models\Amount;
- use App\Models\User;
- use GuzzleHttp\Client;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Storage;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Log;
- class MiniSubController extends Controller
- {
- //申请小微商户
- public function apply_wx(Request $request){
- $store=Store::where('user_id',Auth::user()->id)->first();
- if(empty($store)){
- return $this->error('450001','该店铺不能申请二级商户');
- }
- $out_request_no=$store->out_request_no??date('ymdHis').mt_rand(10000,99999);
- $contact_name=$request->input('contact_name');
- $contact_phone=$request->input('contact_phone');
- $contact_id_card=$request->input('contact_id_card');//"412326199612220311"
- $store_name=$this->filterEmoji($request->input('store_name'))??$this->filterEmoji($store->name);
- $store_name=preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z]+/u", '', $store_name);//^0-9^A-Z^a-z
- $store_name=mb_substr($store_name,0,16);
- if(mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $store_name))==0){
- return $this->error('450001','该店铺名称格式不符需重置');
- }
- $store_url=$request->input('store_url')??"http://api.app.jiuweiyun.cn/api/gzh";//
- $id_card_copy=$request->input('id_card_copy');
- $id_card_national=$request->input('id_card_national');
- $id_card_name=$request->input('id_card_name');
- $id_card_number=$request->input('id_card_number');
- $id_card_valid_time=$request->input('id_card_valid_time');//$request->input('id_card_valid_time');//"2027-09-07"
- // $account_name=$request->input('account_name');//开户人姓名
- // $account_bank=$request->input('account_bank');//银行
- // $bank_address_code=str_pad($request->input('bank_address_code'),6,"0",STR_PAD_RIGHT);//开户城市编码
- // $bank_branch_id=$request->input('sub_branch_id');//开户银行联行号
- // $bank_name=$request->input('bank_name');//开户行(支行)
- // $account_number=$request->input('account_number');//银行卡号
- $url="https://api.mch.weixin.qq.com/v3/ecommerce/applyments/";//这个是微信的申请地址
- $param=array(
- "out_request_no" => $out_request_no,//申请编号,用户自定义就可以
- "organization_type" => "2401",//申请类型 2401代表申请的是小微商户(就是个人,没有营业执照的人),其它类型可参考微信v3文档
- "need_account_info" => false,//true,//是否填写结算银行账户,提现会提到这个账户上,设置true需要添加银行卡信息等。这里我就设置false了。因为绑定银行卡可以通过微信中的小程序“商家助手”自己去绑定。也可以通过微信的绑卡接口在写一个绑定银行卡的功能。
- // "account_info"=> [
- // "bank_account_type" => "75",
- // "account_name"=> $this->getEncrypts($account_name),//开户名称
- // "account_bank"=> $account_bank,//开户银行
- // "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
- // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
- // "bank_name"=> $bank_name,//开户银行全称(含支行)
- // "account_number"=> $this->getEncrypts($account_number),//银行账户'6214833787989683'
- // ],
- "contact_info" => array(//填写一个店铺的管理员信息
- "contact_type" => "65",//管理员类型 65是经营者/法人 其它类型看微信文档
- "contact_name" => $this->getEncrypts($contact_name),//管理员姓名getEncrypts是加密字符串的方法,见下文。
- "mobile_phone" => $this->getEncrypts($contact_phone),//管理员电话getEncrypts是加密字符串的方法
- "contact_id_card_number"=> $this->getEncrypts($contact_id_card),//管理员身份证号码
- ),
- "sales_scene_info" =>array(//店铺信息
- "store_name" =>$store_name,//店铺全称
- "store_url" =>$store_url //店铺的地址
- ),
- "merchant_shortname" =>$store_name,//商户简称
- "id_card_info" =>array(//经营者/法人的身份证信息
- "id_card_copy" =>$this->uploadmedia($id_card_copy),//身份证人像面uploadmedia()是上传图片到微信的方法,见下文
- "id_card_national" =>$this->uploadmedia($id_card_national),//身份证国徽面
- "id_card_name" =>$this->getEncrypts($id_card_name),//身份证上的姓名 getEncrypts()字符串加密方法 见下文
- "id_card_number" =>$this->getEncrypts($id_card_number),//身份证号,此身份证号可以和管理员是一个人
- "id_card_valid_time"=>$id_card_valid_time //身份证国徽面的到期日期
- )
- );
- Log::info(json_encode($param,JSON_UNESCAPED_UNICODE));
- $merchant_id = config('wechat.payment.default.mch_id'); //"1604585534"; //服务商的商户号
- $serial_no = config('wechat.payment.default.serial_no'); //"73024BD5FA3E83F39DC1EE36D53FF206F6B82289";//商户证书的序列号,可在微信商户平台API安全 中获取
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp =time();//时间戳
- $nonce =$this->nonce_str();//获取随机字符串
- $sign =$this->sign($url,'POST',$timestamp,$nonce,json_encode($param),$mch_private_key,$merchant_id,$serial_no);//进行签名操作
- $header =[//设置发送的头信息
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,//签名
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,//服务商的商户号
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书的序列号,注意:是平台证书,不是上文的商户证书序列号
- ];
- $result=$this->curl($url,json_encode($param),$header);//curl方法见下文
- $result=json_decode($result,true);
- if(isset($result['code']) && !empty($result['code'])){
- return $this->error('450001',$result['message']??'未知错误');
- }
- if(isset($result['applyment_id']) && isset($result['out_request_no'])){
- DB::connection('mysql_w')->beginTransaction();
- try{
- Apply::create([
- 'store_id'=>$store->id,
- 'contact_name'=>$contact_name,
- 'contact_phone'=>$contact_phone,
- 'contact_id_card'=>$contact_id_card,
- 'store_name'=>$store_name,
- 'store_url'=>$store_url,
- 'id_card_copy'=>$id_card_copy,
- 'id_card_national'=>$id_card_national,
- 'id_card_name'=> $id_card_name,
- 'id_card_number'=>$id_card_number,
- 'id_card_valid_time'=>$id_card_valid_time,
- // "account_name"=> $account_name,//开户名称
- // "account_bank"=> $account_bank,//开户银行
- // "bank_address_code"=> $bank_address_code,//"410100",//开户银行省市编码
- // "bank_branch_id"=>$bank_branch_id,// "402713354941",//开户银行联行号
- // "bank_name"=> $bank_name,//开户银行全称(含支行)
- // "account_number"=> $account_number,//银行账户'6214833787989683'
- 'applyment_id'=>$result['applyment_id'],
- 'out_request_no'=>$result['out_request_no'],
- ]);
- Store::where('user_id',Auth::user()->id)
- ->update([
- 'username'=>$id_card_name,
- 'idCard'=>$id_card_number,
- 'cardImg1'=>$id_card_copy,
- 'cardImg2'=>$id_card_national,
- 'applyment_id'=>$result['applyment_id'],
- 'out_request_no'=>$result['out_request_no'],
- 'is_apply'=>1,//1已申请待审核 2 审核通过,待签约,3 已签约,已完成
- 'province'=>$request->input('province'),
- 'city'=>$request->input('city'),
- 'area'=>$request->input('area'),
- 'address'=>$request->input('address'),
- ]);
- // AddressW::create([
- // 'name'=>$contact_name,
- // 'phone'=>$contact_phone,
- // 'store_id'=>$store->id,
- // 'province'=>$request->input('province'),
- // 'city'=>$request->input('city'),
- // 'area'=>$request->input('area'),
- // 'address'=>$request->input('address'),
- // 'is_default'=>0
- // ]);
- Log::info('applyment_id:'.$result['applyment_id'].',&&out_request_no:'.$result['out_request_no']);
- DB::connection('mysql_w')->commit();
- return $this->success($result);
- }catch(\Exception $e){
- DB::connection('mysql_w')->rollBack();
- return $this->error('450001',$e->getMessage());
- }
- }else{
- return $this->error('450001','提交失败');
- }
- }
- //过滤微信表情符
- function filterEmoji($str)
- {
- $str = preg_replace_callback(
- '/./u',
- function (array $match) {
- return strlen($match[0]) >= 4 ? '' : $match[0];
- },
- $str);
- return $str;
- }
- //获取所有银行
- public function getAllBank(){
- return $this->success(Bank::whereNotNUll('bank_id')->groupBy('bank_name')->pluck('bank_name','bank_id'));//
- }
- //获取支行
- public function getBankBranch(Request $request){
- $city_code=$request->input('city_code');
- $bank_id=$request->input('bank_id');
- // if($city_code=='1101' || $city_code=='1201' || $city_code=='3101' || $city_code=='5001'){
- // $city_code-=$city_code;
- // }
- $city_code.='00';
- Log::info($city_code.'//'.$bank_id);
- $bank=Bank::where('city_id',$city_code)->where('bank_id',$bank_id)->get();
- return $this->success($bank);
- }
- //查询进件状态
- public function query(Request $request){
- $store=Store::where('user_id',Auth::user()->id)->first();
- $applyment_id=$store->applyment_id??$request->input('applyment_id');//'20200708093101909876';//申请编号
- Log:info('iii---------iii'.$applyment_id);
- if(empty($applyment_id)){
- return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
- }
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- Log::info('------'.$result);
- $result=json_decode($result,true);
- $apply=Apply::where('store_id',$store->id)->where('applyment_id',$applyment_id)->OrderBy('id','desc')->first();
- if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['legal_validation_url']]);
- }
- if($result['applyment_state']=="NEED_SIGN"){
- Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
- Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
- }
- if($result['applyment_state']=="FINISH"){
- Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
- Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
- Apply::where('id',$apply->id)->update(['sub_mchid'=>$result['sub_mchid']]);
- }
- if($result['applyment_state']=="REJECTED"){//已驳回
- Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- if($result['applyment_state']=="FROZEN"){
- Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
- Apply::where('id',$apply->id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- Apply::where('id',$apply->id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
- return $this->success($result);
- // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
- }
- //查询进件状态
- public function query123(Request $request){
- $store=Store::where('user_id',Auth::user()->id)->first();
- // if(Auth::user()->id==32531){
- // $store=Store::where('user_id',9900)->first();
- // }
- $applyment_id=$request->input('applyment_id');//'20200708093101909876';//申请编号$store->applyment_id??
- Log:info('iii---------iii'.$applyment_id);
- if(empty($applyment_id)){
- return $this->error('450001',Auth::user()->id.'/当前申请认证,被驳回');
- }
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/applyments/'.$applyment_id;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- Log::info('------'.$result);
- $result=json_decode($result,true);
- return $this->success($result);
- if($result['applyment_state']=="ACCOUNT_NEED_VERIFY"){
- Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['legal_validation_url']]);
- }
- if($result['applyment_state']=="NEED_SIGN"){
- Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>2]);
- Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid'], 'audit_detail'=>$result['sign_url']]);
- }
- if($result['applyment_state']=="FINISH"){
- Log::info($applyment_id.'-----ooooo-----'.$result['sub_mchid']);
- Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
- Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
- }
- // if($result['applyment_state']=="AUDITING"){
- // Store::where('user_id',Auth::user()->id)->update(['sub_mchid'=>$result['sub_mchid'],'is_apply'=>3]);
- // Apply::where('applyment_id',$applyment_id)->update(['sub_mchid'=>$result['sub_mchid']]);
- // }
- if($result['applyment_state']=="REJECTED"){//已驳回
- Store::where('user_id',Auth::user()->id)->update(['is_apply'=>4]);
- Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- if($result['applyment_state']=="FROZEN"){
- Store::where('user_id',Auth::user()->id)->update(['is_apply'=>5]);//已冻结
- Apply::where('applyment_id',$applyment_id)->update(['audit_detail'=>$result['audit_detail'][0]['reject_reason']]);
- }
- Apply::where('applyment_id',$applyment_id)->update(['applyment_state'=>$result['applyment_state'],'applyment_state_desc'=>$result['applyment_state_desc']]);
- return $this->success($result);
- // $result['applyment_state']=="FINISH"; //完成签约 其它状态:CHECKING:资料校验中 ACCOUNT_NEED_VERIFY:待账户验 AUDITING:审核中 REJECTED:已驳回 NEED_SIGN:待签约 FINISH:完成 FROZEN:已冻结
- }
- //加密敏感字符串
- public function getEncrypts($str="张三"){
- // $public_key_path='D:\phpstudy_pro\WWW\weidian-api/cert/cert.pem';//获取的文件临时保存到服务器
- $public_key_path=config('wechat.payment.default.cert_pem');//这是获取平台证书临时存起来的平台公钥,通过getzhengshu()方法获取,见下文。。。注意是平台证书,不是商户证书
- $public_key=file_get_contents($public_key_path);
- $encrypted='';
- if (openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)) {
- //base64编码
- $sign = base64_encode($encrypted);
- } else {
- throw new Exception('encrypt failed');
- }
- return $sign;
- // if(openssl_public_encrypt($str,$encrypted,$public_key,OPENSSL_PKCS1_OAEP_PADDING)){
- // $sign=base64_encode($encrypted);
- // }else{
- // echo "error";exit;
- // }
- // return $sign;//返回加密的敏感字符串
- }
- //上传证件图片
- public function uploadmedia($path){//$path="uploads/header/z.jpg"
- $path='/www/wwwroot/api.app.jiuweiyun.cn/app-api/public'.$path;
- $url="https://api.mch.weixin.qq.com/v3/merchant/media/upload";//微信的上传地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号,注意是商户证书,不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $fi=new \finfo(FILEINFO_MIME_TYPE);
- $mime_type=$fi->file($path);
- $meta=[
- 'filename'=>pathinfo($path,2),
- 'sha256'=>hash_file('sha256',$path)
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($meta),$mch_private_key,$merchant_id,$serial_no);
- $boundary=uniqid();
- $header=[
- 'Content-Type:multipart/form-data;boundary=' . $boundary,
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id
- ];
- $body='--' . $boundary . "\r\n";
- $body.='Content-Disposition:form-data; name="meta"' . "\r\n";
- $body.='Content-Type:application/json' . "\r\n\r\n";
- $body.=json_encode($meta)."\r\n";
- $body.='--' . $boundary . "\r\n";
- $body.='Content-Disposition:form-data;name="file";filename="' . $meta['filename'] . '"' . "\r\n";
- $body.='Content-Type:' .$mime_type."\r\n\r\n";
- $body.=file_get_contents($path)."\r\n";
- $body.='--'.$boundary .'--'."\r\n";
- $result=$this->curl($url,$body,$header);
- $result=json_decode($result,true);
- return $result['media_id'];//返回微信返回来的图片标识
- }
- //读取商户api证书公钥
- public function getPublicKey(){
- return openssl_get_privatekey(file_get_contents(config('wechat.payment.default.key_path')));//商户平台API安全证书中下载,保存到服务器
- }
- //生成随机字符串
- public function nonce_str($length=32){
- $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
- $str ="";
- for ( $i = 0; $i < $length; $i++ ) {
- $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
- }
- return $str;
- }
- //签名
- public function sign($url,$http_method,$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no){
- $url_parts=parse_url($url);
- $canonical_url=($url_parts['path'].(!empty($url_parts['query'])?"?${url_parts['query']}":""));
- $message=
- $http_method . "\n".
- $canonical_url . "\n".
- $timestamp . "\n".
- $nonce . "\n".
- $body . "\n";
- openssl_sign($message,$raw_sign,$mch_private_key,'sha256WithRSAEncryption');
- $sign=base64_encode($raw_sign);
- $schema='WECHATPAY2-SHA256-RSA2048';
- $token=sprintf(
- 'mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',
- $merchant_id,
- $nonce,
- $sign,
- $timestamp,
- $serial_no
- );
- return $token;
- }
- //curl提交
- public function curl($url,$data=[],$header,$method='POST'){
- $curl=curl_init();
- curl_setopt($curl,CURLOPT_URL,$url);
- curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
- curl_setopt($curl,CURLOPT_HEADER,false);
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
- if($method=="POST"){
- curl_setopt($curl,CURLOPT_POST,TRUE);
- curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
- }
- $result=curl_exec($curl);
- curl_close($curl);
- return $result;
- }
- public function curls($url,$data=[],$header,$method='POST'){
- $curl=curl_init();
- curl_setopt($curl,CURLOPT_URL,$url);
- curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
- curl_setopt($curl,CURLOPT_HEADER,false);
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
- if($method=="POST"){
- curl_setopt($curl,CURLOPT_POST,TRUE);
- curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
- }
- $result=curl_exec($curl);
- $status=curl_getinfo($curl,CURLINFO_HTTP_CODE);
- curl_close($curl);
- return [$result,$status];
- }
- //获取平台证书
- public function getzhengshu(){
- $url="https://api.mch.weixin.qq.com/v3/certificates";//获取地址
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//获取一个随机字符串
- $body="";
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//商户证书序列号
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 '.$sign,
- 'Accept:application/json',
- 'User-Agent:'.$merchant_id
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- $serial_no=$result['data'][0]['serial_no'];//获取的平台证书序列号
- $encrypt_certificate=$result['data'][0]['encrypt_certificate'];
- $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
- $result=$this->decryptToString($encrypt_certificate['associated_data'],$encrypt_certificate['nonce'],$encrypt_certificate['ciphertext'],$sign_key);
- // return null;//file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem');
- // if(empty(file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem'))){
- // file_get_contents('/www/wwwroot/ceshi.api.woaidakele.cn/test-h5/cert/cert.pem',$result);
- file_put_contents(config('wechat.payment.default.cert_pem'),$result);//获取的文件临时保存到服务器
- // }
- return $serial_no;//返回平台证书序列号
- }
- //解密返回的信息
- public function decryptToString($associatedData,$nonceStr,$ciphertext,$aesKey){
- if (strlen($aesKey) != 32) {
- throw new InvalidArgumentException('无效的ApiV3Key,长度应为32个字节');
- }
- // $this->aesKey = $aesKey;
- $ciphertext=\base64_decode($ciphertext);
- if (strlen($ciphertext) <= 16) {
- return false;
- }
- if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
- \sodium_crypto_aead_aes256gcm_is_available()) {
- return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- return '1111';
- }
- // ext-libsodium (need install libsodium-php 1.x via pecl)
- if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
- \Sodium\crypto_aead_aes256gcm_is_available()) {
- return '222';
- return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- }
- // if(function_exists('\sodium_crypto_aead_aes256gcm_is_available')&& \sodium_crypto_aead_aes256gcm_is_available()){
- // return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext,$associatedData,$nonceStr,$aesKey);
- // }
- if(PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())){
- $ctext=substr($ciphertext,0,-16);
- $authTag=substr($ciphertext,-16);
- return \openssl_decrypt(
- $ctext,
- 'aes-256-gcm',
- $aesKey,
- \OPENSSL_RAW_DATA,
- $nonceStr,
- $authTag,
- $associatedData
- );
- }
- throw new \RuntimeException('php7.1');
- }
- //设置银行账户信息
- public function setBankInfo(Request $request){
- Log::error($request->all());
- // if (Auth::user()->id==32531){
- // $user_id='13878';
- // }else{
- // $user_id=Auth::user()->id;
- // }
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $bank_address_code= $request->input('bank_address_code');
- if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
- $bank_address_code=substr($bank_address_code,0,2).'00';
- }
- Log::info($bank_address_code);
- $data=[
- 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
- 'account_bank' =>$request->input('account_bank'),
- 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
- 'bank_name'=>$request->input('bank_name'),
- 'bank_branch_id'=>$request->input('bank_branch_id'),
- 'account_number'=>$this->getEncrypts($request->input('account_number'))
- ];
- Log::info($data);
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- // 'Content-Type:multipart/form-data',
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curls($url,json_encode($data),$header);
- Log::error('修改结算账户返回结果。'.json_encode($result));
- $http_status=$result[1];
- $result=json_decode($result[0],true);
- if($http_status==204){
- Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
- Store::where('user_id',Auth::user()->id)
- ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
- return $this->success($result);
- }else{
- Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].',商户id为:'.$sub_mchid);
- return $this->error('450001',$result['message']);
- }
- }
- //获取银行账户信息
- public function getBankInfo(){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- // if($sub_mchid=='1607429904'){
- // $sub_mchid='1609015697';
- // }
- // if($sub_mchid=='1607448291'){
- // $sub_mchid='1609035479';
- // }
- $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';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- Log::error(Auth::user()->id.'//'.$result);
- $result=json_decode($result,true);
- return $this->success($result);
- }
- public function uploadImg(Request $request)
- {
- $path_url = 'public/wechat/' . date("ymd");
- $path = $request->file('img')->store($path_url);
- if ($path) {
- $url = Storage::url($path);
- return $this->success($url);
- } else {
- return $this->error();
- }
- }
- //获取账户实时余额
- public function getAccountInfo(){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $result=$this->getAccount($sub_mchid);
- if($result['sub_mchid']){
- Store::where('sub_mchid',$result['sub_mchid'])
- ->update([
- 'available_amount'=>round($result['available_amount']/100,2),
- 'pending_amount'=>round($result['pending_amount']/100,2)
- ]);
- }
- return $this->success($result);
- }
- //获取账户信息
- public function getAccount($sub_mchid){
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/balance/'.$sub_mchid;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- return $result;
- }
- //二级商户提现
- public function withdrawal(Request $request){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- Log::info('提现金额:'.$request->input('amount'));
- $withdrawal_amount=str_replace( ',', '',$request->input('amount'));
- if(!is_numeric($withdrawal_amount)){
- return $this->error('450001','提现金额为非数值');
- }
- $amount=floor($withdrawal_amount * 100);
- if($amount < 1){
- return $this->error('450001','提现金额有误请重新输入');
- }
- $phone=[];
- $agent_phone=Store::where('user_id',Auth::user()->id)->value('phone');
- if(!in_array($agent_phone,$phone)){
- $result=$this->getAccount($sub_mchid);
- if(!$result['sub_mchid']){
- return $this->error('450001','可提现金额小于200元,暂不能提现');
- }
- if($result['available_amount'] < 20000){
- return $this->error('450001','可提现金额小于200元,暂不能提现');
- }
- }
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $out_request_no='TX'.date('ymdHis') . substr(microtime(), 2, 6).mt_rand(10000,99999);
- $data=[
- 'sub_mchid'=>$sub_mchid,
- 'out_request_no'=> $out_request_no,
- 'amount'=> (int) $amount,
- 'remark'=> '二级商户提现',
- 'bank_memo'=> '微信二级商户提现'
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- if(isset($result['withdraw_id']) && !empty($result['withdraw_id'])){
- $store=Store::where('user_id',Auth::user()->id)->first();
- Amount::create([
- 'transaction_id'=>$result['withdraw_id'],
- 'order_no'=>$result['out_request_no'],
- 'store_id'=>$store->id,
- 'money'=>$amount/100,
- 'type'=>3,//提现
- 'status'=>0,
- 'status_code'=>'CREATE_SUCCESS',
- 'service_fee'=>0,
- 'remark'=>'提现',
- 'remark_amount'=>$store->account_bank.'-'.$store->account_number,
- ]);
- }else{
- return $this->error('450001',$result['message']);
- }
- return $this->success($result);
- }
- //商户提现状态查询
- public function getWithdrawalStates(Request $request){
- // $withdraw_id=$request->input('withdraw_id');
- // $user_id=$request->input('user_id');
- // $user_id='32531';
- $index=$request->input('index');
- $size=$request->input('size');
- $refunds=Amount::where('type',3)->orderBy('id')->skip($index)->take($size)->get();
- // return $refunds;
- foreach($refunds as $key=>$val){
- $sub_mchid=Store::where('id',$val->store_id)->value('sub_mchid');
- $withdraw_id=$val->order_no;
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/out-request-no/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- if($val->status_code<>$result['status']){
- Log::info('提现状态有误:'.$val->store_id.'---'.$val->order_no.'---'.$result['status']);
- }
- Amount::where('id',$val->id)->update([
- 'transaction_id'=>$result['withdraw_id'],
- ]);
- }
- // $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
- return $result;
- // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- // $reason=$result['reason'];
- // }else{
- // $reason=null;
- // }
- // Amount::where('withdraw_id',$withdraw_id)
- // ->update([
- // 'status'=>0,
- // 'status_code'=>$result['status'],
- // 'reason'=>$reason,
- // ]);
- // return $this->success($result);
- }
- //商户提现状态查询
- public function getWithdrawalState(Request $request){
- $withdraw_id=$request->input('withdraw_id');
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/fund/withdraw/'.$withdraw_id.'?sub_mchid='.$sub_mchid;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- $reason=$result['reason'];
- }else{
- $reason=null;
- }
- Amount::where('withdraw_id',$withdraw_id)
- ->update([
- 'status'=>0,
- 'status_code'=>$result['status'],
- 'reason'=>$reason,
- ]);
- return $this->success($result);
- }
- //获取收款记录
- public function getAccountDetail(Request $request){
- $type=$request->input('type');
- $cycle=$request->input('cycle');
- $page_size=$request->input('page_size');
- $page_index=$request->input('page_index');
- $startime=$request->input('startime');
- $endtime=$request->input('endtime');
- $num=$page_size*($page_index-1);
- $store=Store::where('user_id',Auth::user()->id)->first();
- $data= Amount::where('store_id',$store->id)->where('type',$type);
- if($startime && $endtime){
- $data->whereBetween('created_at',[date("Y-m-d 00:00:00",strtotime($startime)),date("Y-m-d 23:59:59",strtotime($endtime))]);
- }
- $cc=clone $data;
- $count=$data->count();
- if($type==3){
- $ll['account']=$cc->where('status_code','SUCCESS')->sum('money');
- }else{
- $ll['account']=$data->sum('money');
- }
- if($type==4){
- $data->with('refund_money:refund_no,order_no');
- }
- $ll['list']=$data->OrderByDesc('id')->skip($num)->take($page_size)->get();
- return $this->success_list($ll,'',$count);
- }
- //获取按日下载提现异常文件
- public function getDownWithdraw(Request $request){
- $user_id='79143';
- $sub_mchid=Store::where('user_id',$user_id)->value('sub_mchid');
- $bill_type=$request->input('bill_type');
- $bill_date=$request->input('bill_date');
- // $bill_date='';
- $tar_type=$request->input('tar_type');
- // $url='https://api.mch.weixin.qq.com/v3/merchant/fund/withdraw/bill-type/'.$bill_type.'?bill_date='.$bill_date;//查询地址 .'?tar_type='.$tar_type
- $url='https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date='.$bill_date;
- // $url='https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date='.$bill_date;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- // $result=json_decode($result,true);
- return $result;
- }
- public function down(Request $request){
- $url=$request->input('url');
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $curl=curl_init();
- curl_setopt($curl,CURLOPT_URL,$url);
- curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
- curl_setopt($curl,CURLOPT_HEADER,false);
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
- $result=curl_exec($curl);
- curl_close($curl);
- return $result;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- return $this->success($result);
- }
- //开启点金计划
- public function dianjin(Request $request){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changegoldplanstatus';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $data=[
- 'sub_mchid'=>$sub_mchid,
- 'operation_type'=>'OPEN'
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
- Store::where('sub_mchid',$sub_mchid)->update(['is_dianjin'=>1]);
- return $this->success($result);
- }else{
- return $this->error('450001',$result['message']);
- }
- }
- //开启商家小票
- public function businessTips(){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/changecustompagestatus';//点金地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $data=[
- 'sub_mchid'=>$sub_mchid,
- 'operation_type'=> 'OPEN'
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- if(isset($result['sub_mchid']) && !empty($result['sub_mchid'])){
- Store::where('sub_mchid',$sub_mchid)->update(['is_business'=>1]);
- return $this->success($result);
- }else{
- return $this->error('450001',$result['message']);
- }
- }
- //关闭广告
- public function adverting(Request $request){
- $sub_mchid=Store::where('user_id',Auth::user()->id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/goldplan/merchants/close-advertising-show';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $data=[
- 'sub_mchid'=>$sub_mchid
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- if(empty($result)){
- Store::where('sub_mchid',$sub_mchid)->update(['is_adverting'=>1]);
- return $this->success($result);
- }else{
- // Log::info(Auth::user()->nickname.'关闭广告失败:'.json_encode($result));
- return $this->error('450001',$result['message']);
- }
- }
- //退款
- public function refundAmount(Request $request){
- $order_no=$request->input('order_no');
- $order=OrderW::where('order_no',$order_no)->first();
- $sub_mchid=Store::where('id',$order->store_id)->value('sub_mchid');
- if(empty($sub_mchid)){
- return $this->error('450001','店铺信息有误,暂不能退款');
- }
- if(in_array($order->status,[1,2,3]) && $order->is_pay==1){
- $amount=["refund"=> (int) ($order->pay_money*100),"total"=> (int)($order->pay_money*100),"currency"=> "CNY"];
- }else{
- return $this->error('450001','订单状态有误,暂不能退款');
- }
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/apply';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $data=[
- 'sub_mchid'=>$sub_mchid,
- 'sp_appid'=>config('wechat.payment.default.app_id'),
- 'transaction_id'=>$order->wechat_pay_no,
- // 'out_trade_no'=> $order_no,
- 'out_refund_no'=> 'TK'.substr($order_no,2),
- 'amount'=> $amount,
- 'notify_url'=>url('api/miniSub/refund_notify')
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,json_encode($data),$header,'POST');
- $result=json_decode($result,true);
- if(isset($result['refund_id']) && !empty($result['refund_id'])){
- // OrderW::where('order_no',$order_no)
- // ->update(['refund_status'=>'START','refund_no'=>$result['refund_id'],'refund_time'=>$result['create_time']]);
- OrderRefundMoneyW::create([
- 'order_no'=>$order_no,
- 'refund_status'=>'START',
- 'refund_time'=>$result['create_time'],
- 'refund_no'=>$result['refund_id'],
- 'refund_account'=>null,
- 'refund_amount'=>null
- ]);
- return $this->success($result);
- }else{
- return $this->error('450001',$result['message']);
- }
- }
- //退款状态查询
- public function getRefundState(Request $request){
- $out_refund_no=$request->input('out_refund_no');
- $sub_mchid=$request->input('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/ecommerce/refunds/out-refund-no/'.$out_refund_no.'?sub_mchid='.$sub_mchid;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- $result=json_decode($result,true);
- return $result;
- // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- // $reason=$result['reason'];
- // }else{
- // $reason=null;
- // }
- // Amount::where('withdraw_id',$withdraw_id)
- // ->update([
- // 'status'=>0,
- // 'status_code'=>$result['status'],
- // 'reason'=>$reason,
- // ]);
- // return $this->success($result);
- }
- //微信支付状态查询
- public function getPayState(Request $request){
- $out_refund_no=$request->input('out_refund_no');
- $sub_mchid=$request->input('sub_mchid');
- $sp_mchid=config('wechat.payment.default.mch_id');
- // $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;
- $url=' https://api.mch.weixin.qq.com/v3/pay/partner/transactions/id/'.$out_refund_no.'?sp_mchid='.$sp_mchid.'&sub_mchid='.$sub_mchid;
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- return $this->success($result);
- $result=json_decode($result,true);
- return $result;
- // if($result['status']=='FAIL' ||$result['status']=='REFUND' ||$result['status']=='CLOSE'){
- // $reason=$result['reason'];
- // }else{
- // $reason=null;
- // }
- // Amount::where('withdraw_id',$withdraw_id)
- // ->update([
- // 'status'=>0,
- // 'status_code'=>$result['status'],
- // 'reason'=>$reason,
- // ]);
- // return $this->success($result);
- }
- //退款通知
- public function refundNotify(){
- Log::info('开始退款通知');
- defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
- //接收微信返回的数据数据,返回的xml格式
- $xmlData = file_get_contents('php://input');
- //将xml格式转换为数组
- $xmlData=json_decode($xmlData,true);
- $data=$xmlData['resource'];
- $sign_key=config('wechat.payment.default.api_v3'); //APIv3密钥,商户平台API安全中获取
- $result=$this->decryptToString($data['associated_data'],$data['nonce'],$data['ciphertext'],$sign_key);
- $result=json_decode($result,true);
- log::info(json_encode($result));
- if($result['refund_status']=='SUCCESS'){
- if(strpos($result['out_trade_no'],'_')){
- $result['out_trade_no']=explode('_',$result['out_trade_no'])['1'];
- }
- $order=OrderW::withTrashed()->where('order_no',$result['out_trade_no'])->first();
- // $order->refund_status =$result['refund_status'];
- // $order->refund_time =$result['success_time'];
- // $order->refund_no =$result['refund_id'];
- // $order->refund_account =$result['user_received_account'];
- // $order->refund_amount =json_encode($result['amount']);
- // $order->save();
- DB::connection('mysql_w')->transaction(function()use($order,$result) {
- OrderRefundMoneyW::where('order_no', $result['out_trade_no'])->where('refund_no', $result['refund_id'])
- ->update([
- 'refund_status' => $result['refund_status'],
- 'refund_time' => $result['success_time'],
- 'refund_account' => $result['user_received_account'],
- 'refund_amount' => json_encode($result['amount']),
- ]);
- //学分变更
- $integral = $order->total * 5;
- $inte = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 2)->first();
- if (empty($inte)) {
- $inte_add = IntegralW::where('order_no', $order->order_no)->where('store_id', $order->store_id)->where('type', 1)->first();
- if($inte_add){
- $integral_double= $inte_add->integral_double;
- }else{
- $integral_double= integral;
- }
- IntegralW::create([
- 'store_id' => $order->store_id,
- 'order_no' => $order->order_no,
- 'num' => $order->total,
- 'integral' => 0 - $integral,
- 'integral_double' => 0 - $integral_double,
- 'type' => 2,
- 'remark' => '取消订单',
- ]);
- }
- //店铺信息q
- $total_fee = round($result['amount']['refund'] / 100, 2);
- $store = Store::where('id', $order->store_id)->lockForUpdate()->first();
- $start_money = $store->pending_amount;
- $store->pending_amount -= $total_fee;
- $store->integral -= $integral;
- $store->save();
- //退款日志
- Amount::create([
- 'user_id' => $order->user_id,
- 'store_id' => $order->store_id,
- 'order_no' => $result['out_refund_no'],
- 'transaction_id' => $result['refund_id'],
- 'money' => $total_fee,
- 'type' => 4, //退款
- 'status' => 1, //成功
- 'service_fee' => 0,
- 'start_money' => $start_money,//支付前冻结金额
- 'end_money' => $store->pending_amount,//支付后冻结金额
- 'start_amount' => $store->available_amount,//支付前可用金额
- 'end_amount' => $store->available_amount,//支付后可用金额
- 'remark' => '退款',
- ]);
- },5);
- Log::info('完成退款通知');
- return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
- }else{
- OrderW::where('order_no',$result['out_trade_no'])->update([
- 'refund_status'=> $result['refund_status'],
- 'refund_no'=> $result['refund_id'],
- ]);
- Log::info('退款通知失败');
- return response()->json(["code"=>"SUCCESS","message"=> "ERROR_DESCRIPTION",],200);
- }
- //为了防止假数据,验证签名是否和返回的一样。
- //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。
- // $sign = $data['sign'];
- // unset($data['sign']);
- // if($sign == self::getSign($data,config('wechat.payment.default.app_v3'))){
- // //签名验证成功后,判断返回微信返回的
- // if ($data['result_code'] == 'SUCCESS') {
- // //根据返回的订单号做业务逻辑
- // Log::info(json_encode($data));
- // }else{//支付失败,输出错误信息
- // fwrite($file,date("Y-m-d H:i:s")."错误信息:支付失败,".json_encode($data).PHP_EOL);
- // }
- // }else{
- // fwrite($file,date("Y-m-d H:i:s")."错误信息:签名验证失败,".json_encode($data).PHP_EOL);
- // }
- }
- //查询分账
- public function fenzhang()
- {
- $sub_mchid = '1608091453';
- $transaction_id = '4200000933202104068603011660';
- $out_order_no = 'WJ21040615412878976147481';
- $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;//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key = $this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp = time();//时间戳
- $nonce = $this->nonce_str();//随机字符串
- $body = "";
- $sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no);//签名
- $header = [
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result = $this->curl($url, '', $header, 'GET');
- $result = json_decode($result, true);
- return $this->success($result);
- }
- //完结分账信息
- public function finish(Request $request)
- {
- $order_no=$request->input('order_no');
- event(new FinishAccount($order_no));
- return $this->success('完结结束');
- }
- public function setBankInfodemo(Request $request){
- $id=$request->id;
- $sub_mchid=Store::where('user_id',$id)->value('sub_mchid');
- $url='https://api.mch.weixin.qq.com/v3/apply4sub/sub_merchants/'.$sub_mchid.'/modify-settlement';//地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $bank_address_code= $request->input('bank_address_code');
- if($bank_address_code=='1101' || $bank_address_code=='1201' || $bank_address_code=='3101' || $bank_address_code=='5001'){
- $bank_address_code=substr($bank_address_code,0,2).'00';
- }
- $data=[
- 'account_type' =>'ACCOUNT_TYPE_PRIVATE',//ACCOUNT_TYPE_BUSINESS:对公银行账户 ACCOUNT_TYPE_PRIVATE:经营者个人银行卡
- 'account_bank' =>$request->input('account_bank'),
- 'bank_address_code'=>str_pad($bank_address_code,6,"0",STR_PAD_RIGHT),
- 'bank_name'=>$request->input('bank_name'),
- 'bank_branch_id'=>$request->input('bank_branch_id'),
- 'account_number'=>$this->getEncrypts($request->input('account_number'))
- ];
- $sign=$this->sign($url,'POST',$timestamp,$nonce,json_encode($data),$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curls($url,json_encode($data),$header);
- $http_status=$result[1];
- $result=json_decode($result[0],true);
- if($http_status==204){
- Log::error('结算账户绑定成功。商户id为:'.$sub_mchid.'---http状态码为:'.$http_status);
- Store::where('user_id',$id)
- ->update(['account_number'=>$request->input('account_number'),'account_bank'=>$request->input('account_bank')]);
- return $this->success($result);
- }else{
- Log::error('结算账户绑定失败。http状态吗为:'.$http_status.',错误信息为'.$result['message'].'商户id为:'.$sub_mchid);
- return $this->error('450001',$result['message']);
- }
- }
- public function searchAccount(){
- $user=User::find(13878);
- $token = Auth::guard('api')->fromUser($user);
- dd($token);
- }
- public function getBankInfodemo(Request $request){
- $sub_mchid=Store::where('user_id',$request->id)->value('sub_mchid');
- $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';//查询地址
- $merchant_id=config('wechat.payment.default.mch_id');//商户号
- $serial_no=config('wechat.payment.default.serial_no');//不是平台证书序列号
- $mch_private_key=$this->getPublicKey();//读取商户api证书公钥 getPublicKey()获取方法 见下文
- $timestamp=time();//时间戳
- $nonce=$this->nonce_str();//随机字符串
- $body="";
- $sign=$this->sign($url,'GET',$timestamp,$nonce,$body,$mch_private_key,$merchant_id,$serial_no);//签名
- $header=[
- 'Authorization:WECHATPAY2-SHA256-RSA2048 ' . $sign,
- 'Accept:application/json',
- 'User-Agent:' . $merchant_id,
- 'Content-Type:application/json',
- 'Wechatpay-Serial:' . $this->getzhengshu()//获取平台证书序列号
- ];
- $result=$this->curl($url,'',$header,'GET');
- dd($result);
- $result=json_decode($result,true);
- return $this->success($result);
- }
- }
|