给客户开发网站时需要用到支付宝在线付款功能,小云到thinkphp网站溜了一圈,代码是有,可是都不怎么全,因此这篇文章诞生了!
本篇文章讲解了三个类的实现,担保交易、即时到账、双功能收款(该功能支付宝已下线,可给之前已申请过的站长短时间内的使用)
类文件在附件类,请自行下载文章主要讲解如何使用本类
文件下载后解压得到 PayBuy 将本文件夹放到 ThinkLibraryVendor 目录下
然后在 config.PHP中配置一下信息
- 'ALIPAY_PARTNER' => '8888888888888888',//合作身份者id,以2088开头的16位纯数字
- 'ALIPAY_SELLER_EMAIL' => 'xxxxxxxxxx@163.com',//收款账号
- 'ALIPAY_KEY' => 'xxxxxxxxxxxxxxxxxxxxxxxx',//安全检验码,以数字和字母组成的32位
在 function.php 构造付款函数
第一个付款订单生成函数:
- function pay($data){
- $data['expenses'] = isset($data['expenses']) && $data['expenses'] ? $data['expenses'] : "0.00";
- $data['express'] = isset($data['express']) && $data['express'] ? $data['express'] : 'EXPRESS';
- $data['payment'] = isset($data['payment']) && $data['payment'] ? $data['payment'] : 'BUYER_PAY';
- Vendor('PayBuy.AliPay');
- $pay = new AliPay(); //实例化
- $pay->partner = C('ALIPAY_PARTNER'); //合作身份者id,以2088开头的16位纯数字
- $pay->seller_email = C('ALIPAY_SELLER_EMAIL'); //收款账号
- $pay->paykey = C('ALIPAY_KEY'); //安全检验码,以数字和字母组成的32位字符
- $pay->notify_url = $data['notify_url']; //服务器异步通知页面路径,不带参数
- $pay->return_url = $data['return_url']; //服务器同步通知页面路径,不带参数
- $pay->orderid = $data['orderid']; //必填,订单id
- $pay->subject = $data['subject']; //必填商品名称
- $pay->price = $data['price']; //必填,价格
- $pay->expenses = $data['expenses']; //必填,运费
- $pay->express = $data['express']; //必填,三个值可选:EXPRESS(快递)、POST(平邮)、EMS(EMS)
- $pay->payment = $data['payment']; //必填,两个值可选:SELLER_PAY(卖家承担运费)、BUYER_PAY(买家承担运费)
- $pay->message = $data['message']; //订单描述
- $pay->show_url = $data['show_url']; //商品展示地址
- $pay->receive_name = $data['receive_name']; //收货人姓名
- $pay->receive_address = $data['receive_address']; //收货地址
- $pay->receive_zip = $data['receive_zip']; //邮编
- $pay->receive_phone = $data['receive_phone']; //收货人座机
- $pay->receive_mobile = $data['receive_mobile']; //收货人手机
- $pay->waitword = $data['waitword']; //收货人手机
- return $pay->AliPay();
- }
第二个检查订单状态函数:
- function checkpayorder($data=''){
- $alipay_config['partner'] = C('ALIPAY_PARTNER');
- $alipay_config['seller_email'] = C('ALIPAY_SELLER_EMAIL');
- $alipay_config['key'] = C('ALIPAY_KEY');
- $alipay_config['sign_type'] = strtoupper('MD5');
- $alipay_config['input_charset']= strtolower('utf-8');
- $alipay_config['transport'] = 'http';
- Vendor('PayBuy.AliPay');
- $alipayNotify = new AlipayNotify($alipay_config);
- $verify_result = $alipayNotify->verifyReturn();
- if($verify_result) {
- $out_trade_no = $data['out_trade_no'];
- $trade_no = $data['trade_no'];
- $trade_status = $data['trade_status'];
- return $data['trade_status'];
- }else {
- return "error";
- }
- }
第三函数集:(这几个函数是支付宝自带函数,请直接复制粘贴到 function.php 内)
- /*支付宝接口函数开始*/
- 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;
- }
- function createLinkstringUrlencode($para) {
- $arg = "";
- while (list ($key, $val) = each ($para)) {
- $arg.=$key."=".urlencode($val)."&";
- }
- //去掉最后一个&字符
- $arg = substr($arg,0,count($arg)-2);
- //如果存在转义字符,那么去掉转义
- if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
- return $arg;
- }
- 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;
- }
- function argSort($para) {
- ksort($para);
- reset($para);
- return $para;
- }
- function logResult($word='') {
- $fp = fopen("log.txt","a");
- flock($fp, LOCK_EX) ;
- fwrite($fp,"执行日期:".strftime("%Y%m%d%H%M%S",time())." ".$word." ");
- flock($fp, LOCK_UN);
- fclose($fp);
- }
- function getHttpResponsePOST($url, $cacert_url, $para, $input_charset = '') {
- if (trim($input_charset) != '') {
- $url = $url."_input_charset=".$input_charset;
- }
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);//SSL证书认证
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格认证
- curl_setopt($curl, CURLOPT_CAINFO,$cacert_url);//证书地址
- curl_setopt($curl, CURLOPT_HEADER, 0 ); // 过滤HTTP头
- curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);// 显示输出结果
- curl_setopt($curl,CURLOPT_POST,true); // post传输数据
- curl_setopt($curl,CURLOPT_POSTFIELDS,$para);// post传输数据
- $responseText = curl_exec($curl);
- //var_dump( curl_error($curl) );//如果执行curl过程中出现异常,可打开此开关,以便查看异常内容
- curl_close($curl);
- return $responseText;
- }
- 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过程中出现异常,可打开此开关,以便查看异常内容
- curl_close($curl);
- return $responseText;
- }
- function charsetEncode($input,$_output_charset ,$_input_charset) {
- $output = "";
- if(!isset($_output_charset) )$_output_charset = $_input_charset;
- if($_input_charset == $_output_charset || $input ==null ) {
- $output = $input;
- } elseif (function_exists("mb_convert_encoding")) {
- $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
- } elseif(function_exists("iconv")) {
- $output = iconv($_input_charset,$_output_charset,$input);
- } else die("sorry, you have no libs support for charset change.");
- return $output;
- }
- function charsetDecode($input,$_input_charset ,$_output_charset) {
- $output = "";
- if(!isset($_input_charset) )$_input_charset = $_input_charset ;
- if($_input_charset == $_output_charset || $input ==null ) {
- $output = $input;
- } elseif (function_exists("mb_convert_encoding")) {
- $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
- } elseif(function_exists("iconv")) {
- $output = iconv($_input_charset,$_output_charset,$input);
- } else die("sorry, you have no libs support for charset changes.");
- return $output;
- }
- function md5Sign($prestr, $key) {
- $prestr = $prestr . $key;
- return md5($prestr);
- }
- function md5Verify($prestr, $sign, $key) {
- $prestr = $prestr . $key;
- $mysgin = md5($prestr);
- if($mysgin == $sign) {
- return true;
- }
- else {
- return false;
- }
- }
- /*支付宝接口函数结束*/