1 $ali = array( 2 ‘service‘ => ‘mobile.securitypay.pay‘, 3 ‘partner‘ => ‘xxxxxxxxxx‘,// 4 ‘_input_charset‘ => ‘utf-8‘, 5 ‘sign_type‘ => ‘RSA‘, 6 ‘sign‘ => ‘‘, 7 ‘notify_url‘ => urlencode($url),//回调地址 8 ‘out_trade_no‘ => $ordernu,//商户网站唯一订单号 9 ‘subject‘ => $cpPayName,//商品名称 10 ‘payment_type‘ => 1,//支付类型 11 ‘seller_id‘ => ‘‘,//支付宝账号 12 ‘total_fee‘ => $cpPrice,//总金额 13 ‘body‘ => $cpPayName,//商品详情 14 ); 15 $ali = argSort($ali); 16 $str = ‘‘; 17 18 foreach($ali as $key=>$val){ 19 if($key == ‘sign_type‘ || $key == ‘sign‘){ 20 continue; 21 }else{ 22 if($str == ‘‘){ 23 $str = $key.‘=‘.‘"‘.$val.‘"‘; 24 }else{ 25 $str = $str.‘&‘.$key.‘=‘.‘"‘.$val.‘"‘; 26 } 27 } 28 } 29 30 31 $sign = urlencode(sign($str)); 32 $str = $str.‘&sign=‘.‘"‘.$sign.‘"‘.‘&sign_type=‘.‘"‘.$ali[‘sign_type‘].‘"‘;//传给支付宝接口的数据 33 34 35 function argSort($para) { 36 ksort($para); 37 reset($para); 38 return $para; 39 } 40 41 //RSA签名 42 function sign($data) { 43 44 //读取私钥文件 45 46 $priKey = file_get_contents(‘key/rsa_private_key.pem‘);//私钥文件路径 47 //转换为openssl密钥,必须是没有经过pkcs8转换的私钥 48 49 $res = openssl_get_privatekey($priKey); 50 51 //调用openssl内置签名方法,生成签名$sign 52 53 openssl_sign($data, $sign, $res); 54 55 56 //释放资源 57 58 openssl_free_key($res); 59 60 61 //base64编码 62 63 $sign = base64_encode($sign); 64 65 return $sign; 66 67 }