AlipaySubmit.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Components;
  3. use DOMDocument;
  4. use Log;
  5. /**
  6. * Class AlipaySubmit
  7. *
  8. * @author wz812180
  9. *
  10. * @package App\Components
  11. */
  12. class AlipaySubmit
  13. {
  14. var $alipay_gateway_new = 'https://mapi.alipay.com/gateway.do?'; // 支付宝网关地址(新)
  15. var $sign_type = "MD5"; // 加密方式:MD5/RSA
  16. var $partner = "";
  17. var $md5_key = "";
  18. var $private_key = "";
  19. function __construct($sign_type, $partner, $md5_key, $private_key)
  20. {
  21. $this->sign_type = $sign_type;
  22. $this->partner = $partner;
  23. $this->md5_key = $md5_key;
  24. $this->private_key = $private_key;
  25. }
  26. /**
  27. * 生成签名结果
  28. *
  29. * @param array $para_sort 已排序要签名的数组
  30. *
  31. * @return string
  32. */
  33. function buildRequestMysign($para_sort)
  34. {
  35. // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  36. $prestr = $this->createLinkString($para_sort);
  37. switch(strtoupper(trim($this->sign_type))){
  38. case "MD5" :
  39. $mysign = $this->md5Sign($prestr, $this->md5_key);
  40. break;
  41. case "RSA" :
  42. $mysign = $this->rsaSign($prestr, $this->private_key);
  43. break;
  44. default :
  45. $mysign = "";
  46. }
  47. return $mysign;
  48. }
  49. /**
  50. * 生成要请求给支付宝的参数数组
  51. *
  52. * @param array $para_temp 请求前的参数数组
  53. *
  54. * @return array
  55. */
  56. function buildRequestPara($para_temp)
  57. {
  58. // 除去待签名参数数组中的空值和签名参数
  59. $para_filter = $this->paraFilter($para_temp);
  60. // 对待签名参数数组排序
  61. $para_sort = $this->argSort($para_filter);
  62. // 生成签名结果
  63. $mysign = $this->buildRequestMysign($para_sort);
  64. // 签名结果与签名方式加入请求提交参数组中
  65. $para_sort['sign'] = $mysign;
  66. $para_sort['sign_type'] = strtoupper(trim($this->sign_type));
  67. return $para_sort;
  68. }
  69. /**
  70. * 生成要请求给支付宝的参数数组
  71. *
  72. * @param array $para_temp 请求前的参数数组
  73. *
  74. * @return string
  75. */
  76. function buildRequestParaToString($para_temp)
  77. {
  78. // 待请求参数数组
  79. $para = $this->buildRequestPara($para_temp);
  80. // 把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对字符串做urlencode编码
  81. $request_data = $this->createLinkStringUrlEncode($para);
  82. return $request_data;
  83. }
  84. /**
  85. * 建立请求,以表单HTML形式构造(默认)
  86. *
  87. * @param array $para_temp 请求参数数组
  88. * @param string $method 提交方式。两个值可选:post、get
  89. * @param string $button_name 确认按钮显示文字
  90. *
  91. * @return string
  92. */
  93. public function buildRequestForm($para_temp, $method, $button_name)
  94. {
  95. // 待请求参数数组
  96. $para = $this->buildRequestPara($para_temp);
  97. $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->alipay_gateway_new."_input_charset=utf-8' method='".$method."'>";
  98. while(list ($key, $val) = each($para)){
  99. $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
  100. }
  101. // submit按钮控件请不要含有name属性
  102. $sHtml = $sHtml."<input type='submit' value='".$button_name."' style='display:none;'></form>";
  103. $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>";
  104. return $sHtml;
  105. }
  106. /**
  107. * 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
  108. *
  109. * @return string
  110. */
  111. function query_timestamp()
  112. {
  113. $url = $this->alipay_gateway_new."service=query_timestamp&partner=".trim(strtolower($this->partner))."&_input_charset=utf-8";
  114. $doc = new DOMDocument();
  115. $doc->load($url);
  116. $itemEncrypt_key = $doc->getElementsByTagName("encrypt_key");
  117. $encrypt_key = $itemEncrypt_key->item(0)->nodeValue;
  118. return $encrypt_key;
  119. }
  120. /**
  121. * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  122. *
  123. * @param array $para
  124. *
  125. * @return bool|string
  126. */
  127. function createLinkString($para)
  128. {
  129. $arg = "";
  130. while(list ($key, $val) = each($para)){
  131. $arg .= $key."=".$val."&";
  132. }
  133. // 去掉最后一个&字符
  134. $arg = substr($arg, 0, count($arg)-2);
  135. // 如果存在转义字符,那么去掉转义
  136. if(get_magic_quotes_gpc()){
  137. $arg = stripslashes($arg);
  138. }
  139. return $arg;
  140. }
  141. /**
  142. * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对字符串做urlencode编码
  143. *
  144. * @param array $para 需要拼接的数组
  145. *
  146. * @return bool|string
  147. */
  148. function createLinkStringUrlEncode($para)
  149. {
  150. $arg = "";
  151. while(list ($key, $val) = each($para)){
  152. $arg .= $key."=".urlencode($val)."&";
  153. }
  154. // 去掉最后一个&字符
  155. $arg = substr($arg, 0, count($arg)-2);
  156. // 如果存在转义字符,那么去掉转义
  157. if(get_magic_quotes_gpc()){
  158. $arg = stripslashes($arg);
  159. }
  160. return $arg;
  161. }
  162. /**
  163. * RSA签名
  164. *
  165. * @param string $data 待签名数据
  166. * @param string $private_key 商户私钥字符串
  167. *
  168. * @return string
  169. */
  170. function rsaSign($data, $private_key)
  171. {
  172. //以下为了初始化私钥,保证在您填写私钥时不管是带格式还是不带格式都可以通过验证。
  173. $private_key = str_replace("-----BEGIN RSA PRIVATE KEY-----", "", $private_key);
  174. $private_key = str_replace("-----END RSA PRIVATE KEY-----", "", $private_key);
  175. $private_key = str_replace("\n", "", $private_key);
  176. $private_key = "-----BEGIN RSA PRIVATE KEY-----".PHP_EOL.wordwrap($private_key, 64, "\n", TRUE).PHP_EOL."-----END RSA PRIVATE KEY-----";
  177. $res = openssl_get_privatekey($private_key);
  178. if(!$res){
  179. Log::error("私钥格式不正确");
  180. exit();
  181. }
  182. openssl_sign($data, $sign, $res);
  183. openssl_free_key($res);
  184. $sign = base64_encode($sign); // base64编码
  185. return $sign;
  186. }
  187. /**
  188. * 签名字符串
  189. *
  190. * @param string $prestr 需要签名的字符串
  191. * @param string $key 私钥
  192. *
  193. * @return string
  194. */
  195. function md5Sign($prestr, $key)
  196. {
  197. return md5($prestr.$key);
  198. }
  199. /**
  200. * 除去数组中的空值和签名参数
  201. *
  202. * @param array $para 签名参数组
  203. *
  204. * @return array
  205. */
  206. function paraFilter($para)
  207. {
  208. $para_filter = [];
  209. while(list ($key, $val) = each($para)){
  210. if($key == "sign" || $key == "sign_type" || $val == "") continue;
  211. else $para_filter[$key] = $para[$key];
  212. }
  213. return $para_filter;
  214. }
  215. /**
  216. * 对数组排序
  217. *
  218. * @param array $para 排序前的数组
  219. *
  220. * @return mixed
  221. */
  222. function argSort($para)
  223. {
  224. ksort($para);
  225. reset($para);
  226. return $para;
  227. }
  228. }