RequestHandler.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 请求类
  4. * ============================================================================
  5. * api说明:
  6. * init(),初始化函数,默认给一些参数赋值,如cmdno,date等。
  7. * getGateURL()/setGateURL(),获取/设置入口地址,不包含参数值
  8. * getKey()/setKey(),获取/设置密钥
  9. * getParameter()/setParameter(),获取/设置参数值
  10. * getAllParameters(),获取所有参数
  11. * getRequestURL(),获取带参数的请求URL
  12. * getDebugInfo(),获取debug信息
  13. *
  14. * ============================================================================
  15. *
  16. */
  17. class RequestHandler {
  18. /** 网关url地址 */
  19. var $gateUrl;
  20. /** 密钥 */
  21. var $key;
  22. /** 请求的参数 */
  23. var $parameters;
  24. /** debug信息 */
  25. var $debugInfo;
  26. function __construct() {
  27. $this->RequestHandler();
  28. }
  29. function RequestHandler() {
  30. $this->gateUrl = "'https://pay.swiftpass.cn/pay/gateway";
  31. $this->key = "";
  32. $this->parameters = array();
  33. $this->debugInfo = "";
  34. }
  35. /**
  36. *初始化函数。
  37. */
  38. function init() {
  39. //nothing to do
  40. }
  41. /**
  42. *获取入口地址,不包含参数值
  43. */
  44. function getGateURL() {
  45. return $this->gateUrl;
  46. }
  47. /**
  48. *设置入口地址,不包含参数值
  49. */
  50. function setGateURL($gateUrl) {
  51. $this->gateUrl = $gateUrl;
  52. }
  53. /**
  54. *获取密钥
  55. */
  56. function getKey() {
  57. return $this->key;
  58. }
  59. /**
  60. *设置密钥
  61. */
  62. function setKey($key) {
  63. $this->key = $key;
  64. }
  65. /**
  66. *获取参数值
  67. */
  68. function getParameter($parameter) {
  69. return isset($this->parameters[$parameter])?$this->parameters[$parameter]:'';
  70. }
  71. /**
  72. *设置参数值
  73. */
  74. function setParameter($parameter, $parameterValue) {
  75. $this->parameters[$parameter] = $parameterValue;
  76. }
  77. /**
  78. * 一次性设置参数
  79. */
  80. function setReqParams($post,$filterField=null){
  81. if($filterField !== null){
  82. forEach($filterField as $k=>$v){
  83. unset($post[$v]);
  84. }
  85. }
  86. //判断是否存在空值,空值不提交
  87. forEach($post as $k=>$v){
  88. if(empty($v)){
  89. unset($post[$k]);
  90. }
  91. }
  92. $this->parameters = $post;
  93. }
  94. /**
  95. *获取所有请求的参数
  96. *@return array
  97. */
  98. function getAllParameters() {
  99. return $this->parameters;
  100. }
  101. /**
  102. *获取带参数的请求URL
  103. */
  104. function getRequestURL() {
  105. $this->createSign();
  106. $reqPar = "";
  107. ksort($this->parameters);
  108. foreach($this->parameters as $k => $v) {
  109. $reqPar .= $k . "=" . urlencode($v) . "&";
  110. }
  111. //去掉最后一个&
  112. $reqPar = substr($reqPar, 0, strlen($reqPar)-1);
  113. $requestURL = $this->getGateURL() . "?" . $reqPar;
  114. return $requestURL;
  115. }
  116. /**
  117. *获取debug信息
  118. */
  119. function getDebugInfo() {
  120. return $this->debugInfo;
  121. }
  122. /**
  123. *创建md5摘要,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
  124. */
  125. function createSign() {
  126. $signPars = "";
  127. ksort($this->parameters);
  128. foreach($this->parameters as $k => $v) {
  129. if("" != $v && "sign" != $k) {
  130. $signPars .= $k . "=" . $v . "&";
  131. }
  132. }
  133. $signPars .= "key=" . $this->getKey();
  134. $sign = strtoupper(md5($signPars));
  135. $this->setParameter("sign", $sign);
  136. //debug信息
  137. $this->_setDebugInfo($signPars . " => sign:" . $sign);
  138. }
  139. /**
  140. *设置debug信息
  141. */
  142. function _setDebugInfo($debugInfo) {
  143. $this->debugInfo = $debugInfo;
  144. }
  145. }
  146. ?>