首页 后端开发 php教程 PHP支付宝开发之服务窗API

PHP支付宝开发之服务窗API

Dec 29, 2017 pm 06:15 PM
api php

本文主要介绍了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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

为了在 cakephp4 中处理日期和时间,我们将使用可用的 FrozenTime 类。

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

为了进行文件上传,我们将使用表单助手。这是文件上传的示例。

CakePHP 创建验证器 CakePHP 创建验证器 Sep 10, 2024 pm 05:26 PM

可以通过在控制器中添加以下两行来创建验证器。

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 是一个开源MVC 框架。它使开发、部署和维护应用程序变得更加容易。 CakePHP 有许多库可以减少大多数常见任务的过载。

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

See all articles