| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\WechatMerchant;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\utils\CurlHelper;
- use yii\base\Exception;
- use yii\helpers\Json;
- class Merchant
- {
- // 服务商小程序appid
- private $service_app_id;
- // 商户mchid
- private $mch_id;
- // 商户API v3密钥(微信服务商-账户中心-API安全 api v3密钥 https://pay.weixin.qq.com/index.php/core/cert/api_cert)
- private $mch_api_key;
- // 证书编号 (apiclient_cert.pem证书解析后获得)
- private $serial_no;
- // 私钥 apiclient_key.pem(微信服务商-账户中心-API安全 自行下载 https://pay.weixin.qq.com/index.php/core/cert/api_cert)
- private $mch_private_key = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'apiclient_key.pem';
- // 支付平台公钥(接口获取)
- private $public_key_path = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cert_ficates_v3.pem';
- // 微信支付公钥
- private $public_key;
- private $public_key_id;
- /**
- * @var string 上传图片接口地址
- */
- private $upload_image_url = 'https://api.mch.weixin.qq.com/v3/merchant/media/upload';
- /**
- * @var string 申请商户号接口
- */
- private $applyment_url = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/';
- /**
- * @var string 查询商户号申请详情接口
- */
- private $query_applyment_url = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/applyment_id/';
- private $query_applyment_url_code = 'https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/business_code/';
- /**
- * 个体户
- */
- const SUBJECT_TYPE_INDIVIDUAL = 'SUBJECT_TYPE_INDIVIDUAL';
- /**
- * 企业
- */
- const SUBJECT_TYPE_ENTERPRISE = 'SUBJECT_TYPE_ENTERPRISE';
- /**
- * 党政机关事业单位
- */
- const SUBJECT_TYPE_INSTITUTIONS = 'SUBJECT_TYPE_INSTITUTIONS';
- /**
- * 其他组织
- */
- const SUBJECT_TYPE_OTHERS = 'SUBJECT_TYPE_OTHERS';
- /**
- * @var array 主体类型
- */
- private static $subject_type = [
- self::SUBJECT_TYPE_INDIVIDUAL,
- self::SUBJECT_TYPE_ENTERPRISE,
- self::SUBJECT_TYPE_INSTITUTIONS,
- self::SUBJECT_TYPE_OTHERS
- ];
- const CERTIFICATE_TYPE_238 = 'CERTIFICATE_TYPE_2388'; // 事业单位法人证书
- const CERTIFICATE_TYPE_2389 = 'CERTIFICATE_TYPE_2389'; // 统一社会信用代码证书
- const CERTIFICATE_TYPE_2390 = 'CERTIFICATE_TYPE_2390'; // 有偿服务许可证(军队医院适用)
- const CERTIFICATE_TYPE_2391 = 'CERTIFICATE_TYPE_2391'; // 医疗机构执业许可证(军队医院适用)
- const CERTIFICATE_TYPE_2392 = 'CERTIFICATE_TYPE_2392'; // 企业营业执照(挂靠企业的党组织适用)
- const CERTIFICATE_TYPE_2393 = 'CERTIFICATE_TYPE_2393'; // 组织机构代码证(政府机关适用)
- const CERTIFICATE_TYPE_2394 = 'CERTIFICATE_TYPE_2394'; // 社会团体法人登记证书
- const CERTIFICATE_TYPE_2395 = 'CERTIFICATE_TYPE_2395'; // 民办非企业单位登记证书
- const CERTIFICATE_TYPE_2396 = 'CERTIFICATE_TYPE_2396'; // 基金会法人登记证书
- const CERTIFICATE_TYPE_2397 = 'CERTIFICATE_TYPE_2397'; // 慈善组织公开募捐资格证书
- const CERTIFICATE_TYPE_2398 = 'CERTIFICATE_TYPE_2398'; // 农民专业合作社法人营业执照
- const CERTIFICATE_TYPE_2399 = 'CERTIFICATE_TYPE_2399'; // 宗教活动场所登记证
- const CERTIFICATE_TYPE_2400 = 'CERTIFICATE_TYPE_2400'; // 其他证书/批文/证明
- private static $certificate_type = [
- self::CERTIFICATE_TYPE_238,
- self::CERTIFICATE_TYPE_2389,
- self::CERTIFICATE_TYPE_2390,
- self::CERTIFICATE_TYPE_2391,
- self::CERTIFICATE_TYPE_2392,
- self::CERTIFICATE_TYPE_2393,
- self::CERTIFICATE_TYPE_2394,
- self::CERTIFICATE_TYPE_2395,
- self::CERTIFICATE_TYPE_2396,
- self::CERTIFICATE_TYPE_2397,
- self::CERTIFICATE_TYPE_2398,
- self::CERTIFICATE_TYPE_2399,
- self::CERTIFICATE_TYPE_2400
- ];
- const IDENTIFICATION_TYPE_IDCARD = 'IDENTIFICATION_TYPE_IDCARD'; // 中国大陆居民-身份证
- const IDENTIFICATION_TYPE_OVERSEA_PASSPORT = 'IDENTIFICATION_TYPE_OVERSEA_PASSPORT'; // 其他国家或地区居民-护照
- const IDENTIFICATION_TYPE_HONGKONG_PASSPORT = 'IDENTIFICATION_TYPE_HONGKONG_PASSPORT'; // 中国香港居民-来往内地通行证
- const IDENTIFICATION_TYPE_MACAO_PASSPORT = 'IDENTIFICATION_TYPE_MACAO_PASSPORT'; // 中国澳门居民-来往内地通行证
- const IDENTIFICATION_TYPE_TAIWAN_PASSPORT = 'IDENTIFICATION_TYPE_TAIWAN_PASSPORT'; // 中国台湾居民-来往大陆通行证
- private static $identify_type = [
- self::IDENTIFICATION_TYPE_IDCARD,
- self::IDENTIFICATION_TYPE_OVERSEA_PASSPORT,
- self::IDENTIFICATION_TYPE_HONGKONG_PASSPORT,
- self::IDENTIFICATION_TYPE_MACAO_PASSPORT,
- self::IDENTIFICATION_TYPE_TAIWAN_PASSPORT
- ];
- const BANK_ACCOUNT_TYPE_CORPORATE = 'BANK_ACCOUNT_TYPE_CORPORATE'; // 银行对公账户
- const BANK_ACCOUNT_TYPE_PERSONAL = 'BANK_ACCOUNT_TYPE_PERSONAL'; // 银行私人账户
- private static $bank_type = [
- self::BANK_ACCOUNT_TYPE_CORPORATE,
- self::BANK_ACCOUNT_TYPE_PERSONAL
- ];
- // 审核状态
- const APPLYMENT_STATE_EDITTING = 'APPLYMENT_STATE_EDITTING'; // 编辑中
- const APPLYMENT_STATE_AUDITING = 'APPLYMENT_STATE_AUDITING'; // 审核中
- const APPLYMENT_STATE_REJECTED = 'APPLYMENT_STATE_REJECTED'; // 已驳回
- const APPLYMENT_STATE_TO_BE_CONFIRMED = 'APPLYMENT_STATE_TO_BE_CONFIRMED'; // 待账户验证
- const APPLYMENT_STATE_TO_BE_SIGNED = 'APPLYMENT_STATE_TO_BE_SIGNED'; // 待签约
- const APPLYMENT_STATE_SIGNING = 'APPLYMENT_STATE_SIGNING'; // 开通权限中
- const APPLYMENT_STATE_FINISHED = 'APPLYMENT_STATE_FINISHED'; // 已完成
- const APPLYMENT_STATE_CANCELED = 'APPLYMENT_STATE_CANCELED'; // 已作废
- public static $valid_state_desc = [
- self::APPLYMENT_STATE_EDITTING => '编辑中',
- self::APPLYMENT_STATE_AUDITING => '审核中',
- self::APPLYMENT_STATE_REJECTED => '已驳回',
- self::APPLYMENT_STATE_TO_BE_CONFIRMED => '待账户验证',
- self::APPLYMENT_STATE_TO_BE_SIGNED => '待签约',
- self::APPLYMENT_STATE_SIGNING => '开通权限中',
- self::APPLYMENT_STATE_FINISHED => '已完成',
- self::APPLYMENT_STATE_CANCELED => '已作废',
- ];
- public static $valid_state_status = [
- self::APPLYMENT_STATE_EDITTING => 0,
- self::APPLYMENT_STATE_AUDITING => 1,
- self::APPLYMENT_STATE_REJECTED => 2,
- self::APPLYMENT_STATE_TO_BE_CONFIRMED => 3,
- self::APPLYMENT_STATE_TO_BE_SIGNED => 3,
- self::APPLYMENT_STATE_SIGNING => 3,
- self::APPLYMENT_STATE_FINISHED => 3,
- self::APPLYMENT_STATE_CANCELED => 2,
- ];
- /**
- * @var string 获取平台证书接口
- */
- private $certificates_url = 'https://api.mch.weixin.qq.com/v3/certificates';
-
- public $submitErrRollback = 0;
- public function __construct()
- {
- $keys = [
- 'platform_appid',
- 'platform_mch_id',
- 'platform_key',
- 'platform_apiclient_cert',
- 'platform_apiclient_key',
- 'platform_api_key',
- 'platform_public_key',
- 'platform_public_key_id',
- 'platform_serial_no',
- 'platform_mch_appid',
- 'sp_mch_id',
- 'sp_apiclient_key'
- ];
- $data = Option::get($keys, 0, 'saas');
- try {
- if (empty($data)) {
- throw new \Exception('配置信息有误0', 1);
- } else {
- $arr = [];
- foreach ($data as $value) {
- $index = array_search($value['name'], $keys);
- unset($keys[$index]);
- $arr[$value['name']] = $value['value'];
- }
- foreach ($keys as $key) {
- $arr[$key] = '';
- }
- $data = $arr;
- }
- if (empty($data['platform_serial_no']) || empty($data['platform_api_key']) || empty($data['platform_apiclient_key'])) {
- throw new \Exception('配置信息有误1', 1);
- }
- if (\Yii::$app->prod_is_dandianpu()) {
- //供应链系统没必要去判断服务端小程序是否存在
- // if (empty($data['platform_mch_appid'])) {
- // throw new \Exception('配置信息有误2', 1);
- // }
- } else {
- if (empty($data['platform_appid'])) {
- throw new \Exception('配置信息有误3', 1);
- }
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'message' => $e->getMessage(),
- ];
- }
- $cert_pem_file = $this->mch_private_key;
- if (!file_exists($cert_pem_file) || (md5($data['sp_apiclient_key']) != md5_file($cert_pem_file))) {
- file_put_contents($cert_pem_file, $data['sp_apiclient_key']);//platform_apiclient_key
- if (file_exists($this->public_key_path)){
- @unlink($this->public_key_path);
- \Yii::error([__METHOD__]);
- }
- }
- // $this->mch_id = '1508102711'; //sp_mch_id
- // $this->mch_api_key = 'L6u9gjIhOnFC1bzPA4s8JMII6wVCtRCo'; //platform_api_key
- // $this->serial_no = '4956AC2C151A50213DF52A28AB26CEAD092C822F'; //platform_serial_no
- // $this->service_app_id = 'wxb842f5f7c833049e'; //platform_appid
- $this->mch_id = $data['sp_mch_id'];
- $this->mch_api_key = $data['platform_api_key'];
- $this->serial_no = $data['platform_serial_no'];
- $this->service_app_id = $data['platform_appid'];
- $this->public_key = $data['platform_public_key'];
- $this->public_key_id = $data['platform_public_key_id'];
- \Yii::error([$this->mch_id, $this->mch_api_key, $this->serial_no, $this->service_app_id, $cert_pem_file]);
- }
- /**
- * 进件提交
- * @param $contact_info
- * @param $subject_info
- * @param $business_info
- * @param $bank_account_info
- * @param $apply_id
- */
- public function submit($contact_info, $subject_info, $business_info, $bank_account_info, $apply_id = 0,$appid="", $store_id = 0, $settlement_info = []) {
- if ((empty($settlement_info) || empty($settlement_info['qualification_type']))) {
- if ($apply_id) {
- $merchant_info = MerchantInfo::findOne($apply_id);
- $settlement_info = json_decode($merchant_info->settlement_info, true);
- }else{
- $settlement_info = [];
- }
- }
- $handle_res = $this->handleFormat($contact_info, $subject_info, $business_info, $bank_account_info,$appid,$settlement_info);
- $store_id = $store_id ?: get_store_id();
- if ($handle_res['code'] != 0) {
- return $handle_res;
- }
- $t = \Yii::$app->db->beginTransaction();
- // 入表
- if ($apply_id) {
- $merchant_info = MerchantInfo::findOne($apply_id);
- if($merchant_info->state == 'APPLYMENT_STATE_REJECTED'){
- $business_code = $this->getBusinessCode();
- $merchant_info->business_code = $business_code;
- }
- $merchant_info->refuse_desc = '';
- $merchant_info->state = '';
- $merchant_info->status = 0;
- } else {
- $merchant_info = MerchantInfo::findOne(['bind_store_id' => $store_id, 'status' => [0, 2], 'is_delete' => 0]);
- $merchant_info = !empty($merchant_info) ? $merchant_info : new MerchantInfo();
- // todo: man_id
- $merchant_info->saas_user_id = get_saas_user_id();
- $business_code = $this->getBusinessCode();
- $merchant_info->business_code = $business_code;
- $merchant_info->refuse_desc = '';
- $merchant_info->state = '';
- $merchant_info->status = 0;
- }
- $merchant_info->store_id = get_store_id();
- if ($store_id) {
- $merchant_info->bind_store_id = $store_id;
- }
- $merchant_info->contact_info = Json::encode($handle_res['data']['original_data']['contact_info']);
- $merchant_info->subject_info = Json::encode($handle_res['data']['original_data']['subject_info']);
- $merchant_info->business_info = Json::encode($handle_res['data']['original_data']['business_info']);
- $merchant_info->settlement_info = Json::encode($handle_res['data']['original_data']['settlement_info']);
- $merchant_info->bank_account_info = Json::encode($handle_res['data']['original_data']['bank_account_info']);
- if (!$merchant_info->save()) {
- return [
- 'code' => 1,
- 'msg' => $merchant_info->errors[0]
- ];
- }
- // 调接口
- $submitSer = $this->submitSer($merchant_info, $handle_res);
- if ($submitSer['code'] != 0) {
- if($this->submitErrRollback){
- $t->rollback();
- }else{
- $t->commit();
- }
- return $submitSer;
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '提交成功',
- 'data' => $merchant_info,
- ];
- }
- // 调接口
- public function submitSer($merchant_info, $handle_res) {
- $merchant_info_id = $merchant_info->id;
- $data = $handle_res['data']['commit_data'];
- $business_code = $merchant_info->business_code;
- $data['business_code'] = $business_code;
- // 处理图片
- $data = $this->submitPic($data, $handle_res);
- \Yii::warning($data);
- if ($data['code'] != 0) {
- return $data;
- }
- // 调接口
- $result = $this->subApplyment($data, $handle_res);
- \Yii::warning($result);
- if ($result['code'] != 0) {
- $merchant_info->status = 0;
- if (!$merchant_info->save()) {
- return [
- 'code' => 1,
- 'msg' => "数据填充失败"
- ];
- };
- $result['data'] = [
- 'id' => $merchant_info->id,
- 'saas_user_id' => $merchant_info->saas_user_id,
- 'business_code' => $merchant_info->business_code,
- 'contact_info' => Json::decode($merchant_info->contact_info),
- 'subject_info' => Json::decode($merchant_info->subject_info),
- 'business_info' => Json::decode($merchant_info->business_info),
- 'settlement_info' => Json::decode($merchant_info->settlement_info),
- 'bank_account_info' => Json::decode($merchant_info->bank_account_info),
- 'applyment_id' => $merchant_info->applyment_id,
- 'status' => $merchant_info->status
- ];
- return $result;
- }
- $merchant_info->applyment_id = (string)$result['data'];
- $merchant_info->status = 1;
- if (!$merchant_info->save()) {
- \Yii::warning(['<================> 申请进件applyment_id入库失败', $merchant_info->errors]);
- return [
- 'code' => 1,
- 'msg' => 'applyment_id存储异常'
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '提交成功'
- ];
- }
- // 处理图片
- public function submitPic($data, $handle_res) {
- if($data['settlement_info']['qualifications']){
- foreach($data['settlement_info']['qualifications'] as &$item){
- $mediaUpload = $this->mediaUpload($item);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $item = $mediaUpload['data'];
- }
- }
- if($data['settlement_info']['activities_additions']){
- foreach($data['settlement_info']['activities_additions'] as &$item){
- $mediaUpload = $this->mediaUpload($item);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $item = $mediaUpload['data'];
- }
- }
- if($data['contact_info']['business_authorization_letter']){
- $mediaUpload = $this->mediaUpload($data['contact_info']['business_authorization_letter']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['contact_info']['business_authorization_letter'] = $mediaUpload['data'];
- }
- if($data['contact_info']['contact_id_doc_copy']){
- $mediaUpload = $this->mediaUpload($data['contact_info']['contact_id_doc_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['contact_info']['contact_id_doc_copy'] = $mediaUpload['data'];
- }
- if($data['contact_info']['contact_id_doc_copy_back']){
- $mediaUpload = $this->mediaUpload($data['contact_info']['contact_id_doc_copy_back']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['contact_info']['contact_id_doc_copy_back'] = $mediaUpload['data'];
- }
- if ($data['subject_info']['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL || $data['subject_info']['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE) {
- $mediaUpload = $this->mediaUpload($data['subject_info']['business_license_info']['license_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['business_license_info']['license_copy'] = $mediaUpload['data'];
- unset($data['subject_info']['certificate_info']);
- }
- if ($data['subject_info']['subject_type'] == self::SUBJECT_TYPE_INSTITUTIONS || $data['subject_info']['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
- $mediaUpload = $this->mediaUpload($data['subject_info']['certificate_info']['cert_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['certificate_info']['cert_copy'] = $mediaUpload['data'];
- if (!$handle_res['data']['original_data']['subject_info']['organization_info']['is_composite']) {
- $mediaUpload = $this->mediaUpload($data['subject_info']['organization_info']['organization_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['organization_info']['organization_copy'] = $mediaUpload['data'];
- }
- unset($data['subject_info']['business_license_info']);
- } else {
- unset($data['subject_info']['organization_info']);
- }
- if ($data['subject_info']['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
- $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_card_info']['id_card_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['identity_info']['id_card_info']['id_card_copy'] = $mediaUpload['data'];
- $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_card_info']['id_card_national']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['identity_info']['id_card_info']['id_card_national'] = $mediaUpload['data'];
- unset($data['subject_info']['identity_info']['id_doc_info']);
- } else {
- $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_doc_info']['id_doc_copy']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['identity_info']['id_doc_info']['id_doc_copy'] = $mediaUpload['data'];
- $mediaUpload = $this->mediaUpload($data['subject_info']['identity_info']['id_doc_info']['id_doc_copy_back']);
- if ($mediaUpload['code'] !== 0) {
- return $mediaUpload;
- }
- $data['subject_info']['identity_info']['id_doc_info']['id_doc_copy_back'] = $mediaUpload['data'];
- unset($data['subject_info']['identity_info']['id_card_info']);
- }
- return $data;
- }
- /**
- * 数据处理
- * @param $contact_info
- * @param $subject_info
- * @param $business_info
- * @param $bank_account_info
- * @return array
- */
- private function handleFormat($contact_info, $subject_info, $business_info, $bank_account_info, $sub_appid='', $settlement_info = [])
- {
- if (empty($contact_info) || empty($subject_info) || empty($business_info) || empty($bank_account_info)) {
- return [
- 'code' => 1,
- 'msg' => '申请信息缺失'
- ];
- }
- $pay_rate = Option::get('pay_rate', 0, 'saas')['value'];
- if (empty($pay_rate)) {
- $pay_rate = '0.2';
- }
- // 判断超级管理员信息
- if (empty($contact_info['contact_name']) || empty($contact_info['contact_id_number']) || empty($contact_info['mobile_phone'])
- || empty($contact_info['contact_email'])) {
- return [
- 'code' => 1,
- 'msg' => '超级管理员信息有误'
- ];
- }
- // 判断主体资料
- if (!in_array($subject_info['subject_type'], self::$subject_type)) {
- return [
- 'code' => 1,
- 'msg' => '主体类型缺失'
- ];
- }
- // 主体为个体户/企业 营业执照信息必填
- if ($subject_info['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE || $subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
- if (empty($subject_info['business_license_info'])) {
- return [
- 'code' => 1,
- 'msg' => '个体户或企业,营业执照信息必填'
- ];
- }
- if (empty($subject_info['business_license_info']['license_copy']) || empty($subject_info['business_license_info']['license_number'])
- || empty($subject_info['business_license_info']['merchant_name'])
- || empty($subject_info['business_license_info']['legal_person'])) {
- return [
- 'code' => 1,
- 'msg' => '缺少营业执照相关信息'
- ];
- }
- //判断营业执照大小
- $license_copy = $this->saveTempImage($subject_info['business_license_info']['license_copy']);
- if ((floatval(filesize($license_copy)) / 1024 / 1024) > 2) { //判断是否大于2M
- return [
- 'code' => 1,
- 'msg' => '营业执照图片大小过大'
- ];
- }
- //如果为个体
- if (empty($subject_info['business_license_info']['period_begin'])
- && empty($subject_info['business_license_info']['period_end'])) {
- return [
- 'code' => 1,
- 'msg' => '缺少营业执照开始结束日期'
- ];
- }
- if ($subject_info['business_license_info']['period_end'] === '2999-12-31' || $subject_info['business_license_info']['is_long']) {
- $subject_info['business_license_info']['period_end'] = '长期';
- empty($subject_info['business_license_info']['period_begin']) && $subject_info['business_license_info']['period_begin'] = date('Y-m-d');
- }
- }
- // 党政、机关及事业单位/其他组织 登记证书信息必填
- if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INSTITUTIONS || $subject_info['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
- if (empty($subject_info['certificate_info'])) {
- return [
- 'code' => 1,
- 'msg' => '党政、机关及事业单位或其他组织,登记证书信息信息必填'
- ];
- }
- if (empty($subject_info['certificate_info']['cert_copy']) || empty($subject_info['certificate_info']['cert_type']) || empty($subject_info['certificate_info']['cert_number'])
- || empty($subject_info['certificate_info']['merchant_name']) || empty($subject_info['certificate_info']['company_address'])
- || empty($subject_info['certificate_info']['legal_person']) || empty($subject_info['certificate_info']['period_begin']) ) {
- return [
- 'code' => 1,
- 'msg' => '缺少登记证书信息相关信息'
- ];
- }
- if (!$subject_info['certificate_info']['is_long']) {
- if (empty($subject_info['certificate_info']['period_end'])) {
- return [
- 'code' => 1,
- 'msg' => '登记证书证件有效期限结束日期需要填写'
- ];
- }
- } else {
- $subject_info['certificate_info']['period_end'] = '长期';
- }
- if (!in_array($subject_info['certificate_info']['cert_type'], self::$certificate_type)) {
- return [
- 'code' => 1,
- 'msg' => '登记证书类型有误'
- ];
- }
- }
- // 主体为企业/党政、机关及事业单位/其他组织,且证件号码不是18位时必填
- // 若营业执照未三证合一 ,该参数必传
- // 若营业执照三证合一 ,该参数可不传
- 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) {
- if (!$subject_info['organization_info']['is_composite']) {
- if (empty($subject_info['organization_info'])) {
- return [
- 'code' => 1,
- 'msg' => '企业,党政、机关及事业单位或其他组织,组织机构信息必填'
- ];
- }
- if (empty($subject_info['organization_info']['organization_copy']) || empty($subject_info['organization_info']['organization_code'])
- || empty($subject_info['organization_info']['org_period_begin'])) {
- return [
- 'code' => 1,
- 'msg' => '缺少组织结构信息相关信息'
- ];
- }
- if (!$subject_info['organization_info']['is_long']) {
- if (empty($subject_info['organization_info']['org_period_end'])) {
- return [
- 'code' => 1,
- 'msg' => '组织机构证件有效期限结束日期需要填写'
- ];
- }
- } else {
- $subject_info['organization_info']['org_period_end'] = '长期';
- }
- }
- }
- // 经营者/法人身份证件
- if (empty($subject_info['identity_info'])) {
- return [
- 'code' => 1,
- 'msg' => '经营者/法人身份证件需要填写'
- ];
- }
- if ($subject_info['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
- if (empty($subject_info['identity_info']['id_card_info'])) {
- return [
- 'code' => 1,
- 'msg' => '身份证信息需要填写'
- ];
- }
- if (empty($subject_info['identity_info']['id_card_info']['id_card_copy'])
- || empty($subject_info['identity_info']['id_card_info']['id_card_national'])
- || empty($subject_info['identity_info']['id_card_info']['id_card_name'])
- || empty($subject_info['identity_info']['id_card_info']['id_card_number'])
- || empty($subject_info['identity_info']['id_card_info']['id_card_address'])
- || empty($subject_info['identity_info']['id_card_info']['card_period_begin'])) {
- return [
- 'code' => 1,
- 'msg' => '身份证信息填写有误'
- ];
- }
- if (!$subject_info['identity_info']['is_long']) {
- if (empty($subject_info['identity_info']['id_card_info']['card_period_end'])) {
- return [
- 'code' => 1,
- 'msg' => '身份证证件有效期限结束日期需要填写'
- ];
- }
- } else {
- $subject_info['identity_info']['id_card_info']['card_period_end'] = '长期';
- }
- } else {
- if (empty($subject_info['identity_info']['id_doc_info'])) {
- return [
- 'code' => 1,
- 'msg' => '护照或通行证信息需要填写'
- ];
- }
- if (empty($subject_info['identity_info']['id_doc_info']['id_doc_copy'])
- || empty($subject_info['identity_info']['id_doc_info']['id_doc_copy_back'])
- || empty($subject_info['identity_info']['id_doc_info']['id_doc_name'])
- || empty($subject_info['identity_info']['id_doc_info']['id_doc_number'])
- || empty($subject_info['identity_info']['id_doc_info']['doc_period_begin'])) {
- return [
- 'code' => 1,
- 'msg' => '护照或通行证信息填写有误'
- ];
- }
- if (!$subject_info['identity_info']['is_long']) {
- if (empty($subject_info['identity_info']['id_doc_info']['doc_period_end'])) {
- return [
- 'code' => 1,
- 'msg' => '护照或通行证有效期限结束日期需要填写'
- ];
- }
- } else {
- $subject_info['identity_info']['id_doc_info']['doc_period_end'] = '长期';
- }
- }
- if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
- $subject_info['identity_info']['owner'] = null;
- } else {
- $subject_info['identity_info']['owner'] = true;
- }
- // 经营资料
- if (empty($business_info['merchant_shortname']) || empty($business_info['service_phone'])) {
- return [
- 'code' => 1,
- 'msg' => '经营资料填写有误'
- ];
- }
- $business_info['sales_info']['sales_scenes_type'] = ['SALES_SCENES_MINI_PROGRAM']; // 小程序
- if(!empty($sub_appid)){
- $business_info['sales_info']['mini_program_info']['mini_program_sub_appid'] = $sub_appid;//使用商户的小程序信息
- }else{
- $business_info['sales_info']['mini_program_info']['mini_program_appid'] = $this->service_app_id;//使用服务商的小程序信息
- }
- // 结算银行账户信息
- if (!in_array($bank_account_info['bank_account_type'], self::$bank_type)) {
- return [
- 'code' => 1,
- 'msg' => '结算银行账户类型有误'
- ];
- }
- // if (empty($bank_account_info['bank_address_code'])) {
- $bank_account_info['bank_address_code'] = end($bank_account_info["code_list"]);
- // }
- if (empty($bank_account_info['account_name']) || empty($bank_account_info['account_bank']) || empty($bank_account_info['bank_address_code'])
- || empty($bank_account_info['account_number'])) {
- return [
- 'code' => 1,
- 'msg' => '结算银行账户填写有误'
- ];
- }
- if ($bank_account_info['account_bank'] != '其他银行' && isset($bank_account_info['bank_name'])) {
- unset($bank_account_info['bank_name']);
- }
- if ($bank_account_info['account_bank'] == '其他银行' && empty($bank_account_info['bank_name'])) {
- return [
- 'code' => 1,
- 'msg' => '开户银行全称(含支行)填写有误'
- ];
- }
- $settlement_info_default = [
- 'settlement_id' => '719',
- 'qualification_type' => '零售',
- 'activities_id'=>'20191030111cff5b5e',
- ];
- $settlement_info = $settlement_info ? : $settlement_info_default;
- $settlement_info['activities_rate'] = $pay_rate;
- if ($subject_info['subject_type'] == self::SUBJECT_TYPE_INDIVIDUAL) {
- $settlement_info['settlement_id'] = '719';
- } else if ($subject_info['subject_type'] == self::SUBJECT_TYPE_ENTERPRISE) {
- $settlement_info['settlement_id'] = '716';
- } else if ($subject_info['subject_type'] == self::SUBJECT_TYPE_OTHERS) {
- $settlement_info['settlement_id'] = '727';
- } else {
- $settlement_info['settlement_id'] = '716';
- }
- $original_data = [
- 'subject_info' => $subject_info,
- 'contact_info' => $contact_info,
- 'business_info' => $business_info,
- 'settlement_info' => $settlement_info,
- 'bank_account_info' => $bank_account_info
- ];
- $contact_info['contact_name'] = $this->getEncrypt($contact_info['contact_name']);
- $contact_info['contact_id_number'] = $this->getEncrypt($contact_info['contact_id_number']);
- $contact_info['mobile_phone'] = $this->getEncrypt($contact_info['mobile_phone']);
- $contact_info['contact_email'] = $this->getEncrypt($contact_info['contact_email']);
- $contact_info['contact_type'] = $contact_info['contact_type'] ? $contact_info['contact_type'] : 'LEGAL';
- if ($subject_info['identity_info']['id_doc_type'] == self::IDENTIFICATION_TYPE_IDCARD) {
- $subject_info['identity_info']['id_card_info']['id_card_name'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_name']);
- $subject_info['identity_info']['id_card_info']['id_card_number'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_number']);
- $subject_info['identity_info']['id_card_info']['id_card_address'] = $this->getEncrypt($subject_info['identity_info']['id_card_info']['id_card_address']);
- } else {
- $subject_info['identity_info']['id_doc_info']['id_doc_name'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_name']);
- $subject_info['identity_info']['id_doc_info']['id_doc_number'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_number']);
- $subject_info['identity_info']['id_doc_info']['id_doc_address'] = $this->getEncrypt($subject_info['identity_info']['id_doc_info']['id_doc_address']);
- }
- $bank_account_info['account_name'] = $this->getEncrypt($bank_account_info['account_name']);
- $bank_account_info['account_number'] = $this->getEncrypt($bank_account_info['account_number']);
- $commit_data = [
- 'subject_info' => $subject_info,
- 'contact_info' => $contact_info,
- 'business_info' => $business_info,
- 'settlement_info' => $settlement_info,
- 'bank_account_info' => $bank_account_info
- ];
- unset($commit_data['subject_info']['certificate_info']['is_long']);
- unset($commit_data['subject_info']['organization_info']['is_composite']);
- unset($commit_data['subject_info']['organization_info']['is_long']);
- unset($commit_data['subject_info']['identity_info']['is_long']);
- unset($commit_data['bank_account_info']['code_list']);
- unset($commit_data['bank_account_info']['address_list']);
- unset($commit_data['business_info']['sales_info']['mini_program_appid']);
- return [
- 'code' => 0,
- 'data' => [
- 'original_data' => $original_data,
- 'commit_data' => $commit_data
- ]
- ];
- }
- /**
- * 商户进件
- * @param $data
- * @return array
- * @throws Exception
- */
- public function subApplyment($data) {
- $url = $this->applyment_url;
- // 获取支付平台证书编码(也可以用接口中返回的serial_no 来源:https://api.mch.weixin.qq.com/v3/certificates)
- if($this->public_key){
- $serial_no = $this->public_key_id;
- }else{
- $serial_no = $this->parseSerialNo($this->getCertFicates());
- }
- $bodyData = json_encode($data);
- // 获取认证信息
- $authorization = $this->getAuthorization($url, 'POST', $bodyData);
- $header = [
- 'Content-Type:application/json',
- 'Accept:application/json',
- 'User-Agent:*/*',
- 'Authorization:' . $authorization,
- 'Wechatpay-Serial:' . $serial_no
- ];
- $json = $this->getCurl('POST', $url, $bodyData, $header);
- $data = json_decode($json, true);
- if (isset($data['code']) && isset($data['message'])) {
- return [
- 'code' => 1,
- 'msg' => $data['message']
- ];
- }
- if (empty($applyment_id = $data['applyment_id'])) {
- return [
- 'code' => 1,
- 'msg' => '返回错误'
- ];
- }
- return ['code' => 0, 'data' => $applyment_id];
- }
- /**
- * 进件查询
- */
- public function queryApplyment($applyment_id, $code = 0)
- {
- $url = $this->query_applyment_url . $applyment_id;
- if(empty($applyment_id) && $code){
- $url = $this->query_applyment_url_code . $code;
- \Yii::error([__METHOD__, $url]);
- }
- if($this->public_key){
- $serial_no = $this->public_key_id;
- }else{
- $serial_no = $this->parseSerialNo($this->getCertFicates());
- }
- // 获取认证信息
- $authorization = $this->getAuthorization($url);
- $header = [
- 'Content-Type:application/json',
- 'Accept:application/json',
- 'User-Agent:*/*',
- 'Authorization:' . $authorization,
- 'Wechatpay-Serial:' . $serial_no
- ];
- $json = $this->getCurl('GET', $url, '', $header);
- $data = json_decode($json, true);
- if (isset($data['code']) && isset($data['message'])) {
- return [
- 'code' => 1,
- 'msg' => '请求错误:' . $data['message']
- ];
- }
- return $data;
- }
- /**
- * 上传文件
- */
- public function mediaUpload($file_path)
- {
- $file = file_get_contents($file_path);
- $size = strlen($file)/1024/1024;
- \Yii::error([__METHOD__, $size]);
- if ($size >= 2) {
- return [
- 'code' => 1,
- 'msg' => '图片大小不能超过2M',
- ];
- }
- $path_info = pathinfo($file_path);
- $file_name = $path_info['basename'];
- $url = $this->upload_image_url;
- if (\function_exists('exif_imagetype')) {
- $type = exif_imagetype($file_path);
- } else {
- list($width, $height, $type, $attr) = getimagesize($file_path);
- }
- if($this->public_key){
- $serial_no = $this->public_key_id;
- }else{
- $serial_no = $this->parseSerialNo($this->getCertFicates());
- }
- $mime_type = image_type_to_mime_type($type);
- $meta = [
- "filename" => $file_name,
- "sha256" => hash_file('sha256', $file_path)
- ];
- // 获取认证信息
- $authorization = $this->getAuthorization($url, 'POST', json_encode($meta));
- $boundary = uniqid();
- $header = [
- 'Accept:application/json',
- 'User-Agent:*/*',
- 'Content-Type:multipart/form-data;boundary=' . $boundary,
- 'Authorization:' . $authorization,
- 'Wechatpay-Serial:' . $serial_no
- ];
- // 组合参数
- $boundaryStr = "--{$boundary}\r\n";
- $out = $boundaryStr;
- $out .= 'Content-Disposition: form-data; name="meta"' . "\r\n";
- $out .= 'Content-Type: multipart/form-data' . "\r\n";
- $out .= "\r\n";
- $out .= json_encode($meta) . "\r\n";
- $out .= $boundaryStr;
- $out .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n";
- $out .= 'Content-Type: ' . $mime_type . ';' . "\r\n";
- $out .= "\r\n";
- $out .= $file . "\r\n";
- $out .= "--{$boundary}--\r\n";
- $json = $this->getCurl('POST', $url, $out, $header);
- $data = json_decode($json, true);
- if (isset($data['code']) && isset($data['message'])) {
- return [
- 'code' => 1,
- 'msg' => $data['message']
- ];
- }
- if (empty($media_id = $data['media_id'])) {
- return [
- 'code' => 1,
- 'msg' => '返回错误'
- ];
- }
- return ['code' => 0, 'data' => $media_id];
- }
- /**
- * 获取微信支付平台证书
- */
- public function certFicates()
- {
- $url = $this->certificates_url;
- // 获取认证信息
- $authorization = $this->getAuthorization($url);
- $header = [
- 'Content-Type:application/json',
- 'Accept:application/json',
- 'User-Agent:' . $this->mch_id,
- 'Authorization:' . $authorization
- ];
- $json = $this->getCurl('GET', $url, '', $header);
- $data = json_decode($json, true);
- if (isset($data['code']) && isset($data['message'])) {
- return [
- 'code' => 1,
- 'msg' => $data['message']
- ];
- }
- if (empty($cfdata = $data['data'][0])) {
- return [
- 'code' => 1,
- 'msg' => '返回错误'
- ];
- }
- return $cfdata;
- }
- /**
- * 获取认证信息
- * @param string $url
- * @param string $http_method
- * @param string $body
- * @return string
- * @throws Exception
- */
- public function getAuthorization($url, $http_method = 'GET', $body = '')
- {
- if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {
- throw new \Exception("当前PHP环境不支持SHA256withRSA");
- }
- //私钥地址
- $mch_private_key = $this->mch_private_key;
- //商户号
- $merchant_id = $this->mch_id;
- //当前时间戳
- $timestamp = time();
- //随机字符串
- $nonce = $this->getNonceStr();
- //证书编号
- $serial_no = $this->serial_no;
- $url_parts = parse_url($url);
- $canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
- $message = $http_method . "\n" .
- $canonical_url . "\n" .
- $timestamp . "\n" .
- $nonce . "\n" .
- $body . "\n";
- openssl_sign($message, $raw_sign, \openssl_get_privatekey(\file_get_contents($mch_private_key)), 'sha256WithRSAEncryption');
- $sign = base64_encode($raw_sign);
- $schema = 'WECHATPAY2-SHA256-RSA2048';
- $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',
- $merchant_id, $nonce, $timestamp, $serial_no, $sign);
- return $schema . ' ' . $token;
- }
- /**
- * 敏感字符加密
- * @param $str
- * @return string
- * @throws Exception
- */
- private function getEncrypt($str)
- {
- static $content;
- if (empty($content)) {
- $content = $this->getCertFicates();
- }
- // 去除内容中的空格
- $str = preg_replace("/\s| /", '', $str);
- $encrypted = '';
- if (openssl_public_encrypt($str, $encrypted, $content, OPENSSL_PKCS1_OAEP_PADDING)) {
- //base64编码
- $sign = base64_encode($encrypted);
- } else {
- throw new \Exception('encrypt failed');
- }
- return $sign;
- }
- /**
- * 获取支付平台证书
- * @return false|string
- */
- public function getCertFicates()
- {
- if($this->public_key){
- return $this->public_key;
- }
- $public_key_path = $this->public_key_path;
- if (!file_exists($public_key_path)) {
- \Yii::error([__METHOD__]);
- $cfData = $this->certFicates();
- $content = $this->decryptToString($cfData['encrypt_certificate']['associated_data'], $cfData['encrypt_certificate']['nonce'], $cfData['encrypt_certificate']['ciphertext'], $this->mch_api_key);
- file_put_contents($public_key_path, $content);
- }
- else {
- $content = file_get_contents($public_key_path);
- }
- return $content;
- }
- /**
- * 业务编号
- * @return string
- */
- public function getBusinessCode()
- {
- return date('Ymd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
- }
- /**
- * 随机字符串
- * @param int $length
- * @return string
- */
- private function getNonceStr($length = 16)
- {
- // 密码字符集,可任意添加你需要的字符
- $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- $str = "";
- for ($i = 0; $i < $length; $i++) {
- $str .= $chars[mt_rand(0, strlen($chars) - 1)];
- }
- return $str;
- }
- /**
- * @param string $method
- * @param string $url
- * @param array|string $data
- * @param array $headers
- * @param int $timeout
- * @return bool|string
- */
- private function getCurl($method = 'GET', $url, $data, $headers = [], $timeout = 30)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSLVERSION, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
- if (!empty($headers)) {
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- }
- if ($method == 'POST') {
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- $result = curl_exec($curl);
- curl_close($curl);
- return $result;
- }
- /**
- * Decrypt AEAD_AES_256_GCM ciphertext(官方案例-已改造)
- *
- * @param string $associatedData AES GCM additional authentication data
- * @param string $nonceStr AES GCM nonce
- * @param string $ciphertext AES GCM cipher text
- *
- * @return string|bool Decrypted string on success or FALSE on failure
- */
- private function decryptToString($associatedData, $nonceStr, $ciphertext, $aesKey)
- {
- $auth_tag_length_byte = 16;
- $ciphertext = \base64_decode($ciphertext);
- if (strlen($ciphertext) <= $auth_tag_length_byte) {
- return false;
- }
- // ext-sodium (default installed on >= PHP 7.2)
- if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&
- \sodium_crypto_aead_aes256gcm_is_available()) {
- return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- }
- // ext-libsodium (need install libsodium-php 1.x via pecl)
- if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&
- \Sodium\crypto_aead_aes256gcm_is_available()) {
- return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);
- }
- // openssl (PHP >= 7.1 support AEAD)
- if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {
- $ctext = substr($ciphertext, 0, -$auth_tag_length_byte);
- $authTag = substr($ciphertext, -$auth_tag_length_byte);
- return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr,
- $authTag, $associatedData);
- }
- throw new \Exception('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');
- }
- /**
- * 获取证书编号(官方案例-已改造)
- * @param $certificate
- * @return string
- */
- private function parseSerialNo($certificate)
- {
- $info = \openssl_x509_parse($certificate);
- if (!isset($info['serialNumber']) && !isset($info['serialNumberHex'])) {
- throw new \InvalidArgumentException('证书格式错误');
- }
- $serialNo = '';
- // PHP 7.0+ provides serialNumberHex field
- if (isset($info['serialNumberHex'])) {
- $serialNo = $info['serialNumberHex'];
- }
- else {
- // PHP use i2s_ASN1_INTEGER in openssl to convert serial number to string,
- // i2s_ASN1_INTEGER may produce decimal or hexadecimal format,
- // depending on the version of openssl and length of data.
- if (\strtolower(\substr($info['serialNumber'], 0, 2)) == '0x') { // HEX format
- $serialNo = \substr($info['serialNumber'], 2);
- }
- else { // DEC format
- $value = $info['serialNumber'];
- $hexvalues = ['0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
- while ($value != '0') {
- $serialNo = $hexvalues[\bcmod($value, '16')] . $serialNo;
- $value = \bcdiv($value, '16', 0);
- }
- }
- }
- return \strtoupper($serialNo);
- }
- //获取网络图片到临时目录
- 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;
- }
- }
|