Merchant.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\WechatMerchant;
  8. use app\models\MerchantInfo;
  9. use app\models\Option;
  10. use app\utils\CurlHelper;
  11. use yii\base\Exception;
  12. use yii\helpers\Json;
  13. class Merchant
  14. {
  15. // 服务商小程序appid
  16. private $service_app_id;
  17. // 商户mchid
  18. private $mch_id;
  19. // 商户API v3密钥(微信服务商-账户中心-API安全 api v3密钥 https://pay.weixin.qq.com/index.php/core/cert/api_cert)
  20. private $mch_api_key;
  21. // 证书编号 (apiclient_cert.pem证书解析后获得)
  22. private $serial_no;
  23. // 私钥 apiclient_key.pem(微信服务商-账户中心-API安全 自行下载 https://pay.weixin.qq.com/index.php/core/cert/api_cert)
  24. private $mch_private_key = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'apiclient_key.pem';
  25. // 支付平台公钥(接口获取)
  26. private $public_key_path = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cert_ficates_v3.pem';
  27. // 微信支付公钥
  28. private $public_key;
  29. private $public_key_id;
  30. /**
  31. * @var string 上传图片接口地址
  32. */
  33. private $upload_image_url = 'https://api.mch.weixin.qq.com/v3/merchant/media/upload';
  34. /**
  35. * @var string 申请商户号接口
  36. */
  37. private $applyment_url = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/';
  38. /**
  39. * @var string 查询商户号申请详情接口
  40. */
  41. private $query_applyment_url = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/applyment_id/';
  42. private $query_applyment_url_code = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/business_code/';
  43. /**
  44. * 个体户
  45. */
  46. const SUBJECT_TYPE_INDIVIDUAL = 'SUBJECT_TYPE_INDIVIDUAL';
  47. /**
  48. * 企业
  49. */
  50. const SUBJECT_TYPE_ENTERPRISE = 'SUBJECT_TYPE_ENTERPRISE';
  51. /**
  52. * 党政机关事业单位
  53. */
  54. const SUBJECT_TYPE_INSTITUTIONS = 'SUBJECT_TYPE_INSTITUTIONS';
  55. /**
  56. * 其他组织
  57. */
  58. const SUBJECT_TYPE_OTHERS = 'SUBJECT_TYPE_OTHERS';
  59. /**
  60. * @var array 主体类型
  61. */
  62. private static $subject_type = [
  63. self::SUBJECT_TYPE_INDIVIDUAL,
  64. self::SUBJECT_TYPE_ENTERPRISE,
  65. self::SUBJECT_TYPE_INSTITUTIONS,
  66. self::SUBJECT_TYPE_OTHERS
  67. ];
  68. const CERTIFICATE_TYPE_238 = 'CERTIFICATE_TYPE_2388'; // 事业单位法人证书
  69. const CERTIFICATE_TYPE_2389 = 'CERTIFICATE_TYPE_2389'; // 统一社会信用代码证书
  70. const CERTIFICATE_TYPE_2390 = 'CERTIFICATE_TYPE_2390'; // 有偿服务许可证(军队医院适用)
  71. const CERTIFICATE_TYPE_2391 = 'CERTIFICATE_TYPE_2391'; // 医疗机构执业许可证(军队医院适用)
  72. const CERTIFICATE_TYPE_2392 = 'CERTIFICATE_TYPE_2392'; // 企业营业执照(挂靠企业的党组织适用)
  73. const CERTIFICATE_TYPE_2393 = 'CERTIFICATE_TYPE_2393'; // 组织机构代码证(政府机关适用)
  74. const CERTIFICATE_TYPE_2394 = 'CERTIFICATE_TYPE_2394'; // 社会团体法人登记证书
  75. const CERTIFICATE_TYPE_2395 = 'CERTIFICATE_TYPE_2395'; // 民办非企业单位登记证书
  76. const CERTIFICATE_TYPE_2396 = 'CERTIFICATE_TYPE_2396'; // 基金会法人登记证书
  77. const CERTIFICATE_TYPE_2397 = 'CERTIFICATE_TYPE_2397'; // 慈善组织公开募捐资格证书
  78. const CERTIFICATE_TYPE_2398 = 'CERTIFICATE_TYPE_2398'; // 农民专业合作社法人营业执照
  79. const CERTIFICATE_TYPE_2399 = 'CERTIFICATE_TYPE_2399'; // 宗教活动场所登记证
  80. const CERTIFICATE_TYPE_2400 = 'CERTIFICATE_TYPE_2400'; // 其他证书/批文/证明
  81. private static $certificate_type = [
  82. self::CERTIFICATE_TYPE_238,
  83. self::CERTIFICATE_TYPE_2389,
  84. self::CERTIFICATE_TYPE_2390,
  85. self::CERTIFICATE_TYPE_2391,
  86. self::CERTIFICATE_TYPE_2392,
  87. self::CERTIFICATE_TYPE_2393,
  88. self::CERTIFICATE_TYPE_2394,
  89. self::CERTIFICATE_TYPE_2395,
  90. self::CERTIFICATE_TYPE_2396,
  91. self::CERTIFICATE_TYPE_2397,
  92. self::CERTIFICATE_TYPE_2398,
  93. self::CERTIFICATE_TYPE_2399,
  94. self::CERTIFICATE_TYPE_2400
  95. ];
  96. const IDENTIFICATION_TYPE_IDCARD = 'IDENTIFICATION_TYPE_IDCARD'; // 中国大陆居民-身份证
  97. const IDENTIFICATION_TYPE_OVERSEA_PASSPORT = 'IDENTIFICATION_TYPE_OVERSEA_PASSPORT'; // 其他国家或地区居民-护照
  98. const IDENTIFICATION_TYPE_HONGKONG_PASSPORT = 'IDENTIFICATION_TYPE_HONGKONG_PASSPORT'; // 中国香港居民-来往内地通行证
  99. const IDENTIFICATION_TYPE_MACAO_PASSPORT = 'IDENTIFICATION_TYPE_MACAO_PASSPORT'; // 中国澳门居民-来往内地通行证
  100. const IDENTIFICATION_TYPE_TAIWAN_PASSPORT = 'IDENTIFICATION_TYPE_TAIWAN_PASSPORT'; // 中国台湾居民-来往大陆通行证
  101. private static $identify_type = [
  102. self::IDENTIFICATION_TYPE_IDCARD,
  103. self::IDENTIFICATION_TYPE_OVERSEA_PASSPORT,
  104. self::IDENTIFICATION_TYPE_HONGKONG_PASSPORT,
  105. self::IDENTIFICATION_TYPE_MACAO_PASSPORT,
  106. self::IDENTIFICATION_TYPE_TAIWAN_PASSPORT
  107. ];
  108. const BANK_ACCOUNT_TYPE_CORPORATE = 'BANK_ACCOUNT_TYPE_CORPORATE'; // 银行对公账户
  109. const BANK_ACCOUNT_TYPE_PERSONAL = 'BANK_ACCOUNT_TYPE_PERSONAL'; // 银行私人账户
  110. private static $bank_type = [
  111. self::BANK_ACCOUNT_TYPE_CORPORATE,
  112. self::BANK_ACCOUNT_TYPE_PERSONAL
  113. ];
  114. // 审核状态
  115. const APPLYMENT_STATE_EDITTING = 'APPLYMENT_STATE_EDITTING'; // 编辑中
  116. const APPLYMENT_STATE_AUDITING = 'APPLYMENT_STATE_AUDITING'; // 审核中
  117. const APPLYMENT_STATE_REJECTED = 'APPLYMENT_STATE_REJECTED'; // 已驳回
  118. const APPLYMENT_STATE_TO_BE_CONFIRMED = 'APPLYMENT_STATE_TO_BE_CONFIRMED'; // 待账户验证
  119. const APPLYMENT_STATE_TO_BE_SIGNED = 'APPLYMENT_STATE_TO_BE_SIGNED'; // 待签约
  120. const APPLYMENT_STATE_SIGNING = 'APPLYMENT_STATE_SIGNING'; // 开通权限中
  121. const APPLYMENT_STATE_FINISHED = 'APPLYMENT_STATE_FINISHED'; // 已完成
  122. const APPLYMENT_STATE_CANCELED = 'APPLYMENT_STATE_CANCELED'; // 已作废
  123. public static $valid_state_desc = [
  124. self::APPLYMENT_STATE_EDITTING => '编辑中',
  125. self::APPLYMENT_STATE_AUDITING => '审核中',
  126. self::APPLYMENT_STATE_REJECTED => '已驳回',
  127. self::APPLYMENT_STATE_TO_BE_CONFIRMED => '待账户验证',
  128. self::APPLYMENT_STATE_TO_BE_SIGNED => '待签约',
  129. self::APPLYMENT_STATE_SIGNING => '开通权限中',
  130. self::APPLYMENT_STATE_FINISHED => '已完成',
  131. self::APPLYMENT_STATE_CANCELED => '已作废',
  132. ];
  133. public static $valid_state_status = [
  134. self::APPLYMENT_STATE_EDITTING => 0,
  135. self::APPLYMENT_STATE_AUDITING => 1,
  136. self::APPLYMENT_STATE_REJECTED => 2,
  137. self::APPLYMENT_STATE_TO_BE_CONFIRMED => 3,
  138. self::APPLYMENT_STATE_TO_BE_SIGNED => 3,
  139. self::APPLYMENT_STATE_SIGNING => 3,
  140. self::APPLYMENT_STATE_FINISHED => 3,
  141. self::APPLYMENT_STATE_CANCELED => 2,
  142. ];
  143. /**
  144. * @var string 获取平台证书接口
  145. */
  146. private $certificates_url = 'https://api.mch.weixin.qq.com/v3/certificates';
  147. public $submitErrRollback = 0;
  148. public function __construct()
  149. {
  150. $keys = [
  151. 'platform_appid',
  152. 'platform_mch_id',
  153. 'platform_key',
  154. 'platform_apiclient_cert',
  155. 'platform_apiclient_key',
  156. 'platform_api_key',
  157. 'platform_public_key',
  158. 'platform_public_key_id',
  159. 'platform_serial_no',
  160. 'platform_mch_appid',
  161. 'sp_mch_id',
  162. 'sp_apiclient_key'
  163. ];
  164. $data = Option::get($keys, 0, 'saas');
  165. try {
  166. if (empty($data)) {
  167. throw new \Exception('配置信息有误0', 1);
  168. } else {
  169. $arr = [];
  170. foreach ($data as $value) {
  171. $index = array_search($value['name'], $keys);
  172. unset($keys[$index]);
  173. $arr[$value['name']] = $value['value'];
  174. }
  175. foreach ($keys as $key) {
  176. $arr[$key] = '';
  177. }
  178. $data = $arr;
  179. }
  180. if (empty($data['platform_serial_no']) || empty($data['platform_api_key']) || empty($data['platform_apiclient_key'])) {
  181. throw new \Exception('配置信息有误1', 1);
  182. }
  183. if (\Yii::$app->prod_is_dandianpu()) {
  184. //供应链系统没必要去判断服务端小程序是否存在
  185. // if (empty($data['platform_mch_appid'])) {
  186. // throw new \Exception('配置信息有误2', 1);
  187. // }
  188. } else {
  189. if (empty($data['platform_appid'])) {
  190. throw new \Exception('配置信息有误3', 1);
  191. }
  192. }
  193. } catch (\Exception $e) {
  194. return [
  195. 'code' => 1,
  196. 'message' => $e->getMessage(),
  197. ];
  198. }
  199. $cert_pem_file = $this->mch_private_key;
  200. if (!file_exists($cert_pem_file) || (md5($data['sp_apiclient_key']) != md5_file($cert_pem_file))) {
  201. file_put_contents($cert_pem_file, $data['sp_apiclient_key']);//platform_apiclient_key
  202. if (file_exists($this->public_key_path)){
  203. @unlink($this->public_key_path);
  204. \Yii::error([__METHOD__]);
  205. }
  206. }
  207. // $this->mch_id = '1508102711'; //sp_mch_id
  208. // $this->mch_api_key = 'L6u9gjIhOnFC1bzPA4s8JMII6wVCtRCo'; //platform_api_key
  209. // $this->serial_no = '4956AC2C151A50213DF52A28AB26CEAD092C822F'; //platform_serial_no
  210. // $this->service_app_id = 'wxb842f5f7c833049e'; //platform_appid
  211. $this->mch_id = $data['sp_mch_id'];
  212. $this->mch_api_key = $data['platform_api_key'];
  213. $this->serial_no = $data['platform_serial_no'];
  214. $this->service_app_id = $data['platform_appid'];
  215. $this->public_key = $data['platform_public_key'];
  216. $this->public_key_id = $data['platform_public_key_id'];
  217. \Yii::error([$this->mch_id, $this->mch_api_key, $this->serial_no, $this->service_app_id, $cert_pem_file]);
  218. }
  219. /**
  220. * 进件提交
  221. * @param $contact_info
  222. * @param $subject_info
  223. * @param $business_info
  224. * @param $bank_account_info
  225. * @param $apply_id
  226. */
  227. public function submit($contact_info, $subject_info, $business_info, $bank_account_info, $apply_id = 0,$appid="", $store_id = 0, $settlement_info = []) {
  228. if ((empty($settlement_info) || empty($settlement_info['qualification_type']))) {
  229. if ($apply_id) {
  230. $merchant_info = MerchantInfo::findOne($apply_id);
  231. $settlement_info = json_decode($merchant_info->settlement_info, true);
  232. }else{
  233. $settlement_info = [];
  234. }
  235. }
  236. $handle_res = $this->handleFormat($contact_info, $subject_info, $business_info, $bank_account_info,$appid,$settlement_info);
  237. $store_id = $store_id ?: get_store_id();
  238. if ($handle_res['code'] != 0) {
  239. return $handle_res;
  240. }
  241. $t = \Yii::$app->db->beginTransaction();
  242. // 入表
  243. if ($apply_id) {
  244. $merchant_info = MerchantInfo::findOne($apply_id);
  245. if($merchant_info->state == 'APPLYMENT_STATE_REJECTED'){
  246. $business_code = $this->getBusinessCode();
  247. $merchant_info->business_code = $business_code;
  248. }
  249. $merchant_info->refuse_desc = '';
  250. $merchant_info->state = '';
  251. $merchant_info->status = 0;
  252. } else {
  253. $merchant_info = MerchantInfo::findOne(['bind_store_id' => $store_id, 'status' => [0, 2], 'is_delete' => 0]);
  254. $merchant_info = !empty($merchant_info) ? $merchant_info : new MerchantInfo();
  255. // todo: man_id
  256. $merchant_info->saas_user_id = get_saas_user_id();
  257. $business_code = $this->getBusinessCode();
  258. $merchant_info->business_code = $business_code;
  259. $merchant_info->refuse_desc = '';
  260. $merchant_info->state = '';
  261. $merchant_info->status = 0;
  262. }
  263. $merchant_info->store_id = get_store_id();
  264. if ($store_id) {
  265. $merchant_info->bind_store_id = $store_id;
  266. }
  267. $merchant_info->contact_info = Json::encode($handle_res['data']['original_data']['contact_info']);
  268. $merchant_info->subject_info = Json::encode($handle_res['data']['original_data']['subject_info']);
  269. $merchant_info->business_info = Json::encode($handle_res['data']['original_data']['business_info']);
  270. $merchant_info->settlement_info = Json::encode($handle_res['data']['original_data']['settlement_info']);
  271. $merchant_info->bank_account_info = Json::encode($handle_res['data']['original_data']['bank_account_info']);
  272. if (!$merchant_info->save()) {
  273. return [
  274. 'code' => 1,
  275. 'msg' => $merchant_info->errors[0]
  276. ];
  277. }
  278. // 调接口
  279. $submitSer = $this->submitSer($merchant_info, $handle_res);
  280. if ($submitSer['code'] != 0) {
  281. if($this->submitErrRollback){
  282. $t->rollback();
  283. }else{
  284. $t->commit();
  285. }
  286. return $submitSer;
  287. }
  288. $t->commit();
  289. return [
  290. 'code' => 0,
  291. 'msg' => '提交成功',
  292. 'data' => $merchant_info,
  293. ];
  294. }
  295. // 调接口
  296. public function submitSer($merchant_info, $handle_res) {
  297. $merchant_info_id = $merchant_info->id;
  298. $data = $handle_res['data']['commit_data'];
  299. $business_code = $merchant_info->business_code;
  300. $data['business_code'] = $business_code;
  301. // 处理图片
  302. $data = $this->submitPic($data, $handle_res);
  303. \Yii::warning($data);
  304. if ($data['code'] != 0) {
  305. return $data;
  306. }
  307. // 调接口
  308. $result = $this->subApplyment($data, $handle_res);
  309. \Yii::warning($result);
  310. if ($result['code'] != 0) {
  311. $merchant_info->status = 0;
  312. if (!$merchant_info->save()) {
  313. return [
  314. 'code' => 1,
  315. 'msg' => "数据填充失败"
  316. ];
  317. };
  318. $result['data'] = [
  319. 'id' => $merchant_info->id,
  320. 'saas_user_id' => $merchant_info->saas_user_id,
  321. 'business_code' => $merchant_info->business_code,
  322. 'contact_info' => Json::decode($merchant_info->contact_info),
  323. 'subject_info' => Json::decode($merchant_info->subject_info),
  324. 'business_info' => Json::decode($merchant_info->business_info),
  325. 'settlement_info' => Json::decode($merchant_info->settlement_info),
  326. 'bank_account_info' => Json::decode($merchant_info->bank_account_info),
  327. 'applyment_id' => $merchant_info->applyment_id,
  328. 'status' => $merchant_info->status
  329. ];
  330. return $result;
  331. }
  332. $merchant_info->applyment_id = (string)$result['data'];
  333. $merchant_info->status = 1;
  334. if (!$merchant_info->save()) {
  335. \Yii::warning(['<================> 申请进件applyment_id入库失败', $merchant_info->errors]);
  336. return [
  337. 'code' => 1,
  338. 'msg' => 'applyment_id存储异常'
  339. ];
  340. }
  341. return [
  342. 'code' => 0,
  343. 'msg' => '提交成功'
  344. ];
  345. }
  346. // 处理图片
  347. public function submitPic($data, $handle_res) {
  348. if($data['settlement_info']['qualifications']){
  349. foreach($data['settlement_info']['qualifications'] as &$item){
  350. $mediaUpload = $this->mediaUpload($item);
  351. if ($mediaUpload['code'] !== 0) {
  352. return $mediaUpload;
  353. }
  354. $item = $mediaUpload['data'];
  355. }
  356. }
  357. if($data['settlement_info']['activities_additions']){
  358. foreach($data['settlement_info']['activities_additions'] as &$item){
  359. $mediaUpload = $this->mediaUpload($item);
  360. if ($mediaUpload['code'] !== 0) {
  361. return $mediaUpload;
  362. }
  363. $item = $mediaUpload['data'];
  364. }
  365. }
  366. if($data['contact_info']['business_authorization_letter']){
  367. $mediaUpload = $this->mediaUpload($data['contact_info']['business_authorization_letter']);
  368. if ($mediaUpload['code'] !== 0) {
  369. return $mediaUpload;
  370. }
  371. $data['contact_info']['business_authorization_letter'] = $mediaUpload['data'];
  372. }
  373. if($data['contact_info']['contact_id_doc_copy']){
  374. $mediaUpload = $this->mediaUpload($data['contact_info']['contact_id_doc_copy']);
  375. if ($mediaUpload['code'] !== 0) {
  376. return $mediaUpload;
  377. }
  378. $data['contact_info']['contact_id_doc_copy'] = $mediaUpload['data'];
  379. }
  380. if($data['contact_info']['contact_id_doc_copy_back']){
  381. $mediaUpload = $this->mediaUpload($data['contact_info']['contact_id_doc_copy_back']);
  382. if ($mediaUpload['code'] !== 0) {
  383. return $mediaUpload;
  384. }
  385. $data['contact_info']['contact_id_doc_copy_back'] = $mediaUpload['data'];
  386. }
  387. if ($data['subject_info']['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL || $data['subject_info']['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE) {
  388. $mediaUpload = $this->mediaUpload($data['subject_info']['business_license_info']['license_copy']);
  389. if ($mediaUpload['code'] !== 0) {
  390. return $mediaUpload;
  391. }
  392. $data['subject_info']['business_license_info']['license_copy'] = $mediaUpload['data'];
  393. unset($data['subject_info']['certificate_info']);
  394. }
  395. if ($data['subject_info']['subject_type'] == self::SUBJECT_TYPE_INSTITUTIONS || $data['subject_info']['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
  396. $mediaUpload = $this->mediaUpload($data['subject_info']['certificate_info']['cert_copy']);
  397. if ($mediaUpload['code'] !== 0) {
  398. return $mediaUpload;
  399. }
  400. $data['subject_info']['certificate_info']['cert_copy'] = $mediaUpload['data'];
  401. if (!$handle_res['data']['original_data']['subject_info']['organization_info']['is_composite']) {
  402. $mediaUpload = $this->mediaUpload($data['subject_info']['organization_info']['organization_copy']);
  403. if ($mediaUpload['code'] !== 0) {
  404. return $mediaUpload;
  405. }
  406. $data['subject_info']['organization_info']['organization_copy'] = $mediaUpload['data'];
  407. }
  408. unset($data['subject_info']['business_license_info']);
  409. } else {
  410. unset($data['subject_info']['organization_info']);
  411. }
  412. if ($data['subject_info']['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
  413. $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_card_info']['id_card_copy']);
  414. if ($mediaUpload['code'] !== 0) {
  415. return $mediaUpload;
  416. }
  417. $data['subject_info']['identity_info']['id_card_info']['id_card_copy'] = $mediaUpload['data'];
  418. $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_card_info']['id_card_national']);
  419. if ($mediaUpload['code'] !== 0) {
  420. return $mediaUpload;
  421. }
  422. $data['subject_info']['identity_info']['id_card_info']['id_card_national'] = $mediaUpload['data'];
  423. unset($data['subject_info']['identity_info']['id_doc_info']);
  424. } else {
  425. $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_doc_info']['id_doc_copy']);
  426. if ($mediaUpload['code'] !== 0) {
  427. return $mediaUpload;
  428. }
  429. $data['subject_info']['identity_info']['id_doc_info']['id_doc_copy'] = $mediaUpload['data'];
  430. $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_doc_info']['id_doc_copy_back']);
  431. if ($mediaUpload['code'] !== 0) {
  432. return $mediaUpload;
  433. }
  434. $data['subject_info']['identity_info']['id_doc_info']['id_doc_copy_back'] = $mediaUpload['data'];
  435. unset($data['subject_info']['identity_info']['id_card_info']);
  436. }
  437. return $data;
  438. }
  439. /**
  440. * 数据处理
  441. * @param $contact_info
  442. * @param $subject_info
  443. * @param $business_info
  444. * @param $bank_account_info
  445. * @return array
  446. */
  447. private function handleFormat($contact_info, $subject_info, $business_info, $bank_account_info, $sub_appid='', $settlement_info = [])
  448. {
  449. if (empty($contact_info) || empty($subject_info) || empty($business_info) || empty($bank_account_info)) {
  450. return [
  451. 'code' => 1,
  452. 'msg' => '申请信息缺失'
  453. ];
  454. }
  455. $pay_rate = Option::get('pay_rate', 0, 'saas')['value'];
  456. if (empty($pay_rate)) {
  457. $pay_rate = '0.2';
  458. }
  459. // 判断超级管理员信息
  460. if (empty($contact_info['contact_name']) || empty($contact_info['contact_id_number']) || empty($contact_info['mobile_phone'])
  461. || empty($contact_info['contact_email'])) {
  462. return [
  463. 'code' => 1,
  464. 'msg' => '超级管理员信息有误'
  465. ];
  466. }
  467. // 判断主体资料
  468. if (!in_array($subject_info['subject_type'], self::$subject_type)) {
  469. return [
  470. 'code' => 1,
  471. 'msg' => '主体类型缺失'
  472. ];
  473. }
  474. // 主体为个体户/企业 营业执照信息必填
  475. if ($subject_info['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE || $subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
  476. if (empty($subject_info['business_license_info'])) {
  477. return [
  478. 'code' => 1,
  479. 'msg' => '个体户或企业,营业执照信息必填'
  480. ];
  481. }
  482. if (empty($subject_info['business_license_info']['license_copy']) || empty($subject_info['business_license_info']['license_number'])
  483. || empty($subject_info['business_license_info']['merchant_name'])
  484. || empty($subject_info['business_license_info']['legal_person'])) {
  485. return [
  486. 'code' => 1,
  487. 'msg' => '缺少营业执照相关信息'
  488. ];
  489. }
  490. //判断营业执照大小
  491. $license_copy = $this->saveTempImage($subject_info['business_license_info']['license_copy']);
  492. if ((floatval(filesize($license_copy)) / 1024 / 1024) > 2) { //判断是否大于2M
  493. return [
  494. 'code' => 1,
  495. 'msg' => '营业执照图片大小过大'
  496. ];
  497. }
  498. //如果为个体
  499. if (empty($subject_info['business_license_info']['period_begin'])
  500. && empty($subject_info['business_license_info']['period_end'])) {
  501. return [
  502. 'code' => 1,
  503. 'msg' => '缺少营业执照开始结束日期'
  504. ];
  505. }
  506. if ($subject_info['business_license_info']['period_end'] === '2999-12-31' || $subject_info['business_license_info']['is_long']) {
  507. $subject_info['business_license_info']['period_end'] = '长期';
  508. empty($subject_info['business_license_info']['period_begin']) && $subject_info['business_license_info']['period_begin'] = date('Y-m-d');
  509. }
  510. }
  511. // 党政、机关及事业单位/其他组织 登记证书信息必填
  512. if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INSTITUTIONS || $subject_info['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
  513. if (empty($subject_info['certificate_info'])) {
  514. return [
  515. 'code' => 1,
  516. 'msg' => '党政、机关及事业单位或其他组织,登记证书信息信息必填'
  517. ];
  518. }
  519. if (empty($subject_info['certificate_info']['cert_copy']) || empty($subject_info['certificate_info']['cert_type']) || empty($subject_info['certificate_info']['cert_number'])
  520. || empty($subject_info['certificate_info']['merchant_name']) || empty($subject_info['certificate_info']['company_address'])
  521. || empty($subject_info['certificate_info']['legal_person']) || empty($subject_info['certificate_info']['period_begin']) ) {
  522. return [
  523. 'code' => 1,
  524. 'msg' => '缺少登记证书信息相关信息'
  525. ];
  526. }
  527. if (!$subject_info['certificate_info']['is_long']) {
  528. if (empty($subject_info['certificate_info']['period_end'])) {
  529. return [
  530. 'code' => 1,
  531. 'msg' => '登记证书证件有效期限结束日期需要填写'
  532. ];
  533. }
  534. } else {
  535. $subject_info['certificate_info']['period_end'] = '长期';
  536. }
  537. if (!in_array($subject_info['certificate_info']['cert_type'], self::$certificate_type)) {
  538. return [
  539. 'code' => 1,
  540. 'msg' => '登记证书类型有误'
  541. ];
  542. }
  543. }
  544. // 主体为企业/党政、机关及事业单位/其他组织,且证件号码不是18位时必填
  545. // 若营业执照未三证合一 ,该参数必传
  546. // 若营业执照三证合一 ,该参数可不传
  547. if ($subject_info['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE || $subject_info['subject_type'] == self::SUBJECT_TYPE_INSTITUTIONS || $subject_info['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
  548. if (!$subject_info['organization_info']['is_composite']) {
  549. if (empty($subject_info['organization_info'])) {
  550. return [
  551. 'code' => 1,
  552. 'msg' => '企业,党政、机关及事业单位或其他组织,组织机构信息必填'
  553. ];
  554. }
  555. if (empty($subject_info['organization_info']['organization_copy']) || empty($subject_info['organization_info']['organization_code'])
  556. || empty($subject_info['organization_info']['org_period_begin'])) {
  557. return [
  558. 'code' => 1,
  559. 'msg' => '缺少组织结构信息相关信息'
  560. ];
  561. }
  562. if (!$subject_info['organization_info']['is_long']) {
  563. if (empty($subject_info['organization_info']['org_period_end'])) {
  564. return [
  565. 'code' => 1,
  566. 'msg' => '组织机构证件有效期限结束日期需要填写'
  567. ];
  568. }
  569. } else {
  570. $subject_info['organization_info']['org_period_end'] = '长期';
  571. }
  572. }
  573. }
  574. // 经营者/法人身份证件
  575. if (empty($subject_info['identity_info'])) {
  576. return [
  577. 'code' => 1,
  578. 'msg' => '经营者/法人身份证件需要填写'
  579. ];
  580. }
  581. if ($subject_info['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
  582. if (empty($subject_info['identity_info']['id_card_info'])) {
  583. return [
  584. 'code' => 1,
  585. 'msg' => '身份证信息需要填写'
  586. ];
  587. }
  588. if (empty($subject_info['identity_info']['id_card_info']['id_card_copy'])
  589. || empty($subject_info['identity_info']['id_card_info']['id_card_national'])
  590. || empty($subject_info['identity_info']['id_card_info']['id_card_name'])
  591. || empty($subject_info['identity_info']['id_card_info']['id_card_number'])
  592. || empty($subject_info['identity_info']['id_card_info']['id_card_address'])
  593. || empty($subject_info['identity_info']['id_card_info']['card_period_begin'])) {
  594. return [
  595. 'code' => 1,
  596. 'msg' => '身份证信息填写有误'
  597. ];
  598. }
  599. if (!$subject_info['identity_info']['is_long']) {
  600. if (empty($subject_info['identity_info']['id_card_info']['card_period_end'])) {
  601. return [
  602. 'code' => 1,
  603. 'msg' => '身份证证件有效期限结束日期需要填写'
  604. ];
  605. }
  606. } else {
  607. $subject_info['identity_info']['id_card_info']['card_period_end'] = '长期';
  608. }
  609. } else {
  610. if (empty($subject_info['identity_info']['id_doc_info'])) {
  611. return [
  612. 'code' => 1,
  613. 'msg' => '护照或通行证信息需要填写'
  614. ];
  615. }
  616. if (empty($subject_info['identity_info']['id_doc_info']['id_doc_copy'])
  617. || empty($subject_info['identity_info']['id_doc_info']['id_doc_copy_back'])
  618. || empty($subject_info['identity_info']['id_doc_info']['id_doc_name'])
  619. || empty($subject_info['identity_info']['id_doc_info']['id_doc_number'])
  620. || empty($subject_info['identity_info']['id_doc_info']['doc_period_begin'])) {
  621. return [
  622. 'code' => 1,
  623. 'msg' => '护照或通行证信息填写有误'
  624. ];
  625. }
  626. if (!$subject_info['identity_info']['is_long']) {
  627. if (empty($subject_info['identity_info']['id_doc_info']['doc_period_end'])) {
  628. return [
  629. 'code' => 1,
  630. 'msg' => '护照或通行证有效期限结束日期需要填写'
  631. ];
  632. }
  633. } else {
  634. $subject_info['identity_info']['id_doc_info']['doc_period_end'] = '长期';
  635. }
  636. }
  637. if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
  638. $subject_info['identity_info']['owner'] = null;
  639. } else {
  640. $subject_info['identity_info']['owner'] = true;
  641. }
  642. // 经营资料
  643. if (empty($business_info['merchant_shortname']) || empty($business_info['service_phone'])) {
  644. return [
  645. 'code' => 1,
  646. 'msg' => '经营资料填写有误'
  647. ];
  648. }
  649. $business_info['sales_info']['sales_scenes_type'] = ['SALES_SCENES_MINI_PROGRAM']; // 小程序
  650. if(!empty($sub_appid)){
  651. $business_info['sales_info']['mini_program_info']['mini_program_sub_appid'] = $sub_appid;//使用商户的小程序信息
  652. }else{
  653. $business_info['sales_info']['mini_program_info']['mini_program_appid'] = $this->service_app_id;//使用服务商的小程序信息
  654. }
  655. // 结算银行账户信息
  656. if (!in_array($bank_account_info['bank_account_type'], self::$bank_type)) {
  657. return [
  658. 'code' => 1,
  659. 'msg' => '结算银行账户类型有误'
  660. ];
  661. }
  662. // if (empty($bank_account_info['bank_address_code'])) {
  663. $bank_account_info['bank_address_code'] = end($bank_account_info["code_list"]);
  664. // }
  665. if (empty($bank_account_info['account_name']) || empty($bank_account_info['account_bank']) || empty($bank_account_info['bank_address_code'])
  666. || empty($bank_account_info['account_number'])) {
  667. return [
  668. 'code' => 1,
  669. 'msg' => '结算银行账户填写有误'
  670. ];
  671. }
  672. if ($bank_account_info['account_bank'] != '其他银行' && isset($bank_account_info['bank_name'])) {
  673. unset($bank_account_info['bank_name']);
  674. }
  675. if ($bank_account_info['account_bank'] == '其他银行' && empty($bank_account_info['bank_name'])) {
  676. return [
  677. 'code' => 1,
  678. 'msg' => '开户银行全称(含支行)填写有误'
  679. ];
  680. }
  681. $settlement_info_default = [
  682. 'settlement_id' => '719',
  683. 'qualification_type' => '零售',
  684. 'activities_id'=>'20191030111cff5b5e',
  685. ];
  686. $settlement_info = $settlement_info ? : $settlement_info_default;
  687. $settlement_info['activities_rate'] = $pay_rate;
  688. if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
  689. $settlement_info['settlement_id'] = '719';
  690. } else if ($subject_info['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE) {
  691. $settlement_info['settlement_id'] = '716';
  692. } else if ($subject_info['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
  693. $settlement_info['settlement_id'] = '727';
  694. } else {
  695. $settlement_info['settlement_id'] = '716';
  696. }
  697. $original_data = [
  698. 'subject_info' => $subject_info,
  699. 'contact_info' => $contact_info,
  700. 'business_info' => $business_info,
  701. 'settlement_info' => $settlement_info,
  702. 'bank_account_info' => $bank_account_info
  703. ];
  704. $contact_info['contact_name'] = $this->getEncrypt($contact_info['contact_name']);
  705. $contact_info['contact_id_number'] = $this->getEncrypt($contact_info['contact_id_number']);
  706. $contact_info['mobile_phone'] = $this->getEncrypt($contact_info['mobile_phone']);
  707. $contact_info['contact_email'] = $this->getEncrypt($contact_info['contact_email']);
  708. $contact_info['contact_type'] = $contact_info['contact_type'] ? $contact_info['contact_type'] : 'LEGAL';
  709. if ($subject_info['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
  710. $subject_info['identity_info']['id_card_info']['id_card_name'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_name']);
  711. $subject_info['identity_info']['id_card_info']['id_card_number'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_number']);
  712. $subject_info['identity_info']['id_card_info']['id_card_address'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_address']);
  713. } else {
  714. $subject_info['identity_info']['id_doc_info']['id_doc_name'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_name']);
  715. $subject_info['identity_info']['id_doc_info']['id_doc_number'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_number']);
  716. $subject_info['identity_info']['id_doc_info']['id_doc_address'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_address']);
  717. }
  718. $bank_account_info['account_name'] = $this->getEncrypt($bank_account_info['account_name']);
  719. $bank_account_info['account_number'] = $this->getEncrypt($bank_account_info['account_number']);
  720. $commit_data = [
  721. 'subject_info' => $subject_info,
  722. 'contact_info' => $contact_info,
  723. 'business_info' => $business_info,
  724. 'settlement_info' => $settlement_info,
  725. 'bank_account_info' => $bank_account_info
  726. ];
  727. unset($commit_data['subject_info']['certificate_info']['is_long']);
  728. unset($commit_data['subject_info']['organization_info']['is_composite']);
  729. unset($commit_data['subject_info']['organization_info']['is_long']);
  730. unset($commit_data['subject_info']['identity_info']['is_long']);
  731. unset($commit_data['bank_account_info']['code_list']);
  732. unset($commit_data['bank_account_info']['address_list']);
  733. unset($commit_data['business_info']['sales_info']['mini_program_appid']);
  734. return [
  735. 'code' => 0,
  736. 'data' => [
  737. 'original_data' => $original_data,
  738. 'commit_data' => $commit_data
  739. ]
  740. ];
  741. }
  742. /**
  743. * 商户进件
  744. * @param $data
  745. * @return array
  746. * @throws Exception
  747. */
  748. public function subApplyment($data) {
  749. $url = $this->applyment_url;
  750. // 获取支付平台证书编码(也可以用接口中返回的serial_no 来源:https://api.mch.weixin.qq.com/v3/certificates)
  751. if($this->public_key){
  752. $serial_no = $this->public_key_id;
  753. }else{
  754. $serial_no = $this->parseSerialNo($this->getCertFicates());
  755. }
  756. $bodyData = json_encode($data);
  757. // 获取认证信息
  758. $authorization = $this->getAuthorization($url, 'POST', $bodyData);
  759. $header = [
  760. 'Content-Type:application/json',
  761. 'Accept:application/json',
  762. 'User-Agent:*/*',
  763. 'Authorization:' . $authorization,
  764. 'Wechatpay-Serial:' . $serial_no
  765. ];
  766. $json = $this->getCurl('POST', $url, $bodyData, $header);
  767. $data = json_decode($json, true);
  768. if (isset($data['code']) && isset($data['message'])) {
  769. return [
  770. 'code' => 1,
  771. 'msg' => $data['message']
  772. ];
  773. }
  774. if (empty($applyment_id = $data['applyment_id'])) {
  775. return [
  776. 'code' => 1,
  777. 'msg' => '返回错误'
  778. ];
  779. }
  780. return ['code' => 0, 'data' => $applyment_id];
  781. }
  782. /**
  783. * 进件查询
  784. */
  785. public function queryApplyment($applyment_id, $code = 0)
  786. {
  787. $url = $this->query_applyment_url . $applyment_id;
  788. if(empty($applyment_id) && $code){
  789. $url = $this->query_applyment_url_code . $code;
  790. \Yii::error([__METHOD__, $url]);
  791. }
  792. if($this->public_key){
  793. $serial_no = $this->public_key_id;
  794. }else{
  795. $serial_no = $this->parseSerialNo($this->getCertFicates());
  796. }
  797. // 获取认证信息
  798. $authorization = $this->getAuthorization($url);
  799. $header = [
  800. 'Content-Type:application/json',
  801. 'Accept:application/json',
  802. 'User-Agent:*/*',
  803. 'Authorization:' . $authorization,
  804. 'Wechatpay-Serial:' . $serial_no
  805. ];
  806. $json = $this->getCurl('GET', $url, '', $header);
  807. $data = json_decode($json, true);
  808. if (isset($data['code']) && isset($data['message'])) {
  809. return [
  810. 'code' => 1,
  811. 'msg' => '请求错误:' . $data['message']
  812. ];
  813. }
  814. return $data;
  815. }
  816. /**
  817. * 上传文件
  818. */
  819. public function mediaUpload($file_path)
  820. {
  821. $file = file_get_contents($file_path);
  822. $size = strlen($file)/1024/1024;
  823. \Yii::error([__METHOD__, $size]);
  824. if ($size >= 2) {
  825. return [
  826. 'code' => 1,
  827. 'msg' => '图片大小不能超过2M',
  828. ];
  829. }
  830. $path_info = pathinfo($file_path);
  831. $file_name = $path_info['basename'];
  832. $url = $this->upload_image_url;
  833. if (\function_exists('exif_imagetype')) {
  834. $type = exif_imagetype($file_path);
  835. } else {
  836. list($width, $height, $type, $attr) = getimagesize($file_path);
  837. }
  838. if($this->public_key){
  839. $serial_no = $this->public_key_id;
  840. }else{
  841. $serial_no = $this->parseSerialNo($this->getCertFicates());
  842. }
  843. $mime_type = image_type_to_mime_type($type);
  844. $meta = [
  845. "filename" => $file_name,
  846. "sha256" => hash_file('sha256', $file_path)
  847. ];
  848. // 获取认证信息
  849. $authorization = $this->getAuthorization($url, 'POST', json_encode($meta));
  850. $boundary = uniqid();
  851. $header = [
  852. 'Accept:application/json',
  853. 'User-Agent:*/*',
  854. 'Content-Type:multipart/form-data;boundary=' . $boundary,
  855. 'Authorization:' . $authorization,
  856. 'Wechatpay-Serial:' . $serial_no
  857. ];
  858. // 组合参数
  859. $boundaryStr = "--{$boundary}\r\n";
  860. $out = $boundaryStr;
  861. $out .= 'Content-Disposition: form-data; name="meta"' . "\r\n";
  862. $out .= 'Content-Type: multipart/form-data' . "\r\n";
  863. $out .= "\r\n";
  864. $out .= json_encode($meta) . "\r\n";
  865. $out .= $boundaryStr;
  866. $out .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n";
  867. $out .= 'Content-Type: ' . $mime_type . ';' . "\r\n";
  868. $out .= "\r\n";
  869. $out .= $file . "\r\n";
  870. $out .= "--{$boundary}--\r\n";
  871. $json = $this->getCurl('POST', $url, $out, $header);
  872. $data = json_decode($json, true);
  873. if (isset($data['code']) && isset($data['message'])) {
  874. return [
  875. 'code' => 1,
  876. 'msg' => $data['message']
  877. ];
  878. }
  879. if (empty($media_id = $data['media_id'])) {
  880. return [
  881. 'code' => 1,
  882. 'msg' => '返回错误'
  883. ];
  884. }
  885. return ['code' => 0, 'data' => $media_id];
  886. }
  887. /**
  888. * 获取微信支付平台证书
  889. */
  890. public function certFicates()
  891. {
  892. $url = $this->certificates_url;
  893. // 获取认证信息
  894. $authorization = $this->getAuthorization($url);
  895. $header = [
  896. 'Content-Type:application/json',
  897. 'Accept:application/json',
  898. 'User-Agent:' . $this->mch_id,
  899. 'Authorization:' . $authorization
  900. ];
  901. $json = $this->getCurl('GET', $url, '', $header);
  902. $data = json_decode($json, true);
  903. if (isset($data['code']) && isset($data['message'])) {
  904. return [
  905. 'code' => 1,
  906. 'msg' => $data['message']
  907. ];
  908. }
  909. if (empty($cfdata = $data['data'][0])) {
  910. return [
  911. 'code' => 1,
  912. 'msg' => '返回错误'
  913. ];
  914. }
  915. return $cfdata;
  916. }
  917. /**
  918. * 获取认证信息
  919. * @param string $url
  920. * @param string $http_method
  921. * @param string $body
  922. * @return string
  923. * @throws Exception
  924. */
  925. public function getAuthorization($url, $http_method = 'GET', $body = '')
  926. {
  927. if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
  928. throw new \Exception("当前PHP环境不支持SHA256withRSA");
  929. }
  930. //私钥地址
  931. $mch_private_key = $this->mch_private_key;
  932. //商户号
  933. $merchant_id = $this->mch_id;
  934. //当前时间戳
  935. $timestamp = time();
  936. //随机字符串
  937. $nonce = $this->getNonceStr();
  938. //证书编号
  939. $serial_no = $this->serial_no;
  940. $url_parts = parse_url($url);
  941. $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
  942. $message = $http_method . "\n" .
  943. $canonical_url . "\n" .
  944. $timestamp . "\n" .
  945. $nonce . "\n" .
  946. $body . "\n";
  947. openssl_sign($message, $raw_sign, \openssl_get_privatekey(\file_get_contents($mch_private_key)), 'sha256WithRSAEncryption');
  948. $sign = base64_encode($raw_sign);
  949. $schema = 'WECHATPAY2-SHA256-RSA2048';
  950. $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
  951. $merchant_id, $nonce, $timestamp, $serial_no, $sign);
  952. return $schema . ' ' . $token;
  953. }
  954. /**
  955. * 敏感字符加密
  956. * @param $str
  957. * @return string
  958. * @throws Exception
  959. */
  960. private function getEncrypt($str)
  961. {
  962. static $content;
  963. if (empty($content)) {
  964. $content = $this->getCertFicates();
  965. }
  966. // 去除内容中的空格
  967. $str = preg_replace("/\s| /", '', $str);
  968. $encrypted = '';
  969. if (openssl_public_encrypt($str, $encrypted, $content, OPENSSL_PKCS1_OAEP_PADDING)) {
  970. //base64编码
  971. $sign = base64_encode($encrypted);
  972. } else {
  973. throw new \Exception('encrypt failed');
  974. }
  975. return $sign;
  976. }
  977. /**
  978. * 获取支付平台证书
  979. * @return false|string
  980. */
  981. public function getCertFicates()
  982. {
  983. if($this->public_key){
  984. return $this->public_key;
  985. }
  986. $public_key_path = $this->public_key_path;
  987. if (!file_exists($public_key_path)) {
  988. \Yii::error([__METHOD__]);
  989. $cfData = $this->certFicates();
  990. $content = $this->decryptToString($cfData['encrypt_certificate']['associated_data'], $cfData['encrypt_certificate']['nonce'], $cfData['encrypt_certificate']['ciphertext'], $this->mch_api_key);
  991. file_put_contents($public_key_path, $content);
  992. }
  993. else {
  994. $content = file_get_contents($public_key_path);
  995. }
  996. return $content;
  997. }
  998. /**
  999. * 业务编号
  1000. * @return string
  1001. */
  1002. public function getBusinessCode()
  1003. {
  1004. return date('Ymd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
  1005. }
  1006. /**
  1007. * 随机字符串
  1008. * @param int $length
  1009. * @return string
  1010. */
  1011. private function getNonceStr($length = 16)
  1012. {
  1013. // 密码字符集,可任意添加你需要的字符
  1014. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  1015. $str = "";
  1016. for ($i = 0; $i < $length; $i++) {
  1017. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  1018. }
  1019. return $str;
  1020. }
  1021. /**
  1022. * @param string $method
  1023. * @param string $url
  1024. * @param array|string $data
  1025. * @param array $headers
  1026. * @param int $timeout
  1027. * @return bool|string
  1028. */
  1029. private function getCurl($method = 'GET', $url, $data, $headers = [], $timeout = 30)
  1030. {
  1031. $curl = curl_init();
  1032. curl_setopt($curl, CURLOPT_URL, $url);
  1033. curl_setopt($curl, CURLOPT_HEADER, false);
  1034. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  1035. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  1036. curl_setopt($curl, CURLOPT_SSLVERSION, false);
  1037. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  1038. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  1039. if (!empty($headers)) {
  1040. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  1041. }
  1042. if ($method == 'POST') {
  1043. curl_setopt($curl, CURLOPT_POST, TRUE);
  1044. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  1045. }
  1046. $result = curl_exec($curl);
  1047. curl_close($curl);
  1048. return $result;
  1049. }
  1050. /**
  1051. * Decrypt AEAD_AES_256_GCM ciphertext(官方案例-已改造)
  1052. *
  1053. * @param string $associatedData AES GCM additional authentication data
  1054. * @param string $nonceStr AES GCM nonce
  1055. * @param string $ciphertext AES GCM cipher text
  1056. *
  1057. * @return string|bool Decrypted string on success or FALSE on failure
  1058. */
  1059. private function decryptToString($associatedData, $nonceStr, $ciphertext, $aesKey)
  1060. {
  1061. $auth_tag_length_byte = 16;
  1062. $ciphertext = \base64_decode($ciphertext);
  1063. if (strlen($ciphertext) <= $auth_tag_length_byte) {
  1064. return false;
  1065. }
  1066. // ext-sodium (default installed on >= PHP 7.2)
  1067. if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
  1068. \sodium_crypto_aead_aes256gcm_is_available()) {
  1069. return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  1070. }
  1071. // ext-libsodium (need install libsodium-php 1.x via pecl)
  1072. if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
  1073. \Sodium\crypto_aead_aes256gcm_is_available()) {
  1074. return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
  1075. }
  1076. // openssl (PHP >= 7.1 support AEAD)
  1077. if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
  1078. $ctext = substr($ciphertext, 0, -$auth_tag_length_byte);
  1079. $authTag = substr($ciphertext, -$auth_tag_length_byte);
  1080. return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr,
  1081. $authTag, $associatedData);
  1082. }
  1083. throw new \Exception('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
  1084. }
  1085. /**
  1086. * 获取证书编号(官方案例-已改造)
  1087. * @param $certificate
  1088. * @return string
  1089. */
  1090. private function parseSerialNo($certificate)
  1091. {
  1092. $info = \openssl_x509_parse($certificate);
  1093. if (!isset($info['serialNumber']) && !isset($info['serialNumberHex'])) {
  1094. throw new \InvalidArgumentException('证书格式错误');
  1095. }
  1096. $serialNo = '';
  1097. // PHP 7.0+ provides serialNumberHex field
  1098. if (isset($info['serialNumberHex'])) {
  1099. $serialNo = $info['serialNumberHex'];
  1100. }
  1101. else {
  1102. // PHP use i2s_ASN1_INTEGER in openssl to convert serial number to string,
  1103. // i2s_ASN1_INTEGER may produce decimal or hexadecimal format,
  1104. // depending on the version of openssl and length of data.
  1105. if (\strtolower(\substr($info['serialNumber'], 0, 2)) == '0x') { // HEX format
  1106. $serialNo = \substr($info['serialNumber'], 2);
  1107. }
  1108. else { // DEC format
  1109. $value = $info['serialNumber'];
  1110. $hexvalues = ['0', '1', '2', '3', '4', '5', '6', '7',
  1111. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
  1112. while ($value != '0') {
  1113. $serialNo = $hexvalues[\bcmod($value, '16')] . $serialNo;
  1114. $value = \bcdiv($value, '16', 0);
  1115. }
  1116. }
  1117. }
  1118. return \strtoupper($serialNo);
  1119. }
  1120. //获取网络图片到临时目录
  1121. public function saveTempImage($url)
  1122. {
  1123. if (strpos($url, 'http') === false) {
  1124. $url = 'http:' . trim($url);
  1125. }
  1126. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  1127. mkdir(\Yii::$app->runtimePath . '/image');
  1128. }
  1129. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  1130. CurlHelper::download($url, $save_path);
  1131. return $save_path;
  1132. }
  1133. }