首页 > 代码库 > 支付宝移动接口服务端生成签名串代码

支付宝移动接口服务端生成签名串代码

 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 3435 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内置签名方法,生成签名$sign52 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     }