Sumpay.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. include 'HTTPRequester.php';
  3. include 'Security.php';
  4. use think\Request;
  5. class Sumpay
  6. {
  7. private $url = 'https://api.top-ws.com/xjapigateway/form/gateway.htm';
  8. private $merchant_no = ''; //商户号
  9. private $md5Key = ""; //密钥
  10. public function pay($title, $order_no, $pay_amount,$return_url='',$notify_url=''){
  11. $service = "payonline.order.apply.h5"; // 请求的方法 service 值,根据实际值填写
  12. // 请求的业务参数,根据实际填写
  13. $goodsInfo = array();
  14. $goodsInfo['goodsName'] = $title;
  15. $goodsInfo['goodsAmount'] = $pay_amount;
  16. $goodsInfo['goodsNum'] = 1;
  17. $bizParam = array(
  18. "merNo" => $this->merchant_no,
  19. "bankCode" => 'WXPAY',
  20. "orderNo" => $order_no,
  21. "orderAmount" => $pay_amount,
  22. "notifyUrl" => $notify_url,
  23. "returnUrl" => $return_url,
  24. "goodsInfo" => $goodsInfo,
  25. );
  26. // 取业务参数的json串
  27. $serviceJson = json_encode($bizParam);
  28. // 对json进行AES加密
  29. $serviceParam = Security::encrypt($serviceJson, $this->md5Key);
  30. // 对AES后的参数 serviceParam 进行 md5 加签, 注意输出格式必须是字节形式
  31. $plainText = $serviceParam . $this->md5Key;
  32. $md5Res = md5($plainText, true);
  33. // 对md5后的结果进行 base64, 作为签名
  34. $sign = base64_encode($md5Res);
  35. // 作为post请求body的参数
  36. $param = array(
  37. "serviceParam" => $serviceParam,
  38. "sign" => $sign
  39. );
  40. $date = date("yymdHis");
  41. // 设置请求头
  42. $header = array(
  43. "requestId: " . $date . $this->uuid(),
  44. "timestamp: " . $date, // yyyyMMddHHmmss
  45. "version: " . "1.0",
  46. "service: " . $service,
  47. "appId: " . $this->merchant_no
  48. );
  49. $postRes = HTTPRequester::HTTPPost($this->url, $param, $header);
  50. $resJson = json_decode($postRes);
  51. $resServiceParam = $resJson->serviceParam;
  52. $resSign = $resJson->sign;
  53. // 校验响应签名
  54. $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
  55. // 签名通过方可认为请求正常
  56. if ($checkSign) {
  57. $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
  58. $list = json_decode($resServiceJson, true);
  59. return $list;
  60. }
  61. else{
  62. return false;
  63. }
  64. }
  65. public function notifyVerify($data){
  66. $resServiceParam = $data['serviceParam'];
  67. $resSign = $data['sign'];
  68. // 校验响应签名
  69. $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
  70. // 签名通过方可认为请求正常
  71. if ($checkSign) {
  72. $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
  73. $list = json_decode($resServiceJson, true);
  74. return $list;
  75. }
  76. else{
  77. return false;
  78. }
  79. }
  80. public function queryOrder($order_no)
  81. {
  82. $service = "payonline.order.query"; // 请求的方法 service 值,根据实际值填写
  83. // 请求的业务参数,根据实际填写
  84. $bizParam = array(
  85. "merNo" => $this->merchant_no,
  86. "orderNo" => $order_no,
  87. "transType" => "01",
  88. );
  89. // 取业务参数的json串
  90. $serviceJson = json_encode($bizParam);
  91. // 对json进行AES加密
  92. $serviceParam = Security::encrypt($serviceJson, $this->md5Key);
  93. // 对AES后的参数 serviceParam 进行 md5 加签, 注意输出格式必须是字节形式
  94. $plainText = $serviceParam . $this->md5Key;
  95. $md5Res = md5($plainText, true);
  96. // 对md5后的结果进行 base64, 作为签名
  97. $sign = base64_encode($md5Res);
  98. // 作为post请求body的参数
  99. $param = array(
  100. "serviceParam" => $serviceParam,
  101. "sign" => $sign
  102. );
  103. $date = date("yymdHis");
  104. // 设置请求头
  105. $header = array(
  106. "requestId: " . $date . $this->uuid(),
  107. "timestamp: " . $date, // yyyyMMddHHmmss
  108. "version: " . "1.0",
  109. "service: " . $service,
  110. "appId: " . $this->merchant_no
  111. );
  112. $postRes = HTTPRequester::HTTPPost($this->url, $param, $header);
  113. $resJson = json_decode($postRes);
  114. $resServiceParam = $resJson->serviceParam;
  115. $resSign = $resJson->sign;
  116. // 校验响应签名
  117. $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
  118. // 签名通过方可认为请求正常
  119. if ($checkSign) {
  120. $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
  121. return $resServiceJson;
  122. }
  123. else{
  124. return false;
  125. }
  126. }
  127. function uuid($prefix = '')
  128. {
  129. $chars = md5(uniqid(mt_rand(), true));
  130. $uuid = substr($chars,0,8);
  131. $uuid .= substr($chars,8,4);
  132. $uuid .= substr($chars,12,4);
  133. $uuid .= substr($chars,16,1);
  134. return $prefix . $uuid;
  135. }
  136. function checkSign($serviceParam, $md5Key, $sign) {
  137. $md5Res = md5($serviceParam . $md5Key, true);
  138. return base64_encode($md5Res) === $sign;
  139. }
  140. }