| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\constants\OptionSetting;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\Store;
- use app\models\StoreSchedule;
- use app\modules\admin\models\SaasForm;
- use app\modules\admin\models\WechatMiniMerchantForm;
- use app\modules\admin\models\YunstMiniMerchantForm;
- use app\plugins\scanCodePay\models\Order;
- use app\utils\yunst\src\MemberService;
- class YunstMiniMerchantController extends BaseController
- {
- /**
- * 设置通联商户进件
- * @return \yii\web\Response
- */
- public function actionSetMerchantInfo()
- {
- $form = new YunstMiniMerchantForm();
- $form->id = post_params("id", 0) ?: get_params("id", 0);
- //如果是get就获取数据
- if (\Yii::$app->request->isGet) {
- $res = $form->merchantInfo();
- } else {
- $form->merchant_info = post_params("merchant_info");
- $res = $form->save();
- }
- return $this->asJson($res);
- }
- /**
- * 商户进件结果查询
- * @return \yii\web\Response
- */
- public function actionMerchantStatus()
- {
- $Merchant = new \app\utils\yunst\src\Merchant();
- $res = $Merchant->queryMerchantStatus(get_store_id());
- }
- /**
- * 商户进件信息查询
- * @return \yii\web\Response
- */
- public function actionMerchantInfo()
- {
- $Merchant = new \app\utils\yunst\src\Merchant();
- $res = $Merchant->queryMerchantInfo(get_store_id());
- }
- /**
- * 是否可以协议签约
- */
- public function actionIsSignProtocol()
- {
- $merchant_info = MerchantInfo::findOne(['store_id' => get_store_id(), 'status'=> 3, 'is_yunst' => 1, 'is_delete' => 0]);
- if ($merchant_info){
- $store = Store::findOne(['id' => get_store_id()]);
- return $this->asJson(['code'=>0,'msg'=>'协议可签约','data'=>['ratio'=>$store->ratio??0]]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>'商户进件信息不存在']);
- }
- }
- /**
- * 商户协议签约URL
- * @return \yii\web\Response
- */
- public function actionSignProtocol()
- {
- $Merchant = new \app\utils\yunst\src\Merchant();
- $store = Store::findOne(['id' => get_store_id()]);
- $ratio = post_params("ratio", 0);
- if (!$ratio){
- return $this->asJson(['code'=>1,'msg'=>'抽成比例不能为空']);
- }
- if (floatval($store->ratio) > 0) {
- return $this->asJson(['code'=>1,'msg'=>'已经签约过 不可重复操作']);
- }
- $store->ratio = $ratio;
- $store->save();
- //查询商户进件信息
- $merchant_info = MerchantInfo::findOne(['store_id' => get_store_id(),'status'=> 3, 'is_yunst' => 1, 'is_delete' => 0]);
- if (!$merchant_info){
- return $this->asJson(['code'=>1,'msg'=>'商户进件信息不存在']);
- }
- $authorizedCustName = json_decode($merchant_info->business_info,true)['merchant_shortname'];
- $authorizedCustId = $merchant_info->sub_mch_id;
- $data['bizUserId'] = $store->store_number;
- $data['cusid'] = $authorizedCustId;
- $data['commMode'] = 'rate';
- $data['commRatio'] = $ratio;
- $data['commAmoun'] = '';
- $data['authWay']= 'smscode';
- $data['authorizedCustName']= $authorizedCustName;//被授权的客户名称 ISV 入驻通联时提交的营业执照名称
- // $data['authorizedCustName']= "大同三然珠宝有限公司"; //被授权的客户名称 ISV 入驻通联时提交的营业执照名称
- // $data['authorizedCustId']= '082207111000028';
- $data['protocolType']= '2';
- $data['notifyUrl']= \Yii::$app->request->hostInfo;
- $data['jumpUrl']= \Yii::$app->request->hostInfo;
- $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
- if ($oldConf){
- $oldConf = json_decode($oldConf, true);
- // $data['authorizedCustName']= '贵州名宾连锁科技有限公司';
- $data['authorizedCustId']= $oldConf['customer_number'];
- }
- // print_r($data);die();
- $res = $Merchant->signProtocol(get_store_id(),$data);
- // return $this->asJson(['code'=>1,'msg'=>$data]);
- // return $this->asJson(['code'=>1,'msg'=>$res]);
- if ($res['code'] == 10000 && $res['subCode']=='SUCCESS'){
- return $this->asJson(['code'=>0,'data'=>$res['data'],'msg'=>$res['subMsg'],'signProtocolUrl'=>$res['data']['signProtocolUrl']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- // print_r($res);die;
- }
- /**
- * 商户协议签约状态查询
- * @return \yii\web\Response
- */
- public function actionSignProtocolStatus()
- {
- $Merchant = new \app\utils\yunst\src\Merchant();
- $store = Store::findOne(['id' => get_store_id()]);
- // if (!$store->cusid){
- // return [
- // 'code' => 1,
- // 'msg' => '进件商户号不存在'
- // ];
- // }
- $data['cusid'] = $store->cusid;
- $data['protocolType']= '2';
- //print_r($data);die();
- $res = $Merchant->protocolStaus(get_store_id(),$data);
- // return $this->asJson(['code'=>0,'msg'=>$res['subMsg']]);
- if ($res['subCode']!='FAIL'){
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- public function actionPay()
- {
- $orderYunst = new \app\utils\yunst\src\OrderYunst();
- $store = Store::findOne(['id' => get_store_id()]);
- $res = $orderYunst->payYunst();
- return $this->asJson(['code'=>0,'msg'=>$res]);
- if ($res['subCode']!='FAIL'){
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- /**
- * 聚合收款交易对账文件(收银宝接口)
- * @return \yii\web\Response
- */
- public function actionGetFile()
- {
- if (empty(SaasForm::yunstSetting(1))) {
- return $this->asJson(['code' => 1, 'msg' => '通联服务商参数配置不完善']);
- }
- $orderYunst = new \app\utils\yunst\src\OrderYunst();
- $store = Store::findOne(['id' => get_store_id()]);
- if (empty($store->cusid)) {
- return $this->asJson(['code' => 1, 'msg' => '通联参数配置不完善']);
- }
- $date = post_params('date','');
- $res = $orderYunst->getTrxFile($date,$store->cusid);
- if ($res['subCode']!='FAIL'){
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- /**
- *商户结算资金管理类交易对账文件
- * @return \yii\web\Response
- */
- public function actionGetAccountFile()
- {
- if (empty(SaasForm::yunstSetting(1))) {
- return $this->asJson(['code' => 1, 'msg' => '通联服务商参数配置不完善']);
- }
- $orderYunst = new \app\utils\yunst\src\OrderYunst();
- $date = post_params('date','');
- $res = $orderYunst->getCheckAccountFile($date);
- // print_r($res);die;
- if ($res['subCode']!='FAIL'){
- $file_url = $res['data']['detailUrl'];
- // 获取文件内容并转码
- $dataString = file_get_contents($file_url);
- $dataString = mb_convert_encoding($dataString, 'UTF-8', 'GBK');
- // $dataString = "CY0b6eb06781205e81706235034|202401261862102508906876928|服务商抽佣|100|10||20240126101035||
- // CY413362f65eb60431706238185|202401261862115728317091840|服务商抽佣|10|1||20240126110307||
- // AL20240125172538203993|202401261862163495248859136|服务商抽佣|20000|2000||20240126141256||
- // AL20240126142846101335|202401261862169223070093312|服务商抽佣|10000|1000||20240126143542||
- // AL20240126151652308322|202401261862179960379281408|服务商抽佣|10000|1000||20240126151821||
- // AL20240126160130287708|202401261862191204138094592|服务商抽佣|10000|1000||20240126160302||
- // SC20240126161814865844|202401261862195170238402560|服务商抽佣|12000|1200||20240126161847||";
- // 使用 explode() 函数将字符串拆分成数组
- $dataArray = explode('|', $dataString);
- // 删除数组中的最后一个元素
- array_pop($dataArray);
- // 移除数组中的空元素
- // $dataArray = array_filter($dataArray);
- // 将一维数组转换为二维数组,每个子数组包含8个元素
- $resultArray = [];
- $chunkedArray = array_chunk($dataArray, 8);
- // print_r($chunkedArray);die;
- // 输出结果
- // print_r($chunkedArray);die;
- if ($chunkedArray){
- foreach ($chunkedArray as $item) {
- $resultArray[] = [
- 'order_no' => trim($item[0]),//商户订单号
- 'order_time' => $item[1],//云商通联商户订单号
- 'order_type' => $item[2],//订单类型
- 'order_amount' => $item[3],//订单金额
- 'order_fee' => $item[4],//抽用金额
- 'order_status' => $item[5],//手续费
- 'order_remark' => $item[6],//交易完成时间
- 'order_time_str' => $item[7],//原商户订单号
- ];
- }
- foreach ($resultArray as $key=>$item){
- $resultArray[$key]['mch_name'] = '';
- //处理数据结构
- //查询商户信息
- $orderNoHead = substr($item['order_no'], 0, 2);
- if ($orderNoHead == 'SC'){
- $order_info = Order::find()->where(['order_no' => $item['order_no']])->one();
- if ($order_info){
- $store_info = Store::find()->where(['store_id' => $order_info->store_id])->one();
- if ($store_info){
- $resultArray[$key]['mch_name'] = $store_info->name;
- }else{
- $resultArray[$key]['mch_name'] = '平台';
- }
- }else{
- $resultArray[$key]['mch_name'] = '';
- }
- }
- if ($orderNoHead == 'AL'){
- $order_info = \app\models\Order::find()->where(['order_no' => $item['order_no']])->one();
- if ($order_info){
- $store_info = Store::find()->where(['store_id' => $order_info->store_id])->one();
- if ($store_info){
- $resultArray[$key]['mch_name'] = $store_info->name;
- }else{
- $resultArray[$key]['mch_name'] = '平台';
- }
- }else{
- $resultArray[$key]['mch_name'] = '';
- }
- }
- }
- return $this->asJson(['code'=>0,'data'=>$resultArray??'']);
- }else{
- return $this->asJson(['code'=>1,'msg'=>'暂无对账数据']);
- }
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- /**
- * 订单查询 本接口支持查询“服务商抽佣、服务商抽佣退款、商户资金结算/服务商提现”的订单的订单状态
- * @return \yii\web\Response
- */
- public function actionOrderCheck()
- {
- $orderYunst = new \app\utils\yunst\src\OrderYunst();
- $store = Store::findOne(['id' => get_store_id()]);
- $data['bizOrderNo'] = 'SC20240123145958400674';
- $data['orderNo'] ='202401241861428958520807424';
- $res = $orderYunst->getOrderStatus($data);
- return $this->asJson(['code'=>0,'msg'=>$res]);
- if ($res['subCode']!='FAIL'){
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- public function actionOrder()
- {
- $orderYunst = new \app\utils\yunst\src\OrderYunst();
- $store = Store::findOne(['id' => get_store_id()]);
- $data['bizOrderNo'] = 'SC20240123145958400674';
- $data['orderNo'] ='202401241861428958520807424';
- $res = $orderYunst->getOrderStatus($data);
- return $this->asJson(['code'=>0,'msg'=>$res]);
- if ($res['subCode']!='FAIL'){
- return $this->asJson(['code'=>0,'data'=>$res['data']??'','msg'=>$res['subMsg']]);
- }else{
- return $this->asJson(['code'=>1,'msg'=>$res['subMsg']]);
- }
- }
- public function actionList()
- {
- $form = new WechatMiniMerchantForm();
- $form->status = get_params("status",-1);
- $res = $form->list2();
- return $this->asJson($res);
- }
- public function actionGetCountry()
- {
- $form = new WechatMiniMerchantForm();
- $res = $form->getCountry();
- return $this->asJson($res);
- }
- // 获取小程序开始使用/暂停使用
- public function actionMiniIsUse(){
- $form = new WechatMiniMerchantForm();
- $form->id = post_params("id")??get_params("id");
- $form->is_use = post_params("is_use");
- $res = $form->unbind();
- return $this->asJson($res);
- }
- //设置店铺会员号
- public function actionSetStoreNumber() {
- try {
- $store_number = trim(post_params('store_number'));
- $store_id = get_store_id();
- $store = Store::findOne($store_id);
- if (!$store) {
- throw new \Exception('店铺信息不存在');
- }
- if (\Yii::$app->request->isPost) {
- if (!empty(trim($store->yunst_user_id))) {
- throw new \Exception('已经设置并提交成功 不可重复操作!');
- }
- if (!$store_number) {
- throw new \Exception('请输入商城会员编号!');
- }
- $store_info = Store::find()->where(['is_delete' => 0, 'store_number' => $store_number])->one();
- if ($store_info){
- throw new \Exception('会员编号已存在');
- }
- //判断后台开启通联
- $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
- if ($oldConf) {
- $oldConf = json_decode($oldConf, true);
- if (empty($oldConf) || empty(trim($oldConf['appid']))) {
- throw new \Exception('平台配置信息缺失 需完善后重试');
- }
- //调用第三方会员创建接口
- $memberService = new MemberService();
- $res_member = $memberService->createMember($store_number,2);
- if ($res_member['subCode'] == "FAIL"){
- throw new \Exception($res_member['subMsg']);
- }
- $store->yunst_user_id = $res_member['data']['userId'] ?? '';
- }
- $store->store_number = $store_number;
- if (!$store->save()) {
- throw new \Exception(json_encode($store->errors, JSON_UNESCAPED_UNICODE));
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功'
- ]);
- } else {
- $data = [
- 'store_number' => $store->store_number ?? ''
- ];
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data
- ]);
- }
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- }
|