headermask image

header image

支付宝支付接口开发测试成功!

        这两天集中时间开发了she‘s的php版本的支付宝接口,支付宝的官方网站只提供了
jsp和asp版本的接口的示例代码,竟然不提供PHP版本接口代码,

我粗略的看了下jsp版本的代码,
估计是和我找到的php代码一样(在支付宝论坛里)都是热心网友提供的。我是J2ee程序员,根本不懂
PHP,让我按照支付宝的交易服务集成技术文档硬写一个接口出来实在有些难度,

最重要的一点是支付宝不提供这个文档以外的支持。还好有热心的网友分享他们的代码。
目前网络上面流传的PHP接口的代码主要有两个一个是basedzfbphp版本。另外一个是版本,前者只支持支付宝1.0的接口(我弄完才知道又换成后者)。

目前支付宝已经升级到2.0,所以大家可以参考
phpshiwu 这个版本或者参考我刚弄完的这个(基于PHPshiwu)
$partner = "";//合作伙伴ID
$security_code = "";//安全检验码
$seller_email = "";//卖家邮箱
$_input_charset = "utf-8"; //字符编码格式
$sign_type = "MD5"; //加密方式
$transport= "http"; //访问模式,你可以根据自己的服务器是否支持ssl访问而选择http以及https访问模式
$notify_url = ""; // 异步返回地址
$return_url = ""; //同步返回地址
$parameter = array(
"service" => "trade_create_by_buyer", //交易类型,必填实物交易=trade_create_by_buyer(需要填写物流) 虚拟物品交易=create_digital_goods_trade_p 捐赠=create_donate_trade_p
"partner" =>$partner, //合作商户号
"return_url" =>$return_url, //同步返回
"notify_url" =>$notify_url, //异步返回
"_input_charset" => $_input_charset, //字符集,默认为GBK
"subject" => "test", //商品名称,必填
"body" => "test", //商品描述,必填
"out_trade_no" => $db->f("order_id"), //商品外部交易号,必填,每次测试都须修改
"price" => round( $db->f("order_subtotal")+$tax_total-$discount_total, 2), //商品单价,必填
"discount"=>"", //折扣
"payment_type"=>"1", // 商品支付类型 1 =商品购买 2=服务购买 3=网络拍卖 4=捐赠 5=邮费补偿 6=奖金
"quantity" =>"1", //商品数量,必填
"show_url" => "http://www.yoursite.com", //商品相关网站
"logistics_type"=>"EXPRESS", //物流类型:VIRTUAL=虚拟物品 POST=平邮 EMS=EMS EXPRESS=其他快递公司
"logistics_fee"=>"0", //物流费用
"logistics_payment"=>"SELLER_PAY", //物流支付类型: SELLER_PAY=卖家支付 BUYER_PAY=买家支付 BUYER_PAY_AFTER_RECEIVE=货到付款
"seller_email" => $seller_email, //卖家邮箱,必填
  //卖家邮箱,必填
//"receive_name" => $dbbt->f('last_name'), //这三个参数在调试的时候出错,放弃!
//"receive_address" =>$dbbt->f('address_1') ,
//"receive_zip" => $dbbt->f('zip'),
);
$alipay = new alipay_service($parameter,$security_code,$sign_type);
$link=$alipay->create_url();
mosRedirect($link); //自动跳转
print << submit
EOT;
class alipay_notify {
 var $gateway;
 var $security_code; //安全校验码
 var $partner;
 var $sign_type;
 var $mysign;
 var $_input_charset ;
 var $transport;
 function alipay_notify($partner,$security_code,$sign_type = "MD5",$_input_charset = "GBK",$transport= "https") {
  $this->partner = $partner;
  $this->security_code = $security_code;
  $this->sign_type = $sign_type;
  $this->mysign = "";
  $this->_input_charset = $_input_charset ;
  $this->transport = $transport;
  if($this->transport == "https") {
  $this->gateway = "https://www.alipay.com/cooperate/gateway.do?";
  } else $this->gateway = "http://notify.alipay.com/trade/notify_query.do?";

}
 function notify_verify() { //对notify_url的认证
  if($this->transport == "https") {
  $veryfy_url = $this->gateway. "service=notify_verify" ."&partner=" .$this->partner. "¬ify_id=".$_POST["notify_id"];
  } else {
  $veryfy_url = $this->gateway. "notify_id=".$_POST["notify_id"]."&partner=" .$this->partner;
  }
  $veryfy_result = $this->get_verify($veryfy_url);
  $post = $this->para_filter($_POST);
  $sort_post = $this->arg_sort($post);
  while (list ($key, $val) = each ($sort_post)) {
  $arg.=$key."=".$val."&";
  }
  $prestr = substr($arg,0,count($arg)-2); //去掉最后一个&号
  $this->mysign = $this->sign($prestr.$this->security_code);
  if (eregi("true$",$veryfy_result) && $this->mysign == $_POST["sign"]) {
  return true;
  } else return false;
 }
 function return_verify() { //对return_url的认证
  if($this->transport == "https") {
  $veryfy_url = $this->gateway. "service=notify_verify" ."&partner=" .$this->partner. "¬ify_id=".$_GET["notify_id"];
  } else {
  $veryfy_url = $this->gateway. "notify_id=".$_GET["notify_id"]."&partner=" .$this->partner;
  }
  $veryfy_result = $this->get_verify($veryfy_url);
  $GET = $this->para_filter($_GET);
  $sort_get= $this->arg_sort($_GET);
  while (list ($key, $val) = each ($sort_get)) {
  if($key != "sign" && $key != "sign_type")
  $arg.=$key."=".$val."&";
  }
  $prestr = substr($arg,0,count($arg)-2); //去掉最后一个&号
  $this->mysign = $this->sign($prestr.$this->security_code);

//log_result("return_url_log=".$_GET["sign"]."&".$this->mysign."&".$this->charset_decode(implode(",",$_GET),$this->_input_charset ));
 //**********************************上面写日志
  if (eregi("true$",$veryfy_result) && $this->mysign == $_GET["sign"]) {
  return true;
  }else return false;
 }

function get_verify($url,$time_out = "60") {
  $urlarr = parse_url($url);
  $errno = "";
  $errstr = "";
  $transports = "";
  if($urlarr["scheme"] == "https") {
  $transports = "ssl://";
  $urlarr["port"] = "443";
  } else {
  $transports = "tcp://";
  $urlarr["port"] = "80";
  }
  $fp=@fsockopen($transports . $urlarr['host'],$urlarr['port'],$errno,$errstr,$time_out);
  if(!$fp) {
  die("ERROR: $errno - $errstr
\n");
  } else {
  fputs($fp, "POST ".$urlarr["path"]." HTTP/1.1\r\n");
  fputs($fp, "Host: ".$urlarr["host"]."\r\n");
  fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  fputs($fp, "Content-length: ".strlen($urlarr["query"])."\r\n");
  fputs($fp, "Connection: close\r\n\r\n");
  fputs($fp, $urlarr["query"] . "\r\n\r\n");
  while(!feof($fp)) {
  $info[]=@fgets($fp, 1024);
  }

fclose($fp);
  $info = implode(",",$info);
  while (list ($key, $val) = each ($_POST)) {
  $arg.=$key."=".$val."&";
  }
  log_result("return_url_log=".$url.$this->charset_decode($info,$this->_input_charset));
  log_result("return_url_log=".$this->charset_decode($arg,$this->_input_charset));
  return $info;
  }

}

function arg_sort($array) {
  ksort($array);
  reset($array);
  return $array;

}

function sign($prestr) {
  $sign='';
  if($this->sign_type == 'MD5') {
  $sign = md5($prestr);
  }elseif($this->sign_type =='DSA') {
  //DSA 签名方法待后续开发
  die("DSA 签名方法待后续开发,请先使用MD5签名方式");
  }else {
  die("支付宝暂不支持".$this->sign_type."类型的签名方式");
  }
  return $sign;

}
 function para_filter($parameter) { //除去数组中的空值和签名模式
  $para = array();
  while (list ($key, $val) = each ($parameter)) {
  if($key == "sign" || $key == "sign_type" || $val == "")continue;
  else $para[$key] = $parameter[$key];

}
  return $para;
 }

//实现多种字符编码方式
 function charset_encode($input,$_output_charset ,$_input_charset ="GBK" ) {
  $output = "";
  if(!isset($_output_charset) )$_output_charset = $this->parameter['_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 charset_decode($input,$_input_charset ,$_output_charset="GBK" ) {
  $output = "";
  if(!isset($_input_charset) )$_input_charset = $this->_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;
 }
}
class alipay_service {

var $gateway = "https://www.alipay.com/cooperate/gateway.do?"; //支付接口
 var $parameter; //全部需要传递的参数
 var $security_code; //安全校验码
 var $mysign; //签名

//构造支付宝外部服务接口控制
 function alipay_service($parameter,$security_code,$sign_type = "MD5",$transport= "https") {
  $this->parameter = $this->para_filter($parameter);
  $this->security_code = $security_code;
  $this->sign_type = $sign_type;
  $this->mysign = '';
  $this->transport = $transport;
  if($parameter['_input_charset'] == "")
  $this->parameter['_input_charset']='utf-8';
  if($this->transport == "https") {
  $this->gateway = "https://www.alipay.com/cooperate/gateway.do?";
  } else $this->gateway = "http://www.alipay.com/cooperate/gateway.do?";
  $sort_array = array();
  $arg = "";
  $sort_array = $this->arg_sort($this->parameter);
  while (list ($key, $val) = each ($sort_array)) {
  $arg.=$key."=".$this->charset_encode($val,$this->parameter['_input_charset'])."&";
  }
  $prestr = substr($arg,0,count($arg)-2); //去掉最后一个问号
  print_r($prestr);
  $this->mysign = $this->sign($prestr.$this->security_code);
  print_r("aa");
  print_r($this->mysign);
 }
 function create_url() {
  $url = $this->gateway;
  $sort_array = array();
  $arg = "";
  $sort_array = $this->arg_sort($this->parameter);
  while (list ($key, $val) = each ($sort_array)) {
  $arg.=$key."=".urlencode($this->charset_encode($val,$this->parameter['_input_charset']))."&";
  }
  $url.= $arg."sign=" .$this->mysign ."&sign_type=".$this->sign_type;
  return $url;

}

function arg_sort($array) {
  ksort($array);
  reset($array);
  return $array;

}

function sign($prestr) {
  $mysign = "";
  if($this->sign_type == 'MD5') {
  $mysign = md5($prestr);
  //$mysign = $prestr;
  }elseif($this->sign_type =='DSA') {
  //DSA 签名方法待后续开发
  die("DSA 签名方法待后续开发,请先使用MD5签名方式");
  }else {
  die("支付宝暂不支持".$this->sign_type."类型的签名方式");
  }
  return $mysign;

}
 function para_filter($parameter) { //除去数组中的空值和签名模式
  $para = array();
  while (list ($key, $val) = each ($parameter)) {
  if($key == "sign" || $key == "sign_type" || $val == "")continue;
  else $para[$key] = $parameter[$key];

}
  return $para;
 }
 //实现多种字符编码方式
 function charset_encode($input,$_output_charset ,$_input_charset ="utf-8" ) {
  $output = "";
  if(!isset($_output_charset) )$_output_charset = $this->parameter['_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;
 }

}
?>



If you liked my post, feel free to subscribe to my rss feeds

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*