RegisterMiniForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\third\ali;
  8. use app\models\AggregateQrcode;
  9. use app\models\Option;
  10. use app\models\Salesman;
  11. use app\models\StoreAliMini;
  12. use app\models\StoreAliMiniVersion;
  13. use app\models\StoreMiniToken;
  14. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenAuthAppAesSetRequest;
  15. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvCreateRequest;
  16. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniIsvQueryRequest;
  17. use app\utils\QrCode;
  18. use yii\helpers\Json;
  19. class RegisterMiniForm extends BaseForm
  20. {
  21. /**
  22. * 注册小程序form
  23. */
  24. public $param_info;
  25. public $status;
  26. public function rules()
  27. {
  28. return [
  29. [['status'], 'integer']
  30. ];
  31. }
  32. public function inits()
  33. {
  34. try {
  35. $mini_id = $this->mini_id;
  36. $store_id = $this->store_id;
  37. if (!empty($mini_id)) {
  38. $store_mini = StoreAliMini::find()->where(['OR', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
  39. if (empty($store_mini)) {
  40. return [
  41. 'code' => 1,
  42. 'msg' => "查找不到小程序信息"
  43. ];
  44. }
  45. $this->store_mini = $store_mini;
  46. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  47. }
  48. //获取二维码信息
  49. $qrcode_id = $this->qrcode_id;
  50. if (!empty($qrcode_id)) {
  51. $qrcode = AggregateQrcode::findOne($qrcode_id);
  52. if (empty($qrcode)) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => "二维码不存在"
  56. ];
  57. }
  58. if ($qrcode->ali_status == 1) {
  59. return [
  60. 'code' => 1,
  61. 'msg' => "二维码已经被使用"
  62. ];
  63. }
  64. $this->qrcode = $qrcode;
  65. }
  66. } catch (\Exception $e) {
  67. return [
  68. 'code' => 1,
  69. 'msg' => $e->getMessage()
  70. ];
  71. }
  72. }
  73. //快速创建小程序 alipay.open.mini.isv.create(isv服务商代商户创建小程序) 获取order_no 依然需要使用被动模式获取auth_code、auth_token、userid、refresh_token
  74. public function fastRegisterMini()
  75. {
  76. $t = \Yii::$app->db->beginTransaction();
  77. try {
  78. $result = $this->inits();
  79. if ($result['code'] === 1) {
  80. return $result;
  81. }
  82. //获取商城ID
  83. $store_id = $this->store_id;
  84. //获取小程序model
  85. $store_mini = $this->store_mini ?: new StoreAliMini();
  86. //获取请求数据
  87. $param_info = $this->param_info;
  88. //获取最大的id
  89. $mini_id = StoreAliMini::find()->max('id');
  90. //生成订单号
  91. $out_order_no = date("YmdHis") . sprintf("%03d", $store_id) . sprintf("%03d", $mini_id + 1);
  92. //设置请求支付宝数据
  93. $param = json_encode([
  94. 'create_mini_request' => [
  95. //订单号
  96. 'out_order_no' => $out_order_no,
  97. //支付宝账户
  98. 'alipay_account' => $param_info['alipay_account'],
  99. //法人姓名
  100. 'legal_personal_name' => $param_info['legal_personal_name'],
  101. //公司企业名称
  102. 'cert_name' => $param_info['license_name'],
  103. //企业编码
  104. 'cert_no' => $param_info['license_no'],
  105. //小程序名称
  106. 'app_name' => $param_info['app_name'],
  107. //商家手机号
  108. 'contact_phone' => $param_info['service_phone'],
  109. //商家名称
  110. 'contact_name' => $param_info['contact_name'],
  111. 'is_individual' => true
  112. ]
  113. ]);
  114. //保存商城ID
  115. $store_mini->store_id = $store_id;
  116. //保存企业名称
  117. $store_mini->license_name = $param_info['license_name'];
  118. //保存企业编码
  119. $store_mini->license_no = $param_info['license_no'];
  120. //小程序名称
  121. $store_mini->app_name = $param_info['app_name'];
  122. //联系电话
  123. $store_mini->service_phone = $param_info['service_phone'];
  124. //营业执照
  125. $store_mini->license_pic = $param_info['license_pic'];
  126. //支付宝账户
  127. $store_mini->alipay_account = $param_info['alipay_account'];
  128. //法人姓名
  129. $store_mini->legal_personal_name = $param_info['legal_personal_name'];
  130. //商户姓名
  131. $store_mini->contact_name = $param_info['contact_name'];
  132. if (!empty($param_info['license_date'])) {
  133. //判断营业执照是否为长期
  134. $is_long_effective = (int)(strtotime($param_info['license_date']) >= strtotime("2099-12-31"));
  135. //营业执照有效期
  136. $store_mini->license_date = $param_info['license_date'];
  137. //
  138. $store_mini->is_long_effective = $is_long_effective;
  139. }
  140. //请求接口
  141. $request = new AlipayOpenMiniIsvCreateRequest();
  142. $result = $this->miniCommon($request, $param, 1);
  143. if ($result->code == 10000) {
  144. //保存订单号
  145. $store_mini->order_no = $result->order_no;
  146. $store_mini->order_cancel = 0;
  147. if (!$store_mini->save()) {
  148. throw new \Exception(json_encode($store_mini->errors));
  149. }
  150. $t->commit();
  151. return [
  152. 'code' => 0,
  153. 'msg' => "成功",
  154. 'data' => $result,
  155. 'id' => $store_mini->id
  156. ];
  157. } elseif ($result->sub_msg === "非法请求参数,【检查结果不是个体工商户】") {
  158. $form = new PerfectMiniForm();
  159. $form->store_id = $this->store_id;
  160. $result = $form->updateBussiness($param_info['license_pic'], $param_info['license_no']);
  161. $t->commit();
  162. if ($result['code'] === 0) {
  163. $res = self::fastRegisterMini();
  164. if ($res['code'] === 0) {
  165. return $res;
  166. }
  167. }
  168. if ($result['code'] === 2) {
  169. return [
  170. 'code' => 0,
  171. 'msg' => $result['msg'],
  172. 'data' => [
  173. 'status' => 1
  174. ],
  175. ];
  176. }
  177. throw new \Exception($result['msg']);
  178. }
  179. throw new \Exception($result->sub_msg);
  180. } catch (\Exception $e) {
  181. $t->rollBack();
  182. return [
  183. 'code' => 1,
  184. 'msg' => $e->getMessage()
  185. ];
  186. }
  187. }
  188. //查询快速注册状态 alipay.open.mini.isv.query(isv查询代商家创建小程序记录) 根据order_no获取快速注册状态 获取appid
  189. public function getFastRegisterStatus()
  190. {
  191. try {
  192. $result = $this->inits();
  193. if ($result['code'] === 1) {
  194. return $result;
  195. }
  196. //获取小程序model
  197. $store_mini = $this->store_mini;
  198. //判断订单号是否为空
  199. if (empty($store_mini->order_no)) {
  200. throw new \Exception("订单不存在");
  201. }
  202. $param = json_encode([
  203. "order_no" => $store_mini->order_no,
  204. ]);
  205. //请求配置信息
  206. $request = new AlipayOpenMiniIsvQueryRequest();
  207. $result = $this->miniCommon($request, $param, 1);
  208. //如果请求成功
  209. if ($result->code == 10000) {
  210. //判断请求状态
  211. switch ($result->status) {
  212. case "TIMEOUT":
  213. $store_mini->order_cancel = 1;
  214. $msg = "已超时";
  215. break;
  216. case "AGREED":
  217. $msg = "已同意";
  218. break;
  219. case "REJECTED":
  220. $store_mini->order_cancel = 1;
  221. $msg = "已拒绝";
  222. break;
  223. default:
  224. $msg = "审核中";
  225. break;
  226. }
  227. //如果商家已经确认时会返回小程序ID
  228. if (!empty($result->min_app_id)) {
  229. $store_mini->auth_app_id = $result->min_app_id;
  230. }
  231. //保存小程序ID
  232. if (!$store_mini->save()) {
  233. throw new \Exception(json_encode($store_mini->errors));
  234. }
  235. return [
  236. 'code' => 0,
  237. 'msg' => $msg,
  238. 'data' => $result
  239. ];
  240. }else{
  241. //请求失败
  242. throw new \Exception($result->sub_msg);
  243. }
  244. } catch (\Exception $e) {
  245. return [
  246. 'code' => 1,
  247. 'msg' => $e->getMessage()
  248. ];
  249. }
  250. }
  251. //获取aes密钥用于登录以及支付 alipay.open.auth.app.aes.set(授权应用aes密钥设置) 根据appid获取aes_key
  252. public function getAesKey()
  253. {
  254. try {
  255. $result = $this->inits();
  256. if ($result['code'] === 1) {
  257. return $result;
  258. }
  259. //获取小程序model
  260. $store_mini = $this->store_mini;
  261. //商城ID
  262. $store_id = $this->store_id;
  263. //请求数据
  264. $param = json_encode([
  265. "merchant_app_id" => $store_mini->auth_app_id
  266. ]);
  267. //请求配置信息
  268. $request = new AlipayOpenAuthAppAesSetRequest();
  269. $result = $this->miniCommon($request, $param, 1);
  270. //如果请求成功
  271. if ($result->code == 10000) {
  272. //如果小程序信息为空 则更新模板aes_key
  273. if (empty($store_mini)) {
  274. Option::set('ali_template_aes', $result->aes_key, 0, 'saas');
  275. } else {
  276. //如果不为空 则更新小程序aes_key,保存信息
  277. $store_mini->aes_key = $result->aes_key;
  278. if (!$store_mini->save()) {
  279. throw new \Exception(json_encode($store_mini->errors));
  280. }
  281. //保存后将信息放入配置信息里,以便于以后支付使用
  282. $data = [];
  283. $data['aes_key'] = $store_mini->aes_key;
  284. $data['app_id'] = $store_mini->auth_app_id;
  285. $data['alipay_public_key'] = "";
  286. $data['app_public_key'] = "";
  287. $data['app_private_key'] = "";
  288. $data['name'] = $store_mini->license_name;
  289. $data['user_id'] = $store_mini->userid;
  290. $res = Option::set(Option::OPTOPN_KEY, Json::encode($data), $store_id, 'alipay');
  291. //记录到缓存信息
  292. if ($res) {
  293. \Yii::$app->cache->delete('alipay_config_cache_' . $store_id);
  294. \Yii::$app->cache->set('alipay_config_cache_' . $store_id, Json::encode($data));
  295. }
  296. }
  297. return [
  298. 'code' => 0,
  299. 'msg' => "获取成功",
  300. 'data' => $result->aes_key
  301. ];
  302. } else {
  303. //请求到接口失败
  304. throw new \Exception($result->sub_msg);
  305. }
  306. } catch (\Exception $e) {
  307. return [
  308. 'code' => 1,
  309. 'msg' => $e->getMessage()
  310. ];
  311. }
  312. }
  313. /**
  314. * 传递授权回调参数
  315. * @return array
  316. */
  317. public function getParams()
  318. {
  319. $store_id = $this->store_id;
  320. $status = $this->status;
  321. $data = [
  322. 'appid' => $this->base_info['appid'],
  323. 'redirect_uri' => urlencode(\Yii::$app->request->hostInfo . '/index.php/alipay/token'),
  324. 'state' => $store_id
  325. ];
  326. $saas_user_id = get_saas_user_id();
  327. $salesman = Salesman::findOne(['saas_user_id' => $saas_user_id, 'is_delete' => 0]);
  328. $token_log = StoreMiniToken::findOne(['store_id' => $store_id]) ?: new StoreMiniToken();
  329. $token_log->store_id = $store_id;
  330. $token_log->created_at = time();
  331. $token_log->type = 1;
  332. $token_log->status = 1;
  333. $token_log->salesman_id = $salesman ? $salesman->id : 0;
  334. $token_log->save();
  335. // if ((int)$status === 1) {
  336. // $token_log->status = 1;
  337. // } else {
  338. // $url = "https://openauth.alipay.com/oauth2/appToAppBatchAuth.htm?app_id=" . $data['appid'] . "&application_type=WEBAPP&redirect_uri=" . $data['redirect_uri'] . '&state=' . $data['state'];
  339. // $filename = md5(date('Ym') . 'token' . $token_log->id);
  340. // $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  341. // $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  342. // QrCode::image($url, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  343. // $token_log->url = $pic_url;
  344. // }
  345. $pic_url = \Yii::$app->request->hostInfo . '/web/v1/statics/images/ali_pay_apply.png';
  346. return [
  347. 'code' => 0,
  348. 'url' => $pic_url,
  349. 'msg' => "获取成功"
  350. ];
  351. }
  352. }