当前位置:Gxlcms > PHP教程 > PHP支付宝开发之服务窗API

PHP支付宝开发之服务窗API

时间:2021-07-01 10:21:17 帮助过:4人阅读

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

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

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

  1. <?php
  2. class AlipayService{
  3. /**
  4. - 服务接口信息
  5. */
  6. public $service = null;
  7. /**
  8. - 签名信息
  9. */
  10. public $sign = null;
  11. /**
  12. - 签名类型
  13. */
  14. public $sign_type = null;
  15. /**
  16. - 字符集
  17. */
  18. public $charset = null;
  19. /**
  20. - 解析的biz_content数据
  21. */
  22. public $request = null;
  23. /**
  24. - 用户openid
  25. */
  26. public $from_user_id = null;
  27. /**
  28. - 消息类型
  29. */
  30. public $msg_type = null;
  31. /**
  32. - 事件类型
  33. */
  34. public $event_type = null;
  35. /**
  36. - 行为参数
  37. */
  38. public $action_param = null;
  39. /**
  40. - 支付宝用户信息
  41. */
  42. public $user_info = null;
  43. /**
  44. - 文本消息内容
  45. */
  46. public $text = null;
  47. /**
  48. - 图片媒体id
  49. */
  50. public $media_id = null;
  51. /**
  52. - 图片格式
  53. */
  54. public $format = null;
  55. /**
  56. - 是否开启调试
  57. */
  58. private $debug = false;
  59. /**
  60. - 接口类型
  61. */
  62. private $interface_type = array(
  63. 'qrcode' => 'alipay.mobile.public.qrcode.create',
  64. 'follow' => 'alipay.mobile.public.follow.list',
  65. 'gis_get' => 'alipay.mobile.public.gis.get',
  66. 'menu_get' => 'alipay.mobile.public.menu.get',
  67. 'menu_add' => 'alipay.mobile.public.menu.add',
  68. 'down_media' => 'alipay.mobile.public.multimedia.download',
  69. 'menu_update' => 'alipay.mobile.public.menu.update',
  70. 'info_query' => 'alipay.mobile.public.info.query',
  71. 'info_modify' => 'alipay.mobile.public.info.modify',
  72. 'shortlink' => 'alipay.mobile.public.shortlink.create',
  73. 'label_add' => 'alipay.mobile.public.label.add',
  74. 'label_del' => 'alipay.mobile.public.label.delete',
  75. 'label_update' => 'alipay.mobile.public.label.update',
  76. 'label_query' => 'alipay.mobile.public.label.query',
  77. 'label_user_add' => 'alipay.mobile.public.label.user.add',
  78. 'label_user_del' => 'alipay.mobile.public.label.user.delete',
  79. 'label_user_query' => 'alipay.mobile.public.label.user.query',
  80. 'message_custom' => 'alipay.mobile.public.message.custom.send',
  81. 'message_total' => 'alipay.mobile.public.message.total.send',
  82. 'message_single' => 'alipay.mobile.public.message.single.send',
  83. 'message_label_send' => 'alipay.mobile.public.message.label.send',
  84. );
  85. /**
  86. - 私有密钥地址,替换为你自己的
  87. */
  88. private $private_rsa_key_path ='rsa_private_key.pem';
  89. /**
  90. - 私有密钥地址,替换为你自己的
  91. */
  92. private $public_rsa_key_path ='rsa_public_key.pem';
  93. /**
  94. - 支付宝窗的app id 替换成你自己的
  95. */
  96. private $app_id = '2015120200901652';
  97. /**
  98. - 开启DEBUG参数
  99. - @params bool debug true 开启调试 false 关闭调试
  100. - @author widuu <admin@widuu.com>
  101. */
  102. public function __construct( $debug = false ){
  103. /* 是否开启DEBUG */
  104. if( $debug ) $this->debug = true;
  105. }
  106. /**
  107. - 获取参数,解析请求参数
  108. -
  109. - @author widuu <admin@widuu.com>
  110. */
  111. public function get_request(){
  112. if( !emptyempty($_POST) ){
  113. // 请求的服务接口
  114. $this->service = $_POST['service'];
  115. // 获取请求字符集
  116. $this->charset = $_POST['charset'];
  117. // 获取请求的biz_content
  118. $request_biz_content = $_POST['biz_content'];
  119. // 加密算法
  120. $this->sign_type = $_POST['sign_type'];
  121. // 加密字符串
  122. $this->sign = $_POST['sign'];
  123. // 如果请求格式不是Utf-8 转换格式为Utf-8
  124. if( strtolower($this->charset) != 'utf-8' ){
  125. $request_biz_content = iconv('GBK', 'utf-8', $request_biz_content);
  126. }
  127. // 解析字符串为xml
  128. $request_xml = @simplexml_load_string($request_biz_content, "SimpleXMLElement" , LIBXML_NOCDATA );
  129. // 解析为数组
  130. $request_array = json_decode(json_encode($request_xml),true);
  131. $this->request = $request_array;
  132. /* 解析 */
  133. $this->analysis($request_array);
  134. if($this->debug) $this->write_log('REQUEST_INFO',var_export($request_array,true));
  135. // 默认验证方法
  136. if( $this->service == 'alipay.service.check'){
  137. $this->verify($_POST);
  138. exit();
  139. }
  140. /* 返回结果 */
  141. return $request_array;
  142. }
  143. }
  144. /**
  145. - 回复文本内容
  146. - @params string content 文本数据
  147. - @params bool mass ture为群发
  148. - @author widuu <admin@widuu.com>
  149. */
  150. public function text($content,$mass=false){
  151. $info['text'] = array( 'content' => $content );
  152. /* 组织内容 */
  153. $biz_content = $this->common_response('text',$info,$mass);
  154. /* 判断是否为群发 */
  155. if($mass){
  156. $method = 'message_total';
  157. }else{
  158. $method = 'message_custom';
  159. }
  160. $sys_params = $this->common_system($method,$biz_content);
  161. $sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
  162. /* 返回结果 结果是JSON数据 */
  163. $result = $this->response_post($sys_params);
  164. return $result;
  165. }
  166. /**
  167. - 回复图文内容
  168. - @params array articles 拼接的图文消息数组
  169. - @params bool mass ture为群发
  170. - @author widuu <admin@widuu.com>
  171. */
  172. public function articles($articles,$mass=false){
  173. $info['articles'] = array($articles);
  174. /* 组织内容 */
  175. $biz_content = $this->common_response('image-text',$info,$mass);
  176. /* 判断是否群发 */
  177. if($mass){
  178. $method = 'message_total';
  179. }else{
  180. $method = 'message_custom';
  181. }
  182. /* 加密参数 */
  183. $sys_params = $this->common_system($method,$biz_content);
  184. /* 加密字符 */
  185. $sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
  186. /* 返回结果 结果是JSON数据 */
  187. $result = $this->response_post($sys_params);
  188. return $result;
  189. }
  190. /**
  191. - 关注事件
  192. -
  193. - @author widuu <admin@widuu.com>
  194. */
  195. public function is_follow(){
  196. $request = $this->request;
  197. if( $request['MsgType'] == 'event' && $request['EventType'] == 'follow' ){
  198. return true;
  199. }else{
  200. return false;
  201. }
  202. }
  203. /**
  204. - 取消关注事件
  205. -
  206. - @author widuu <admin@widuu.com>
  207. */
  208. public function is_unfollow(){
  209. $request = $this->request;
  210. if( $request['MsgType'] == 'event' && $request['EventType'] == 'unfollow' ){
  211. return true;
  212. }else{
  213. return false;
  214. }
  215. }
  216. /**
  217. - 下载用户发来的图片
  218. - @param media_id string 图片id
  219. - @param filename string 保存图片地址和名称
  220. - @author widuu <admin@widuu.com>
  221. */
  222. public function down_media($media_id,$filename){
  223. $sys_params = $this->common_system('down_media',array('mediaId'=>$media_id));
  224. $sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
  225. /* 返回数据 */
  226. $result = $this->response_post($sys_params,true);
  227. $result = file_put_contents($filename, $result);
  228. if( $this->debug ){
  229. $this->write_log('SAVE_IMAGE','保存图片'.(string)$result);
  230. }
  231. return $result;
  232. }
  233. /**
  234. - (添加|更新|获取)自定义菜单
  235. - @param string $type (add|update|get)
  236. - @param array $menu 菜单数组,如果是获取菜单可以留空
  237. - @author widuu <admin@widuu.com>
  238. */
  239. public function menu( $type,$menu = array() ){
  240. if( !in_array( $type, array('get','update','add')) ){
  241. if( $this->debug ){
  242. $this->write_log('ERROR','菜单操作方法错误');
  243. }
  244. return false;
  245. }
  246. /* 拼接接口方法 */
  247. $method = 'menu_'.$type;
  248. $sys_params = $this->common_system($method,$menu);
  249. /* 加密字符串 */
  250. $sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));
  251. /* 请求获取结果 */
  252. $result = $this->response_post($sys_params);
  253. /* 转义并解析JSON 数据 */
  254. $menu_json = json_decode(iconv('GBK', 'utf-8', $result),true);
  255. /* 组织接口信息 */
  256. $interface = 'alipay_mobile_public_'.$method.'_response';
  257. /* 遇到错误返回 */
  258. if( $menu_json[$interface]['code'] != 200 ){
  259. if( $this->debug ){
  260. $this->write_log('GET_MENU_ERROR',$menu_json[$interface]['msg']);
  261. }
  262. return false;
  263. }
  264. /* 根据类型不同返回不同的结果 */
  265. if( $type == 'get' ){
  266. return $menu_json[$interface]['menu_content'];
  267. }else{
  268. return $menu_json[$interface]['msg'];
  269. }
  270. }
  271. /**
  272. - POST数据方法
  273. - @param array params 参数数组
  274. - @author widuu <admin@widuu.com>
  275. */
  276. private function response_post($params,$type=false){
  277. // 下载媒体和请求网关
  278. if($down){
  279. $url = 'https://openfile.alipay.com/chat/multimedia.do';
  280. }else{
  281. $url = 'https://openapi.alipay.com/gateway.do';
  282. }
  283. $ch = curl_init();
  284. curl_setopt($ch, CURLOPT_URL, $url);
  285. curl_setopt($ch, CURLOPT_HEADER, 0);
  286. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  287. curl_setopt($ch, CURLOPT_POST, 1);
  288. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  289. curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
  290. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  291. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  292. curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  293. $curl = curl_exec($ch);
  294. curl_close($ch);
  295. return $curl;
  296. }
  297. /**
  298. - 拼接回复数据
  299. - @param string $type 回复类型
  300. - @param array $info 回复内容
  301. - @param bool $mass 是否为群发
  302. - @author widuu <admin@widuu.com>
  303. */
  304. private function common_response($type,$info,$mass=false){
  305. $request = $this->request;
  306. $params = array();
  307. // 如果不是群发
  308. if( !$mass ) $params['toUserId'] = $request['FromUserId'];
  309. $params['msgType'] = $type;
  310. $params['createTime'] = time();
  311. $content = array_merge($params,$info);
  312. return $content;
  313. }
  314. /**
  315. - 拼接加密参数
  316. - @param string $interface_type 接口类型
  317. - @param array $biz_content 返回biz_content的数组
  318. - @author widuu <admin@widuu.com>
  319. */
  320. private function common_system($interface_type,$biz_content){
  321. /* 接口集合 */
  322. $type = $this->interface_type;
  323. $method = $type[$interface_type];
  324. /* 公共参数 */
  325. $params = array (
  326. 'method' => $method,
  327. 'charset' => 'UTF-8',
  328. 'sign_type' => 'RSA',
  329. 'app_id' => $this->app_id,
  330. 'timestamp' => date ( 'Y-m-d H:i:s', time () ),
  331. 'version'=>'1.0',
  332. );
  333. /* 获取某些接口时没有biz_content参数 */
  334. if( count($biz_content) > 0 ){
  335. $params['biz_content'] = json_encode($biz_content);
  336. }
  337. /* 返回系统参数 */
  338. return $params;
  339. }
  340. /**
  341. - 服务验证
  342. - @params array params 是自动获的验证信息
  343. - @author widuu <admin@widuu.com>
  344. */
  345. private function verify($params){
  346. /* 参数为空 */
  347. if( emptyempty($params) ){
  348. if( $this->debug ){
  349. $this->write_log('ERROR','验证参数为空');
  350. }
  351. }
  352. /* 构建参数,使用字典排序再拼接字符串 */
  353. $query_data = $this->build_query($params);
  354. /* 验证信息,有可能php版本BUG不支持验证 */
  355. $verify_result = $this->ras_verify($query_data);
  356. /* 返回验证结果 */
  357. if( $verify_result ){
  358. /* 取公有密钥的字符串合并为一行 */
  359. $public_rsa_string = file_get_contents($this->public_rsa_key_path);
  360. $public_rsa_string = str_replace ( "-----BEGIN PUBLIC KEY-----", "", $public_rsa_string );
  361. $public_rsa_string = str_replace ( "-----END PUBLIC KEY-----", "", $public_rsa_string );
  362. $public_rsa_string = str_replace ( "\r", "", $public_rsa_string );
  363. $public_rsa_string = str_replace ( "\n", "", $public_rsa_string );
  364. /* 构建加密字符串 */
  365. $response_xml = "<success>true</success><biz_content>$public_rsa_string</biz_content>";
  366. /* 生成验证信息 */
  367. $sign = $this->rsa_sign ( $response_xml );
  368. /* 构建返回数据 */
  369. $response = "<?xml version=\"1.0\" encoding=\"GBK\"?><alipay><response>$response_xml</response><sign>$sign</sign><sign_type>RSA</sign_type></alipay>";
  370. if( $this->debug ){
  371. $this->write_log('CHECK_RESPONSE',$response);
  372. }
  373. /*
输出返回信息 */ echo $response; exit(); }else{ if( $this->debug ){ $this->write_log('ERROR','验证失败'); } } } /** - 拼接为字符串函数 - @params array params 拼接函数 - @author widuu <admin@widuu.com> */ private function build_query($params){ /* 删除sign字符串 */ unset($params['sign']); /* 字典排序 */ 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(),'PEM_read_bio' ) ){ 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['MsgType']){ case 'image': $this->media_id = $params['Image']['MediaId']; $this->format = $params['Image']['Format']; break; case 'text': $this->text = $params['Text']['Content']; break; case 'event': $this->event_type = $params['EventType']; $this->action_param = $params['ActionParam']; break; default: break; } $this->msg_type = $params['MsgType']; $this->user_info = json_decode($params['UserInfo'],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 = '' ){ 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的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行