| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace LdzfPay\Api\Common;
- use LdzfPay\Api\RegexUtil;
- /**
- * 请求参数校验 加解密工具类
- */
- Class CheckReqDataAndEncrypt{
- /**
- * @param $map HashMap 参数校验
- */
- public static function doCheck($map) {
- $log = new Logger();
- $service = $map->get ("service");
- $serviceRuleMap = regexUtil::$serviceRule;
- //service对应的必填字段校验
- $paramStr = StringUtil::trim($serviceRuleMap[$service]);
- if($paramStr!=null && strlen($paramStr)>0) {
- //该service对应的必传字段数组
- $arr = explode(",",$paramStr);
- foreach ($arr as $key) {
- if (""==$map->get($key) || null==$map->get($key)) {
- $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 不能为空");
- $log->logInfo("--------------------log end---------------------");
- die("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 不能为空");
- }
- }
- // 历史订单查询接口mer_order_info_query trade_no和order_id这两个字段必填字段传一个即可
- if($service == "mer_order_info_query"){
- $order_id = $map->get ("order_id");
- $trade_no = $map->get ("trade_no");
- if((""==$order_id||null==$order_id) && (""==$trade_no||null==$trade_no)){
- $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 "."order_id 和 trade_no" ." 不能同时为空");
- $log->logInfo("--------------------log end---------------------");
- die("[UMF SDK]请求接口 service = ".$service." 的参数值 "."order_id 和 trade_no" ." 不能同时为空");
- }
- }
- }
- $ruleMap = regexUtil::$reqRule;
- $ruleMapKeys = array_keys($ruleMap);
- $mapKeys = $map->keys();
- foreach($mapKeys as $key){
- $value = StringUtil::trim($map->get($key));
- if(!self::containsValue($key,$ruleMapKeys)){ // 字段不需要正则校验情形
- continue;
- }
- else if(self::containsValue($key,$ruleMapKeys)){ // 字段需要正则校验情形
- $regex = $ruleMap[$key];
- if(""!=$value && null!=$value && !empty($regex)){
- if(preg_match($regex, $value)){
- $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验通过");
- } else {
- $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验未通过,对应的正则表达式为 regex = " . $regex);
- $log->logInfo("--------------------log end---------------------");
- die("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验未通过,对应的正则表达式为 regex = " . $regex);
- }
- }
- }
- }
- }
- /*
- * $arr中是否包含$value
- */
- public static function containsValue($value,$arr) {
- while ($curValue = current($arr)) {
- if ($curValue == $value) {
- return true;
- }
- next($arr);
- }
- return false;
- }
- /**
- * 敏感字段加密
- */
- public static function doEncrypt($map) {
- $log = new Logger ();
- $chkKeys = array(
- "card_id",
- "valid_date",
- "cvv2",
- "pass_wd",
- "identity_code",
- "card_holder",
- "recv_account",
- "recv_user_name",
- "identity_holder",
- "identityCode",
- "cardHolder",
- "mer_cust_name",
- "account_name",
- "bank_account",
- "endDate",
- );
- foreach($chkKeys as $key){
- $value = StringUtil::trim($map->get($key));
- if(""==$value || null==$value){
- continue;
- }
- $log->logInfo("[UMF SDK]请求接口 service = ".$map->get("service")." 的参数值 ".$key ." 是敏感字段需要进行RSA加密");
- $value=iconv("UTF-8", "GBK", $value);
- $value = RSACryptUtil::encrypt ( $value );
- $map->put($key,$value);
- }
- return $map;
- }
- }
|