全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
楼主: vpsoffer.com
打印 上一主题 下一主题

[已解决] 不求人.自已做好WHMCS的ALIPAY接口.

[复制链接]
11#
 楼主| 发表于 2010-1-15 13:53:47 | 只看该作者
return接口
  1. <?php

  2. # Required File Includes
  3. include("../../../dbconnect.php");
  4. include("../../../includes/functions.php");
  5. include("../../../includes/gatewayfunctions.php");
  6. include("../../../includes/invoicefunctions.php");
  7. class alipay_notify {
  8.         var $gateway;           //支付接口
  9.         var $security_code;          //安全校验码
  10.         var $partner;           //合作伙伴ID
  11.         var $sign_type;         //加密方式 系统默认
  12.         var $mysign;            //签名     
  13.         var $_input_charset;    //字符编码格式
  14.         var $transport;         //访问模式
  15.         function alipay_notify($partner,$security_code,$sign_type = "MD5",$_input_charset = "GBK",$transport= "https") {
  16.                 $this->partner        = $partner;
  17.                 $this->security_code  = $security_code;
  18.                 $this->sign_type      = $sign_type;
  19.                 $this->mysign         = "";
  20.                 $this->_input_charset = $_input_charset ;
  21.                 $this->transport      = $transport;
  22.                 if($this->transport == "https") {
  23.                         $this->gateway = "https://www.alipay.com/cooperate/gateway.do?";
  24.                 }else $this->gateway = "http://notify.alipay.com/trade/notify_query.do?";
  25.         }
  26. /****************************************对notify_url的认证*********************************/
  27.         function notify_verify() {   
  28.                 if($this->transport == "https") {
  29.                         $veryfy_url = $this->gateway. "service=notify_verify" ."&partner=" .$this->partner. "&notify_id=".$_POST["notify_id"];
  30.                 } else {
  31.                         $veryfy_url = $this->gateway. "partner=".$this->partner."&notify_id=".$_POST["notify_id"];
  32.                 }
  33.                 $veryfy_result = $this->get_verify($veryfy_url);
  34.                 $post          = $this->para_filter($_POST);
  35.                 $sort_post     = $this->arg_sort($post);
  36.                 while (list ($key, $val) = each ($sort_post)) {
  37.                         $arg.=$key."=".$val."&";
  38.                 }
  39.                 $prestr = substr($arg,0,count($arg)-2);  //去掉最后一个&号
  40.                 $this->mysign = $this->sign($prestr.$this->security_code);
  41.                 //log_result("notify_url_log:sign=".$_POST["sign"]."&mysign=".$this->mysign."&".$this->charset_decode(implode(",",$_POST),$this->_input_charset ));
  42.                 if (eregi("true$",$veryfy_result) && $this->mysign == $_POST["sign"])  {
  43.                         return true;
  44.                 } else return false;
  45.         }
  46. /*******************************************************************************************/

  47. /**********************************对return_url的认证***************************************/       
  48.         function return_verify() {  
  49.                 $sort_get= $this->arg_sort($_GET);
  50.                 while (list ($key, $val) = each ($sort_get)) {
  51.                         if($key != "sign" && $key != "sign_type")
  52.                         $arg.=$key."=".$val."&";
  53.                 }
  54.                 $prestr = substr($arg,0,count($arg)-2);  //去掉最后一个&号
  55.                 $this->mysign = $this->sign($prestr.$this->security_code);
  56.                 /*while (list ($key, $val) = each ($_GET)) {
  57.                 $arg_get.=$key."=".$val."&";
  58.                 }*/
  59.                 //log_result("return_url_log=".$_GET["sign"]."&".$this->mysign."&".$this->charset_decode(implode(",",$_GET),$this->_input_charset ));
  60.                 if ($this->mysign == $_GET["sign"])  return true;
  61.                 else return false;
  62.         }
  63. /*******************************************************************************************/

  64.         function get_verify($url,$time_out = "60") {
  65.                 $urlarr     = parse_url($url);
  66.                 $errno      = "";
  67.                 $errstr     = "";
  68.                 $transports = "";
  69.                 if($urlarr["scheme"] == "https") {
  70.                         $transports = "ssl://";
  71.                         $urlarr["port"] = "443";
  72.                 } else {
  73.                         $transports = "tcp://";
  74.                         $urlarr["port"] = "80";
  75.                 }
  76.                 $fp=@fsockopen($transports . $urlarr['host'],$urlarr['port'],$errno,$errstr,$time_out);
  77.                 if(!$fp) {
  78.                         die("ERROR: $errno - $errstr
  79. \n");
  80.                 } else {
  81.                         fputs($fp, "POST ".$urlarr["path"]." HTTP/1.1\r\n");
  82.                         fputs($fp, "Host: ".$urlarr["host"]."\r\n");
  83.                         fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  84.                         fputs($fp, "Content-length: ".strlen($urlarr["query"])."\r\n");
  85.                         fputs($fp, "Connection: close\r\n\r\n");
  86.                         fputs($fp, $urlarr["query"] . "\r\n\r\n");
  87.                         while(!feof($fp)) {
  88.                                 $info[]=@fgets($fp, 1024);
  89.                         }
  90.                         fclose($fp);
  91.                         $info = implode(",",$info);
  92.                         while (list ($key, $val) = each ($_POST)) {
  93.                                 $arg.=$key."=".$val."&";
  94.                         }
  95.                         //log_result("notify_url_log=".$url.$this->charset_decode($info,$this->_input_charset));
  96.                         //log_result("notify_url_log=".$this->charset_decode($arg,$this->_input_charset));
  97.                         return $info;
  98.                 }
  99.         }

  100.         function arg_sort($array) {
  101.                 ksort($array);
  102.                 reset($array);
  103.                 return $array;
  104.         }

  105.         function sign($prestr) {
  106.                 $sign='';
  107.                 if($this->sign_type == 'MD5') {
  108.                         $sign = md5($prestr);
  109.                 }elseif($this->sign_type =='DSA') {
  110.                         //DSA 签名方法待后续开发
  111.                         die("DSA 签名方法待后续开发,请先使用MD5签名方式");
  112.                 }else {
  113.                         die("支付宝暂不支持".$this->sign_type."类型的签名方式");
  114.                 }
  115.                 return $sign;
  116.         }
  117. /***********************除去数组中的空值和签名模式*****************************/
  118.         function para_filter($parameter) {
  119.                 $para = array();
  120.                 while (list ($key, $val) = each ($parameter)) {
  121.                         if($key == "sign" || $key == "sign_type" || $val == "")continue;
  122.                         else        $para[$key] = $parameter[$key];
  123.                 }
  124.                 return $para;
  125.         }
  126. /********************************************************************************/

  127. /******************************实现多种字符编码方式*****************************/
  128.         function charset_encode($input,$_output_charset ,$_input_charset ="utf-8" ) {
  129.                 $output = "";
  130.                 if(!isset($_output_charset) )$_output_charset  = $this->parameter['_input_charset'];
  131.                 if($_input_charset == $_output_charset || $input ==null ) {
  132.                         $output = $input;
  133.                 } elseif (function_exists("mb_convert_encoding")){
  134.                         $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
  135.                 } elseif(function_exists("iconv")) {
  136.                         $output = iconv($_input_charset,$_output_charset,$input);
  137.                 } else die("sorry, you have no libs support for charset change.");
  138.                 return $output;
  139.         }
  140. /********************************************************************************/

  141. /******************************实现多种字符解码方式******************************/
  142.         function charset_decode($input,$_input_charset ,$_output_charset="utf-8"  ) {
  143.                 $output = "";
  144.                 if(!isset($_input_charset) )$_input_charset  = $this->_input_charset ;
  145.                 if($_input_charset == $_output_charset || $input ==null ) {
  146.                         $output = $input;
  147.                 } elseif (function_exists("mb_convert_encoding")){
  148.                         $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
  149.                 } elseif(function_exists("iconv")) {
  150.                         $output = iconv($_input_charset,$_output_charset,$input);
  151.                 } else die("sorry, you have no libs support for charset changes.");
  152.                 return $output;
  153.         }
  154. /*********************************************************************************/
  155. }$gatewaymodule = "alipay"; # Enter your gateway module name here replacing template
  156. $GATEWAY = getGatewayVariables($gatewaymodule);
  157. if (!$GATEWAY["type"]) die("Module Not Activated"); # Checks gateway module is active before accepting callback

  158. $_input_charset  = "utf-8";   //字符编码格式 目前支持 GBK 或 utf-8
  159. $sign_type       = "MD5";     //加密方式 系统默认(不要修改)
  160. $transport       = "https";   //访问模式,你可以根据自己的服务器是否支持ssl访问而选择http以及https访问模式(系统默认,不要修改)
  161. $gatewayPID = $GATEWAY['partnerID'];
  162. $gatewaySELLER_EMAIL = $GATEWAY['seller_email'];
  163. $gatewaySECURITY_CODE = $GATEWAY['security_code'];
  164. $alipay = new alipay_notify($gatewayPID,$gatewaySECURITY_CODE,$sign_type,$_input_charset,$transport);
  165. $verify_result = $alipay->return_verify();
  166. if(!$verify_result) {
  167.         logTransaction($GATEWAY["name"],$_GET,"Unsuccessful");
  168. }
  169. else{
  170.         # Get Returned Variables
  171.         $status = $_GET['trade_status'];    //获取支付宝传递过来的交易状态
  172.         $invoiceid = $_GET['out_trade_no']; //获取支付宝传递过来的订单号
  173.         $transid = $_GET['trade_no'];       //获取支付宝传递过来的交易号
  174.         $amount = $_GET['total_fee'];       //获取支付宝传递过来的总价格
  175.         $fee = 0;
  176.         if($status == 'TRADE_FINISHED' || $status == 'TRADE_SUCCESS') {
  177.                 $invoiceid = checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
  178.                 checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does
  179.                 addInvoicePayment($invoiceid,$transid,$amount,$fee,$gatewaymodule);
  180.                 logTransaction($GATEWAY["name"],$_POST,"Successful");
  181.         }
  182. }
  183. $url=$GATEWAY['systemurl'];
  184. echo "<script>window.location.href='$url';</script>";
  185. ?>
复制代码

评分

参与人数 2威望 +45 收起 理由
cnx + 15 精品文章
cpuer + 30 感谢分享

查看全部评分

12#
 楼主| 发表于 2010-1-15 13:54:28 | 只看该作者
后台同步接口
  1. <?php

  2. # Required File Includes
  3. include("../../../dbconnect.php");
  4. include("../../../includes/functions.php");
  5. include("../../../includes/gatewayfunctions.php");
  6. include("../../../includes/invoicefunctions.php");
  7. class alipay_notify {
  8.         var $gateway;           //支付接口
  9.         var $security_code;          //安全校验码
  10.         var $partner;           //合作伙伴ID
  11.         var $sign_type;         //加密方式 系统默认
  12.         var $mysign;            //签名     
  13.         var $_input_charset;    //字符编码格式
  14.         var $transport;         //访问模式
  15.         function alipay_notify($partner,$security_code,$sign_type = "MD5",$_input_charset = "GBK",$transport= "https") {
  16.                 $this->partner        = $partner;
  17.                 $this->security_code  = $security_code;
  18.                 $this->sign_type      = $sign_type;
  19.                 $this->mysign         = "";
  20.                 $this->_input_charset = $_input_charset ;
  21.                 $this->transport      = $transport;
  22.                 if($this->transport == "https") {
  23.                         $this->gateway = "https://www.alipay.com/cooperate/gateway.do?";
  24.                 }else $this->gateway = "http://notify.alipay.com/trade/notify_query.do?";
  25.         }
  26. /****************************************对notify_url的认证*********************************/
  27.         function notify_verify() {   
  28.                 if($this->transport == "https") {
  29.                         $veryfy_url = $this->gateway. "service=notify_verify" ."&partner=" .$this->partner. "&notify_id=".$_POST["notify_id"];
  30.                 } else {
  31.                         $veryfy_url = $this->gateway. "partner=".$this->partner."&notify_id=".$_POST["notify_id"];
  32.                 }
  33.                 $veryfy_result = $this->get_verify($veryfy_url);
  34.                 $post          = $this->para_filter($_POST);
  35.                 $sort_post     = $this->arg_sort($post);
  36.                 while (list ($key, $val) = each ($sort_post)) {
  37.                         $arg.=$key."=".$val."&";
  38.                 }
  39.                 $prestr = substr($arg,0,count($arg)-2);  //去掉最后一个&号
  40.                 $this->mysign = $this->sign($prestr.$this->security_code);
  41.                 //log_result("notify_url_log:sign=".$_POST["sign"]."&mysign=".$this->mysign."&".$this->charset_decode(implode(",",$_POST),$this->_input_charset ));
  42.                 if (eregi("true$",$veryfy_result) && $this->mysign == $_POST["sign"])  {
  43.                         return true;
  44.                 } else return false;
  45.         }
  46. /*******************************************************************************************/

  47. /**********************************对return_url的认证***************************************/       
  48.         function return_verify() {  
  49.                 $sort_get= $this->arg_sort($_GET);
  50.                 while (list ($key, $val) = each ($sort_get)) {
  51.                         if($key != "sign" && $key != "sign_type")
  52.                         $arg.=$key."=".$val."&";
  53.                 }
  54.                 $prestr = substr($arg,0,count($arg)-2);  //去掉最后一个&号
  55.                 $this->mysign = $this->sign($prestr.$this->security_code);
  56.                 /*while (list ($key, $val) = each ($_GET)) {
  57.                 $arg_get.=$key."=".$val."&";
  58.                 }*/
  59.                 //log_result("return_url_log=".$_GET["sign"]."&".$this->mysign."&".$this->charset_decode(implode(",",$_GET),$this->_input_charset ));
  60.                 if ($this->mysign == $_GET["sign"])  return true;
  61.                 else return false;
  62.         }
  63. /*******************************************************************************************/

  64.         function get_verify($url,$time_out = "60") {
  65.                 $urlarr     = parse_url($url);
  66.                 $errno      = "";
  67.                 $errstr     = "";
  68.                 $transports = "";
  69.                 if($urlarr["scheme"] == "https") {
  70.                         $transports = "ssl://";
  71.                         $urlarr["port"] = "443";
  72.                 } else {
  73.                         $transports = "tcp://";
  74.                         $urlarr["port"] = "80";
  75.                 }
  76.                 $fp=@fsockopen($transports . $urlarr['host'],$urlarr['port'],$errno,$errstr,$time_out);
  77.                 if(!$fp) {
  78.                         die("ERROR: $errno - $errstr
  79. \n");
  80.                 } else {
  81.                         fputs($fp, "POST ".$urlarr["path"]." HTTP/1.1\r\n");
  82.                         fputs($fp, "Host: ".$urlarr["host"]."\r\n");
  83.                         fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  84.                         fputs($fp, "Content-length: ".strlen($urlarr["query"])."\r\n");
  85.                         fputs($fp, "Connection: close\r\n\r\n");
  86.                         fputs($fp, $urlarr["query"] . "\r\n\r\n");
  87.                         while(!feof($fp)) {
  88.                                 $info[]=@fgets($fp, 1024);
  89.                         }
  90.                         fclose($fp);
  91.                         $info = implode(",",$info);
  92.                         while (list ($key, $val) = each ($_POST)) {
  93.                                 $arg.=$key."=".$val."&";
  94.                         }
  95.                         //log_result("notify_url_log=".$url.$this->charset_decode($info,$this->_input_charset));
  96.                         //log_result("notify_url_log=".$this->charset_decode($arg,$this->_input_charset));
  97.                         return $info;
  98.                 }
  99.         }

  100.         function arg_sort($array) {
  101.                 ksort($array);
  102.                 reset($array);
  103.                 return $array;
  104.         }

  105.         function sign($prestr) {
  106.                 $sign='';
  107.                 if($this->sign_type == 'MD5') {
  108.                         $sign = md5($prestr);
  109.                 }elseif($this->sign_type =='DSA') {
  110.                         //DSA 签名方法待后续开发
  111.                         die("DSA 签名方法待后续开发,请先使用MD5签名方式");
  112.                 }else {
  113.                         die("支付宝暂不支持".$this->sign_type."类型的签名方式");
  114.                 }
  115.                 return $sign;
  116.         }
  117. /***********************除去数组中的空值和签名模式*****************************/
  118.         function para_filter($parameter) {
  119.                 $para = array();
  120.                 while (list ($key, $val) = each ($parameter)) {
  121.                         if($key == "sign" || $key == "sign_type" || $val == "")continue;
  122.                         else        $para[$key] = $parameter[$key];
  123.                 }
  124.                 return $para;
  125.         }
  126. /********************************************************************************/

  127. /******************************实现多种字符编码方式*****************************/
  128.         function charset_encode($input,$_output_charset ,$_input_charset ="utf-8" ) {
  129.                 $output = "";
  130.                 if(!isset($_output_charset) )$_output_charset  = $this->parameter['_input_charset'];
  131.                 if($_input_charset == $_output_charset || $input ==null ) {
  132.                         $output = $input;
  133.                 } elseif (function_exists("mb_convert_encoding")){
  134.                         $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
  135.                 } elseif(function_exists("iconv")) {
  136.                         $output = iconv($_input_charset,$_output_charset,$input);
  137.                 } else die("sorry, you have no libs support for charset change.");
  138.                 return $output;
  139.         }
  140. /********************************************************************************/

  141. /******************************实现多种字符解码方式******************************/
  142.         function charset_decode($input,$_input_charset ,$_output_charset="utf-8"  ) {
  143.                 $output = "";
  144.                 if(!isset($_input_charset) )$_input_charset  = $this->_input_charset ;
  145.                 if($_input_charset == $_output_charset || $input ==null ) {
  146.                         $output = $input;
  147.                 } elseif (function_exists("mb_convert_encoding")){
  148.                         $output = mb_convert_encoding($input,$_output_charset,$_input_charset);
  149.                 } elseif(function_exists("iconv")) {
  150.                         $output = iconv($_input_charset,$_output_charset,$input);
  151.                 } else die("sorry, you have no libs support for charset changes.");
  152.                 return $output;
  153.         }
  154. /*********************************************************************************/
  155. }
  156. $gatewaymodule = "alipay"; # Enter your gateway module name here replacing template
  157. $GATEWAY = getGatewayVariables($gatewaymodule);
  158. if (!$GATEWAY["type"]) die("Module Not Activated"); # Checks gateway module is active before accepting callback

  159. $_input_charset  = "utf-8";   //字符编码格式 目前支持 GBK 或 utf-8
  160. $sign_type       = "MD5";     //加密方式 系统默认(不要修改)
  161. $transport       = "https";   //访问模式,你可以根据自己的服务器是否支持ssl访问而选择http以及https访问模式(系统默认,不要修改)
  162. $gatewayPID = $GATEWAY['partnerID'];
  163. $gatewaySELLER_EMAIL = $GATEWAY['seller_email'];
  164. $gatewaySECURITY_CODE = $GATEWAY['security_code'];
  165. $alipay = new alipay_notify($gatewayPID,$gatewaySECURITY_CODE,$sign_type,$_input_charset,$transport);
  166. $verify_result = $alipay->notify_verify();
  167. if(!$verify_result) {
  168.         logTransaction($GATEWAY["name"],$_POST,"Unsuccessful");
  169.         exit;
  170. }
  171. # Get Returned Variables
  172. $status = $_POST['trade_status'];    //获取支付宝传递过来的交易状态
  173. $invoiceid = $_POST['out_trade_no']; //获取支付宝传递过来的订单号
  174. $transid = $_POST['trade_no'];       //获取支付宝传递过来的交易号
  175. $amount = $_POST['total_fee'];       //获取支付宝传递过来的总价格
  176. $fee = 0;
  177. if($status == 'TRADE_FINISHED' || $status == 'TRADE_SUCCESS') {
  178.         $invoiceid = checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
  179.         checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does
  180.         addInvoicePayment($invoiceid,$transid,$amount,$fee,$gatewaymodule);
  181.         logTransaction($GATEWAY["name"],$_POST,"Successful");
  182. }
  183. ?>
复制代码

评分

参与人数 2威望 +45 收起 理由
cnx + 15 精品文章
cpuer + 30 感谢分享

查看全部评分

13#
发表于 2010-1-15 13:58:24 | 只看该作者
感谢楼主分享代码,先收藏了
14#
发表于 2010-1-15 14:02:38 | 只看该作者
楼主很好淘宝上面鸟的都要300块钱XXX
15#
发表于 2010-1-15 14:08:43 | 只看该作者

回复 14# 的帖子

很贵,呵呵。
16#
发表于 2010-1-15 14:10:45 | 只看该作者
我有个朋友送的。。没想过这东西还要卖钱~
17#
发表于 2010-1-15 14:12:00 | 只看该作者

回复 16# 的帖子

这东西有了后搞个接口就行了
18#
发表于 2010-1-15 14:31:05 | 只看该作者
看来备案个国内空间用破解了
19#
发表于 2010-1-15 14:33:33 | 只看该作者
感谢楼主分享。

从开发人员的角度去说,像这东西,收费是正常的。

人家不会无缘无故的给你帮忙,就是平时你找朋友帮忙,也还要请人家吃顿饭。

当然,这个世界上也有很多雷锋,我就见过免费给人家做企业网站不要钱的。

而且像楼主这样需要二次开发的电子商务项目,收费就更正常不过了。因为你不可能要别人无偿给你干活,然后你拿着别人的成果去赚钱。这个说不过去的。

二次开发的价格没有标准,有贵有便宜,有人白菜价,也有人高收费。这也决定了为什么同样的工作,大家的收入相差很大。
20#
发表于 2010-1-15 14:37:11 | 只看该作者
玩编程的都是人才啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-20 08:04 , Processed in 0.073984 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表