basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";; class AlipayThirdAppForm extends Model { public $store_id; public $status; public $params_info; public $base_info; public $template_info; public function rules() { return [ [['store_id', 'status'], 'integer'], [['params_info'], 'safe'] ]; } //构造函数 public function __construct($config = []) { parent::__construct($config); //获取三方平台信息 //三方平台Appid $this->base_info['appid'] = Option::get("alipay_appid", 0, 'saas')['value']; //获取当前时间戳 $this->base_info['timestamp'] = date("Y-m-d H:i:s"); //设置加解密方式 $this->base_info['sign_type'] = "RSA2"; //获取私钥 $this->base_info['private_key'] = Option::get("alipay_app_private_key", 0, 'saas')['value']; //获取公钥 $this->base_info['public_key'] = Option::get("alipay_public_key", 0, 'saas')['value']; //获取ase_key,加签方式 $this->base_info['alipay_aes_key'] = Option::get("alipay_aes_key", 0, 'saas')['value']; //获取模板aes加签方式 $this->template_info['ali_template_aes'] = Option::get("ali_template_aes", 0, 'saas')['value']; //获取模板appid $this->template_info['ali_template_appid'] = Option::get("ali_template_appid", 0, 'saas')['value']; //获取模板auth_token $this->template_info['ali_template_token'] = Option::get("ali_template_token", 0, 'saas')['value']; } public function miniCommon($request, $biz_content = null, $is_hidden_token = 0, $token = '') { try { //模板信息 $template_info = $this->template_info; //请求的基础信息 $base_info = $this->base_info; //模板token $auth_token = $template_info['ali_template_token']; //小程序ID $store_id = $this->store_id; if (!empty($store_id)) { $store_token = StoreMiniToken::findOne(['store_id' => $store_id]); //如果小程序ID存在,则将token从模板转成商家小程序的token if (!empty($store_token->app_auth_token)) { $auth_token = $store_token->app_auth_token; } } //设置基础请求数据 $aop = new AopClient(); //设置appid $aop->appId = $base_info['appid']; //设置私钥 $aop->rsaPrivateKey = $base_info['private_key']; //设置公钥 $aop->alipayrsaPublicKey = $base_info['public_key']; //设置ase_key,加签方式 $aop->encryptKey = $base_info['alipay_aes_key']; //设置加解密方式 $aop->signType = $base_info['sign_type']; //处理没有biz_content情况 if ($biz_content) { $request->setBizContent($biz_content); } //处理不需要token情况 if ($is_hidden_token === 0) { //开始请求 $result = $aop->execute($request, null, ($token ?: $auth_token)); } else { //开始请求 $result = $aop->execute($request); } //请求后的数据处理 $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return $result->$responseNode; } catch (\Exception $e) { return [ 'sub_msg' => $e->getMessage() ]; } } public function getParams() { try { $store_id = $this->store_id; $data = [ 'appid' => $this->base_info['appid'], 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/index.php/alipay/token'), 'state' => $store_id ]; $token_log = StoreMiniToken::findOne(['store_id' => $store_id]) ?: new StoreMiniToken(); $token_log->store_id = $store_id; $token_log->created_at = time(); $token_log->type = 1; $token_log->salesman_id = 0; $token_log->save(); $url = "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=WEBAPP&redirect_uri=" . $data['redirect_uri'] . '&state=' . $data['state']; $filename = md5(date('Ym') . 'token' . $token_log->id); $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg'; $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg'); QrCode::image($url, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path); $token_log->url = $pic_url; if (!$token_log->save()) { throw new \Exception("生成授权链接失败"); } return [ 'code' => 0, 'url' => $pic_url, 'msg' => "获取成功" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @return array|void * 查询授权状态 */ public function getStatus() { try { $store_id = $this->store_id; $token_log = StoreMiniToken::findOne(['store_id' => $store_id]); if ($token_log && ((int)$token_log->status === 1) && !empty($token_log->app_auth_token)) { return [ 'code' => 0, 'msg' => '授权完成,请填写营业执照编号以及营业执照' ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @return array * 创建事务 */ public function getAppPay(): array { try { $store_id = $this->store_id; $params_info = $this->params_info; $token_log = StoreMiniToken::findOne(['store_id' => $store_id]); //获取事务状态 $batch_status = intval($token_log->batch_status); //如果已经开始审核,且没有失败,没有被商户手动拒绝 if ($batch_status > 4 && $batch_status !== 8) { throw new \Exception("审核中,无需操作"); } if ( empty($params_info['account']) || empty($params_info['contact_name']) || empty($params_info['contact_mobile']) || empty($params_info['contact_email']) || empty($params_info['license_no']) || empty($params_info['license_pic']) ) { throw new \Exception("请完善信息"); } $param = json_encode([ 'account' => $params_info['account'] ?: $token_log->account, 'contact_info' => [ 'contact_name' => $params_info['contact_name'] ?: $token_log->contact_name, 'contact_mobile' => $params_info['contact_mobile'] ?: $token_log->contact_mobile, 'contact_email' => $params_info['contact_email'] ?: $token_log->contact_email ] ]); //调用接口配置信息 $request = new AlipayOpenAgentCreateRequest(); //调用接口 $result = $this->miniCommon($request, $param); if ($result->code == 10000) { //保存事务编号,用作签约等操作 $batch_no = $result->batch_no; $token_log->batch_no = $batch_no; $token_log->account = $params_info['account']; $token_log->contact_name = $params_info['contact_name']; $token_log->contact_mobile = $params_info['contact_mobile']; $token_log->contact_email = $params_info['contact_email']; //保存状态信息 $token_log->batch_status = 1; //保存信息 if (!$token_log->save()) { throw new \Exception(json_encode($token_log->errors)); } $result = $this->appPay($token_log, $batch_no); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } $result = $this->openAgentConfirm($token_log, $batch_no); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } return [ 'code' => 0, 'msg' => $result['msg'], 'data' => $result['data'] ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @param $model * @param string $batch_no * @return array * 签约app支付 */ public function appPay($model, string $batch_no = '') { try { $params_info = $this->params_info; $request = new AlipayOpenAgentMobilepaySignRequest(); $license_pic = $this->saveTempImage($params_info['license_pic']); //设置业务编号 $request->setBatchNo($batch_no); //设置商家经营类目编码 $request->setMccCode("A0002_B0201"); //特殊资质证书 if (!empty($params_info['special_license_pic'])) { $request->setSpecialLicensePic($params_info['other_data']); } $request->setBusinessLicenseNo($params_info['license_no']); $request->setDateLimitation("2099-12-31"); $request->setBusinessLicensePic("@". $license_pic); $request->setAppName("应用签约"); //调用接口 $result = $this->miniCommon($request,null, 1); //如果请求成功或者报同类产品已经签约就显示成功 if (($result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") { $model->batch_status = 2; $model->license_pic = $params_info['license_pic']; $model->license_no = $params_info['license_no']; $model->other_data = $params_info['other_data']; if ($result->sub_code == "ANT_PRODUCT_CONFLICT") { $model->batch_status = 2; } if (!$model->save()) { throw new \Exception(json_encode($model->errors)); } return [ 'code' => 0, 'msg' => "操作成功", 'batch_status' => $model->batch_status ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @param $model * @param string $batch_no * @return array * 提交事务 */ public function openAgentConfirm($model, string $batch_no = '') { try { //请求参数 $param = json_encode([ //业务编号 'batch_no' => $batch_no ]); //请求配置信息 $request = new AlipayOpenAgentConfirmRequest(); $result = $this->miniCommon($request, $param, 1); //请求成功 if ($result->code == 10000) { $model->batch_status = 3; if (!$model->save()) { throw new \Exception($model->errors); } return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * @return array|void * 查询状态 */ public function openAgentOrderStatus() { try { $store_id = $this->store_id; $model = StoreMiniToken::find()->where(['and', ['in', 'batch_status', [3, 5, 6]], ['store_id' => $store_id]])->one(); if (empty($model)) { throw new \Exception('没有待查询的信息'); } $batch_no = $model->batch_no; if (empty($batch_no)) { throw new \Exception('未创建事务'); } //请求参数 $param = json_encode([ //业务编号 'batch_no' => $batch_no ]); $request = new AlipayOpenAgentOrderQueryRequest(); $result = $this->miniCommon($request, $param, 1); if ($result->code == 10000) { $status = $result->order_status; $msg = "成功"; switch ($status) { case "MERCHANT_INFO_HOLD": //异常单 4 $model->batch_status = 4; $model->fail_reason = "订单异常"; break; case "MERCHANT_AUDITING": //审核中 3 $model->batch_status = 3; $model->fail_reason = "订单审核中"; break; case "MERCHANT_CONFIRM": //待确认 5 $model->batch_status = 5; $model->batch_url = $result->confirm_url; $model->fail_reason = "等待商户确认,请注意查收邮件以及短信或访问" . $result->confirm_url . "进行授权"; break; case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6 $model->batch_status = 2; $model->fail_reason = "商户确认成功"; break; case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7 $model->batch_status = 7; $model->fail_reason = "商户超时未确认"; break; case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8 $model->batch_status = 8; $model->fail_reason = $result->reject_reason; break; } if (!$model->save()) { throw new \Exception($model->errors); } return [ 'code' => 0, 'msg' => $msg, 'data' => $result ]; } throw new \Exception($result->sub_msg); } catch (\Exception $e) { $file_name = \Yii::$app->runtimePath . '/logs/app_pay.log'; file_put_contents($file_name, "\r\n" . '[商城申请App支付][' . date('Y-m-d H:i:s') . ']' . $e->getMessage(), FILE_APPEND); return ; } } public function getAppInfo() { try { $store_id = $this->store_id; $model = StoreMiniToken::find()->where(['store_id' => $store_id]) ->select('id, status, batch_status, contact_name, contact_mobile, contact_email, license_no, license_pic, other_data, batch_url, fail_reason, account')->asArray()->one(); $form = new AlipayThirdMerchantForm(); $form->ali_mini_id = $model['id']; $form->app_type = 1; $form->openAgentOrderStatus(); //获取app分账权限 $ali_sign = AliMiniSign::findOne(['app_type' => 1, 'ali_mini_id' => $model['id'], 'type' => 1]); $model['ali_sign'] = $ali_sign ?: [ 'batch_status' => 0 ]; return [ 'code' => 0, 'data' => $model ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } //获取网络图片到临时目录 public function saveTempImage($url) { if (strpos($url, 'http') === false) { $url = 'http:' . trim($url); } if (!is_dir(\Yii::$app->runtimePath . '/image')) { mkdir(\Yii::$app->runtimePath . '/image'); } $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg'; CurlHelper::download($url, $save_path); return $save_path; } }