MerchantHaikeApplyRepository.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. <?php
  2. /**
  3. * @package benben_merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-16
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\merchant\apply;
  11. use app\common\dao\merchant\apply\MerchantHaikeApplyDao;
  12. use app\common\model\system\merchant\MerchantIntention;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\merchant\order\OrderMerchantRepository;
  15. use app\traits\HaikeApiRequest;
  16. use crmeb\jobs\SendSmsJob;
  17. use think\facade\Db;
  18. use think\facade\Log;
  19. use Lakala\src\LakalaBoss;
  20. use Lakala\src\AES;
  21. use think\facade\Queue;
  22. /**
  23. * 海科商户入网业务申请
  24. * Class MerchantHaikeApplyRepository
  25. * @package app\common\repositories\merchant
  26. * @author php迷途小书童
  27. * @created 2021/1/28 14:28
  28. */
  29. class MerchantHaikeApplyRepository extends BaseRepository
  30. {
  31. use HaikeApiRequest;
  32. /**
  33. * MerchantRepository constructor.
  34. * @param MerchantHaikeApplyDao $dao
  35. */
  36. public function __construct(MerchantHaikeApplyDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取单条订单信息
  42. * @return array|\think\Model|null
  43. * @author php迷途小书童
  44. * @created 2021/1/16 10:45
  45. */
  46. public function getOne(string $agent_apply_no)
  47. {
  48. $where = ['agent_apply_no' => $agent_apply_no];
  49. return ($this->dao->getWhere($where));
  50. }
  51. /**
  52. * 创建申请订单
  53. * @return mixed
  54. * @author php迷途小书童
  55. * @created 2021/1/16 14:57
  56. */
  57. public function createOrder(array $params)
  58. {
  59. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  60. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  61. if (!$intention)
  62. return ['code' => 500, 'msg' => '无申请信息'];
  63. $card_positive_person = parse_url($intention->card_positive_person, PHP_URL_PATH);//法人正面照
  64. Log::info('card_positive_person'.$card_positive_person);
  65. $card_back_person = parse_url($intention->card_back_person, PHP_URL_PATH);//法人反面照
  66. Log::info('card_back_person'.$card_back_person);
  67. $open_account = parse_url($intention->open_account, PHP_URL_PATH);//开户许可证
  68. Log::info('open_account'.$open_account);
  69. $business_premises1 = parse_url($intention->doorhead, PHP_URL_PATH);//门头照
  70. Log::info('doorhead'.$business_premises1);
  71. $business_premises2 = parse_url($intention->business_premises2, PHP_URL_PATH);//室内照1
  72. Log::info('business_premises2'.$business_premises2);
  73. $business_premises3 = parse_url($intention->business_premises3, PHP_URL_PATH);//室内照2
  74. Log::info('business_premises3'.$business_premises3);
  75. if (in_array($intention->type, [1,2])) {
  76. $business_license = parse_url($intention->business_license, PHP_URL_PATH);//营业执照
  77. Log::info('business_license'.$business_license);
  78. $business_license_base64 = $this->getThumb($business_license);
  79. $img_business_license = $this->uploadImg($business_license_base64)['image_id'];
  80. }
  81. if ($intention->type == 3) {
  82. $business_premises4 = parse_url($intention->business_premises4, PHP_URL_PATH);//小微入网图片
  83. Log::info('business_premises4'.$business_premises4);
  84. $business_premises4_base64 = $this->getThumb($business_premises4);
  85. $img_business_premises4 = $this->uploadImg($business_premises4_base64)['image_id'];
  86. }
  87. $card_positive_person_base64 = $this->getThumb($card_positive_person);
  88. $card_back_person_base64 = $this->getThumb($card_back_person);
  89. $open_account_base64 = $this->getThumb($open_account);
  90. $business_premises1_base64 = $this->getThumb($business_premises1);
  91. $business_premises2_base64 = $this->getThumb($business_premises2);
  92. $business_premises3_base64 = $this->getThumb($business_premises3);
  93. $img_card_positive_person = $this->uploadImg($card_positive_person_base64)['image_id'];
  94. $img_card_back_person = $this->uploadImg($card_back_person_base64)['image_id'];
  95. $img_open_account = $this->uploadImg($open_account_base64)['image_id'];
  96. $img_business_premises1 = $this->uploadImg($business_premises1_base64)['image_id'];
  97. $img_business_premises2 = $this->uploadImg($business_premises2_base64)['image_id'];
  98. $img_business_premises3 = $this->uploadImg($business_premises3_base64)['image_id'];
  99. $orderId = $this->getNewOrderId('MERAPPLY');
  100. $merchantApplyArray = [
  101. 'acc_type' => $intention->acc_type,
  102. 'type' => $intention->type,
  103. 'agent_apply_no' => $orderId,
  104. 'phone' => $intention->phone,
  105. 'mer_name' => $intention->mer_name,
  106. 'email' => $intention->email,
  107. 'name' => $this->getNameById($intention->mer_intention_id),
  108. 'card_positive_person' => $img_card_positive_person,
  109. 'card_back_person' => $img_card_back_person,
  110. 'legal_cert_no' => $intention->idcard_no,
  111. 'haike_receive_flag' => (string)$intention->haike_receive_flag,
  112. 'business_addr' => $intention->business_addr,
  113. 'mer_short_name' => $intention->mer_short_name,
  114. 'addr' => $intention->addr,
  115. 'bank_account' => $intention->bank_account,
  116. 'credit_code' => $intention->credit_code,
  117. 'bank_branch' => $intention->bank_branch,
  118. 'bank_code' => $this->getBankCode($intention->bank_branch),
  119. 'idcard_no' => $intention->idcard_no,
  120. 'province_code' => $this->getProvinceCode($intention->province_id),
  121. 'city_code' => $this->getCityCode($intention->city_id),
  122. 'area_code' => $this->getAreaCode($intention->area_id),
  123. 'bank_province_code' => $this->getProvinceCode($intention->province_id),
  124. 'bank_city_code' => $this->getCityCode($intention->city_id),
  125. 'card_positive_bank' => $img_open_account,
  126. 'business_premises1' => $img_business_premises1,
  127. 'business_premises2' => $img_business_premises2,
  128. 'business_premises3' => $img_business_premises3,
  129. ];
  130. $merchantApplyArray['is_again'] = $params['is_again'];
  131. if ($params['is_again'] > 0) {
  132. $merchantApplyArray['merch_no'] = $params['merch_no'];
  133. }
  134. if ($intention->type == 3) {
  135. $merchantApplyArray['business_premises4'] = $img_business_premises4;
  136. }
  137. if (in_array($intention->type, [1,2])) {
  138. $merchantApplyArray['credit_img'] = $img_business_license;
  139. }
  140. $response = $this->merchantApply($merchantApplyArray);
  141. Log::info($response);
  142. if ($response['return_code'] !== 10000) {
  143. return ['code' => 500, 'msg' => $response['return_msg']];
  144. }
  145. if ($params['is_again'] != 2) {
  146. $intention->merch_no = $merch_no = $response['merch_no'];
  147. }else {
  148. $response['merch_no'] = $merch_no = $intention->merch_no;
  149. }
  150. $intention->agent_apply_no = $orderId;
  151. $intention->save();
  152. $response = $this->merchantWithdrawalSetting([
  153. "merch_no"=> $response['merch_no'],
  154. "settlement_cycle" => $intention->acc_type == "10A" ? "T1" : "D1"
  155. ]);
  156. Log::info($response);
  157. $paramOrder = [
  158. 'apply_id' => $intention->mer_intention_id ? : 0,
  159. 'mer_id' => $intention->mer_id ? : 0,
  160. 'uid' => $intention->uid,
  161. 'merch_no' => $merch_no,
  162. 'agent_apply_no' => $orderId,
  163. 'trade_type' => 'WX,ALI',
  164. 'platform' => 1,
  165. 'apply_type' => 1,
  166. ];
  167. Db::transaction(function () use ($paramOrder) {
  168. $merchantOrder = $this->dao->create($paramOrder);
  169. return $merchantOrder;
  170. });
  171. return ['code' => 200, 'orderId' => $orderId];
  172. }
  173. /**
  174. * 创建申请订单lakala
  175. * @return mixed
  176. */
  177. public function createOrderlakala(array $params)
  178. {
  179. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  180. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  181. if (!$intention)
  182. return ['code' => 500, 'msg' => '无申请信息'];
  183. $datas=[
  184. "row"=> [
  185. "merchant_name"=>$intention->mer_name,//子商户名称
  186. "merchant_short_n1ame"=>$intention->mer_name,//子商户简称
  187. "merc_start_time"=>$intention->merc_start_time,
  188. "merc_end_time"=>$intention->merc_end_time,
  189. "reg_address"=> $intention->addr,//注册/经营地址
  190. "merchant_id_type"=> "00",//是否三证合一
  191. "merchant_id_no"=>$intention->credit_code ,//信用代码\营业执照号
  192. "province"=>$this->getProvinceCode($intention->province_id) ,//省份地址i
  193. "city"=>$this->getProvinceCode($intention->city_id) ,//省份地址i
  194. "region"=>$this->getAreaCode($intention->area_id),//省份地址i
  195. "mccCd"=>$intention->mcc_cd ,//行业大类
  196. "mccSubCd"=>$intention->mcc_sub_cd ,//行业细类
  197. "merc_attr"=>$intention->merc_attr ,//商户性质
  198. "min_rtn_amt"=>$intention->min_rtn_amt ,//最低留存金额 以分为单位
  199. "fee_charge_glg"=>$intention->fee_charge_glg,//分账手续费承担方式
  200. /// mercIdEffDt
  201. ],
  202. "illey"=>[
  203. "enterpriseOwner"=>$intention->name,
  204. "enterpriseOwnerIdType"=>"00",
  205. "enterpriseOwnerIdNo"=>$intention->idcard_no,
  206. "enterpriseOwnerIdEffDt"=>$intention->identification_valid_date_start,
  207. "enterpriseOwnerIdExpDt"=>$intention->identification_valid_date_end,
  208. "ownImg1"=>$intention->card_positive_person,
  209. "ownImg2"=>$intention->card_back_person,
  210. ],
  211. "son"=>[
  212. "usrOprNm"=>$intention->name,
  213. "usrOprEmail"=>$intention->email,
  214. "usrOprMbl"=>$intention->name,
  215. ],
  216. "link"=>[
  217. "contactType"=>"0",
  218. "contactName"=>$intention->name,
  219. "contactMobile"=>$intention->phone,
  220. ],
  221. "stu"=>[
  222. "bank_name"=>$intention->bank_name,
  223. "stl_bank_name"=>$intention->bank_deposit,
  224. "bank_no"=>$this->getBankCode($intention->bank_branch),
  225. "account_no"=>$intention->bank_account,
  226. "bank_prov"=>"",
  227. "bank_city"=>"",
  228. "pas_stl_flg"=>$intention->pas_stl_flg,
  229. "stl_beg_amt"=>$intention->stl_beg_amt,
  230. ],
  231. ];
  232. $res=$this->lakalaru($datas,$intention->mer_id);
  233. return $res;
  234. // $paramOrder = [
  235. // 'apply_id' => $intention->mer_intention_id ? : 0,
  236. // 'mer_id' => $intention->mer_id ? : 0,
  237. // 'uid' => $intention->uid,
  238. // 'merch_no' => $merch_no,
  239. // 'agent_apply_no' => $orderId,
  240. // 'trade_type' => 'WX,ALI',
  241. // 'platform' => 1,
  242. // 'apply_type' => 1,
  243. // ];
  244. //
  245. // Db::transaction(function () use ($paramOrder) {
  246. // $merchantOrder = $this->dao->create($paramOrder);
  247. // return $merchantOrder;
  248. // });
  249. }
  250. /**
  251. * 拉卡拉入网
  252. * @param array $datas 商户数据
  253. * @param $mer_id 商户id
  254. * @data 2021-2-5
  255. */
  256. public function lakalaru($datas,$mer_id){
  257. $ids=$mer_id;
  258. $data = $datas["row"];
  259. $sonStore = $datas["illey"]; //子商户
  260. $sonManage = $datas["son"]; //子商户
  261. $sonLink = $datas["link"];//子商户联系方式
  262. $stu = $datas["stu"];//子商户结算信息开
  263. $password = substr(md5(time()),-16);
  264. $aes = AES::init();
  265. $lakalaBoss = LakalaBoss::getInstance();
  266. $params = [
  267. 'reqTime' => date('YmdHis'), //请求时间
  268. 'merchantName' => $data['merchant_name'] ?? '', //子商户名称
  269. 'merchantShortName' => $data['merchant_short_n1ame'] ?? '', //子商户简称
  270. 'mercIdEffDt' => date("Ymd",strtotime($data['merc_start_time'])) ?? '', //营业执照生效日期
  271. 'mercIdExpDt' => date("Ymd",strtotime($data['merc_end_time'])) ?? '',//'20260726', 营业执照到期日期
  272. 'regAddress' => $data['reg_address'] ?? '', //注册/经营地址
  273. 'merchantIdType' => $data['merchant_id_type'], //是否三证合一
  274. 'merchantIdNo' => $aes->encrypt($data['merchant_id_no'],$password), //信用代码\营业执照号
  275. 'province' => $data['province'] ?? '',//归属省
  276. 'city' => $data['city'] ?? '', //归属市
  277. 'region' => $data['region'] ?? '', //归属地区
  278. 'mccCd' => $data['mcc_cd'] ?? '', //行业大类
  279. 'mccSubCd' => $data['mcc_sub_cd'] ?? '', //行业细类
  280. 'mercAttr' => (string)$data['merc_attr'] ?? '',//商户性质
  281. 'administrator' => stripslashes(json_encode([ //子商户管理员信息开始
  282. [
  283. 'usrOprNm' => $sonManage['usrOprNm'], //子商户管理员姓名
  284. 'usrOprMbl' => $aes->encrypt($sonManage['usrOprNm'],$password), //子商户管理员姓名
  285. 'usrOprEmail' => $sonManage['usrOprEmail'], //子商户管理员Email
  286. 'mercCertTyp' => '1-RSA', //子商户接口类型
  287. ]
  288. ],JSON_UNESCAPED_UNICODE)),
  289. 'contact' => stripslashes(json_encode([ //子商户联系人信息开始
  290. [
  291. 'contactType' => $sonLink['contactType'], // 3 紧急联系人
  292. 'contactName' => $sonLink['contactName'], //子商户联系人姓名
  293. 'contactMobile' => $aes->encrypt($sonLink['contactMobile'],$password), //子商户联系人手机
  294. ]
  295. ],JSON_UNESCAPED_UNICODE)),
  296. 'owner' => stripslashes(json_encode([ //子商户法人信息
  297. [
  298. 'enterpriseOwner' => $sonStore['enterpriseOwner'], //子商户法人姓名
  299. 'enterpriseOwnerIdType' => $sonStore['enterpriseOwnerIdType'], //子商户法人证件类型 00:身份证
  300. 'enterpriseOwnerIdNo' => $aes->encrypt($sonStore['enterpriseOwnerIdNo'],$password),//子商户法人证件号码
  301. 'enterpriseOwnerIdEffDt' => $sonStore['enterpriseOwnerIdEffDt'], //子商户法人证件生效日期
  302. 'enterpriseOwnerIdExpDt' => $sonStore['enterpriseOwnerIdExpDt'], //子商户法人证件失效日期
  303. 'ownImg1' =>$sonStore['ownImg1'],//$sonStore['ownImg1'] ? explode("/",$sonStore['ownImg1'])[3]:"",//子商户法人证件照片正面
  304. 'othImg2' =>$sonStore['ownImg2'],//$sonStore['othImg2'] ? explode("/",$sonStore['othImg2'])[3] : "",//子商户法人证件照片反面
  305. ]
  306. ],JSON_UNESCAPED_UNICODE)),
  307. 'stlLst' => stripslashes(json_encode([ //结算信息
  308. [
  309. 'stlSign' => '0', //结算账号公私标志 0-对公
  310. 'selfStlFlg' => 'Y', //商户自结算标志 N-不允许 Y-允许
  311. 'bankName' => $stu['bank_name'] ?? '', //银行代码
  312. 'stlBankName' => $stu['stl_bank_name'] ?? '',//开户行名称
  313. 'lBnkNo' => $stu['bank_no'] ?? '',//联行行号
  314. 'stlOac' => $aes->encrypt($stu['account_no'],$password),//$stu['account_no'] //银行帐号
  315. 'bnkOpnName' => $stu['stl_bank_name'] ?? '', //银行开户名称
  316. 'opnBnkProv' => $stu['bank_prov'] ?? '', //支行归属省
  317. 'opnBnkCity' => $stu['bank_city'] ?? '', //支行归属市
  318. 'stlCls' => '1',//$stu['st_class'], //商户结算类型 1-结算到账户2-结算到银行账户
  319. 'stlPerd' => '0',//$stu['stl_perd'], //结算周期0-日 1-周2-旬3-月4-季5-半年6-年7-指定结算日
  320. 'pasStlFlg' => $stu['pas_stl_flg'], //暂停结算标志 0-正常结算 1-暂停结算
  321. 'minRtnAmt' => $data['min_rtn_amt'], //最低留存金额 以分为单位
  322. 'stlTrfDays' => '1',//$stu['stl_trf_days'], //结算划款天数
  323. 'stlDay' => "1", //结算日当结算周日为7时必填
  324. 'nextStlDt' => date('Ymd'), //下一结算日期
  325. 'stlBegAmt' => $stu['stl_beg_amt'], //结算起始金额
  326. ]
  327. ],JSON_UNESCAPED_UNICODE)),
  328. 'feeChargeFlg' => $data['fee_charge_glg'], //分账手续费承担方式
  329. 'certPhotoImg' => "photoimg.jpg",// $data['cert_photo_image'] ? explode("/",$data['cert_photo_image'])[3] : "", //证照照片
  330. 'secretKey' =>$lakalaBoss->signencrypt($password), //临时密钥
  331. ];
  332. //查看是否存在商户id值
  333. $isMerc = Db::name("merchant")->where(['mer_id'=>$ids])->find();
  334. if($isMerc['mer_id']){
  335. $params['outMerchantId'] = $isMerc['mer_id'];
  336. $result = $lakalaBoss->editStore($params);
  337. if($result['code'] != 0){
  338. return app('json')->fail($result['message']);
  339. }
  340. if($result['data']['status'] == 'S'){
  341. $updateArr = [
  342. 'mer_id' => $ids,
  343. 'send_data' => json_encode($params),
  344. 'result_data' => json_encode($result),
  345. "update_time" => time(),
  346. ];
  347. Db::name("merchant_extend")->where(['mer_id'=>$ids])->update($updateArr);
  348. // return app('json')->success($result['message']);
  349. return ['code' => 200, 'message' => $result['message']];
  350. }
  351. }else{
  352. $result = $lakalaBoss->registerStore($params);
  353. return $result;
  354. if($result['code'] != 0){
  355. // $this->error($result['message']);
  356. // return app('json')->fail($result['message']);
  357. return ['code' => 400, 'message' => $result['message']];
  358. }
  359. if($result['data']['status'] == 'S'){
  360. //更新信息到数据库
  361. $rs = Db::name("merchant_intention")->where(['mer_id'=>$ids])->update(['mer_id'=>$result['data']['outMerchantId'],'request_id'=>$result['data']['requestId']]);
  362. $insertArr = [
  363. 'mer_id' => $ids,
  364. 'send_data' => json_encode($params),
  365. 'result_data' => json_encode($result),
  366. "update_time" => time(),
  367. "create_time" => time(),
  368. ];
  369. Db::name("merchant_extend")->insert($insertArr);
  370. if($rs){
  371. //对图片进行压缩文件
  372. // system();
  373. /**
  374. * 子商户号
  375. * 最终图片路径
  376. */
  377. // self::upload_img($result['data']['outMerchantId'],'');
  378. return ['code' => 200, 'message' => "操作成功"];
  379. }
  380. return ['code' => 400, 'message' => "网络繁忙,请稍后进行操作!"];
  381. }else{
  382. return ['code' => 400, 'message' => $result['message']];
  383. }
  384. }
  385. }
  386. public function getNameById($id)
  387. {
  388. return Db::name('merchant_intention')->where('mer_intention_id', $id)->value('name');
  389. }
  390. public function getBankCode($branch)
  391. {
  392. return Db::name('bank_code')->where('bank_branch', $branch)->value('bank_code');
  393. }
  394. public function getProvinceCode($province_id)
  395. {
  396. return Db::name('user_province')->where('province_id', $province_id)->value('province_code');
  397. }
  398. public function getCityCode($city_id)
  399. {
  400. return Db::name('user_city')->where('city_id', $city_id)->value('city_code');
  401. }
  402. public function getAreaCode($area_id)
  403. {
  404. return Db::name('user_area')->where('area_id', $area_id)->value('area_code');
  405. }
  406. /**
  407. * 获取唯一订单号
  408. * @return string
  409. * @author php迷途小书童
  410. * @created 2021/1/16 14:57
  411. */
  412. public function getNewOrderId($trade_type)
  413. {
  414. list($msec, $sec) = explode(' ', microtime());
  415. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  416. $orderId = $trade_type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  417. return $orderId;
  418. }
  419. /**
  420. * 微信业务申请
  421. * @author php迷途小书童
  422. * @created 2021/2/3 15:57
  423. */
  424. public function haikeWxApply($params)
  425. {
  426. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  427. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  428. if (!$intention)
  429. return ['return_code' => 500, 'return_msg' => '申请信息不存在'];
  430. if (!$intention->merch_no)
  431. return ['return_code' => 500, 'return_msg' => '请先发起入网'];
  432. if (!$intention->haike_wx_appid)
  433. return ['return_code' => 500, 'return_msg' => '请先配置微信公众号APPID'];
  434. $authApply = $this->dao->getWhere(['apply_id' => $intention->mer_intention_id,'trade_type' => 'WXAPPLY']);
  435. if ($authApply)
  436. $this->dao->delete($authApply->id);
  437. $orderId = $this->getNewOrderId('WXAPPLY');
  438. $paramOrder = [
  439. 'apply_id' => $intention->mer_intention_id ? : 0,
  440. 'mer_id' => $intention->mer_id ? : 0,
  441. 'mer_name' => $intention->mer_name,
  442. 'uid' => $intention->uid,
  443. 'phone' => $intention->phone,
  444. 'merch_no' => $intention->merch_no,
  445. 'agent_apply_no' => $orderId,
  446. 'trade_type' => 'WXAPPLY',
  447. 'platform' => 1,
  448. 'apply_type' => 2,
  449. ];
  450. $this->createApplyOrder($paramOrder);
  451. //刷卡手续费比例
  452. $souxufee = systemConfig('yanglao');
  453. $rate = bcmul($souxufee,100);
  454. $paramOrder = [
  455. 'merch_no' => $intention->merch_no,
  456. 'agent_apply_no' => $orderId,
  457. 'apply_notice' => env('APP_URL')."/api/merchant/apply_notice",
  458. 'haike_wx_type' => (string)$intention->haike_wx_type,
  459. 'rate' => (string)$rate,
  460. ];
  461. $response = $this->wxApply($paramOrder);
  462. if ($response['return_code'] !== 10000) {
  463. return ['return_code' => 500, 'return_msg' => $response['return_msg']];
  464. }
  465. $intention->haike_wx_status = 1;
  466. $intention->save();
  467. return ['return_code' => 200, 'return_msg' => '发起成功'];
  468. }
  469. /**
  470. * 微信公众号配置
  471. * @author php迷途小书童
  472. * @created 2021/2/3 15:57
  473. */
  474. public function haikeWxAppidConfAdd($params)
  475. {
  476. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  477. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  478. if (!$intention)
  479. return ['return_code' => 500, 'return_msg' => '申请信息不存在'];
  480. if (!$intention->merch_no)
  481. return ['return_code' => 500, 'return_msg' => '请先发起入网'];
  482. if (!$intention->haike_wx_appid)
  483. return ['return_code' => 500, 'return_msg' => '请先配置微信公众号APPID'];
  484. $paramOrder = [
  485. 'merch_no' => $intention->merch_no,
  486. 'wx_appid' => $intention->haike_wx_appid,
  487. ];
  488. $response = $this->wxAppidConfAdd($paramOrder);
  489. if ($response['return_code'] !== 10000) {
  490. return ['return_code' => 500, 'return_msg' => '未知错误'];
  491. }
  492. if ($response['result'] === "0") {
  493. $intention->haike_wx_appid_status = 1;
  494. $intention->save();
  495. return ['return_code' => 200, 'return_msg' => $response['msg']];
  496. } else {
  497. return ['return_code' => 500, 'return_msg' => $response['msg']];
  498. }
  499. }
  500. /**
  501. * 微信业务二次认证
  502. * @author php迷途小书童
  503. * @created 2021/2/3 15:57
  504. */
  505. public function haikeWxAuthApply($params)
  506. {
  507. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  508. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  509. if (!$intention)
  510. return ['code' => 500, 'msg' => '申请信息不存在'];
  511. if (!$intention->merch_no)
  512. return ['code' => 500, 'msg' => '请先发起入网'];
  513. $infoResponse = $this->merchantInfo(['merch_no' => $intention->merch_no]);
  514. if ($infoResponse['return_code'] !== 10000)
  515. return ['code' => 500, 'msg' => $infoResponse['return_msg']];
  516. $wxsub_merch_no = $infoResponse['wx_biz_info']['wxsub_merch_no'];
  517. $intention->haike_wxsub_merch_no = $wxsub_merch_no;
  518. $save = $intention->save();
  519. if (!$save)
  520. return ['code' => 500, 'msg' => '更新微信子商户号失败'];
  521. $card_positive_person = parse_url($intention->card_positive_person, PHP_URL_PATH);//法人正面照
  522. Log::info('card_positive_person'.$card_positive_person);
  523. $card_back_person = parse_url($intention->card_back_person, PHP_URL_PATH);//法人反面照
  524. Log::info('card_back_person'.$card_back_person);
  525. $business_license = parse_url($intention->business_license, PHP_URL_PATH);//营业执照
  526. Log::info('business_license'.$business_license);
  527. $open_account = parse_url($intention->open_account, PHP_URL_PATH);//开户许可证
  528. Log::info('open_account'.$open_account);
  529. $business_premises1 = parse_url($intention->doorhead, PHP_URL_PATH);//门头照
  530. Log::info('doorhead'.$business_premises1);
  531. $business_premises2 = parse_url($intention->business_premises2, PHP_URL_PATH);//室内照1
  532. Log::info('business_premises2'.$business_premises2);
  533. $card_positive_person_base64 = $this->getThumb($card_positive_person);
  534. $card_back_person_base64 = $this->getThumb($card_back_person);
  535. $business_license_base64 = $this->getThumb($business_license);
  536. $open_account_base64 = $this->getThumb($open_account);
  537. $business_premises1_base64 = $this->getThumb($business_premises1);
  538. $business_premises2_base64 = $this->getThumb($business_premises2);
  539. $img_card_positive_person = $this->uploadImg($card_positive_person_base64)['image_id'];
  540. $img_card_back_person = $this->uploadImg($card_back_person_base64)['image_id'];
  541. $img_open_account = $this->uploadImg($open_account_base64)['image_id'];
  542. $img_business_license = $this->uploadImg($business_license_base64)['image_id'];
  543. $img_business_premises1 = $this->uploadImg($business_premises1_base64)['image_id'];
  544. $img_business_premises2 = $this->uploadImg($business_premises2_base64)['image_id'];
  545. $orderId = $this->getNewOrderId('WXAUTHAPPLY');
  546. $authApplyArray = [
  547. 'acc_type' => $intention->acc_type,
  548. 'type' => $intention->type,
  549. 'agent_apply_no' => $orderId,
  550. 'phone' => $intention->phone,
  551. 'mer_name' => $intention->mer_name,
  552. 'email' => $intention->email,
  553. 'name' => $this->getNameById($intention->mer_intention_id),
  554. 'card_positive_person' => $img_card_positive_person,
  555. 'card_back_person' => $img_card_back_person,
  556. 'identification_valid_date_start' => $intention->identification_valid_date_start,
  557. 'identification_valid_date_end' => $intention->identification_valid_date_end,
  558. 'legal_cert_no' => $intention->idcard_no,
  559. 'business_addr' => $intention->business_addr,
  560. 'mer_short_name' => $intention->mer_short_name,
  561. 'addr' => $intention->addr,
  562. 'bank_account' => $intention->bank_account,
  563. 'credit_code' => $intention->credit_code,
  564. 'bank_branch' => $intention->bank_branch,
  565. 'bank_code' => $this->getBankCode($intention->bank_branch),
  566. 'idcard_no' => $intention->idcard_no,
  567. 'province_code' => $this->getProvinceCode($intention->province_id),
  568. 'city_code' => $this->getCityCode($intention->city_id),
  569. 'area_code' => $this->getAreaCode($intention->area_id),
  570. 'bank_province_code' => $this->getProvinceCode($intention->province_id),
  571. 'bank_city_code' => $this->getCityCode($intention->city_id),
  572. 'credit_img' => $img_business_license,
  573. 'card_positive_bank' => $img_open_account,
  574. 'business_premises1' => $img_business_premises1,
  575. 'business_premises2' => $img_business_premises2,
  576. ];
  577. $authApplyArray['merch_no'] = $intention->merch_no;
  578. $authApplyArray['wx_merch_no'] = $intention->haike_wxsub_merch_no;
  579. $response = $this->wxAuthApply($authApplyArray);
  580. Log::info($response);
  581. if ($response['return_code'] !== 10000) {
  582. return ['code' => 500, 'msg' => $response['return_msg']];
  583. }
  584. $intention->haike_wx_auth_status = 1;
  585. $intention->save();
  586. $authApply = $this->dao->getWhere(['apply_id' => $intention->mer_intention_id,'trade_type' => 'WXAUTHAPPLY']);
  587. if ($authApply)
  588. $this->dao->delete($authApply->id);
  589. $paramOrder = [
  590. 'apply_id' => $intention->mer_intention_id ? : 0,
  591. 'mer_id' => $intention->mer_id ? : 0,
  592. 'mer_name' => $intention->mer_name,
  593. 'uid' => $intention->uid,
  594. 'phone' => $intention->phone,
  595. 'merch_no' => $intention->merch_no,
  596. 'wx_merch_no' => $intention->haike_wxsub_merch_no,
  597. 'agent_apply_no' => $orderId,
  598. 'trade_type' => 'WXAUTHAPPLY',
  599. 'platform' => 1,
  600. 'apply_type' => 2,
  601. ];
  602. $this->createApplyOrder($paramOrder);
  603. return ['code' => 200, 'msg' => $response['apply_no']];
  604. }
  605. /**
  606. * 支付宝业务申请
  607. * @author php迷途小书童
  608. * @created 2021/2/3 15:57
  609. */
  610. public function haikeAliApply($params)
  611. {
  612. $intentionRepository = app()->make(MerchantIntentionApplyRepository::class);
  613. $intention = $intentionRepository->getOne(['mer_intention_id' => $params['mer_intention_id']]);
  614. if (!$intention)
  615. return ['return_code' => 500, 'return_msg' => '申请信息不存在'];
  616. if (!$intention->merch_no)
  617. return ['return_code' => 500, 'return_msg' => '请先发起入网'];
  618. $authApply = $this->dao->getWhere(['apply_id' => $intention->mer_intention_id,'trade_type' => 'ALIAPPLY']);
  619. if ($authApply)
  620. $this->dao->delete($authApply->id);
  621. $orderId = $this->getNewOrderId('ALIAPPLY');
  622. $paramOrder = [
  623. 'apply_id' => $intention->mer_intention_id ? : 0,
  624. 'mer_id' => $intention->mer_id ? : 0,
  625. 'mer_name' => $intention->mer_name,
  626. 'uid' => $intention->uid,
  627. 'phone' => $intention->phone,
  628. 'merch_no' => $intention->merch_no,
  629. 'agent_apply_no' => $orderId,
  630. 'trade_type' => 'ALI',
  631. 'platform' => 1,
  632. 'apply_type' => 3,
  633. ];
  634. $this->createApplyOrder($paramOrder);
  635. $souxufee = systemConfig('yanglao');
  636. $rate = bcmul($souxufee,100);
  637. $paramOrder = [
  638. "merch_no"=> $intention->merch_no,
  639. "agent_apply_no"=> $orderId,
  640. "apply_notice"=> env('APP_URL')."/api/merchant/apply_notice",
  641. "haike_ali_type" => (string)$intention->haike_ali_type,
  642. "rate"=> (string)$rate,
  643. ];
  644. try {
  645. $response = $this->aliApply($paramOrder);
  646. if ($response['return_code'] !== 10000) {
  647. return ['return_code' => 500, 'return_msg' => $response['return_msg']];
  648. }
  649. } catch (\Throwable $e) {
  650. return ['return_code' => 500, 'return_msg' => '支付宝发起失败'];
  651. }
  652. $intention->haike_ali_status = 1;
  653. $intention->save();
  654. return ['return_code' => 200, 'return_msg' => '发起成功'];
  655. }
  656. public function createApplyOrder($paramOrder)
  657. {
  658. Db::transaction(function () use ($paramOrder) {
  659. $merchantOrder = $this->dao->create($paramOrder);
  660. return $merchantOrder;
  661. });
  662. }
  663. /**
  664. * 海科发起退款
  665. * @return array
  666. * @author php迷途小书童
  667. * @created 2021/2/5 17:40
  668. */
  669. public function haikeRefundApply($params)
  670. {
  671. $orderMerchantRepository = app()->make(OrderMerchantRepository::class);
  672. $recharge = $orderMerchantRepository->getOneById($params['recharge_id']);
  673. if (!$recharge)
  674. return ['return_code' => 500, 'return_msg' => '充值信息不存在'];
  675. if (!$recharge->paid)
  676. return ['return_code' => 500, 'return_msg' => '订单未完成支付'];
  677. $orderId = $this->getNewOrderId($recharge->trade_type.'REFUNDAPPLY');
  678. $recharge->refund_no = $orderId;
  679. $recharge->status = 2;
  680. $recharge->save();
  681. try {
  682. $response = $this->doRefundApply([
  683. "out_trade_no" => $recharge->out_trade_no,
  684. "refund_no" => $orderId,
  685. "money" => $recharge->money,
  686. ]);
  687. if ($response['return_code'] !== 10000) {
  688. return ['return_code' => 500, 'return_msg' => $response['error_msg']];
  689. }
  690. } catch (\Throwable $e) {
  691. return ['return_code' => 500, 'return_msg' => '发起退款失败'];
  692. }
  693. //修改订单状态
  694. Db::transaction(function () use ($recharge) {
  695. $recharge->refund_time = date('Y-m-d H:i:s');
  696. $recharge->status = 3;
  697. $recharge->save();
  698. });
  699. return ['return_code' => 200, 'return_msg' => '发起成功'];
  700. }
  701. /**
  702. * 回调处理
  703. * @return bool
  704. * @author php迷途小书童
  705. * @created 2021/1/16 15:12
  706. */
  707. public function doNotify($params)
  708. {
  709. $order = $this->dao->getWhere(['agent_apply_no' => $params['agent_apply_no']]);
  710. if (!$order)
  711. return false;
  712. if ($order->status == 3)
  713. return true;
  714. switch ($order->trade_type){
  715. //海科入网审核结果
  716. case 'WX,ALI':
  717. Queue::push(SendSmsJob::class, [
  718. 'tempId' => 'HAIKE_CHECK_MERCHANT_CODE',
  719. 'id' => $order->id
  720. ]);
  721. break;
  722. }
  723. //修改订单状态
  724. Db::transaction(function () use ($order, $params) {
  725. $intention = MerchantIntention::getInstance()->where('mer_intention_id', $order->apply_id)->find();
  726. if (!$intention)
  727. return true;
  728. if ($order->status == 3) {
  729. $order->applysms = 1;
  730. }
  731. try {
  732. //如果海科入网通过,且申请类型是个人,则申请微信和支付宝业务
  733. if ($order->apply_type == 1 && in_array($params['status'], [3,7])) {
  734. $this->haikeWxApply(['mer_intention_id' => $intention->mer_intention_id]);
  735. $this->haikeAliApply(['mer_intention_id' => $intention->mer_intention_id]);
  736. }
  737. //微信业务通过,且申请类型是个人,发起微信二次认证
  738. if ($order->apply_type == 2 && in_array($params['status'], [3])) {
  739. $return = $this->haikeWxAuthApply(['mer_intention_id' => $intention->mer_intention_id]);
  740. if ($return['code'] == 200) {
  741. $this->haikeWxAppidConfAdd(['mer_intention_id' => $intention->mer_intention_id]);
  742. }
  743. }
  744. } catch (\Throwable $e) {
  745. Log::info('mer_intention_id'. $intention->mer_intention_id . $e->getMessage());
  746. }
  747. //海科入网拒绝
  748. if ($order->apply_type == 1 && in_array($params['status'], [1,5,6])) {
  749. $intention->status = 2;
  750. $intention->check_result = $params['msg'] ?? '';
  751. $intention->save();
  752. }
  753. //微信报备失败
  754. if ($order->apply_type == 2 && in_array($params['status'], [2])) {
  755. $result_msg = '';
  756. try {
  757. $str = substr($params['msg'], strpos($params['msg'], '{'));
  758. $array = json_decode($str, true);
  759. $result_msg = $array['result_msg'] ?? '';
  760. } catch (\Throwable $e) {
  761. Log::info('微信报备回调:'.$e->getMessage());
  762. }
  763. $intention->status = 2;
  764. $intention->haike_wx_status = 2;
  765. $intention->check_result = $result_msg;
  766. $intention->save();
  767. }
  768. //支付宝报备失败
  769. if ($order->apply_type == 3 && in_array($params['status'], [2])) {
  770. $result_msg = '';
  771. try {
  772. $str = substr($params['msg'], strpos($params['msg'], '{'));
  773. $array = json_decode($str, true);
  774. $result_msg = $array['result_msg'] ?? '';
  775. } catch (\Throwable $e) {
  776. Log::info('支付宝报备回调:'.$e->getMessage());
  777. }
  778. $intention->status = 2;
  779. $intention->haike_ali_status = 2;
  780. $intention->check_result = $result_msg;
  781. $intention->save();
  782. }
  783. $order->status = $params['status'] ?? 0;
  784. $order->check_result = $params['msg'] ?? '';
  785. $order->apply_no = $params['apply_no'] ?? '';
  786. $order->update_time = date('Y-m-d H:i:s');
  787. $order->save();
  788. });
  789. return true;
  790. }
  791. /**
  792. * 图片压缩
  793. * @return bool|mixed
  794. * @author php迷途小书童
  795. * @created 2021/1/30 17:17
  796. */
  797. public function getThumb($img)
  798. {
  799. $img = app()->getRootPath().'/public'.$img;
  800. $image = \think\Image::open($img);
  801. // 返回图片的宽度
  802. $width1 = $width = $image->width();
  803. if ($width > 1000) {
  804. $width1 = 1000;
  805. }
  806. $multiple = bcdiv($width, $width1);
  807. // 返回图片的高度
  808. $height = $image->height();
  809. $height1 = bcdiv($height, $multiple);
  810. $image->thumb($width1, $height1)->save($img);
  811. return $this->getImageBase64($img);
  812. }
  813. /**
  814. * 获取base64
  815. * @return mixed
  816. * @author php迷途小书童
  817. * @created 2021/1/30 17:11
  818. */
  819. public function getImageBase64($img)
  820. {
  821. $file = $img;
  822. if($fp = fopen($file,"rb", 0))
  823. {
  824. $gambar = fread($fp,filesize($file));
  825. fclose($fp);
  826. return chunk_split(base64_encode($gambar));
  827. } else {
  828. return false;
  829. }
  830. }
  831. }