登录  /  注册

PHP支付宝开发之服务窗API

*文
发布: 2017-12-29 18:15:07
原创
1315人浏览过

本文主要介绍了php版本的支付宝服务窗api接口开发,感兴趣的小伙伴们可以参考一下。希望对大家有所帮助。

支付宝服务窗API接口的开发对于许多网站要充值的朋友来讲是非常的重要的,今天我们就一起来看一篇关于php版本的支付宝服务窗API接口的开发例子。

这两天没事要接入支付宝服务窗,看支付宝的DEMO,我的神,我怎么评价好呢?阅读性不是很好,很阻碍简单的开发。所以我就根据提供的API简单的开发了点,接口还有很多不完善,有兴趣的可以自己完善一下,下边我就把代码贴出来,有时间再写如何使用。

<?php 
class AlipayService{ 
 /** 
 - 服务接口信息 
 */ 
 public $service = null; 
 /** 
 - 签名信息 
 */ 
 public $sign = null; 
 /** 
 - 签名类型 
 */ 
 public $sign_type = null; 
 /** 
 - 字符集 
 */ 
 public $charset = null; 
 /** 
 - 解析的biz_content数据 
 */ 
 public $request = null; 
 /** 
 - 用户openid 
 */ 
 public $from_user_id = null; 
 /** 
 - 消息类型 
 */ 
 public $msg_type = null; 
 /** 
 - 事件类型 
 */ 
 public $event_type = null; 
 /** 
 - 行为参数 
 */ 
 public $action_param = null; 
 /** 
 - 支付宝用户信息 
 */ 
 public $user_info = null; 
 /** 
 - 文本消息内容 
 */ 
 public $text = null; 
 /** 
 - 图片媒体id 
 */ 
 public $media_id = null; 
 /** 
 - 图片格式 
 */ 
 public $format = null; 
 /** 
 - 是否开启调试 
 */ 
 private $debug = false; 
 /** 
 - 接口类型 
 */ 
 private $interface_type = array( 
  &#39;qrcode&#39; => &#39;alipay.mobile.public.qrcode.create&#39;, 
  &#39;follow&#39; => &#39;alipay.mobile.public.follow.list&#39;, 
  &#39;gis_get&#39; => &#39;alipay.mobile.public.gis.get&#39;, 
  &#39;menu_get&#39; => &#39;alipay.mobile.public.menu.get&#39;,  
  &#39;menu_add&#39; => &#39;alipay.mobile.public.menu.add&#39;, 
  &#39;down_media&#39; => &#39;alipay.mobile.public.multimedia.download&#39;, 
  &#39;menu_update&#39; => &#39;alipay.mobile.public.menu.update&#39;, 
  &#39;info_query&#39; => &#39;alipay.mobile.public.info.query&#39;, 
  &#39;info_modify&#39; => &#39;alipay.mobile.public.info.modify&#39;, 
  &#39;shortlink&#39; => &#39;alipay.mobile.public.shortlink.create&#39;, 
  &#39;label_add&#39; => &#39;alipay.mobile.public.label.add&#39;, 
  &#39;label_del&#39; => &#39;alipay.mobile.public.label.delete&#39;, 
  &#39;label_update&#39; => &#39;alipay.mobile.public.label.update&#39;, 
  &#39;label_query&#39;  => &#39;alipay.mobile.public.label.query&#39;, 
  &#39;label_user_add&#39; => &#39;alipay.mobile.public.label.user.add&#39;, 
  &#39;label_user_del&#39; => &#39;alipay.mobile.public.label.user.delete&#39;, 
  &#39;label_user_query&#39; => &#39;alipay.mobile.public.label.user.query&#39;, 
  &#39;message_custom&#39; => &#39;alipay.mobile.public.message.custom.send&#39;, 
  &#39;message_total&#39; => &#39;alipay.mobile.public.message.total.send&#39;, 
  &#39;message_single&#39; => &#39;alipay.mobile.public.message.single.send&#39;, 
  &#39;message_label_send&#39; => &#39;alipay.mobile.public.message.label.send&#39;, 
 ); 
 /** 
 - 私有密钥地址,替换为你自己的 
 */ 
 private $private_rsa_key_path =&#39;rsa_private_key.pem&#39;; 
 /** 
 - 私有密钥地址,替换为你自己的 
 */ 
 private $public_rsa_key_path =&#39;rsa_public_key.pem&#39;; 
 /** 
 - 支付宝窗的app id 替换成你自己的 
 */ 
 private $app_id = &#39;2015120200901652&#39;; 
 /** 
 - 开启DEBUG参数 
 - @params bool debug true 开启调试 false 关闭调试 
 - @author widuu <admin@widuu.com> 
 */ 
 public function __construct( $debug = false ){ 
 /* 是否开启DEBUG */ 
 if( $debug ) $this->debug = true; 
 } 
 /** 
 - 获取参数,解析请求参数 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function get_request(){ 
 if( !emptyempty($_POST) ){ 
  // 请求的服务接口 
  $this->service = $_POST[&#39;service&#39;]; 
  // 获取请求字符集 
  $this->charset = $_POST[&#39;charset&#39;]; 
  // 获取请求的biz_content 
  $request_biz_content = $_POST[&#39;biz_content&#39;]; 
  // 加密算法 
  $this->sign_type = $_POST[&#39;sign_type&#39;]; 
  // 加密字符串 
  $this->sign = $_POST[&#39;sign&#39;]; 
  // 如果请求格式不是Utf-8 转换格式为Utf-8 
  if( strtolower($this->charset) != &#39;utf-8&#39; ){ 
  $request_biz_content = iconv(&#39;GBK&#39;, &#39;utf-8&#39;, $request_biz_content); 
  } 
  // 解析字符串为xml 
  $request_xml = @simplexml_load_string($request_biz_content, "SimpleXMLElement" , LIBXML_NOCDATA ); 
  // 解析为数组 
  $request_array = json_decode(json_encode($request_xml),true); 
  $this->request = $request_array; 
  /* 解析 */ 
  $this->analysis($request_array); 
  if($this->debug) $this->write_log(&#39;REQUEST_INFO&#39;,var_export($request_array,true)); 
  // 默认验证方法 
  if( $this->service == &#39;alipay.service.check&#39;){ 
  $this->verify($_POST); 
  exit(); 
  } 
  /* 返回结果 */ 
  return $request_array; 
 } 
 } 
 /** 
 - 回复文本内容 
 - @params string content 文本数据 
 - @params bool mass ture为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 public function text($content,$mass=false){ 
 $info[&#39;text&#39;] = array( &#39;content&#39; => $content ); 
 /* 组织内容 */ 
 $biz_content = $this->common_response(&#39;text&#39;,$info,$mass); 
 /* 判断是否为群发 */ 
 if($mass){ 
  $method = &#39;message_total&#39;; 
 }else{ 
  $method = &#39;message_custom&#39;; 
 } 
 $sys_params = $this->common_system($method,$biz_content); 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回结果 结果是JSON数据 */ 
 $result = $this->response_post($sys_params); 
 return $result; 
 } 
 /** 
 - 回复图文内容 
 - @params array articles 拼接的图文消息数组 
 - @params bool mass ture为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 public function articles($articles,$mass=false){ 
 $info[&#39;articles&#39;] = array($articles); 
 /* 组织内容 */ 
 $biz_content = $this->common_response(&#39;image-text&#39;,$info,$mass); 
 /* 判断是否群发 */ 
 if($mass){ 
  $method = &#39;message_total&#39;; 
 }else{ 
  $method = &#39;message_custom&#39;; 
 } 
 /* 加密参数 */ 
 $sys_params = $this->common_system($method,$biz_content); 
 /* 加密字符 */ 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回结果 结果是JSON数据 */ 
 $result = $this->response_post($sys_params); 
 return $result; 
 } 
 /** 
 - 关注事件 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function is_follow(){ 
 $request = $this->request; 
 if( $request[&#39;MsgType&#39;] == &#39;event&#39; && $request[&#39;EventType&#39;] == &#39;follow&#39; ){ 
  return true; 
 }else{ 
  return false; 
 } 
 } 
 /** 
 - 取消关注事件 
 - 
 - @author widuu <admin@widuu.com> 
 */ 
 public function is_unfollow(){ 
 $request = $this->request; 
 if( $request[&#39;MsgType&#39;] == &#39;event&#39; && $request[&#39;EventType&#39;] == &#39;unfollow&#39; ){ 
  return true; 
 }else{ 
  return false; 
 } 
 } 
 /** 
 - 下载用户发来的图片 
 - @param media_id string 图片id 
 - @param filename string 保存图片地址和名称 
 - @author widuu <admin@widuu.com> 
 */ 
 public function down_media($media_id,$filename){ 
 $sys_params = $this->common_system(&#39;down_media&#39;,array(&#39;mediaId&#39;=>$media_id)); 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 返回数据 */ 
 $result = $this->response_post($sys_params,true); 
 $result = file_put_contents($filename, $result); 
 if( $this->debug ){ 
  $this->write_log(&#39;SAVE_IMAGE&#39;,&#39;保存图片&#39;.(string)$result); 
 } 
 return $result; 
 } 
 /** 
 - (添加|更新|获取)自定义菜单 
 - @param string $type (add|update|get) 
 - @param array $menu 菜单数组,如果是获取菜单可以留空 
 - @author widuu <admin@widuu.com> 
 */ 
 public function menu( $type,$menu = array() ){ 
 if( !in_array( $type, array(&#39;get&#39;,&#39;update&#39;,&#39;add&#39;)) ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;菜单操作方法错误&#39;); 
  } 
  return false; 
 } 
 /* 拼接接口方法 */ 
 $method = &#39;menu_&#39;.$type; 
 $sys_params = $this->common_system($method,$menu); 
 /* 加密字符串 */ 
 $sys_params[&#39;sign&#39;] = $this->rsa_sign($this->build_query($sys_params)); 
 /* 请求获取结果 */ 
 $result = $this->response_post($sys_params); 
 /* 转义并解析JSON 数据 */ 
 $menu_json = json_decode(iconv(&#39;GBK&#39;, &#39;utf-8&#39;, $result),true); 
 /* 组织接口信息 */ 
 $interface = &#39;alipay_mobile_public_&#39;.$method.&#39;_response&#39;; 
 /* 遇到错误返回 */ 
 if( $menu_json[$interface][&#39;code&#39;] != 200 ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;GET_MENU_ERROR&#39;,$menu_json[$interface][&#39;msg&#39;]); 
  } 
  return false; 
 } 
 /* 根据类型不同返回不同的结果 */ 
 if( $type == &#39;get&#39; ){ 
  return $menu_json[$interface][&#39;menu_content&#39;]; 
 }else{ 
  return $menu_json[$interface][&#39;msg&#39;]; 
 } 
 } 
 
 /** 
 - POST数据方法 
 - @param array params 参数数组 
 - @author widuu <admin@widuu.com> 
 */ 
 private function response_post($params,$type=false){ 
 // 下载媒体和请求网关 
 if($down){ 
  $url = &#39;https://openfile.alipay.com/chat/multimedia.do&#39;; 
 }else{ 
  $url = &#39;https://openapi.alipay.com/gateway.do&#39;; 
 } 
 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, $url); 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($ch, CURLOPT_POST, 1); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
 curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); 
 $curl = curl_exec($ch); 
 curl_close($ch); 
 return $curl; 
 } 
 /** 
 - 拼接回复数据 
 - @param string $type 回复类型 
 - @param array $info 回复内容 
 - @param bool $mass 是否为群发 
 - @author widuu <admin@widuu.com> 
 */ 
 private function common_response($type,$info,$mass=false){ 
 $request = $this->request; 
 $params = array(); 
 // 如果不是群发 
 if( !$mass ) $params[&#39;toUserId&#39;] = $request[&#39;FromUserId&#39;]; 
 $params[&#39;msgType&#39;] = $type; 
 $params[&#39;createTime&#39;] = time(); 
 $content = array_merge($params,$info); 
 return $content; 
 } 
 /** 
 - 拼接加密参数 
 - @param string $interface_type 接口类型 
 - @param array $biz_content 返回biz_content的数组 
 - @author widuu <admin@widuu.com> 
 */ 
 
 private function common_system($interface_type,$biz_content){ 
 /* 接口集合 */ 
 $type = $this->interface_type; 
 $method = $type[$interface_type]; 
 /* 公共参数 */ 
 $params = array ( 
  &#39;method&#39; => $method, 
  &#39;charset&#39; => &#39;UTF-8&#39;, 
  &#39;sign_type&#39; => &#39;RSA&#39;, 
  &#39;app_id&#39; => $this->app_id, 
  &#39;timestamp&#39; => date ( &#39;Y-m-d H:i:s&#39;, time () ), 
  &#39;version&#39;=>&#39;1.0&#39;, 
 ); 
 /* 获取某些接口时没有biz_content参数 */ 
 if( count($biz_content) > 0 ){ 
  $params[&#39;biz_content&#39;] = json_encode($biz_content); 
 } 
 /* 返回系统参数 */ 
 return $params; 
 } 
 /** 
 - 服务验证 
 - @params array params 是自动获的验证信息 
 - @author widuu <admin@widuu.com> 
 */ 
 private function verify($params){ 
 /* 参数为空 */ 
 if( emptyempty($params) ){ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;验证参数为空&#39;); 
  } 
 } 
 /* 构建参数,使用字典排序再拼接字符串 */ 
 $query_data = $this->build_query($params); 
 /* 验证信息,有可能php版本BUG不支持验证 */ 
 $verify_result = $this->ras_verify($query_data); 
 /* 返回验证结果 */ 
 if( $verify_result ){ 
  /* 取公有密钥的字符串合并为一行 */ 
  $public_rsa_string = file_get_contents($this->public_rsa_key_path); 
  $public_rsa_string = str_replace ( "-----BEGIN PUBLIC KEY-----", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "-----END PUBLIC KEY-----", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "\r", "", $public_rsa_string ); 
  $public_rsa_string = str_replace ( "\n", "", $public_rsa_string ); 
  /* 构建加密字符串 */ 
  $response_xml = "<success>true</success><biz_content>$public_rsa_string</biz_content>"; 
  /* 生成验证信息 */ 
  $sign = $this->rsa_sign ( $response_xml ); 
  /* 构建返回数据 */ 
  $response = "<?xml version=\"1.0\" encoding=\"GBK\"?><alipay><response>$response_xml</response><sign>$sign</sign><sign_type>RSA</sign_type></alipay>"; 
  if( $this->debug ){ 
  $this->write_log(&#39;CHECK_RESPONSE&#39;,$response); 
  } 
  /* 输出返回信息 */ 
  echo $response; 
  exit(); 
 }else{ 
  if( $this->debug ){ 
  $this->write_log(&#39;ERROR&#39;,&#39;验证失败&#39;); 
  } 
 } 
 } 
 /** 
 - 拼接为字符串函数 
 - @params array params 拼接函数 
 - @author widuu <admin@widuu.com> 
 */ 
 private function build_query($params){ 
 /* 删除sign字符串 */ 
 unset($params[&#39;sign&#39;]); 
 /* 字典排序 */ 
 ksort($params); 
 /* 拼接 */ 
 $query_array = array(); 
 foreach ($params as $k => $v) { 
  $query_array[] = "$k"."="."$v"; 
 } 
 $query_data = implode("&", $query_array); 
 /* 返回拼接好的字符串 */ 
 return $query_data; 
 } 
 /** 
 - 验证加密sign,有些PHP版本不支持,不支持情况直接返回true 
 - @params string query_data 加密字符串 
 - @author widuu <admin@widuu.com> 
 */ 
 private function ras_verify($query_data){ 
 /* 读取公钥文件,PEM格式 */ 
 $pubKey = file_get_contents($this->public_rsa_key_path); 
 /* 转换为openssl格式密钥 */ 
 $res = openssl_get_publickey($pubKey); 
 /* 调用openssl内置方法验签 */ 
 $result = (bool) openssl_verify($query_data, base64_decode($this->sign), $res); 
 /* 释放资源 */ 
 openssl_free_key($res); 
 /* 有些PHP版本错误,直接返回true */ 
 if( strpos( openssl_error_string(),&#39;PEM_read_bio&#39; ) ){ 
  return true; 
 } 
 /* 返回验签结果 */ 
 return $result; 
 } 
 /** 
 - 通过私有密钥加密数据 
 - @params string data 加密数据 
 - @author widuu <admin@widuu.com> 
 */ 
 private function rsa_sign($data) { 
 /* 读取私钥 */ 
 $priKey = file_get_contents ( $this->private_rsa_key_path ); 
 /* 转换为openssl格式密钥 */ 
 $res = openssl_get_privatekey ( $priKey ); 
 /* 调用openssl 加密 */ 
 openssl_sign ( $data, $sign, $res ); 
 /* 释放资源 */ 
 openssl_free_key ( $res ); 
 /* Base64加密 */ 
 $sign = base64_encode ( $sign ); 
 /* 返回加密参数 */ 
 return $sign; 
 } 
 private function analysis($params){ 
 switch($params[&#39;MsgType&#39;]){ 
  case &#39;image&#39;: 
  $this->media_id = $params[&#39;Image&#39;][&#39;MediaId&#39;]; 
  $this->format = $params[&#39;Image&#39;][&#39;Format&#39;]; 
  break; 
  case &#39;text&#39;: 
  $this->text = $params[&#39;Text&#39;][&#39;Content&#39;]; 
  break; 
  case &#39;event&#39;: 
  $this->event_type = $params[&#39;EventType&#39;]; 
  $this->action_param = $params[&#39;ActionParam&#39;]; 
  break; 
  default: 
  break; 
 } 
 $this->msg_type = $params[&#39;MsgType&#39;]; 
 $this->user_info = json_decode($params[&#39;UserInfo&#39;],true); 
 } 
 /** 
 - DEBUG 为true时的拼接字符串 
 - @param string $level 自定义标识符 
 - @param string $info 自定义内容 
 - @param string $log_path 自定义日志路径 
 - @author widuu <admin@widuu.com> 
 */ 
 public function write_log($level,$info,$log_path = &#39;&#39; ){ 
 if( emptyempty($log_path) ){ //phpfensi.com 
  $log_path = dirname ( __FILE__ ) . "/log.txt"; 
 } 
 file_put_contents($log_path, "[$level]".date ( "Y-m-d H:i:s" ) . " " . $info . "\r\n", FILE_APPEND ); 
 } 
}
登录后复制

好了以上就是小编为各位整理的一篇关于支付宝服务窗API接口的开发例子,这个有前提条件的就是我们必须要申请一个权限才可以,这个官方可以申请小编就不介绍。

相关推荐:

PHP的支付宝支付接口总结

PHP支付宝接口实例精讲

PHP实现以支付宝为例的RSA签名生成订单功能

以上就是PHP支付宝开发之服务窗API的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号