备注:
<?php class CI_Alipay{ public function __construct() { //合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner $alipay_config['partner'] = 'xxxxxxx'; // echo 1111; //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 $alipay_config['seller_id'] = $alipay_config['partner']; //商户的私钥,此处填写原始私钥去头去尾,RSA公私钥生成:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.nBDxfy&treeId=58&articleId=103242&docType=1 $alipay_config['private_key'] = 'MIICXAIBAAKBgQDB4XXqokW7afdP6h+AuGi0vJrokiPSvPTIdmnbpjez0yJ1TQRFflIypT1SwOh5LexkaQbyPpVLGQPrfoLyE/8Ji3X6cSwYswzGUNMpiViUsY2l4nGmA8sv1JxYdboMtIpSo89CaUjSPpWi0RnQuPZOfiom8Ql/zN4K2MJF3aZ1fQIDAQABAoGAH9NozdlRZRBh/oZKIZEj7vc+3oqOwtstu3Jksh+46RXxhBveZaH00Ed7p4fvIWU+dsUe1MkdLLSwhgnAaQiLRrZnmSszMXJa+B+6orlj0az7N31xPmSIR5ZxwjV4/OY5k7hj+7rbTDjxiEiHBDN8dDc/JwLqyGnEMkl60/ZNeikCQQDzLfucjgTNMPIVnLprDre+uPAnAbEwt7Hb9ySsPGodLxywheFi8LFU+0awVKfZnePiJei/CNGh2vihD2dlMaT3AkEAzBogXT8148E2os2sFWxjzn8gm/YTbot4SmCO8ep4ZTrlkUDPOp+6xfZHhN+y7SGxMjvG+Xe5n+6nGDaqeP5AKwJARnH2ppZ5i5t3NY2Obz73OZfHcfVKiCteqbMBe6eQsd2Y+QX/kbLaHB0ikCgl66OShUuoDdcXK0UMq1OUBzuHZwJBAIe3D7IxZ+oL/gJ/dF/CdRNIISzTkdQ91TzPu/1MhcX6RdAyIhm/QHiF7MSD4vwfHS5j5UjYvYuFK4LERtKxbYUCQBHCBeHMWJQ9n8QLvdB8t48sn6yxahdFuoVtdU5tME2qNBSrHT3a5ap2vflto3H37APPdN5vXW/rsGs8IuUJ6aU='; //支付宝的公钥,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['alipay_public_key']= 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB';//MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 $alipay_config['notify_url'] = "xxxxx"; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 $alipay_config['return_url'] = "xxxxxx"; //签名方式 $alipay_config['sign_type'] = strtoupper('RSA'); //字符编码格式 目前支持 gbk 或 utf-8 $alipay_config['input_charset']= strtolower('utf-8'); //ca证书路径地址,用于curl中ssl校验 //请保证cacert.pem文件在当前文件夹目录中 $alipay_config['cacert'] = getcwd().'/cacert.pem'; //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http $alipay_config['transport'] = 'http'; // 支付类型 ,无需修改 $alipay_config['payment_type'] = "1"; // 产品类型,无需修改 $alipay_config['service'] = "create_direct_pay_by_user"; //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ $this->alipay_config = $alipay_config; $this->alipay_gateway_new = 'https://mapi.alipay.com/gateway.do?'; $this->http_verify_url = 'http://notify.alipay.com/trade/notify_query.do?'; } /** * 建立请求,以表单HTML形式构造(默认)****1**** * @param $para_temp 请求参数数组 * @param $method 提交方式。两个值可选:post、get * @param $button_name 确认按钮显示文字 * @return 提交表单HTML文本 */ //1 function buildRequestForm($out_trade_no, $subject, $total_fee,$body,$notify_url) { if(!$out_trade_no || !$subject || !$total_fee || !$body || !$notify_url){ return '缺少数据'; } //测试数据 $para_temp = array( "service" => $this->alipay_config['service'], "partner" => $this->alipay_config['partner'], "seller_id" => $this->alipay_config['seller_id'], "payment_type" => $this->alipay_config['payment_type'], "notify_url" => $notify_url, "return_url" => $this->alipay_config['return_url'], "anti_phishing_key"=>$this->alipay_config['anti_phishing_key'], "exter_invoke_ip"=>$this->alipay_config['exter_invoke_ip'], "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $total_fee, "body" => $body, "_input_charset" => trim(strtolower($this->alipay_config['input_charset'])) //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1 //如"参数名"=>"参数值" ); //待请求参数数组 $method = 'GET'; $button_name = '确定'; $para = $this->buildRequestPara($para_temp); // var_dump($para);die; $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->alipay_gateway_new."_input_charset=".trim(strtolower($this->alipay_config['input_charset']))."' method='".$method."'>"; while (list ($key, $val) = each ($para)) { $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; } //submit按钮控件请不要含有name属性 $sHtml = $sHtml."<input type='submit' value='".$button_name."' style='display:none;'></form>"; $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; // echo $sHtml; return $sHtml; } /** * 生成要请求给支付宝的参数数组 * @param $para_temp 请求前的参数数组 * @return 要请求的参数数组 */ // 2 function buildRequestPara($para_temp) { //除去待签名参数数组中的空值和签名参数 $para_filter = $this->paraFilter($para_temp); //对待签名参数数组排序 $para_sort = $this->argSort($para_filter); //生成签名结果 // return $para_sort; $mysign = $this->buildRequestMysign($para_sort); // return $mysign; //签名结果与签名方式加入请求提交参数组中 $para_sort['sign'] = $mysign; $para_sort['sign_type'] = strtoupper(trim($this->alipay_config['sign_type'])); return $para_sort; } /** * 除去数组中的空值和签名参数 * @param $para 签名参数组 * return 去掉空值与签名参数后的新签名参数组 */ function paraFilter($para) { $para_filter = array(); while (list ($key, $val) = each ($para)) { if($key == "sign" || $key == "sign_type" || $val == "")continue; else $para_filter[$key] = $para[$key]; } return $para_filter; } /** * 对数组排序 * @param $para 排序前的数组 * return 排序后的数组 */ function argSort($para) { ksort($para); reset($para); return $para; } /** * 生成签名结果 * @param $para_sort 已排序要签名的数组 * return 签名结果字符串 */ function buildRequestMysign($para_sort) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 $prestr = $this->createLinkstring($para_sort); // return $prestr; $mysign = ""; switch (strtoupper(trim($this->alipay_config['sign_type']))) { case "RSA" : $mysign = $this->rsaSign($prestr, $this->alipay_config['private_key']); break; default : $mysign = "xx"; } return $mysign; } /** * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 * @param $para 需要拼接的数组 * return 拼接完成以后的字符串 */ function createLinkstring($para) { $arg = ""; while (list ($key, $val) = each ($para)) { $arg.=$key."=".$val."&"; } //去掉最后一个&字符 $arg = substr($arg,0,count($arg)-2); //如果存在转义字符,那么去掉转义 if(get_magic_quotes_gpc()){$arg = stripslashes($arg);} return $arg; } /** * RSA签名 * @param $data 待签名数据 * @param $private_key 商户私钥字符串 * return 签名结果 */ function rsaSign($data, $private_key) { //以下为了初始化私钥,保证在您填写私钥时不管是带格式还是不带格式都可以通过验证。 $private_key=str_replace("-----BEGIN RSA PRIVATE KEY-----","",$private_key); $private_key=str_replace("-----END RSA PRIVATE KEY-----","",$private_key); $private_key=str_replace("\n","",$private_key); $private_key="-----BEGIN RSA PRIVATE KEY-----".PHP_EOL .wordwrap($private_key, 64, "\n", true). PHP_EOL."-----END RSA PRIVATE KEY-----"; $res=openssl_get_privatekey($private_key); if($res) { openssl_sign($data, $sign,$res); } else { echo "您的私钥格式不正确!"."<br/>"."The format of your private_key is incorrect!"; exit(); } openssl_free_key($res); //base64编码 $sign = base64_encode($sign); return $sign; } //验签 /** * 获取返回时的签名验证结果 * @param $para_temp 通知返回来的参数数组 * @param $sign 返回的签名结果 * @return 签名验证结果 */ public function getSignVeryfy($para_temp, $sign) { //除去待签名参数数组中的空值和签名参数 $para_filter = $this->paraFilter($para_temp); //对待签名参数数组排序 $para_sort = $this->argSort($para_filter); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 $prestr = $this->createLinkstring($para_sort); $isSgin = false; // return $this->alipay_config['sign_type']; switch (strtoupper(trim($this->alipay_config['sign_type']))) { case "RSA" : $isSgin = $this->rsaVerify($prestr, trim($this->alipay_config['alipay_public_key']), $sign); break; default : $isSgin = false; } return $isSgin; } /** * 获取远程服务器ATN结果,验证返回URL * @param $notify_id 通知校验ID * @return 服务器ATN结果 * 验证结果集: * invalid命令参数不对 出现这个错误,请检测返回处理中partner和key是否为空 * true 返回正确信息 * false 请检查防火墙或者是服务器阻止端口问题以及验证时间是否超过一分钟 */ function getResponse($notify_id) { $transport = strtolower(trim($this->alipay_config['transport'])); $partner = trim($this->alipay_config['partner']); $veryfy_url = ''; if($transport == 'https') { $veryfy_url = $this->https_verify_url; } else { $veryfy_url = $this->http_verify_url; } $veryfy_url = $veryfy_url."notify_id=" . $notify_id."&partner=" . $partner; $responseTxt = $this->getHttpResponseGET($veryfy_url, $this->alipay_config['cacert']); // return $veryfy_url; return $responseTxt; } /** * RSA验签 * @param $data 待签名数据 * @param $alipay_public_key 支付宝的公钥字符串 * @param $sign 要校对的的签名结果 * return 验证结果 */ function rsaVerify($data, $alipay_public_key, $sign) { //以下为了初始化私钥,保证在您填写私钥时不管是带格式还是不带格式都可以通过验证。 $alipay_public_key=str_replace("-----BEGIN PUBLIC KEY-----","",$alipay_public_key); $alipay_public_key=str_replace("-----END PUBLIC KEY-----","",$alipay_public_key); $alipay_public_key=str_replace("\n","",$alipay_public_key); $alipay_public_key='-----BEGIN PUBLIC KEY-----'.PHP_EOL.wordwrap($alipay_public_key, 64, "\n", true) .PHP_EOL.'-----END PUBLIC KEY-----'; $res=openssl_get_publickey($alipay_public_key); if($res) { $result = (bool)openssl_verify($data, base64_decode($sign), $res); } else { return "您的支付宝公钥格式不正确!"."<br/>"."The format of your alipay_public_key is incorrect!"; exit(); } openssl_free_key($res); return $result; } /** * 远程获取数据,GET模式 * 注意: * 1.使用Crul需要修改服务器中php.ini文件的设置,找到php_curl.dll去掉前面的";"就行了 * 2.文件夹中cacert.pem是SSL证书请保证其路径有效,目前默认路径是:getcwd().'\\cacert.pem' * @param $url 指定URL完整路径地址 * @param $cacert_url 指定当前工作目录绝对路径 * return 远程输出的数据 */ function getHttpResponseGET($url,$cacert_url) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头 curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);//SSL证书认证 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格认证 curl_setopt($curl, CURLOPT_CAINFO,$cacert_url);//证书地址 $responseText = curl_exec($curl); //var_dump( curl_error($curl) );//如果执行curl过程中出现异常,可打开此开关,以便查看异常内容 // var_dump($cacert_url);die; curl_close($curl); return $responseText; } //支付宝退款类 public function tuikuan($batch_no,$batch_num,$detail_data){ /**************************请求参数**************************/ //批次号,必填,格式:当天日期[8位]+序列号[3至24位],如:201603081000001 // $batch_no = $_POST['WIDbatch_no']; //退款笔数,必填,参数detail_data的值中,“#”字符出现的数量加1,最大支持1000笔(即“#”字符出现的数量999个) // $batch_num = $_POST['WIDbatch_num']; //退款详细数据,必填,格式(支付宝交易号^退款金额^备注),多笔请用#隔开 // $detail_data = $_POST['WIDdetail_data']; //构造要请求的参数数组,无需改动 $parameter = array( "service" => trim('refund_fastpay_by_platform_pwd'),// 调用的接口名,无需修改 "partner" => trim($this->alipay_config['partner']), // 合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://openhome.alipay.com/platform/keyManage.htm?keyType=partner "notify_url" => trim("http://www.isolar88.com/notify/alituikuan"), "seller_user_id" => trim($this->alipay_config['partner']),// 卖家支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 "refund_date" => trim(date("Y-m-d H:i:s",time())), // 退款日期 时间格式 yyyy-MM-dd HH:mm:ss//date_default_timezone_set('PRC');//设置当前系统服务器时间为北京时间,PHP5.1以上可使用。 "batch_no" => $batch_no, "batch_num" => $batch_num, "detail_data" => $detail_data, "_input_charset" => trim(strtolower($this->alipay_config['input_charset'])) ); $para = $this->buildRequestPara($parameter); $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$this->alipay_gateway_new."_input_charset=".trim(strtolower($this->alipay_config['input_charset']))."' method='".$method."'>"; while (list ($key, $val) = each ($para)) { $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; } //submit按钮控件请不要含有name属性 $sHtml = $sHtml."<input type='submit' value='".$button_name."' style='display:none;'></form>"; $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; // var_dump($para);die; return $sHtml; } }
备注:
代码:
<?php header('Content-type:text/html; Charset=utf-8'); /*** 请填写以下配置信息 ***/ $appid = '2016xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID $returnUrl = 'xxxx'; //付款成功后的同步回调地址 $notifyUrl = 'xxxxxx'; //付款成功后的异步回调地址 $outTradeNo = uniqid(); //你自己的商品订单号 $payAmount = 0.01; //付款金额,单位:元 $orderName = '支付测试'; //订单标题 $signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2 $rsaPrivateKey='xxxxx/lxwT0bkMlQna8pae18p0qtaKjyENrWe5MrEi/OFtokrGGUjBQEy2pd1xd4dpQMFBviMieH1KML+r0SiEECeYi60L1Eph26ZNwa3HO7v1/vnYkcpIVd1jHvKqegOKuYAlf7wT4tXyPULLjNiHvj3Fg/ojzmyJ5eTkjrPV/N6lKNzFQikeVY1Q7AkTI7tG0B5z9ULF8KXpE0niGNOxFHulgugmkL0GiOH3xHVeqg3EHwT4pzs8YbR2D7eCFlKl/cM/vVXgZ0ypyHhEIjORhrhDSzRYGxo+Fd7nobjPhwfc+OKi0uF+peHfN5z/okw1mC6RqxYV+QIDAQABAoIBAHUWoQZUBNPZNhzmqXLtMv729jxL1YBsE6n6hgstl4oIjsV56H0+OIfUZ/WvWXfBG9JSlsfMXutpS1OoGIe2kwQq6x0MKqGrqT9fyz9MD0ZKzmIT9N7w371I0CZ0m8ju8eFprct01IRduFEWYgs15d85i8BAaJlKff29Dtrhp78XHiro3UM5pW77ei5A/ahdPzSdXB8lxgYqpIMD39jvx7nDlzzxLyVG7nTyEH5xe88+q3F9Fl3vreVkCIQcJj6DkgMrzBW4DS7NvyO97Kodvkrme0O7xkg0HkhR9mT+jfLNV6mIj6zKEvL+8JRad2ENm1hqaQUhpoxpLrYWC79pHYECgYEA/vYm+FizN+RaOG5t4HkL8FBns6+2dmGc+IWGj7ELJ+ULk8UR8qJDHAady9089FvBNvdSDteQXF/bHbiOfisuSJom1uWugPpYD0R/82gXFYxvqseJKLV+I82w6yJBodpCxuafcH2ZaNsg4rjM1+GFWSGtFbKgknpH/e+k1hJ9evECgYEA4JKz9c5PKxmyZuFfvWaP2GoJ2BbE7iKKJpVTaP6ro+nd2myIJZ2xXO+3xTanvVDKGLS9WDjvdNjk3RAW/0f6PBHEh5yOBeLDi4NBXrulLWr4hzaqKW4japm9iU9Q5m4fukmjTrgQzuExnnxDOJCTXesiIUOK9dKPj9ZoAfK1+4kCgYEA9L3ZPr/2mYpsSItIkhfgK/ki8suTPSQKiE20k66BZPRrHpgn2F+GSBrZ0l43KuIBIQBcFr86ZlHlhiwpLFfLAAppihPEZS7q5Qz+PesHbbn3X1pXAWrnAPtr/upNUk6vlzq+hj8qygq/EKQQjpJ/bMX03kUv+ivDe7FU6+Gn7iECgYEA2jTRlZKsDCYA2cQ9IayJMm/+NrARE44ODGVl0Yw6vJ8dyQFR3LL3xmTKfF6Z5/KbHBxqKlHQIgopfr8GiW+nrjxZaKduvDtVY7bairDrU1EwQTmVZ4Iz+X1+0+90nSH9xCWO/4sMEehwLR1yt4CLIGO7GvUlDaxVgB6Fmvb5ankCgYEArmNzUp+MaN55EBnY8o/LTxiuuRAkziuXB7cXYRvAq6GDUtaxN88Rkqm39s9jXvbwwRfIVk6ea9JufnjiM0SHfc3vtrU67Tg7XHbrV5l+PSbrExzU4Ot3i89aOYFeIoHUSzTew4vvrIV/Hj2nffbI2fZeFgI0FglUaN8dCe6sNhk='; //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310 /*** 配置结束 ***/ $aliPay = new Alibabapay(); $aliPay->setAppid($appid); $aliPay->setReturnUrl($returnUrl); $aliPay->setNotifyUrl($notifyUrl); $aliPay->setRsaPrivateKey($rsaPrivateKey); $aliPay->setTotalFee($payAmount); $aliPay->setOutTradeNo($outTradeNo); $aliPay->setOrderName($orderName); $sHtml = $aliPay->doPay(); echo $sHtml; class Alibabapay { protected $appId; protected $charset; protected $returnUrl; protected $notifyUrl; //私钥值 protected $rsaPrivateKey; protected $totalFee; protected $outTradeNo; protected $orderName; public function __construct() { $this->charset = 'utf8'; } public function setAppid($appid) { $this->appId = $appid; } public function setReturnUrl($returnUrl) { $this->returnUrl = $returnUrl; } public function setNotifyUrl($notifyUrl) { $this->notifyUrl = $notifyUrl; } public function setRsaPrivateKey($rsaPrivateKey) { $this->rsaPrivateKey = $rsaPrivateKey; } public function setTotalFee($payAmount) { $this->totalFee = $payAmount; } public function setOutTradeNo($outTradeNo) { $this->outTradeNo = $outTradeNo; } public function setOrderName($orderName) { $this->orderName = $orderName; } /** * 发起订单 * @return array */ public function doPay() { //请求参数 $requestConfigs = array( 'out_trade_no'=>$this->outTradeNo, 'product_code'=>'QUICK_WAP_WAY', 'total_amount'=>$this->totalFee, //单位 元 'subject'=>$this->orderName, //订单标题 ); $commonConfigs = array( //公共参数 'app_id' => $this->appId, 'method' => 'alipay.trade.wap.pay', //接口名称 'format' => 'JSON', 'return_url' => $this->returnUrl, 'charset'=>$this->charset, 'sign_type'=>'RSA2', 'timestamp'=>date('Y-m-d H:i:s'), 'version'=>'1.0', 'notify_url' => $this->notifyUrl, 'biz_content'=>json_encode($requestConfigs), ); $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']); return $this->buildRequestForm($commonConfigs); } /** * 建立请求,以表单HTML形式构造(默认) * @param $para_temp 请求参数数组 * @return 提交表单HTML文本 */ protected function buildRequestForm($para_temp) { $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=".$this->charset."' method='POST'>"; while (list ($key, $val) = each ($para_temp)) { if (false === $this->checkEmpty($val)) { $val = str_replace("'","'",$val); $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; } } //submit按钮控件请不要含有name属性 $sHtml = $sHtml."<input type='submit' value='ok' style='display:none;''></form>"; $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; return $sHtml; } public function generateSign($params, $signType = "RSA") { return $this->sign($this->getSignContent($params), $signType); } protected function sign($data, $signType = "RSA") { $priKey=$this->rsaPrivateKey; $res = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($priKey, 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----"; ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置'); if ("RSA2" == $signType) { openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持 } else { openssl_sign($data, $sign, $res); } $sign = base64_encode($sign); return $sign; } /** * 校验$value是否非空 * if not set ,return true; * if is null , return true; **/ protected function checkEmpty($value) { if (!isset($value)) return true; if ($value === null) return true; if (trim($value) === "") return true; return false; } public function getSignContent($params) { ksort($params); $stringToBeSigned = ""; $i = 0; foreach ($params as $k => $v) { if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { // 转换成目标字符集 $v = $this->characet($v, $this->charset); if ($i == 0) { $stringToBeSigned .= "$k" . "=" . "$v"; } else { $stringToBeSigned .= "&" . "$k" . "=" . "$v"; } $i++; } } unset ($k, $v); return $stringToBeSigned; } /** * 转换字符集编码 * @param $data * @param $targetCharset * @return string */ function characet($data, $targetCharset) { if (!empty($data)) { $fileType = $this->charset; if (strcasecmp($fileType, $targetCharset) != 0) { $data = mb_convert_encoding($data, $targetCharset, $fileType); //$data = iconv($fileType, $targetCharset.'//IGNORE', $data); } } return $data; } }
原创文章,转载请标明出处!