BaseAlipay.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * @note: 支付宝支付
  4. * @author: jdzor <895947580@qq.com>
  5. * @date: 2019/3/14
  6. */
  7. namespace payment\alipay;
  8. class BaseAlipay
  9. {
  10. const GATEWAY_URL = 'https://openapi.alipay.com/gateway.do';
  11. protected $charset = 'UTF-8';
  12. protected $signType = 'RSA2';
  13. protected $format = 'JSON';
  14. protected $version = '1.0';
  15. //该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。
  16. //m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
  17. protected $timeoutExpress = '30m';
  18. protected $appId;
  19. protected $rsaPrivateKey;
  20. protected $alipayPublicKey; //支付宝公钥,非应用公钥
  21. protected $notifyUrl;
  22. protected $returnUrl;
  23. protected function __construct($config)
  24. {
  25. isset($config['app_id']) || json_error('缺少参数:app_id');
  26. isset($config['rsa_private_key']) || json_error('缺少参数:rsa_private_key');
  27. isset($config['alipay_public_key']) || json_error('缺少参数:alipay_public_key');
  28. $this->appId = $config['app_id'];
  29. $this->rsaPrivateKey = $config['rsa_private_key'];
  30. $this->alipayPublicKey = $config['alipay_public_key'];
  31. }
  32. //公共请求
  33. protected function commonRequest($requestConfig, $method)
  34. {
  35. //公共参数
  36. $commonConfig = [
  37. 'app_id' => $this->appId,
  38. 'method' => $method,
  39. 'charset' => $this->charset,
  40. 'format' => $this->format,
  41. 'version' => $this->version,
  42. 'sign_type' => $this->signType,
  43. 'biz_content' => json_encode($requestConfig),
  44. 'timestamp' => date('Y-m-d H:i:s')
  45. ];
  46. $this->notifyUrl && $commonConfig['notify_url'] = $this->notifyUrl;
  47. $this->returnUrl && $commonConfig['return_url'] = $this->returnUrl;
  48. $commonConfig['sign'] = $this->generateSign($commonConfig, $commonConfig['sign_type']);
  49. if ($method == 'alipay.trade.app.pay') {
  50. //APP支付 返回参数给移动端
  51. return http_build_query($commonConfig);
  52. } elseif ($method == 'alipay.trade.wap.pay') {
  53. //手机网站支付 返回参数
  54. return $commonConfig;
  55. } elseif ($method == 'alipay.trade.page.pay') {
  56. //电脑网站支付 返回参数
  57. return $commonConfig;
  58. }
  59. $result = $this->curlPost(self::GATEWAY_URL, $commonConfig);
  60. return gbk2utf8($result);
  61. }
  62. protected function generateSign($param, $signType = "RSA")
  63. {
  64. return $this->sign($this->getSignContent($param), $signType);
  65. }
  66. protected function sign($data, $signType = "RSA")
  67. {
  68. $privateKey = $this->rsaPrivateKey;
  69. $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  70. wordwrap($privateKey, 64, "\n", true) .
  71. "\n-----END RSA PRIVATE KEY-----";
  72. ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  73. //OPENSSL_ALGO_SHA256 php5.4.8 以上版本才支持
  74. if ("RSA2" == $signType) {
  75. openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
  76. } else {
  77. openssl_sign($data, $sign, $res);
  78. }
  79. $sign = base64_encode($sign);
  80. return $sign;
  81. }
  82. protected function checkEmpty($value)
  83. {
  84. if (!isset($value)) return true;
  85. if ($value === null) return true;
  86. if (trim($value) === "") return true;
  87. return false;
  88. }
  89. protected function getSignContent($params)
  90. {
  91. ksort($params);
  92. $stringToBeSigned = "";
  93. $i = 0;
  94. foreach ($params as $k => $v) {
  95. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  96. //转换成目标字符集
  97. $v = $this->character($v, $this->charset);
  98. if ($i == 0) {
  99. $stringToBeSigned .= "$k" . "=" . "$v";
  100. } else {
  101. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  102. }
  103. $i++;
  104. }
  105. }
  106. return $stringToBeSigned;
  107. }
  108. protected function character($data, $targetCharset)
  109. {
  110. if (!empty($data)) {
  111. $fileType = $this->charset;
  112. if (strcasecmp($fileType, $targetCharset) != 0) {
  113. $data = mb_convert_encoding($data, $targetCharset, $fileType);
  114. //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
  115. }
  116. }
  117. return $data;
  118. }
  119. protected function buildOrderStr($data)
  120. {
  121. return http_build_query($data);
  122. }
  123. protected function curlPost($url, $postData = '')
  124. {
  125. $ch = curl_init();
  126. curl_setopt($ch, CURLOPT_URL, $url);
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. curl_setopt($ch, CURLOPT_POST, 1);
  129. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postData) ? http_build_query($postData) : $postData);
  130. curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置cURL允许执行的最长秒数
  131. curl_setopt($ch, CURLOPT_HEADER, FALSE); //设置header
  132. //https请求 不验证证书和host
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  135. if (!($data = curl_exec($ch))) $data = ['errno' => curl_errno($ch), 'error' => curl_error($ch)] + curl_getinfo($ch);
  136. curl_close($ch);
  137. return $data;
  138. }
  139. protected function rsaCheck($param)
  140. {
  141. $sign = $param['sign'];
  142. $signType = $param['sign_type'];
  143. unset($param['sign_type']);
  144. unset($param['sign']);
  145. return $this->verify($this->getSignContent($param), $sign, $signType);
  146. }
  147. protected function verify($data, $sign, $signType = 'RSA')
  148. {
  149. $pubKey = $this->alipayPublicKey;
  150. $res = "-----BEGIN PUBLIC KEY-----\n" .
  151. wordwrap($pubKey, 64, "\n", true) .
  152. "\n-----END PUBLIC KEY-----";
  153. ($res) or die('支付宝RSA公钥错误。请检查公钥文件格式是否正确');
  154. //调用openssl内置方法验签,返回bool值
  155. if ("RSA2" == $signType) {
  156. $result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
  157. } else {
  158. $result = (bool)openssl_verify($data, base64_decode($sign), $res);
  159. }
  160. // if (!$this->checkEmpty($this->alipayPublicKey)) {
  161. // openssl_free_key($result); //释放资源
  162. // }
  163. return $result;
  164. }
  165. }