CheckReqDataAndEncrypt.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace LdzfPay\Api\Common;
  3. use LdzfPay\Api\RegexUtil;
  4. /**
  5. * 请求参数校验 加解密工具类
  6. */
  7. Class CheckReqDataAndEncrypt{
  8. /**
  9. * @param $map HashMap 参数校验
  10. */
  11. public static function doCheck($map) {
  12. $log = new Logger();
  13. $service = $map->get ("service");
  14. $serviceRuleMap = regexUtil::$serviceRule;
  15. //service对应的必填字段校验
  16. $paramStr = StringUtil::trim($serviceRuleMap[$service]);
  17. if($paramStr!=null && strlen($paramStr)>0) {
  18. //该service对应的必传字段数组
  19. $arr = explode(",",$paramStr);
  20. foreach ($arr as $key) {
  21. if (""==$map->get($key) || null==$map->get($key)) {
  22. $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 不能为空");
  23. $log->logInfo("--------------------log end---------------------");
  24. die("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 不能为空");
  25. }
  26. }
  27. // 历史订单查询接口mer_order_info_query trade_no和order_id这两个字段必填字段传一个即可
  28. if($service == "mer_order_info_query"){
  29. $order_id = $map->get ("order_id");
  30. $trade_no = $map->get ("trade_no");
  31. if((""==$order_id||null==$order_id) && (""==$trade_no||null==$trade_no)){
  32. $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 "."order_id 和 trade_no" ." 不能同时为空");
  33. $log->logInfo("--------------------log end---------------------");
  34. die("[UMF SDK]请求接口 service = ".$service." 的参数值 "."order_id 和 trade_no" ." 不能同时为空");
  35. }
  36. }
  37. }
  38. $ruleMap = regexUtil::$reqRule;
  39. $ruleMapKeys = array_keys($ruleMap);
  40. $mapKeys = $map->keys();
  41. foreach($mapKeys as $key){
  42. $value = StringUtil::trim($map->get($key));
  43. if(!self::containsValue($key,$ruleMapKeys)){ // 字段不需要正则校验情形
  44. continue;
  45. }
  46. else if(self::containsValue($key,$ruleMapKeys)){ // 字段需要正则校验情形
  47. $regex = $ruleMap[$key];
  48. if(""!=$value && null!=$value && !empty($regex)){
  49. if(preg_match($regex, $value)){
  50. $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验通过");
  51. } else {
  52. $log->logInfo("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验未通过,对应的正则表达式为 regex = " . $regex);
  53. $log->logInfo("--------------------log end---------------------");
  54. die("[UMF SDK]请求接口 service = ".$service." 的参数值 ".$key ." 正则校验未通过,对应的正则表达式为 regex = " . $regex);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. /*
  61. * $arr中是否包含$value
  62. */
  63. public static function containsValue($value,$arr) {
  64. while ($curValue = current($arr)) {
  65. if ($curValue == $value) {
  66. return true;
  67. }
  68. next($arr);
  69. }
  70. return false;
  71. }
  72. /**
  73. * 敏感字段加密
  74. */
  75. public static function doEncrypt($map) {
  76. $log = new Logger ();
  77. $chkKeys = array(
  78. "card_id",
  79. "valid_date",
  80. "cvv2",
  81. "pass_wd",
  82. "identity_code",
  83. "card_holder",
  84. "recv_account",
  85. "recv_user_name",
  86. "identity_holder",
  87. "identityCode",
  88. "cardHolder",
  89. "mer_cust_name",
  90. "account_name",
  91. "bank_account",
  92. "endDate",
  93. );
  94. foreach($chkKeys as $key){
  95. $value = StringUtil::trim($map->get($key));
  96. if(""==$value || null==$value){
  97. continue;
  98. }
  99. $log->logInfo("[UMF SDK]请求接口 service = ".$map->get("service")." 的参数值 ".$key ." 是敏感字段需要进行RSA加密");
  100. $value=iconv("UTF-8", "GBK", $value);
  101. $value = RSACryptUtil::encrypt ( $value );
  102. $map->put($key,$value);
  103. }
  104. return $map;
  105. }
  106. }