123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace app\common\library;
- use think\Controller;
- class AliPay3 extends Controller
- {
- /*
- 配置参数
- */
- public function __construct()
- {
- $this->appId = "220431411";
- $this->rsaPrivateKey = "-----BEGIN PRIVATE KEY-----
- MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC95Mqn1Q9bTF0H
- ncQOXRCsP10I6U7f83OX2+8WMxMr30J8AX6QxU3sWRY/eDMPmnnSifsG6n1ZsgJQ
- DaN8CRFjsGGA6E0Fglphf0A22MkuTsyQRSAnhmQCQbjFetdrLUBGcwXzX9H1uLhe
- F8opCjK3YdnnIYfadlq5Y8Ogw5RsmjX7sRN2l6hvONPlrN5qKd0GjGbttfYrNgxt
- D7Upbr+MVZ//RoeN3DfGSmdPQjEMVrbnHaNp92hxdVTo2t0ZlqpsICJlDF8lFzFv
- 0DVmn0tsR1rlCjmASy0LRLrIQJhp4ojhl2Jew8zx/XUEWQpyjsBGXETh4zeHXURa
- Cw8NsofrAgMBAAECggEAQirawqhm9XUiZWj/y9F54csia9x/82EtQhkMsjuRzpO4
- 4uTSaTeW82gHbI5BnIpDMK2LntlOXFa+n45hDPNL7+Sh1P0c+XJ2c8qXYYUgdNZY
- SV0M2zjk2DR2YblwHpRh9SpXRHp6+pgnBgJcGvjGu0eJWhRA9kqPSlIM9JOxwDVR
- 2RUn52VFmLqwe/QY3RlMTBS6lHxZqo8qwok+BsHAD79o5EDVKj64tvCD6Ew3WzZi
- NySctdRKt+GN/l4IWgwzll8dkMEZbqMhB6Kb4KAU0Bkzdqz539Q5CapNsYTTHW+j
- VbCq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0TR1iN/5YUWkSzh
- 8fG3+e+kj9gWbkU5j8jkGqG0fXE7uhKvhuPeUOvuAnVtUw8VKI6GssfFUafs0rMT
- 4No/s8wF7AzndyD/YoqpCqe5xToPNVHZ+5JHa9K92alTvvcx1ukRfJp1SI0JHXg1
- goUhfUrLOXV2QSgoshBHUTocWuIY4CdKP1tVOelBMcwItY9KBujfs7WMUFibe6gg
- 5V+KqYoK0uhpvZmFb9IHCGdujVdPGxL0tPH/hGBNrbgm1gOBubB9yEnH3hTj9gGg
- arcELZGyRi9HQU0haG3c79f4XDvirSTsnHnd7aIM9BKPdlkq8QZANGyuO7Rs1zkF
- CwIDAQAB
- -----END PUBLIC KEY-----";//平台公钥
- }
- /*
- * 支付宝支付
- */
- public function aliPay($body, $total_amount, $order_no, $notify_url,$attach,$callbackurl)
- {
- $params = [
- "service"=>"App.Order.Pay",
- "timestamp"=>date('Y-m-d H:i:s'),
- "nonce"=>time(),
- "member_id"=>$this->appId,
- "mem_order"=>$order_no,
- "bank_code"=>"953",
- // "notifyurl"=>"https://".$_SERVER['SERVER_NAME']."/api/Pay/alipayOrderNotify",
- "notifyurl"=>$notify_url,
- // "callbackurl"=>"https://".$_SERVER['SERVER_NAME']."/pages/mine/order-list?num=1",
- "callbackurl"=>$callbackurl,
- "amount"=>$total_amount,
- "productname"=>$body,
- "attach"=>$attach
- ];
- $apiKey = "4n79o0izlo0ax9z45lopjj0hgwl249bk";//商户中心的APIKEY
- ksort($params);
- $signString = "";
- foreach($params as $key => $val){
- $signString .= $val;
- }
- $signString .= $apiKey;
- $params['sign'] = strtoupper(md5($signString));
- // var_dump($params['sign']);
- //到这里签名完成
- //对notifyurl进行rsa加密
- $params['notifyurl'] = $this->sslEn($params['notifyurl'],$this->rsaPrivateKey);//$key是商户私钥
- //最后POST提交 $params
- $url = "https://apis.wqkeji.cn";
- $re = $this->httpPost($url,$params);//提交返回
- //判断是否为数组
- $re = is_array($re)?$re:json_decode($re,true);
- //对data进行平台公钥解密
- $data = $this->sslDe($re['data'], $this->aliPayRsaPublicKey);
- return json_decode($data,true);
- }
- /**
- * RSA 私钥、公钥加密
- * @param $data
- * @param $key
- * @param $type
- * @return string
- */
- function sslEn($data, $key, $type = 'pi')
- {
- $encrypted = "";
- if ($type == 'pi') {
- $r = openssl_pkey_get_private($key);
- //私钥加密
- foreach (str_split($data, 117) as $chunk) {
- openssl_private_encrypt($chunk, $encryptData, $key);
- $encrypted .= $encryptData;
- }
- } elseif ($type == 'pu') {
- $r = openssl_pkey_get_public($key);
- //公钥加密
- foreach (str_split($data, 117) as $chunk) {
- openssl_public_encrypt($chunk, $encryptData, $key);
- $encrypted .= $encryptData;
- }
- }
- $encrypt_data = base64_encode($encrypted);
- return $encrypt_data;
- }
- /**
- * RSA 私钥、公钥解密
- * @param $data
- * @param $key
- * @param $type
- * @return string
- */
- function sslDe($data, $key, $type = 'pu')
- {
- $hex_encrypt_data = trim($data);
- $encrypt_data = base64_decode($hex_encrypt_data);//对BASE64进行转换
- $decrypted = "";
- if ($type == 'pu') {
- $r = openssl_pkey_get_public($key);
- //公钥解密
- $arrThrunk = str_split($encrypt_data, 256);
- foreach ($arrThrunk as $trunk) {
- $temp = '';
- if (openssl_public_decrypt($trunk, $temp, $key)) {
- $decrypted .= $temp;
- } else {
- return '';
- }
- }
- } elseif ($type == 'pi') {
- $r = openssl_pkey_get_private($key);
- //私钥解密 ---- 分段解密
- $arrThrunk = str_split($encrypt_data, 256);
- foreach ($arrThrunk as $trunk) {
- $temp = '';
- if (openssl_private_decrypt($trunk, $temp, $key)) {
- $decrypted .= $temp;
- } else {
- return '';
- }
- }
- }
- return $decrypted;
- }
- /**
- * RSA 私钥、公钥解密
- * @param $data
- * @param $key
- * @param $type
- * @return string
- */
- function sslDeReturn($data, $type = 'pu')
- {
- $key = $this->aliPayRsaPublicKey;
- $hex_encrypt_data = trim($data);
- $encrypt_data = base64_decode($hex_encrypt_data);//对BASE64进行转换
- $decrypted = "";
- if ($type == 'pu') {
- $r = openssl_pkey_get_public($key);
- //公钥解密
- $arrThrunk = str_split($encrypt_data, 256);
- foreach ($arrThrunk as $trunk) {
- $temp = '';
- if (openssl_public_decrypt($trunk, $temp, $key)) {
- $decrypted .= $temp;
- } else {
- return '';
- }
- }
- } elseif ($type == 'pi') {
- $r = openssl_pkey_get_private($key);
- //私钥解密 ---- 分段解密
- $arrThrunk = str_split($encrypt_data, 256);
- foreach ($arrThrunk as $trunk) {
- $temp = '';
- if (openssl_private_decrypt($trunk, $temp, $key)) {
- $decrypted .= $temp;
- } else {
- return '';
- }
- }
- }
- return $decrypted;
- }
- //HTTP post 工具
- function httpPost($url, $post_data)
- {
- // 模拟提交数据函数
- $curl = curl_init(); // 启动一个CURL会话
- curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
- if (isset($_SERVER['HTTP_USER_AGENT'])) {
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
- }
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
- if (!empty($post_data)) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data)); // Post提交的数据包
- }
- curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
- curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
- curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
- // 发出请求
- $response = curl_exec($curl);
- curl_close($curl); // 关闭CURL会话
- return $response;
- }
- }
|