最近做了功能,涉及到了微信支付单笔转账功能,不得不说,支付宝的到款速度还是很给力。
首先必须要有去申请企业支付宝,这个东西需要营业执照,申请一般1-2天即可申请完毕,最后还会有个对公账号打款8.22元。现在我们主要说下程序方面的实现
第一,首先需要建立一个开发者应用,会分配你一个appid
2,进行相对应的功能应用进行签约,时间一般一天就可以通过
3,填写公钥,公钥这块用支付宝提供的工具生成即可。
4以上执行完毕后,我们就可以根据文档进行功能开发
5.请求地址 /gateway.do
,通过文档,我们知道其实我们就把相应的参数(公共请求参数和请求参数)发给支付宝
6.支付宝接受到参数会给我们相应的反馈,在请求参数时,我们会把所有的请求参数json后放入到公共参数 biz_content中,具体参看下面代码
<?phprequire_once '../zhifubao/Rsa.class.php';require_once '../zhifubao/Base.class.php';require_once '../zhifubao/Alipay.class.php';//构建支付请求 可以传递MD5 RSA RSA2三种参数$obj = new Alipay();$data = ['payee_account' => 'xxxxx', //收款方账户'amount' => 'xxxx', //金额];//UTF-8格式的json数据$res = iconv('gbk','utf-8',$obj->transfer($data));echo '<pre>';//转换为数组$res = json_decode($res,true);print_r($res);
alipay类
<?phpclass Alipay extends Base{const TRANSFER = '/gateway.do';public function __construct() {}//转账public function transfer($data){//公共请求参数$pub_params = ['app_id' => self::APPID,'method' => 'alipay.fund.trans.toaccount.transfer', //接口名称 应填写固定值alipay.fund.trans.toaccount.transfer'format' => 'JSON', //目前仅支持JSON'charset' => 'UTF-8','sign_type' => 'RSA2',//签名方式'sign' => '', //签名'timestamp' => date('Y-m-d H:i:s'), //发送时间 格式0000-00-00 00:00:00'version' => '1.0', //固定为1.0'biz_content' => '', //业务请求参数的集合];//请求参数$api_params = ['out_biz_no' => date('YmdHis'),//商户转账订单号'payee_type' => 'ALIPAY_LOGONID', //收款方账户类型'payee_account' => $data['payee_account'], //收款方账户'amount' => $data['amount'], //金额];$pub_params['biz_content'] = json_encode($api_params,JSON_UNESCAPED_UNICODE);$pub_params = $this->setRsa2Sign($pub_params);return $this->curlRequest(self::TRANSFER, $pub_params);}}
Base类
<?phpclass Base extends RSA{/*** 以下信息需要根据自己实际情况修改*///自己的私钥const APPPRIKEY = 'xxxxx'; //修改成自己的 应用私钥//申请到的APPIDconst APPID = 'xxxxx';//支付宝公钥,不是你自己生成的公钥const NEW_ALIPUBKE ='xxxx'; //修改成自己的 支付宝公钥public function getStr($arr,$type = 'RSA'){//筛选if(isset($arr['sign'])){unset($arr['sign']);}if(isset($arr['sign_type']) && $type == 'RSA'){unset($arr['sign_type']);}//排序ksort($arr);//拼接return $this->getUrl($arr,false);}//将数组转换为url格式的字符串public function getUrl($arr,$encode = true){if($encode){return http_build_query($arr);}else{return urldecode(http_build_query($arr));}}//获取签名RSA2public function getRsa2Sign($arr){return $this->rsaSign($this->getStr($arr,'RSA2'), self::APPPRIKEY,'RSA2') ;}//获取含有签名的数组RSApublic function setRsa2Sign($arr){$arr['sign'] = $this->getRsa2Sign($arr);return $arr;}public function checkSign($arr){if($this->getRsa2Sign($arr) == $arr['sign']){return true;}else{return false;}}public function curlRequest($url,$data = ''){$ch = curl_init();$params[CURLOPT_URL] = $url; //请求url地址$params[CURLOPT_HEADER] = false; //是否返回响应头信息$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回$params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向$params[CURLOPT_TIMEOUT] = 30; //超时时间if(!empty($data)){$params[CURLOPT_POST] = true;$params[CURLOPT_POSTFIELDS] = $data;}$params[CURLOPT_SSL_VERIFYPEER] = false;//请求https时设置,还有其他解决方案$params[CURLOPT_SSL_VERIFYHOST] = false;//请求https时,其他方案查看其他博文curl_setopt_array($ch, $params); //传入curl参数$content = curl_exec($ch); //执行curl_close($ch); //关闭连接return $content;}}
rsa 验证类
<?phpclass RSA{/*** RSA签名* @param $data 待签名数据* @param $private_key 私钥字符串* return 签名结果*/function rsaSign($data, $private_key,$type = 'RSA') {$search = ["-----BEGIN RSA PRIVATE KEY-----","-----END RSA PRIVATE KEY-----","\n","\r","\r\n"];$private_key=str_replace($search,"",$private_key);$private_key=$search[0] . PHP_EOL . wordwrap($private_key, 64, "\n", true) . PHP_EOL . $search[1];$res=openssl_get_privatekey($private_key);if($res){if($type == 'RSA'){openssl_sign($data, $sign,$res);}elseif($type == 'RSA2'){//OPENSSL_ALGO_SHA256openssl_sign($data, $sign,$res,OPENSSL_ALGO_SHA256);}openssl_free_key($res);}else {exit("私钥格式有误");}$sign = base64_encode($sign);return $sign;}/*** RSA验签* @param $data 待签名数据* @param $public_key 公钥字符串* @param $sign 要校对的的签名结果* return 验证结果*/function rsaCheck($data, $public_key, $sign,$type = 'RSA') {$search = ["-----BEGIN PUBLIC KEY-----","-----END PUBLIC KEY-----","\n","\r","\r\n"];$public_key=str_replace($search,"",$public_key);$public_key=$search[0] . PHP_EOL . wordwrap($public_key, 64, "\n", true) . PHP_EOL . $search[1];$res=openssl_get_publickey($public_key);if($res){if($type == 'RSA'){$result = (bool)openssl_verify($data, base64_decode($sign), $res);}elseif($type == 'RSA2'){$result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_SHA256);}openssl_free_key($res);}else{exit("公钥格式有误!");}return $result;}}
代码已上传csdn,可以下载查看
/download/hanyudanei/10398119