| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\third\ali;
- use app\models\AggregateQrcode;
- use app\models\Option;
- use app\models\Salesman;
- use app\models\StoreAliMini;
- use app\models\StoreAliMiniVersion;
- use app\models\StoreMiniToken;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesSetRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvCreateRequest;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvQueryRequest;
- use app\utils\QrCode;
- use yii\helpers\Json;
- class RegisterMiniForm extends BaseForm
- {
- /**
- * 注册小程序form
- */
- public $param_info;
- public $status;
- public function rules()
- {
- return [
- [['status'], 'integer']
- ];
- }
- public function inits()
- {
- try {
- $mini_id = $this->mini_id;
- $store_id = $this->store_id;
- if (!empty($mini_id)) {
- $store_mini = StoreAliMini::find()->where(['OR', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
- if (empty($store_mini)) {
- return [
- 'code' => 1,
- 'msg' => "查找不到小程序信息"
- ];
- }
- $this->store_mini = $store_mini;
- $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
- }
- //获取二维码信息
- $qrcode_id = $this->qrcode_id;
- if (!empty($qrcode_id)) {
- $qrcode = AggregateQrcode::findOne($qrcode_id);
- if (empty($qrcode)) {
- return [
- 'code' => 1,
- 'msg' => "二维码不存在"
- ];
- }
- if ($qrcode->ali_status == 1) {
- return [
- 'code' => 1,
- 'msg' => "二维码已经被使用"
- ];
- }
- $this->qrcode = $qrcode;
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //快速创建小程序 alipay.open.mini.isv.create(isv服务商代商户创建小程序) 获取order_no 依然需要使用被动模式获取auth_code、auth_token、userid、refresh_token
- public function fastRegisterMini()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取商城ID
- $store_id = $this->store_id;
- //获取小程序model
- $store_mini = $this->store_mini ?: new StoreAliMini();
- //获取请求数据
- $param_info = $this->param_info;
- //获取最大的id
- $mini_id = StoreAliMini::find()->max('id');
- //生成订单号
- $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $mini_id + 1);
-
- //设置请求支付宝数据
- $param = json_encode([
- 'create_mini_request' => [
- //订单号
- 'out_order_no' => $out_order_no,
- //支付宝账户
- 'alipay_account' => $param_info['alipay_account'],
- //法人姓名
- 'legal_personal_name' => $param_info['legal_personal_name'],
- //公司企业名称
- 'cert_name' => $param_info['license_name'],
- //企业编码
- 'cert_no' => $param_info['license_no'],
- //小程序名称
- 'app_name' => $param_info['app_name'],
- //商家手机号
- 'contact_phone' => $param_info['service_phone'],
- //商家名称
- 'contact_name' => $param_info['contact_name'],
- 'is_individual' => true
- ]
- ]);
- //保存商城ID
- $store_mini->store_id = $store_id;
- //保存企业名称
- $store_mini->license_name = $param_info['license_name'];
- //保存企业编码
- $store_mini->license_no = $param_info['license_no'];
- //小程序名称
- $store_mini->app_name = $param_info['app_name'];
- //联系电话
- $store_mini->service_phone = $param_info['service_phone'];
- //营业执照
- $store_mini->license_pic = $param_info['license_pic'];
- //支付宝账户
- $store_mini->alipay_account = $param_info['alipay_account'];
- //法人姓名
- $store_mini->legal_personal_name = $param_info['legal_personal_name'];
- //商户姓名
- $store_mini->contact_name = $param_info['contact_name'];
-
- if (!empty($param_info['license_date'])) {
- //判断营业执照是否为长期
- $is_long_effective = (int)(strtotime($param_info['license_date']) >= strtotime("2099-12-31"));
- //营业执照有效期
- $store_mini->license_date = $param_info['license_date'];
- //
- $store_mini->is_long_effective = $is_long_effective;
- }
- //请求接口
- $request = new AlipayOpenMiniIsvCreateRequest();
- $result = $this->miniCommon($request, $param, 1);
- if ($result->code == 10000) {
- //保存订单号
- $store_mini->order_no = $result->order_no;
- $store_mini->order_cancel = 0;
- if (!$store_mini->save()) {
- throw new \Exception(json_encode($store_mini->errors));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result,
- 'id' => $store_mini->id
- ];
- } elseif ($result->sub_msg === "非法请求参数,【检查结果不是个体工商户】") {
- $form = new PerfectMiniForm();
- $form->store_id = $this->store_id;
- $result = $form->updateBussiness($param_info['license_pic'], $param_info['license_no']);
- $t->commit();
- if ($result['code'] === 0) {
- $res = self::fastRegisterMini();
- if ($res['code'] === 0) {
- return $res;
- }
- }
- if ($result['code'] === 2) {
- return [
- 'code' => 0,
- 'msg' => $result['msg'],
- 'data' => [
- 'status' => 1
- ],
- ];
- }
- throw new \Exception($result['msg']);
- }
- throw new \Exception($result->sub_msg);
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //查询快速注册状态 alipay.open.mini.isv.query(isv查询代商家创建小程序记录) 根据order_no获取快速注册状态 获取appid
- public function getFastRegisterStatus()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取小程序model
- $store_mini = $this->store_mini;
- //判断订单号是否为空
- if (empty($store_mini->order_no)) {
- throw new \Exception("订单不存在");
- }
- $param = json_encode([
- "order_no" => $store_mini->order_no,
- ]);
- //请求配置信息
- $request = new AlipayOpenMiniIsvQueryRequest();
- $result = $this->miniCommon($request, $param, 1);
- //如果请求成功
- if ($result->code == 10000) {
- //判断请求状态
- switch ($result->status) {
- case "TIMEOUT":
- $store_mini->order_cancel = 1;
- $msg = "已超时";
- break;
- case "AGREED":
- $msg = "已同意";
- break;
- case "REJECTED":
- $store_mini->order_cancel = 1;
- $msg = "已拒绝";
- break;
- default:
- $msg = "审核中";
- break;
- }
- //如果商家已经确认时会返回小程序ID
- if (!empty($result->min_app_id)) {
- $store_mini->auth_app_id = $result->min_app_id;
- }
- //保存小程序ID
- if (!$store_mini->save()) {
- throw new \Exception(json_encode($store_mini->errors));
- }
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'data' => $result
- ];
- }else{
- //请求失败
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取aes密钥用于登录以及支付 alipay.open.auth.app.aes.set(授权应用aes密钥设置) 根据appid获取aes_key
- public function getAesKey()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取小程序model
- $store_mini = $this->store_mini;
- //商城ID
- $store_id = $this->store_id;
- //请求数据
- $param = json_encode([
- "merchant_app_id" => $store_mini->auth_app_id
- ]);
- //请求配置信息
- $request = new AlipayOpenAuthAppAesSetRequest();
- $result = $this->miniCommon($request, $param, 1);
- //如果请求成功
- if ($result->code == 10000) {
- //如果小程序信息为空 则更新模板aes_key
- if (empty($store_mini)) {
- Option::set('ali_template_aes', $result->aes_key, 0, 'saas');
- } else {
- //如果不为空 则更新小程序aes_key,保存信息
- $store_mini->aes_key = $result->aes_key;
- if (!$store_mini->save()) {
- throw new \Exception(json_encode($store_mini->errors));
- }
- //保存后将信息放入配置信息里,以便于以后支付使用
- $data = [];
- $data['aes_key'] = $store_mini->aes_key;
- $data['app_id'] = $store_mini->auth_app_id;
- $data['alipay_public_key'] = "";
- $data['app_public_key'] = "";
- $data['app_private_key'] = "";
- $data['name'] = $store_mini->license_name;
- $data['user_id'] = $store_mini->userid;
- $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $store_id, 'alipay');
- //记录到缓存信息
- if ($res) {
- \Yii::$app->cache->delete('alipay_config_cache_' . $store_id);
- \Yii::$app->cache->set('alipay_config_cache_' . $store_id, Json::encode($data));
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $result->aes_key
- ];
- } else {
- //请求到接口失败
- throw new \Exception($result->sub_msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 传递授权回调参数
- * @return array
- */
- public function getParams()
- {
- $store_id = $this->store_id;
- $status = $this->status;
- $data = [
- 'appid' => $this->base_info['appid'],
- 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/index.php/alipay/token'),
- 'state' => $store_id
- ];
- $saas_user_id = get_saas_user_id();
- $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
- $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->status = 1;
- $token_log->salesman_id = $salesman ? $salesman->id : 0;
- $token_log->save();
- // if ((int)$status === 1) {
- // $token_log->status = 1;
- // } else {
- // $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;
- // }
- $pic_url = \Yii::$app->request->hostInfo . '/web/v1/statics/images/ali_pay_apply.png';
- return [
- 'code' => 0,
- 'url' => $pic_url,
- 'msg' => "获取成功"
- ];
- }
- }
|