AliPay3.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\library;
  3. use think\Controller;
  4. class AliPay3 extends Controller
  5. {
  6. /*
  7. 配置参数
  8. */
  9. public function __construct()
  10. {
  11. $this->appId = "220431411";
  12. $this->rsaPrivateKey = "-----BEGIN PRIVATE KEY-----
  13. MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC95Mqn1Q9bTF0H
  14. ncQOXRCsP10I6U7f83OX2+8WMxMr30J8AX6QxU3sWRY/eDMPmnnSifsG6n1ZsgJQ
  15. DaN8CRFjsGGA6E0Fglphf0A22MkuTsyQRSAnhmQCQbjFetdrLUBGcwXzX9H1uLhe
  16. F8opCjK3YdnnIYfadlq5Y8Ogw5RsmjX7sRN2l6hvONPlrN5qKd0GjGbttfYrNgxt
  17. D7Upbr+MVZ//RoeN3DfGSmdPQjEMVrbnHaNp92hxdVTo2t0ZlqpsICJlDF8lFzFv
  18. 0DVmn0tsR1rlCjmASy0LRLrIQJhp4ojhl2Jew8zx/XUEWQpyjsBGXETh4zeHXURa
  19. Cw8NsofrAgMBAAECggEAQirawqhm9XUiZWj/y9F54csia9x/82EtQhkMsjuRzpO4
  20. 4uTSaTeW82gHbI5BnIpDMK2LntlOXFa+n45hDPNL7+Sh1P0c+XJ2c8qXYYUgdNZY
  21. SV0M2zjk2DR2YblwHpRh9SpXRHp6+pgnBgJcGvjGu0eJWhRA9kqPSlIM9JOxwDVR
  22. 2RUn52VFmLqwe/QY3RlMTBS6lHxZqo8qwok+BsHAD79o5EDVKj64tvCD6Ew3WzZi
  23. NySctdRKt+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商户私钥
  41. $this->aliPayRsaPublicKey = "-----BEGIN PUBLIC KEY-----
  42. MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp7+ID0TR1iN/5YUWkSzh
  43. 8fG3+e+kj9gWbkU5j8jkGqG0fXE7uhKvhuPeUOvuAnVtUw8VKI6GssfFUafs0rMT
  44. 4No/s8wF7AzndyD/YoqpCqe5xToPNVHZ+5JHa9K92alTvvcx1ukRfJp1SI0JHXg1
  45. goUhfUrLOXV2QSgoshBHUTocWuIY4CdKP1tVOelBMcwItY9KBujfs7WMUFibe6gg
  46. 5V+KqYoK0uhpvZmFb9IHCGdujVdPGxL0tPH/hGBNrbgm1gOBubB9yEnH3hTj9gGg
  47. arcELZGyRi9HQU0haG3c79f4XDvirSTsnHnd7aIM9BKPdlkq8QZANGyuO7Rs1zkF
  48. CwIDAQAB
  49. -----END PUBLIC KEY-----";//平台公钥
  50. }
  51. /*
  52. * 支付宝支付
  53. */
  54. public function aliPay($body, $total_amount, $order_no, $notify_url,$attach,$callbackurl)
  55. {
  56. $params = [
  57. "service"=>"App.Order.Pay",
  58. "timestamp"=>date('Y-m-d H:i:s'),
  59. "nonce"=>time(),
  60. "member_id"=>$this->appId,
  61. "mem_order"=>$order_no,
  62. "bank_code"=>"953",
  63. // "notifyurl"=>"https://".$_SERVER['SERVER_NAME']."/api/Pay/alipayOrderNotify",
  64. "notifyurl"=>$notify_url,
  65. // "callbackurl"=>"https://".$_SERVER['SERVER_NAME']."/pages/mine/order-list?num=1",
  66. "callbackurl"=>$callbackurl,
  67. "amount"=>$total_amount,
  68. "productname"=>$body,
  69. "attach"=>$attach
  70. ];
  71. $apiKey = "4n79o0izlo0ax9z45lopjj0hgwl249bk";//商户中心的APIKEY
  72. ksort($params);
  73. $signString = "";
  74. foreach($params as $key => $val){
  75. $signString .= $val;
  76. }
  77. $signString .= $apiKey;
  78. $params['sign'] = strtoupper(md5($signString));
  79. // var_dump($params['sign']);
  80. //到这里签名完成
  81. //对notifyurl进行rsa加密
  82. $params['notifyurl'] = $this->sslEn($params['notifyurl'],$this->rsaPrivateKey);//$key是商户私钥
  83. //最后POST提交 $params
  84. $url = "https://apis.wqkeji.cn";
  85. $re = $this->httpPost($url,$params);//提交返回
  86. //判断是否为数组
  87. $re = is_array($re)?$re:json_decode($re,true);
  88. //对data进行平台公钥解密
  89. $data = $this->sslDe($re['data'], $this->aliPayRsaPublicKey);
  90. return json_decode($data,true);
  91. }
  92. /**
  93. * RSA 私钥、公钥加密
  94. * @param $data
  95. * @param $key
  96. * @param $type
  97. * @return string
  98. */
  99. function sslEn($data, $key, $type = 'pi')
  100. {
  101. $encrypted = "";
  102. if ($type == 'pi') {
  103. $r = openssl_pkey_get_private($key);
  104. //私钥加密
  105. foreach (str_split($data, 117) as $chunk) {
  106. openssl_private_encrypt($chunk, $encryptData, $key);
  107. $encrypted .= $encryptData;
  108. }
  109. } elseif ($type == 'pu') {
  110. $r = openssl_pkey_get_public($key);
  111. //公钥加密
  112. foreach (str_split($data, 117) as $chunk) {
  113. openssl_public_encrypt($chunk, $encryptData, $key);
  114. $encrypted .= $encryptData;
  115. }
  116. }
  117. $encrypt_data = base64_encode($encrypted);
  118. return $encrypt_data;
  119. }
  120. /**
  121. * RSA 私钥、公钥解密
  122. * @param $data
  123. * @param $key
  124. * @param $type
  125. * @return string
  126. */
  127. function sslDe($data, $key, $type = 'pu')
  128. {
  129. $hex_encrypt_data = trim($data);
  130. $encrypt_data = base64_decode($hex_encrypt_data);//对BASE64进行转换
  131. $decrypted = "";
  132. if ($type == 'pu') {
  133. $r = openssl_pkey_get_public($key);
  134. //公钥解密
  135. $arrThrunk = str_split($encrypt_data, 256);
  136. foreach ($arrThrunk as $trunk) {
  137. $temp = '';
  138. if (openssl_public_decrypt($trunk, $temp, $key)) {
  139. $decrypted .= $temp;
  140. } else {
  141. return '';
  142. }
  143. }
  144. } elseif ($type == 'pi') {
  145. $r = openssl_pkey_get_private($key);
  146. //私钥解密 ---- 分段解密
  147. $arrThrunk = str_split($encrypt_data, 256);
  148. foreach ($arrThrunk as $trunk) {
  149. $temp = '';
  150. if (openssl_private_decrypt($trunk, $temp, $key)) {
  151. $decrypted .= $temp;
  152. } else {
  153. return '';
  154. }
  155. }
  156. }
  157. return $decrypted;
  158. }
  159. /**
  160. * RSA 私钥、公钥解密
  161. * @param $data
  162. * @param $key
  163. * @param $type
  164. * @return string
  165. */
  166. function sslDeReturn($data, $type = 'pu')
  167. {
  168. $key = $this->aliPayRsaPublicKey;
  169. $hex_encrypt_data = trim($data);
  170. $encrypt_data = base64_decode($hex_encrypt_data);//对BASE64进行转换
  171. $decrypted = "";
  172. if ($type == 'pu') {
  173. $r = openssl_pkey_get_public($key);
  174. //公钥解密
  175. $arrThrunk = str_split($encrypt_data, 256);
  176. foreach ($arrThrunk as $trunk) {
  177. $temp = '';
  178. if (openssl_public_decrypt($trunk, $temp, $key)) {
  179. $decrypted .= $temp;
  180. } else {
  181. return '';
  182. }
  183. }
  184. } elseif ($type == 'pi') {
  185. $r = openssl_pkey_get_private($key);
  186. //私钥解密 ---- 分段解密
  187. $arrThrunk = str_split($encrypt_data, 256);
  188. foreach ($arrThrunk as $trunk) {
  189. $temp = '';
  190. if (openssl_private_decrypt($trunk, $temp, $key)) {
  191. $decrypted .= $temp;
  192. } else {
  193. return '';
  194. }
  195. }
  196. }
  197. return $decrypted;
  198. }
  199. //HTTP post 工具
  200. function httpPost($url, $post_data)
  201. {
  202. // 模拟提交数据函数
  203. $curl = curl_init(); // 启动一个CURL会话
  204. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  205. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  206. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
  207. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  208. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  209. }
  210. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  211. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  212. if (!empty($post_data)) {
  213. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data)); // Post提交的数据包
  214. }
  215. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  216. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  217. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  218. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  219. // 发出请求
  220. $response = curl_exec($curl);
  221. curl_close($curl); // 关闭CURL会话
  222. return $response;
  223. }
  224. }