basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";; class AlipayThirdForm extends Model { /** * @var mixed */ public $alipay_aes_key; public $appid; public $method; public $sign_type; public $sign; public $version = "1.0"; public $charset; public $biz_content; public $timestamp; public $auth_code; public $baseUrl = "https://openapi.alipay.com/gateway.do"; public $private_key; public $public_key; public $license_no; //营业执照编号 public $license_pic; //营业执照照片 public $template_version; //模版版本号 public $ext; //ext public $template_id; //模版ID public $app_version; //版本号 public $bundle_id; //小程序投放的端参数 public $AppName; public $AppEnglishName; public $AppSlogan; public $AppLogo; public $AppDesc; public $ServicePhone; public $ServiceEmail; public $MiniCategoryIds; public $page_num; public $page_size; public $auditData; //审核资料 public $is_filter; public $gray_strategy; public $route_url; public $mode; public $page_redirection; public $route_pattern; public $mini_id; public $version_id; public $role; public $logon_id; public $license_name; public $license_date; public $is_long_effective; public $alipay_account; public $legal_personal_name; public $contact_name; public $merchant_id; public $bind_store_id; public $apply_msg; public $fun = 'setPicture'; /*支付回调url*/ private static $notify_url = 'alipay/notify'; public function rules() { return [ [["page_num", "page_size", 'is_filter', 'gray_strategy', 'version_id', 'role', 'is_long_effective', 'merchant_id', "bind_store_id"], 'integer'], [ [ "appid", "method", "sign_type", "sign", "version", "charset", "biz_content", "timestamp", "auth_code", "baseUrl", "license_no", "license_pic", "template_version", "ext", "template_id", "app_version", "bundle_id", "AppName", "AppEnglishName", "AppSlogan", "AppLogo", "AppDesc", "ServicePhone", "ServiceEmail", "MiniCategoryIds", "auditData", "route_pattern", "page_redirection", "mode", "route_url", "page_num", "page_size", "gray_strategy", "url_param", "query_param", "describe", "color", "size", "mini_id", "logon_id", "license_name", "license_date", "apply_msg" ], 'string' ], ]; } public function __construct($config = []) { parent::__construct($config); $this->appid = Option::get("alipay_appid", 0, 'saas')['value']; $this->timestamp = date("Y-m-d H:i:s"); $this->sign_type = "RSA2"; $this->charset = "utf-8"; $this->private_key = Option::get("alipay_app_private_key", 0, 'saas')['value']; $this->public_key = Option::get("alipay_public_key", 0, 'saas')['value']; $this->alipay_aes_key = Option::get("alipay_aes_key", 0, 'saas')['value']; } /** * 公共参数请求 * @return \$1|false|mixed|\SimpleXMLElement * @throws \Exception * * */ public function miniCommon($request, $is_hidden_biz_content = 0, $id = 0, $is_hidden_token = 0, $is_new = 0) { try { $auth_token = ""; $mini_id = $this->mini_id; if (!empty($this->bind_store_id)) { $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); $mini_id = $StoreAliMini->id; } if (!empty($id != false ? $id : $mini_id)) { $mini = StoreAliMini::findOne($id != false ? $id : $mini_id); $auth_token = $mini->auth_token; } if (empty($auth_token) && $is_new == 1) { $auth_token = Option::get("ali_template_token", 0, 'saas')['value']; } //换取token $aop = new AopClient(); $aop->gatewayUrl = $this->baseUrl; $aop->appId = $this->appid; $aop->rsaPrivateKey = $this->private_key; $aop->alipayrsaPublicKey = $this->public_key; $aop->encryptKey = $this->alipay_aes_key; $aop->encryptType = "AES"; $aop->apiVersion = '1.0'; $aop->signType = $this->sign_type; $aop->postCharset = $this->charset; if ($is_hidden_biz_content == 0) { $request->setBizContent($this->biz_content); } if (!empty($auth_token) && empty($is_hidden_token)) { $result = $aop->execute($request, null, $auth_token); } else { $result = $aop->execute($request); } \Yii::error($result); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; return $result->$responseNode; } catch (\Exception $e) { return (object)[ 'sub_msg' => $e->getMessage() ]; } } /** * 传递授权回调参数 * @return array */ public function getParams() { $data = [ 'appid' => $this->appid, 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/admin/#/setting/alipayApplet') ]; return [ 'code' => 0, 'url' => "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=TINYAPP&redirect_uri=" . $data['redirect_uri'], 'msg' => "获取成功" ]; } /** * 保存授权码 * 获取保存AuthToken以及小程序信息 * 同时获取aes密钥 * 通过授权码AuthCode换取AuthToken * */ public function setAuthCode() { try { $param = [ "grant_type" => "authorization_code", "code" => $this->auth_code ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.auth.token.app"; $request = new AlipayOpenAuthTokenAppRequest(); $result = $this->miniCommon($request, 0, 0, 0, 0); \Yii::error($result); $token = $result->tokens[0]; if (!empty($result->code) && $result->code == 10000) { $mini = StoreAliMini::find()->where(['store_id' => get_store_id(), 'auth_app_id' => $token->auth_app_id])->one(); if (empty($mini)) { $mini = new StoreAliMini(); $mini->store_id = get_store_id(); $mini->auth_app_id = $token->auth_app_id; } $mini->auth_code = $this->auth_code; $mini->userid = $token->user_id; $mini->auth_token = $token->app_auth_token; $mini->refresh_token = json_encode([ 'app_refresh_token' => $token->app_refresh_token, 'end_time' => time() * 1 + $token->re_expires_in ]); $res = $mini->save(); if ($res) { //获取应用aes密钥 $res = $this->aesAuth(0, $mini->id); if ($res['code'] === 0) { $res = $this->getAppTokenInfo($mini->id); if ($res['code'] === 0) { $res = $this->updateMiniVersion($mini->id); if ($res['code'] === 0) { $res = $this->getMiniBaseInfo($mini->id); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } else { throw new \Exception($res['msg']); } } else { throw new \Exception($res['msg']); } } else { throw new \Exception($res['msg']); } } else { throw new \Exception($mini->errors); } } else { throw new \Exception($result->sub_msg); } return [ 'code' => 0, 'msg' => "success" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } /** * 授权应用aes密钥设置/查询 */ public function aesAuth($is_get = 0, $id = 0) { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($id); $auth_app_id = Option::get("ali_template_appid", 0, 'saas')['value'];; if (!empty($mini)) { $auth_app_id = $mini->auth_app_id; } if (empty($auth_app_id)) { throw new \Exception("请先配置小程序信息"); } \Yii::error($auth_app_id); $param = [ "merchant_app_id" => $auth_app_id ]; \Yii::error($param); $this->biz_content = json_encode($param); $this->method = "alipay.open.auth.app.aes.set"; $request = new AlipayOpenAuthAppAesSetRequest(); if ($is_get == 1) { $this->method = "alipay.open.auth.app.aes.get"; $request = new AlipayOpenAuthAppAesGetRequest(); } if (empty($id)) { $is_new = 1; } else { $is_new = 0; } $result = $this->miniCommon($request, 0, $id, 1, $is_new); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { if (!empty($id) && !empty($mini)) { $mini->aes_key = $result->aes_key; } else { Option::set('ali_template_aes', $result->aes_key, 0, 'saas'); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result->aes_key ]; } $res = $mini->save(); if ($res) { $t->commit(); $data['aes_key'] = $mini->aes_key; $data['app_id'] = $mini->auth_app_id; $data['alipay_public_key'] = ""; $data['app_public_key'] = ""; $data['app_private_key'] = ""; $data['user_id'] = $mini->userid; $data['name'] = $mini->license_name; $bind_store_id = $mini->store_id; $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $bind_store_id, 'alipay'); if ($res) { \Yii::$app->cache->delete('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id()); \Yii::$app->cache->set('alipay_config_cache_' . !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id(), Json::encode($data)); } return [ 'code' => 0, 'msg' => "操作成功" ]; } else { throw new \Exception($mini->errors); } } else { throw new \Exception($result->sub_msg . $result->code); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 个人账户升级为个体工商户 */ public function updateBussiness($id = 0) { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id); if (empty($mini)) { throw new \Exception("授权失败"); } $mini->license_no = $this->license_no; $mini->license_pic = $this->license_pic; $index = strpos($this->license_pic, \Yii::$app->request->hostName); $len = $index + strlen(\Yii::$app->request->hostName); $license_pic = substr($this->license_pic, $len); $license_pic = $this->image2Base64(\Yii::$app->basePath . $license_pic); if ($license_pic == false) { throw new \Exception("图片转Base64失败"); } $param = [ "license_no" => $this->license_no, "license_pic" => $license_pic ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.individual.business.certify"; $request = new AlipayOpenMiniIndividualBusinessCertifyRequest(); $request->setLicenseNo($this->license_no); $request->setLicensePic($license_pic); $result = $this->miniCommon($request, 1); if (!empty($result->code) && $result->code == 10000) { $mini->is_merchant = $result->certify_result ? 1 : 0; $res = $mini->save(); if ($res) { $t->commit(); return [ 'code' => 0, 'msg' => "操作成功" ]; } else { throw new \Exception($mini->errors); } } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询某个应用授权AppAuthToken的授权信息 */ public function getAppTokenInfo($id) { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ "app_auth_token" => $mini->auth_token ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.auth.token.app.query"; $request = new AlipayOpenAuthTokenAppQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序基于模板上传版本 */ public function updateMiniVersion($id = 0) { $t = \Yii::$app->db->beginTransaction(); try { $id = !empty($id) ? $id : $this->mini_id; if (!empty($this->bind_store_id)) { $StoreAliMini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); $id = $StoreAliMini->id; } $mini = StoreAliMini::findOne($id); $version = StoreAliMiniVersion::find()->where(['mini_id' => $id]) ->orderBy("create_time desc") ->asArray() ->one(); if ($version['is_submit'] == 1) { $res = $this->miniAuditCancel(); if ($res['code'] !== 0) { throw new \Exception("有版本代码正在审核"); } } if (empty($version)) { $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", 1); } else { \Yii::error("版本号:" . $version['template_version']); $app_version = substr($version['template_version'], 7); \Yii::error("版本号:" . $app_version); $app_version = date('y') . '.' . sprintf("%03d", $id) . '.' . sprintf("%03d", $app_version + 1); } \Yii::error("版本号:" . $app_version); if (empty($mini)) { throw new \Exception("没有进行授权"); } $ext = json_encode([ 'extEnable' => true, 'ext' => [ 'shopId' => $this->appid, 'mini_id' => $id, 'store_id' => get_store_id(), 'apiurl' => "https://" . \Yii::$app->request->hostName ] ]); $param = [ 'ext' => $ext, 'template_id' => Option::get("ali_template_appid", 0, 'saas')['value'], 'app_version' => $app_version, ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.upload"; $request = new AlipayOpenMiniVersionUploadRequest(); $result = $this->miniCommon($request, 0, $id); if (!empty($result->code) && $result->code == 10000) { $MiniVersion = new StoreAliMiniVersion(); $MiniVersion->mini_id = $id; $MiniVersion->template_version = $app_version; $MiniVersion->create_time = time(); $MiniVersion->is_up = 1; $res = $MiniVersion->save(); if ($res) { $t->commit(); return [ 'code' => 0, 'msg' => "构建成功", 'data' => $result, "res" => $MiniVersion->errors ]; } else { throw new \Exception(json_encode($MiniVersion->errors)); } } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序查询版本构建状态 * alipay.open.mini.version.build.query */ public function getMiniStatus() { try { if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); } else { $mini_id = $this->mini_id; $mini = StoreAliMini::findOne($mini_id); } if (empty($mini)) { throw new \Exception("没有进行授权"); } $version = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); $param = [ 'app_version' => $version->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.build.query"; $request = new AlipayOpenMiniVersionBuildQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序查询版本列表 * alipay.open.mini.version.build.query */ public function getMiniVersionList() { try { $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $this->method = "alipay.open.mini.version.list.query"; $request = new AlipayOpenMiniVersionListQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序删除版本 * alipay.open.mini.version.delete */ public function delMiniVersion() { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($this->mini_id); $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version, ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.delete"; $request = new AlipayOpenMiniVersionDeleteRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $MiniVersion->delete(); $t->commit(); return [ 'code' => 0, 'msg' => "删除成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序生成体验版 * alipay.open.mini.experience.create */ public function createMiniExperience() { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($this->mini_id); $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version, ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.experience.create"; $request = new AlipayOpenMiniExperienceCreateRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { StoreAliMiniVersion::updateAll(['is_experience' => 0, 'experience_url' => ''], ['and', ['<>', 'id', $MiniVersion->id], ['mini_id' => $this->mini_id]]); $res = $this->createMiniExperienceQrcode(); if ($res['code'] === 0) { $MiniVersion->is_experience = 1; $MiniVersion->experience_url = $res['url']; $MiniVersion->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $res['url'] ]; } else { throw new \Exception("获取体验码失败"); } } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取体验码 * alipay.open.mini.experience.query */ public function createMiniExperienceQrcode() { try { $mini = StoreAliMini::findOne($this->mini_id); $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version, 'bundle_id' => $this->bundle_id ?? "com.alipay.alipaywallet", ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.experience.query"; $request = new AlipayOpenMiniExperienceQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $msg = "获取成功"; if ($result->status == "expVersionPackaging") { $msg = "体验版打包中"; } elseif ($result->status == "notExpVersion") { $msg = "非体验版"; } return [ 'code' => 0, 'msg' => $msg, 'url' => $result->exp_qr_code_url ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序取消体验版 * alipay.open.mini.experience.create() */ public function createMiniExperienceCancel() { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($this->mini_id); $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.experience.cancel"; $request = new AlipayOpenMiniExperienceCancelRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $MiniVersion->is_experience = 0; $MiniVersion->experience_url = ""; $MiniVersion->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询小程序基础信息 * alipay.open.mini.baseinfo.query() */ public function getMiniBaseInfo($id = 0, $type = 0) { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); $id = $mini->id; } else { $mini = StoreAliMini::findOne($id != 0 ? $id : $this->mini_id); } if (empty($mini)) { throw new \Exception("没有进行授权"); } $this->method = "alipay.open.mini.baseinfo.query"; $request = new AlipayOpenMiniBaseinfoQueryRequest(); $result = $this->miniCommon($request, 1, $id != 0 ? $id : $this->mini_id); if (!empty($result->code) && $result->code == 10000) { if ($type === 0) { $mini->app_name = $result->app_name; $mini->app_desc = $result->app_desc; $mini->app_slogan = $result->app_slogan; $mini->app_english_name = $result->app_english_name; $mini->service_email = $result->service_email; $mini->service_phone = $result->service_phone; $mini->app_logo = $result->app_logo; } $mini->category_name = $result->category_names; $res = $mini->save(); if ($res) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result, 'category' => $result->category_names ]; } else { throw new \Exception("插入数据错误"); } } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序修改基础信息 * alipay.open.mini.baseinfo.modify(小程序修改基础信息) */ public function setMiniBaseInfo() { $t = \Yii::$app->db->beginTransaction(); try { $mini_id = $this->mini_id; if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); $mini_id = $mini->id; } else { $mini = StoreAliMini::findOne($mini_id); } if (empty($mini)) { throw new \Exception("没有进行授权"); } $mini_category_ids = ''; if (!empty($this->bind_store_id) && !empty($this->MiniCategoryIds)) { foreach ($this->MiniCategoryIds as $item) { $arr = []; foreach ($item as $child) { array_push($arr, $child['value']); } $str = implode('_', $arr); $mini_category_ids .= $str . ';'; } } else { $mini_category_ids = implode(';', $this->MiniCategoryIds); } $this->method = "alipay.open.mini.baseinfo.modify"; $request = new AlipayOpenMiniBaseinfoModifyRequest(); $mini->app_name = $this->AppName; $mini->app_english_name = $this->AppEnglishName; $mini->app_slogan = $this->AppSlogan; if (empty($this->bind_store_id)) { !empty($this->license_no) && $mini->license_no = $this->license_no; !empty($this->license_pic) && $mini->license_pic = $this->license_pic; !empty($this->license_name) && $mini->license_name = $this->license_name; !empty($this->is_long_effective) && $mini->is_long_effective = $this->is_long_effective; !empty($this->license_date) && $mini->license_date = $this->license_date; $mini->is_long_effective = !empty($mini->is_long_effective) ? 1 : 0; } $mini->app_logo = $this->AppLogo; $mini->app_desc = $this->AppDesc; $mini->service_phone = $this->ServicePhone; $mini->service_email = $this->ServiceEmail; $mini->mini_categoryIds = $mini_category_ids; $mini->category_data = json_encode($this->MiniCategoryIds); $res = $mini->save(); if ($res) { if (empty($this->bind_store_id)) { $res = $this->updateBussiness($mini_id); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } \Yii::error($this->AppName); \Yii::error($this->AppEnglishName); \Yii::error($this->AppSlogan); \Yii::error($this->AppLogo); \Yii::error($this->AppDesc); \Yii::error($this->ServicePhone); \Yii::error($this->ServiceEmail); \Yii::error($this->MiniCategoryIds); $request->setAppName($this->AppName); //小程序应用名称。 $request->setAppEnglishName($this->AppEnglishName); //小程序应用英文名称。 $request->setAppSlogan($this->AppSlogan); //小程序应用简介 $request->setAppLogo("@" . $this->saveTempImage($this->AppLogo)); //小程序应用logo图标,图片格式必须为:png、jpeg、jpg,建议上传像素为180*180。 $request->setAppDesc($this->AppDesc); //小程序应用描述,20-200个字 $request->setServicePhone($this->ServicePhone); //小程序客服电话。 $request->setServiceEmail($this->ServiceEmail); //小程序客服邮箱。 $request->setMiniCategoryIds($mini_category_ids); //新小程序前台类目。格式为 第一个一级类目_第一个二级类目;第二个一级类目_第二个二级类目_第二个三级类目。详细类目可以通过 $result = $this->miniCommon($request, 1); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } throw new \Exception("信息保存失败"); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询使用模板的小程序列表 * alipay.open.mini.template.usage.query(查询使用模板的小程序列表) */ // public function getTemplateUsage(){ // try { // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one(); // if(empty($mini)){ // throw new \Exception("没有进行授权"); // } // $param = [ // 'template_id'=>$this->template_id, // 'page_num'=>$this->page_num, // 'page_size'=>$this->page_size, // 'template_version'=>$this->template_version, // 'bundle_id'=>$this->bundle_id, // ]; // $this->biz_content = json_encode($param); // $this->method = "alipay.open.mini.template.usage.query"; // $request = new AlipayOpenMiniTemplateUsageQueryRequest(); // $result = $this->miniCommon($request); // if(!empty($result->code)&&$result->code == 10000){ // return [ // 'code'=>0, // 'msg'=>"成功", // 'data'=>$result // ]; // }else{ // throw new \Exception($result->sub_msg); // } // }catch (\Exception $e){ // return [ // 'code'=>1, // 'msg'=>$e->getMessage() // ]; // } // } /** * 添加成员 * alipay.open.app.members.create(添加成员) */ public function miniAddMember() { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $role = $this->role; switch ($role) { case 0: throw new \Exception("请选择身份"); break; case 1: $role = "DEVELOPER"; break; case 2: $role = "EXPERIENCER"; break; } $param = [ 'logon_id' => $this->logon_id, 'role' => $role ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.app.members.create"; $request = new AlipayOpenAppMembersCreateRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取成员列表 */ public function getRoleList() { try { $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $role = $this->role; switch ($role) { case 0: throw new \Exception("请选择身份"); break; case 1: $role = "DEVELOPER"; break; case 2: $role = "EXPERIENCER"; break; } $param = [ 'role' => $role ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.app.members.query"; $request = new AlipayOpenAppMembersQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取审核资料 * */ public function getMiniApplyInfo() { $mini = StoreAliMini::find()->alias('sam') ->where(['sam.id' => $this->mini_id, 'samv.id' => $this->version_id]) ->select(" sam.id, sam.license_name, sam.license_date, sam.is_long_effective, sam.license_no, sam.license_pic, sam.mini_categoryIds, sam.app_name, sam.app_english_name, sam.app_slogan, sam.app_logo, sam.app_desc, sam.service_phone, sam.service_email, samv.mini_id, samv.id as version_id, samv.desc, samv.template_version as version ") ->leftJoin(['samv' => StoreAliMiniVersion::tableName()], 'sam.id = samv.mini_id') ->asArray() ->one(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $mini ]; } /** * 小程序提交审核 * alipay.open.mini.version.audit.apply(小程序提交审核) */ public function miniAuditApply() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); } else { $mini = StoreAliMini::findOne($this->auditData['id']); } $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini->id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $this->method = "alipay.open.mini.version.audit.apply"; $request = new AlipayOpenMiniVersionAuditApplyRequest(); $auditData = $this->auditData; if (!empty($this->bind_store_id)) { $mini_category_ids = $auditData['mini_category_ids']; } else { $mini_category_ids = implode(';', $auditData['mini_category_ids']); } $auditData['version_desc'] = '商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线商城当面付上线'; !empty($auditData['service_phone']) && $request->setServicePhone($auditData['service_phone']); //小程序手机号 !empty($auditData['service_email']) && $request->setServiceEmail($auditData['service_email']); //小程序邮箱地址 $request->setVersionDesc($auditData['version_desc']); //小程序版本描述,30-500个字符。 $request->setRegionType("CHINA"); //小程序服务区 !empty($auditData['license_no']) && $request->setLicenseNo($auditData['license_no']); //营业执照号 !empty($auditData['mini_category_ids']) && $request->setMiniCategoryIds($mini_category_ids); !empty($auditData['license_name']) && $request->setLicenseName($auditData['license_name']); //营业执照名称 !empty($auditData['license_pic']) && $request->setFirstLicensePic("@" . $this->saveTempImage($auditData['license_pic'])); //营业执照 !empty($auditData['license_date']) && $request->setLicenseValidDate(!empty($auditData['is_long_effective']) ? '9999-12-31' : $auditData['license_date']); //营业执照有效期 $request->setAppVersion($MiniVersion->template_version); //小程序版本号 !empty($auditData['app_name']) && $request->setAppName($auditData['app_name']); //小程序名称 !empty($auditData['app_english_name']) && $request->setAppEnglishName($auditData['app_english_name']); //英文名称 !empty($auditData['app_slogan']) && $request->setAppSlogan($auditData['app_slogan']); //小程序应用简介,一句话描述小程序功能 !empty($auditData['app_logo']) && $request->setAppLogo("@" . $this->saveTempImage($auditData['app_logo'])); //小程序logo图标 !empty($auditData['app_desc']) && $request->setAppDesc($auditData['app_desc']); //小程序应用描述 $mini->license_name = $auditData['license_name']; $mini->license_date = $auditData['license_date']; $mini->is_long_effective = $auditData['is_long_effective']; $mini->license_no = $auditData['license_no']; $mini->license_pic = $auditData['license_pic']; $mini->mini_categoryIds = $mini_category_ids; $mini->app_name = $auditData['app_name']; $mini->app_english_name = $auditData['app_english_name']; $mini->app_slogan = $auditData['app_slogan']; $mini->app_logo = $auditData['app_logo']; $mini->app_desc = $auditData['app_desc']; $mini->service_phone = $auditData['service_phone']; $mini->service_email = $auditData['service_email']; $mini->category_name = json_encode($auditData['mini_category_ids']); if (empty($this->bind_store_id)) { $res = $mini->save(); } else { $res = true; } if ($res) { $result = $this->miniCommon($request, 1, $mini->id); if (!empty($result->code) && $result->code != 10000) { throw new \Exception($result->sub_msg); } $MiniVersion->is_submit = 1; $res = $MiniVersion->save(); if ($res) { $t->commit(); return [ 'code' => 0, 'msg' => "开始审核", 'data' => $result, ]; } else { return $MiniVersion->errors; } } else { return $mini->errors; } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() . $e->getFile() ]; } } /** * 小程序撤销审核 * alipay.open.mini.version.audit.cancel(小程序撤销审核) */ public function miniAuditCancel() { $t = \Yii::$app->db->beginTransaction(); try { $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.audit.cancel"; $request = new AlipayOpenMiniVersionAuditCancelRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $MiniVersion->is_submit = 0; $MiniVersion->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序类目树查询 * alipay.open.mini.category.query(小程序类目树查询) */ public function getCategoryList() { try { if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id')->orderBy('id desc')->one(); } else { $mini = StoreAliMini::findOne($this->mini_id); } if (empty($mini)) { throw new \Exception("没有进行授权"); } $is_filter = $this->is_filter == 1; $param = [ "is_filter" => $is_filter ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.category.query"; $request = new AlipayOpenMiniCategoryQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $arr = json_decode(json_encode($result->mini_category_list), true); $data = $this->getdata($arr); return [ 'code' => 0, 'msg' => "成功", 'data' => $data ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //处理子父级数据 public function getdata($data, $id = "0") { foreach ($data as $k => $v) { if ($v['parent_category_id'] == $id) { $v['children'] = $this->getdata($data, $v['category_id']); $arr[] = $v; } } return $arr; } /** * 小程序退回开发 * alipay.open.mini.version.audited.cancel(小程序退回开发) */ public function miniVersionCancel() { $t = \Yii::$app->db->beginTransaction(); try { $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.audited.cancel"; $request = new AlipayOpenMiniVersionAuditedCancelRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $MiniVersion->is_submit = 0; $MiniVersion->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序版本详情查询 * alipay.open.mini.version.detail.query(小程序版本详情查询) */ public function miniVersionDetail() { try { $mini = StoreAliMini::findOne($this->mini_id); $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.detail.query"; $request = new AlipayOpenMiniVersionDetailQueryRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序灰度上架 * alipay.open.mini.version.gray.online(小程序灰度上架) */ // public function miniGrayOnline(){ // try { // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one(); // if(empty($mini)){ // throw new \Exception("没有进行授权"); // } // $gray_strategy = 'p'.(int)abs($this->gray_strategy);//只支持10/30/50 // $param = [ // 'app_version'=>$this->app_version, // 'gray_strategy'=>$gray_strategy, // 'bundle_id'=>$this->bundle_id, // ]; // $this->biz_content = json_encode($param); // $this->method = "alipay.open.mini.version.gray.online"; // $request = new AlipayOpenMiniVersionGrayOnlineRequest(); // $result = $this->miniCommon($request); // if(!empty($result->code)&&$result->code == 10000){ // return [ // 'code'=>0, // 'msg'=>"成功", // 'data'=>$result // ]; // }else{ // throw new \Exception($result->sub_msg); // } // }catch (\Exception $e){ // return [ // 'code'=>1, // 'msg'=>$e->getMessage() // ]; // } // } /** * 小程序结束灰度 * alipay.open.mini.version.gray.cancel(小程序结束灰度) */ // public function miniGrayCancel(){ // try { // $mini = StoreAliMini::find()->where(['store_id'=>get_store_id()])->one(); // if(empty($mini)){ // throw new \Exception("没有进行授权"); // } // $param = [ // 'app_version'=>$this->app_version, // 'bundle_id'=>$this->bundle_id, // ]; // $this->biz_content = json_encode($param); // $this->method = "alipay.open.mini.version.gray.cancel"; // $request = new AlipayOpenMiniVersionGrayCancelRequest(); // $result = $this->miniCommon($request); // if(!empty($result->code)&&$result->code == 10000){ // return [ // 'code'=>0, // 'msg'=>"成功", // 'data'=>$result // ]; // }else{ // throw new \Exception($result->sub_msg); // } // }catch (\Exception $e){ // return [ // 'code'=>1, // 'msg'=>$e->getMessage() // ]; // } // } /** * 小程序上架 * alipay.open.mini.version.online(小程序上架) */ public function miniOnline() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $mini = StoreAliMini::find()->where(['store_id' => $this->bind_store_id])->select('id,aes_key,auth_app_id,userid,license_name')->orderBy('id desc')->one(); $mini_id = $mini->id; } else { $mini_id = $this->mini_id; $mini = StoreAliMini::findOne($mini_id); } $MiniVersion = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy("create_time desc")->one(); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'app_version' => $MiniVersion->template_version ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.version.online"; $request = new AlipayOpenMiniVersionOnlineRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $MiniVersion->is_submit = 4; $res = $MiniVersion->save(); if ($res) { $t->commit(); return [ 'code' => 0, 'msg' => '成功', 'data' => $result ]; } else { throw new \Exception("保存数据错误"); } } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 小程序生成推广二维码接口 * alipay.open.app.qrcode.create(小程序生成推广二维码接口) */ public function miniQrcodeCreate() { $t = \Yii::$app->db->beginTransaction(); try { $mini = StoreAliMini::findOne($this->mini_id); if (empty($mini)) { throw new \Exception("没有进行授权"); } $param = [ 'url_param' => "pages/home/home", 'query_param' => "mini_id=" . $this->mini_id, 'describe' => "小程序开发上线使用" ]; $this->biz_content = json_encode($param); $this->method = "alipay.open.app.qrcode.create"; $request = new AlipayOpenAppQrcodeCreateRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 图片转base64 */ public function image2Base64($file) { if ($fp = fopen($file, "rb", 0)) { $gambar = fread($fp, filesize($file)); fclose($fp); $base64 = chunk_split(base64_encode($gambar)); return $base64; } else { return false; } } /** * 获取列表 */ public function getMiniList() { $query = StoreAliMini::find()->where(['store_id' => get_store_id()]) ->select("id,userid,auth_app_id,license_name,license_no,app_logo,app_name,category_name,mini_categoryIds,apply_phone_msg,is_get_phone,apply_phone_fail"); if ($this->AppName) { $query->andWhere(['LIKE', 'app_name', $this->AppName]); } $query->andWhere(['is_cancel' => 0]); $pagination = pagination_make($query); $admins = $pagination['list']; foreach ($admins as &$admin) { $version = StoreAliMiniVersion::find()->where(['mini_id' => $admin['id']])->orderBy('create_time desc')->asArray()->one(); \Yii::error($version); $admin['is_submit'] = !empty($version['is_submit']) ? $version['is_submit'] * 1 : 0; if (empty($version)) { $admin['is_submit'] = -1; if ($admin['order_cancel'] == 1) { $admin['is_submit'] = -2; } } $admin['apply_msg'] = []; if (!empty($admin['apply_phone_msg'])) { $admin['apply_msg'] = json_decode($admin['apply_phone_msg']); } $admin['is_get_phone'] = empty($this->getField($admin['id'])['data']) ? 2 : $admin['is_get_phone']; $admin['is_experience'] = !empty($version['is_experience']) ? $version['is_experience'] : 0; $admin['experience_url'] = !empty($version['experience_url']) ? $version['experience_url'] : ''; $admin['mini_categoryIds'] = !empty($admin['mini_categoryIds']) ? explode(';', $admin['mini_categoryIds']) : []; } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $admins, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ]; } /** * 获取版本列表 */ // public function getMiniVersionList(){ // $query = StoreAliMiniVersion::find()->where(['mini_id'=>$this->mini_id])->select("id,template_version,create_time,is_up,is_experience,experience_url,is_submit,is_put"); // $pagination = pagination_make($query); // $admins = $pagination['list']; // return [ // 'code'=>0, // 'msg'=>"获取成功", // 'data'=>$admins, // 'pageNo' => $pagination['pageNo'], // 'totalCount' => $pagination['totalCount'], // ]; // } /** * 获取修改信息 */ public function getMiniInfo() { $query = StoreAliMini::find()->where(['id' => $this->mini_id])->select("id,app_name,app_english_name,app_slogan,app_logo,app_desc,service_phone,service_email,license_no,license_pic,license_name,license_date,is_long_effective,category_name,mini_categoryIds"); $mini_info = $query->asArray()->one(); $version_info = StoreAliMiniVersion::find()->where(['mini_id' => $this->mini_id])->orderBy('create_time desc')->select('template_version,is_submit')->one(); $mini_info['version'] = $version_info->template_version; $mini_info['is_submit'] = $version_info->is_submit; $mini_info['mini_category_ids'] = !empty($mini_info['mini_categoryIds']) ? explode(';', $mini_info['mini_categoryIds']) : []; return [ 'code' => 0, 'msg' => "获取成功", 'data' => $mini_info ]; } /** * 单一版本信息 */ // public function getVersionInfo(){ // $mini = StoreAliMiniVersion::find()->where(['id'=>$this->version_id])->select('id,create_time,template_version as version,is_up,is_experience,experience_url,is_submit,is_put')->asArray()->one(); // $mini['create_time'] = date("Y-m-d H:i:s",$mini['create_time']); // return [ // 'code'=>0, // 'msg'=>"获取成功", // 'data'=>$mini // ]; // } /** * isv服务商代商户创建小程序 */ public function isvCreateMini() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id')->orderBy('id desc')->one(); $mini_id = $mini->id; } else { $mini_id = $this->mini_id; $store_id = get_store_id(); } if (!empty($mini_id)) { $ali_mini = StoreAliMini::find()->where(['id' => $mini_id])->one(); } else { //$ali_mini = StoreAliMini::find()->where(['license_no'=>$this->license_no])->one(); //if(!empty($ali_mini)){ //throw new \Exception("营业执照信息已经存在"); // } $ali_mini = new StoreAliMini(); } $new_mini = StoreAliMini::find()->where(['store_id' => $store_id])->orderBy('id desc')->one(); $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $new_mini->id + 1); $param = [ 'create_mini_request' => [ 'out_order_no' => $out_order_no, 'alipay_account' => $this->alipay_account, 'legal_personal_name' => $this->legal_personal_name, 'cert_name' => $this->license_name, 'cert_no' => $this->license_no, 'app_name' => $this->AppName, 'contact_phone' => $this->ServicePhone, 'contact_name' => $this->contact_name, 'is_individual' => true ] ]; $ali_mini->store_id = $store_id; $ali_mini->license_name = $this->license_name; $ali_mini->order_no = $out_order_no; $ali_mini->license_no = $this->license_no; $ali_mini->app_name = $this->AppName; $ali_mini->service_phone = $this->ServicePhone; $ali_mini->license_pic = $this->license_pic; $ali_mini->alipay_account = $this->alipay_account; $ali_mini->legal_personal_name = $this->legal_personal_name; if (!empty($this->merchant_id)) { $ali_mini->merchant_id = $this->merchant_id; } $ali_mini->contact_name = $this->contact_name; $res = $ali_mini->save(); if ($res) { $this->biz_content = json_encode($param); $this->method = "alipay.open.mini.isv.create"; $request = new AlipayOpenMiniIsvCreateRequest(); $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result, 'id' => $ali_mini->id ]; } throw new \Exception($result->sub_msg); } else { throw new \Exception("插入数据失败"); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile() ]; } } //创建事务 public function agentCreate($id = 0) { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_status,batch_no,alipay_account,contact_name,service_phone,service_email,')->orderBy('id desc')->one(); $id = $mini->id; } else { $id = !empty($id) ? $id : $this->mini_id; $mini = StoreAliMini::findOne($id); } \Yii::error(json_encode($mini)); if (empty($mini)) { throw new \Exception("查询不到任何信息"); } $data = [ 'account' => $mini->alipay_account, 'contact_info' => [ 'contact_name' => $mini->contact_name, 'contact_mobile' => $mini->service_phone, 'contact_email' => $mini->service_email ] ]; $this->method = "alipay.open.agent.create"; $this->biz_content = json_encode($data); $request = new AlipayOpenAgentCreateRequest(); $batch_no = $mini->batch_no; $res = [ 'code' => 0, 'msg' => "执行成功" ]; if ($mini->batch_status == 0 || $mini->batch_status == 1) { $result = $this->miniCommon($request); if (!empty($result->code) && $result->code == 10000) { $mini->batch_no = $result->batch_no; $mini->batch_status = 1; $res = $mini->save(); if (!$res) { throw new \Exception("保存信息失败"); } $batch_no = $result->batch_no; } } if ($mini->batch_status === 1 || $mini->batch_status === 4) { $res = $this->faceToFace($id, $batch_no); \Yii::error("当面付"); \Yii::error($res); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } if ($mini->batch_status === 2) { $res = $this->agentConfirm($id, $batch_no); \Yii::error("事务提交"); \Yii::error($res); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } if ($mini->batch_status === 3) { $res = $this->getBatchStatus($id, $batch_no); \Yii::error("查询状态"); \Yii::error($res); if ($res['code'] !== 0) { throw new \Exception($res['msg']); } } $t->commit(); return $res; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //当面付签约 public function faceToFace($id = 0, $batch_no = '') { try { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,mini_categoryIds,app_logo')->orderBy('id desc')->one(); } else { $mini_id = !empty($id) ? $id : $this->mini_id; $mini = StoreAliMini::findOne($mini_id); } if (empty($mini)) { throw new \Exception("查询不到任何信息"); } $this->method = "alipay.open.agent.facetoface.sign"; $request = new AlipayOpenAgentFacetofaceSignRequest(); $request->setBatchNo($batch_no); $request->setMccCode("A0002_B0201"); $request->setRate('0.38'); $request->setSignAndAuth(false); $request->setShopSignBoardPic('@' . $this->saveTempImage($mini->app_logo)); \Yii::error($id); $result = $this->miniCommon($request, 1, $mini_id, 1); \Yii::error($result); if ((!empty($result->code) && $result->code == 10000) || $result->sub_code == "ANT_PRODUCT_CONFLICT") { $mini->batch_status = 2; if ($result->sub_code == "ANT_PRODUCT_CONFLICT") { $mini->batch_status = 6; } $res = $mini->save(); if ($res) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception("添加数据失败"); } } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } //提交事务 public function agentConfirm($id = 0, $batch_no = '') { try { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no')->orderBy('id desc')->one(); } else { $mini_id = !empty($id) ? $id : $this->mini_id; $mini = StoreAliMini::findOne($mini_id); } if (empty($mini)) { throw new \Exception("查询不到任何信息"); } $data = [ 'batch_no' => $batch_no ]; $this->method = "alipay.open.agent.confirm"; $this->biz_content = json_encode($data); $request = new AlipayOpenAgentConfirmRequest(); $result = $this->miniCommon($request, 0, $id, 1); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { $mini->batch_status = 3; $res = $mini->save(); if ($res) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception("添加数据失败"); } } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //查询状态 public function getBatchStatus($id = 0, $batch = '') { try { \Yii::error("查询状态"); if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; \Yii::error($store_id); $mini = StoreAliMini::find()->where(['store_id' => $store_id])->select('id,batch_no,alipay_account')->orderBy('id desc')->one(); } else { $mini_id = !empty($id) ? $id : $this->mini_id; \Yii::error($mini_id); $mini = StoreAliMini::findOne($mini_id); } \Yii::error($mini); if (empty($mini)) { throw new \Exception("查询不到任何信息"); } $data = [ 'batch_no' => $batch ]; $this->method = "alipay.open.agent.order.query"; $this->biz_content = json_encode($data); $request = new AlipayOpenAgentOrderQueryRequest(); $result = $this->miniCommon($request, 0, $id, 1); \Yii::error("查询状态查询状态查询状态查询状态"); \Yii::error(json_encode($result)); if (!empty($result->code) && $result->code == 10000) { $status = $result->order_status; switch ($status) { case "MERCHANT_INFO_HOLD": //异常单 4 $mini->batch_status = 4; break; case "MERCHANT_AUDITING": //审核中 3 $mini->batch_status = 3; break; case "MERCHANT_CONFIRM": //待确认 5 $mini->batch_status = 5; break; case "MERCHANT_CONFIRM_SUCCESS": //商户确认成功 6 $mini->batch_status = 6; break; case "MERCHANT_CONFIRM_TIME_OUT": //商户超时未确认 7 $mini->batch_status = 7; break; case "MERCHANT_APPLY_ORDER_CANCELED": //审核失败或商户拒绝 8 $mini->batch_status = 8; break; } $res = $mini->save(); if ($res) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception("添加数据失败"); } } throw new \Exception($result->sub_msg); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //关联普通二维码 public function bindQrcode($id) { $t = \Yii::$app->db->beginTransaction(); try { $qrcode = AggregateQrcode::find()->where(['ali_mini_id' => $id])->one(); if (empty($qrcode->param_url) || empty($qrcode->ali_url)) { throw new \Exception("参数错误"); } $data = [ "route_url" => $qrcode->param_url, 'mode' => "EXACT", "page_redirection" => $qrcode->ali_url ]; \Yii::error($data); $this->method = "alipay.open.mini.qrcode.bind"; $this->biz_content = json_encode($data); $request = new AlipayOpenMiniQrcodeBindRequest(); $result = $this->miniCommon($request, 0, $id); if (!empty($result->code) && $result->code == 10000) { $qrcode->ali_status = 1; $res = $qrcode->save(); if (!$res) { throw new \Exception('保存数据失败,绑定二维码成功'); } $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //支付 public function payconfig($id, $out_trade_no, $user_id, $pay_price) { $t = \Yii::$app->db->beginTransaction(); try { $data = [ "out_trade_no" => $out_trade_no, 'total_amount' => $pay_price, "subject" => "当面付", "buyer_id" => $user_id ]; $this->method = "alipay.trade.create"; $this->biz_content = json_encode($data); $request = new AlipayTradeCreateRequest(); $request->setNotifyUrl(pay_notify_url(self::$notify_url)); $result = $this->miniCommon($request, 0, $id, 0, 1); if (!empty($result->code) && $result->code == 10000) { $t->commit(); $data = [ 'trade_no' => $result->trade_no, 'order_no' => $out_trade_no ]; return [ 'code' => 0, 'msg' => "成功", 'data' => $data ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取敏感字段 public function getField($id = 0) { try { $mini_id = !empty($id) ? $id : $this->mini_id; \Yii::error($mini_id); $mini = StoreAliMini::findOne($mini_id); $this->method = "alipay.open.app.api.query"; $request = new AlipayOpenAppApiQueryRequest(); $result = $this->miniCommon($request, 1, $mini->id); \Yii::error("获取敏感字段"); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { if (!empty($result->apis)) { $scene = $this->getScene($mini->id); if ($scene['code'] === 0) { return [ 'code' => 0, 'msg' => "成功", 'data' => $scene['data'] ]; } throw new \Exception($scene['msg']); } return [ 'code' => 0, 'msg' => "成功", 'data' => [] ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取敏感字段使用场景 public function getScene($id) { try { if (empty($id)) { throw new \Exception("未知的小程序信息"); } $data = [ 'field_name' => 'mobile', 'api_name' => 'getPhoneNumber' ]; $this->method = "alipay.open.app.api.scene.query"; $this->biz_content = json_encode($data); $request = new AlipayOpenAppApiSceneQueryRequest(); $result = $this->miniCommon($request, 0, $id); \Yii::error("获取敏感字段场景值"); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { return [ 'code' => 0, 'msg' => "成功", 'data' => $result->auth_field_scene ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //申请接入敏感字段 public function applyPhone() { try { $apply_msg = $this->apply_msg; $mini_id = $apply_msg['id']; if (empty($mini_id)) { throw new \Exception("获取小程序信息失败"); } \Yii::error($mini_id); $mini = StoreAliMini::findOne($mini_id); $this->method = "alipay.open.app.api.field.apply"; $request = new AlipayOpenAppApiFieldApplyRequest(); !empty($apply_msg['video']) && $request->setVideo('@' . $this->saveTempImage($apply_msg['video'])); foreach ($apply_msg['picture'] as $index => $item) { \Yii::error($item); \Yii::error($this->saveTempImage($item)); switch ($index) { case 0: $request->setPicture1('@' . $this->saveTempImage($item)); break; case 1: $request->setPicture2('@' . $this->saveTempImage($item)); break; case 2: $request->setPicture3('@' . $this->saveTempImage($item)); break; case 3: $request->setPicture4('@' . $this->saveTempImage($item)); break; case 4: $request->setPicture5('@' . $this->saveTempImage($item)); break; } } $data = [ 'api_name' => "getPhoneNumber", 'field_name' => "mobile", "package_code" => "20180927110154092444", 'scene_code' => $apply_msg['scene_code'], 'qps_answer' => $apply_msg['qps_answer'], 'customer_answer' => $apply_msg['customer_answer'], 'memo' => $apply_msg['memo'] ]; $request->setAuthFieldApply(json_encode($data)); $result = $this->miniCommon($request, 1, $apply_msg['id']); \Yii::error($result); $mini->apply_phone_msg = json_encode($apply_msg); $mini->save(); if (!empty($result->code) && $result->code == 10000) { $mini->is_get_phone = 1; $mini->save(); return [ 'code' => 0, 'msg' => "开始审核", 'data' => $result ]; } else { throw new \Exception($result->sub_msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //手动查询手机号申请状态 public function getPhoneStatus($id) { try { $mini_id = $this->mini_id; $mini = StoreAliMini::find()->where(['id' => $mini_id])->select("id,is_get_phone,apply_phone_fail,auth_app_id")->one(); if (empty($mini)) { throw new \Exception("获取小程序信息失败"); } $this->method = "alipay.open.app.api.field.query"; $request = new AlipayOpenAppApiFieldQueryRequest(); $result = $this->miniCommon($request, 1, $mini_id); \Yii::error("获取敏感字段申请状态"); \Yii::error($result); if (!empty($result->code) && $result->code == 10000) { $data = $result->auth_field_response->records; $data_arr = json_decode(json_encode($data), true); \Yii::error($data_arr); $status = $data_arr[0]['status']; if ($status === "AGREE") { $mini->is_get_phone = 2; } elseif ($status === "REJECT" || $status === "INVALID") { $reason = !empty($data->reason) ? $data->reason : ''; $mini->is_get_phone = 3; $mini->apply_phone_fail = $reason; } if ($mini->save()) { return [ 'code' => 0, 'msg' => "成功", 'data' => $data, 'status' => $status ]; } throw new \Exception("保存数据错误" . json_encode($mini->errors)); } else { throw new \Exception($result->sub_msg); } } 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; } public function getIsvCreateMini() { $mini = StoreAliMini::find()->where(['id' => $this->mini_id])->select("id,license_name,,license_no,app_name,license_pic,service_phone,alipay_account,legal_personal_name,contact_name")->asArray()->one(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $mini ]; } // 获取小程序审核状态 public function getMiniShenheStatus($id){ try { $store_ali_mini = StoreAliMini::findOne($id); if (!$store_ali_mini || !$store_ali_mini->order_no) { return false; } $data = [ "order_no" => $store_ali_mini->order_no, ]; $this->method = "alipay.open.mini.isv.query"; $this->biz_content = json_encode($data); $request = new AlipayOpenMiniIsvQueryRequest(); $result = $this->miniCommon($request,0,$id); if(!empty($result->code)&&$result->code == 10000){ return $result; }else{ throw new \Exception($result->sub_msg); } }catch (\Exception $e){ return [ 'code'=> 2, 'msg'=>$e->getMessage() ]; } } }