本文主要介绍了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(

'qrcode' => 'alipay.mobile.public.qrcode.create',

'follow' => 'alipay.mobile.public.follow.list',

'gis_get' => 'alipay.mobile.public.gis.get',

'menu_get' => 'alipay.mobile.public.menu.get',

'menu_add' => 'alipay.mobile.public.menu.add',

'down_media' => 'alipay.mobile.public.multimedia.download',

'menu_update' => 'alipay.mobile.public.menu.update',

'info_query' => 'alipay.mobile.public.info.query',

'info_modify' => 'alipay.mobile.public.info.modify',

'shortlink' => 'alipay.mobile.public.shortlink.create',

'label_add' => 'alipay.mobile.public.label.add',

'label_del' => 'alipay.mobile.public.label.delete',

'label_update' => 'alipay.mobile.public.label.update',

'label_query' => 'alipay.mobile.public.label.query',

'label_user_add' => 'alipay.mobile.public.label.user.add',

'label_user_del' => 'alipay.mobile.public.label.user.delete',

'label_user_query' => 'alipay.mobile.public.label.user.query',

'message_custom' => 'alipay.mobile.public.message.custom.send',

'message_total' => 'alipay.mobile.public.message.total.send',

'message_single' => 'alipay.mobile.public.message.single.send',

'message_label_send' => 'alipay.mobile.public.message.label.send',

);

/**

- 私有密钥地址,替换为你自己的

*/

private $private_rsa_key_path ='rsa_private_key.pem';

/**

- 私有密钥地址,替换为你自己的

*/

private $public_rsa_key_path ='rsa_public_key.pem';

/**

- 支付宝窗的app id 替换成你自己的

*/

private $app_id = '2015120200901652';

/**

- 开启DEBUG参数

- @params bool debug true 开启调试 false 关闭调试

- @author widuu

*/

public function __construct( $debug = false ){

/* 是否开启DEBUG */

if( $debug ) $this->debug = true;

}

/**

- 获取参数,解析请求参数

-

- @author widuu

*/

public function get_request(){

if( !emptyempty($_POST) ){

// 请求的服务接口

$this->service = $_POST['service'];

// 获取请求字符集

$this->charset = $_POST['charset'];

// 获取请求的biz_content

$request_biz_content = $_POST['biz_content'];

// 加密算法

$this->sign_type = $_POST['sign_type'];

// 加密字符串

$this->sign = $_POST['sign'];

// 如果请求格式不是Utf-8 转换格式为Utf-8

if( strtolower($this->charset) != 'utf-8' ){

$request_biz_content = iconv('GBK', 'utf-8', $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('REQUEST_INFO',var_export($request_array,true));

// 默认验证方法

if( $this->service == 'alipay.service.check'){

$this->verify($_POST);

exit();

}

/* 返回结果 */

return $request_array;

}

}

/**

- 回复文本内容

- @params string content 文本数据

- @params bool mass ture为群发

- @author widuu

*/

public function text($content,$mass=false){

$info['text'] = array( 'content' => $content );

/* 组织内容 */

$biz_content = $this->common_response('text',$info,$mass);

/* 判断是否为群发 */

if($mass){

$method = 'message_total';

}else{

$method = 'message_custom';

}

$sys_params = $this->common_system($method,$biz_content);

$sys_params['sign'] = $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

*/

public function articles($articles,$mass=false){

$info['articles'] = array($articles);

/* 组织内容 */

$biz_content = $this->common_response('image-text',$info,$mass);

/* 判断是否群发 */

if($mass){

$method = 'message_total';

}else{

$method = 'message_custom';

}

/* 加密参数 */

$sys_params = $this->common_system($method,$biz_content);

/* 加密字符 */

$sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));

/* 返回结果 结果是JSON数据 */

$result = $this->response_post($sys_params);

return $result;

}

/**

- 关注事件

-

- @author widuu

*/

public function is_follow(){

$request = $this->request;

if( $request['MsgType'] == 'event' && $request['EventType'] == 'follow' ){

return true;

}else{

return false;

}

}

/**

- 取消关注事件

-

- @author widuu

*/

public function is_unfollow(){

$request = $this->request;

if( $request['MsgType'] == 'event' && $request['EventType'] == 'unfollow' ){

return true;

}else{

return false;

}

}

/**

- 下载用户发来的图片

- @param media_id string 图片id

- @param filename string 保存图片地址和名称

- @author widuu

*/

public function down_media($media_id,$filename){

$sys_params = $this->common_system('down_media',array('mediaId'=>$media_id));

$sys_params['sign'] = $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('SAVE_IMAGE','保存图片'.(string)$result);

}

return $result;

}

/**

- (添加|更新|获取)自定义菜单

- @param string $type (add|update|get)

- @param array $menu 菜单数组,如果是获取菜单可以留空

- @author widuu

*/

public function menu( $type,$menu = array() ){

if( !in_array( $type, array('get','update','add')) ){

if( $this->debug ){

$this->write_log('ERROR','菜单操作方法错误');

}

return false;

}

/* 拼接接口方法 */

$method = 'menu_'.$type;

$sys_params = $this->common_system($method,$menu);

/* 加密字符串 */

$sys_params['sign'] = $this->rsa_sign($this->build_query($sys_params));

/* 请求获取结果 */

$result = $this->response_post($sys_params);

/* 转义并解析JSON 数据 */

$menu_json = json_decode(iconv('GBK', 'utf-8', $result),true);

/* 组织接口信息 */

$interface = 'alipay_mobile_public_'.$method.'_response';

/* 遇到错误返回 */

if( $menu_json[$interface]['code'] != 200 ){

if( $this->debug ){

$this->write_log('GET_MENU_ERROR',$menu_json[$interface]['msg']);

}

return false;

}

/* 根据类型不同返回不同的结果 */

if( $type == 'get' ){

return $menu_json[$interface]['menu_content'];

}else{

return $menu_json[$interface]['msg'];

}

}

/**

- POST数据方法

- @param array params 参数数组

- @author widuu

*/

private function response_post($params,$type=false){

// 下载媒体和请求网关

if($down){

$url = 'https://openfile.alipay.com/chat/multimedia.do';

}else{

$url = 'https://openapi.alipay.com/gateway.do';

}

$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

*/

private function common_response($type,$info,$mass=false){

$request = $this->request;

$params = array();

// 如果不是群发

if( !$mass ) $params['toUserId'] = $request['FromUserId'];

$params['msgType'] = $type;

$params['createTime'] = time();

$content = array_merge($params,$info);

return $content;

}

/**

- 拼接加密参数

- @param string $interface_type 接口类型

- @param array $biz_content 返回biz_content的数组

- @author widuu

*/

private function common_system($interface_type,$biz_content){

/* 接口集合 */

$type = $this->interface_type;

$method = $type[$interface_type];

/* 公共参数 */

$params = array (

'method' => $method,

'charset' => 'UTF-8',

'sign_type' => 'RSA',

'app_id' => $this->app_id,

'timestamp' => date ( 'Y-m-d H:i:s', time () ),

'version'=>'1.0',

);

/* 获取某些接口时没有biz_content参数 */

if( count($biz_content) > 0 ){

$params['biz_content'] = json_encode($biz_content);

}

/* 返回系统参数 */

return $params;

}

/**

- 服务验证

- @params array params 是自动获的验证信息

- @author widuu

*/

private function verify($params){

/* 参数为空 */

if( emptyempty($params) ){

if( $this->debug ){

$this->write_log('ERROR','验证参数为空');

}

}

/* 构建参数,使用字典排序再拼接字符串 */

$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 = "true$public_rsa_string";

/* 生成验证信息 */

$sign = $this->rsa_sign ( $response_xml );

/* 构建返回数据 */

$response = "<?xml version=\"1.0\" encoding=\"GBK\"?>$response_xml$signRSA";

if( $this->debug ){

$this->write_log('CHECK_RESPONSE',$response);

}

/* 输出返回信息 */

echo $response;

exit();

}else{

if( $this->debug ){

$this->write_log('ERROR','验证失败');

}

}

}

/**

- 拼接为字符串函数

- @params array params 拼接函数

- @author widuu

*/

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

*/

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

*/

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

*/

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支付宝开发之服务窗API相关推荐

  1. 支付宝生活号服务窗接入问题汇总

    2019独角兽企业重金招聘Python工程师标准>>> 最近因公司业务需求,需要接入支付宝生活号服务窗的事件订阅功能,但接入过程中也是一波三折各种坑.总体感受,接入支付宝不如接入微信 ...

  2. 支付宝开发问题-“您当前注册的企业账号风险等级过高,根据国家相关法律法则及《支付宝服务协议》规定,我司无法为您提供支付服务”

    这属于支付宝开发最头痛的问题之一 这个问题导致号都开不出来 这个问题最主要的原因是:法人黑了 其他原因[不重要]: 1.申请前必须拥有经过实名认证的支付宝账户 2.企业或个体工商户可申请(纯个人不能申 ...

  3. 魅族5.0以上支付宝开发程序崩溃,异常ActivityNotFoundException,其实是flyme6的安全模式搞的鬼

    问题:魅族手机做支付宝开发的时候,我是这样做的,不管支付是否成功都去打开一个Activity,然而这个时候程序崩溃了,异常信息也很奇怪:ActivityNotFoundException,这个错误只有 ...

  4. 支付宝开发 ——第三方支付

    支付宝开发 第三方支付 在线支付 在线支付是指卖方与买方通过因特网上的电子商务网站进行交易时,银行为其提供网上资金结算服务的一种业务.它为企业和个人提供了一个安全.快捷.方便的电子商务应用环境和网上资 ...

  5. java支付宝网页授权登录界面_支付宝开发平台之第三方授权登录与获取用户信息...

    对于第三方登录,我们常见了,很多应用可以进行第三方登录,我常用的有:QQ.微信.新浪.支付宝等等,今天我们就一起来简单学习一下支付宝第三方授权登录. 打开支付宝开发平台,注册成为开发者,点击开发者中心 ...

  6. java支付宝开发流程

    不管是支付宝支付,还是微信支付,还是银联支付等,大部分的支付流程都是相似的,学会了其中的思想,那么其他支付方式也就很简单了. 支付宝支付流程: 1.A网站以POST请求方式提交参数给支付宝接口,在支付 ...

  7. 开通支付宝开发平台,实现支付宝支付,支付宝支付证书生成教程

    1.创建应用 登录 支付宝开发平台 2.创建网页/移动支付 3.按照要求填写创建网页移动应用的表单 注意: 创建后无法提交审核,需要进行开发设置 4.设置证书 5.证书模式 6.点击下载秘钥工具,按照 ...

  8. web项目接入支付宝开发(超详细)

    web项目接入支付宝开发"超详细" 前言 接入开发前准备 代码开发 项目测试 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 最近公司项目涉及到支付宝支付的接入,说简单 ...

  9. DirectX游戏开发之一个API玩转音乐

    DirectX游戏开发之一个API玩转音乐 当你闭上眼睛,打开一款游戏,如刺客信条,英雄联盟,DNF,或者是当有一个人坐在你背后玩一款新游戏,第一时间吸引你的是什么? 没错,就是各种游戏的音效,包括背 ...

最新文章

  1. wxWidgets:wxMBConv概述
  2. shiro学习(17):easyui布局测试
  3. 45个实用的前端开发工具汇总
  4. 今日头条推荐算法原理全文详解之六
  5. 【用户研究】【实战】——“得到”APP 可用性测试
  6. PR值计算公式带来的思考
  7. delphi信封打印程序
  8. 行列式的组合定义及其应用--反对称阵的Pfaffian
  9. 再谈微服务负载均衡器:Ribbon均衡器和SpringCloud自带LoadBalancer均衡器
  10. 脖子黑色素沉淀怎么去除,有效方法
  11. 简述Z-Stack的基本工作原理与流程(OSAL操作系统)
  12. 盈鹏飞嵌入式_EVB-T335 TF卡功能测试手册
  13. 问题:npm如何设置仓库地址?
  14. 云时代下,传统和新型存储的博弈已经开始
  15. android studio 使用 jni 编译 opencv 完整实例 之 图像边缘检测!从此在andrid中自由使用 图像匹配、识别、检测...
  16. 《约会专家》片尾【约会宝典】总结
  17. linux如何卸载金山安全终端,安全客户端
  18. 用matlab弄个按钮怎么来,matlab按钮生按钮
  19. 极限存在准则 两个重要极限——“高等数学”
  20. matlab stk 设置,[ STK ](八)使用 Matlab,在 STK 中建立卫星间连线

热门文章

  1. PC构件行业发展概况及竞争格局、发展趋势
  2. R基础2——数据形式
  3. SCIP 1.3 总结
  4. 《TIME》评选的08年50个最棒的网站
  5. 别在问PMP和ACP哪个更有用了,看完这一篇你就知道!
  6. linux中的firmware
  7. 空间二连杆机器人标准DH法和改进DH法建系实例
  8. java毕业生设计短视频交流点播系统计算机源码+系统+mysql+调试部署+lw
  9. 社交零售多商户分销商城APP小程序系统
  10. 游戏策划师概率论笔试面试知识总结