抖音担保支付,虽然官方社区论坛已经有人整理分享了部分源码,但是并不全,我这边重新封装了一下,并且整合了入账,退款、查询等方法

<?php/*** 快手支付*/class KspayService{protected $appid;protected $appSecret;public $data = null;public function __construct($appid, $appSecret){$this->appid = $appid; //小程序APPID$this->appSecret = $appSecret; //小程序的appsecret}/*** 通过跳转获取用户的openid,跳转流程如下:* @return 用户的openid*/public function getOpenid($code){$url = 'https://open.kuaishou.com/oauth2/mp/code2session';$dat = array('app_id' => $this->appid,'app_secret' => $this->appSecret,'js_code' => $code);if(isset($code)){$res = self::curlPost($url,$dat);$data = json_decode($res,true);if($data['result'] == 1){$openid = $data['open_id'];return $openid;}else{echo "获取失败";}}else{echo "参数错误";}}/*** 获取accessToken* @return [type] [description]*/public function _getAccessToken() {$postData['app_id'] = $this->appid;$postData['app_secret'] = $this->appSecret;$postData['grant_type'] = 'client_credentials';$res = $this->curlPost('https://open.kuaishou.com/oauth2/access_token', $postData);$res = json_decode($res, 1);return $res['access_token'];}/*** 生成签名*  @return 签名*/public function makeSign($query, $postData) {unset($query['access_token']);$arr = array_merge($query, $postData);foreach ($arr as $k => $item) {if (empty($item)) {unset($arr[$k]);}}ksort($arr, 2);$str = '';foreach ($arr as $k => $v) {$str .= $k . '=' . $v . '&';}$str = substr($str, 0, strlen($str) - 1);$md5 = $str .$this->appSecret;return md5($md5);}/*** 预下单* @param  [type] $orderid   [description]* @param  [type] $price     [description]* @param  [type] $subject   [description]* @param  [type] $body      [description]* @param  [type] $openid    [description]* @param  [type] $notifyUrl [description]* @return [type]            [description]*/public function createOrder($orderid,$price,$subject,$body,$openid,$notifyUrl){$time = time();$price = $price*100;$config = ['access_token' => $this->_getAccessToken(),'app_id' => $this->appid,];$data = ['open_id' => $openid,'out_order_no' =>  $orderid, //订单号'total_amount' => $price,  //金额 单位:分'detail' => $body,         //支付的内容'subject' => $subject,     //支付的标题'type' => 1302,'expire_time' => 3600,'notify_url'=> $notifyUrl,];$data['sign'] = $this->makeSign($config,$data);$url = 'https://open.kuaishou.com/openapi/mp/developer/epay/create_order?' . http_build_query($config);$json = json_encode($data, 320);$res = $this->jsonPost($url, $json);return json_decode($res,true);}/*** 订单结算* @return [type] [description]*/public function settle($orderid,$amount){$config = ['access_token' => $this->_getAccessToken(),'app_id' => $this->appid,];$params = ['out_order_no'  => $orderid,'out_settle_no' => 'js'.$orderid,'reason' =>'用户申请结算',//退款理由'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/jiesuan_notify.php'];$params['sign'] = $this->makeSign($config,$params);$url = "https://open.kuaishou.com/openapi/mp/developer/epay/settle?". http_build_query($config);$json = json_encode($params, 320);$res = $this->jsonPost($url, $json);return json_decode($res, true);}/*** 订单查询* @return [type] [description]*/public function queryOrder($orderid){$config = ['access_token' => $this->_getAccessToken(),'app_id' => $this->appid,];$params = ['out_order_no'    => $orderid,];$params['sign'] = $this->makeSign($config,$params);$url = "https://open.kuaishou.com/openapi/mp/developer/epay/query_order?". http_build_query($config);$json = json_encode($params, 320);$res = $this->jsonPost($url, $json);return json_decode($res, true);}/*** 退款* @return [type] [description]*/public function createRefund($orderid,$amount){$config = ['access_token' => $this->_getAccessToken(),'app_id' => $this->appid,];$params = ['out_order_no'   => $orderid,'out_refund_no' => 'tk'.$orderid,'reason' =>'用户申请退款',//退款理由'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/tk_notify.php','refund_amount' => $amount,];$params['sign'] = $this->makeSign($config,$params);$url = "https://open.kuaishou.com/openapi/mp/developer/epay/apply_refund?". http_build_query($config);$json = json_encode($params, 320);$res = $this->jsonPost($url, $json);return json_decode($res, true);}public function jsonPost($url, $data = NULL, $times = 0) {$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_TIMEOUT, 2); //超时时间2秒curl_setopt($curl, CURLOPT_POSTFIELDS, $data);curl_setopt($curl, CURLOPT_HEADER, 0);curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8','Content-Length:' . strlen($data),'Cache-Control: no-cache','Pragma: no-cache'));curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$res = curl_exec($curl);curl_close($curl);return $res;}public static function curlGet($url = '', $options = array()){$ch = curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 30);if (!empty($options)) {curl_setopt_array($ch, $options);}//https请求 不验证证书和hostcurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);$data = curl_exec($ch);curl_close($ch);return $data;}public function curlPost($url = '', $postData = '', $options = array()){if (is_array($postData)) {$postData = http_build_query($postData);}$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数if (!empty($options)) {curl_setopt_array($ch, $options);}//https请求 不验证证书和hostcurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);$data = curl_exec($ch);curl_close($ch);return $data;}}
?>

回调:(快手支付的回调不需要对返回的参数做任何处理,也就是不需要排序、组装,直接拼接appsecret然后md5即可)

$result = file_get_contents('php://input');
$result = json_decode($result, true);
$kwaisign  = isset($_SERVER['HTTP_KWAISIGN']) ? $_SERVER['HTTP_KWAISIGN'] : '';if(true){//完成你的逻辑$orderid = $result['data']['out_order_no'];if($result['data']['status'] == 'SUCCESS'){$appSecret = '';   //您的appsecret$resulta = json_encode($result);$notify = md5($resulta.$appSecret);if($notify == $kwaisign){//校验成功,更新您的数据库//下边是固定返回格式$res = ['result'=>1,'message_id'=>$result['message_id']];echo json_encode($res);}else{echo 'Signature error';}}
}else{echo 'pay error';
}

小程序端的支付代码不再发,比较简单,有需要可以联系我,需要注意的是,快手小程序的入账需要根据订单号一笔一笔的处理,这点比较扯淡,不知道后期会不会调整

如果帮助到了您,别忘了支持一下

快手小程序担保支付php源码封装相关推荐

  1. 抖音头条小程序担保支付php版demo源码

    首先把配置文件填写完整: 1 2 3 4 5 6 7 // 支付相关配置 private static $config = array(     'app_id'        => '', / ...

  2. 微信小程序盲盒系统源码 附带教程

    微信小程序盲盒系统源码 可对接微信支付 附带教程 盲盒小程序的教学 服务器安装宝塔面板 设置好网站 数据库 设置好SSL证书 上传微擎框架 框架安装好 上传小程序后台 /addons 这个目录是放置后 ...

  3. 微信小程序宠物商城项目源码来了~

    微信小程序蒙服汇项目源码来了~ 微信小程序垃圾分类项目源码来了~ 微信小程序校园社团管理系统项目源码来了~ 团队承接各类小程序定制,需要加微 code_gg_boy .小商城,购物,公司的一些管理,流 ...

  4. 基于线上的茶叶购买小程序(论文+程序设计源码+数据库文件)

    微信小程序在日常生活中应用越来越广,网上消费.游戏娱乐等成为了一种常见应用方式.为进一步了解和探索微信小程序在点餐系统中的潜在价值和意义,本文通过使用 Javascript 技术.Spring boo ...

  5. tinkphp1.0贺岁版小程序应用平台系统源码

    介绍: tinkphp1.0贺岁版小程序应用平台系统源码 安装说明:直接放入服务器或者空间,访问域名根据安装向导进行安装. 程序魅力:此程序是类似微信小程序一样的机制系统,但不是微信小程序,跟微信不搭 ...

  6. 新版WIFI小程序分销系统微信源码序WiFi大师版流量主搭建独立源码WiFi分销源码

    网传版本,未测试,新版WIFI大师v4.47小程序源码,WIFI分销系统带流量主和独立运行版. 更新内容: 1.后台更多板块列表新增分页 2.平台管理端图标优化 3.平台管理端新增平台统计 4.优化后 ...

  7. 2022最新找茬小程序完美运营版源码

    正文: 2022最新找茬小程序完美运营版源码,内有完整教程,有兴趣的自行去体验吧,其它的就没什么好介绍的了. 程序: wwysu.lanzoup.com/inUoJ0cg9ekh 图片:

  8. 婚礼类小程序前端界面模板源码

    模板介绍 一款通用的婚礼策划,婚纱摄影类小程序前端模板.支持在线预约,婚纱定制等手机微信小程序模板. 图片演示 小程序源码下载地址:婚礼类小程序前端界面模板源码-小程序文档类资源-CSDN文库http ...

  9. 计算机毕业设计JAVA课堂管理系统小程序用户端mybatis+源码+调试部署+系统+数据库+lw

    计算机毕业设计JAVA课堂管理系统小程序用户端mybatis+源码+调试部署+系统+数据库+lw 计算机毕业设计JAVA课堂管理系统小程序用户端mybatis+源码+调试部署+系统+数据库+lw 本源 ...

最新文章

  1. PointPillar:利用伪图像高效实现3D目标检测
  2. 并发编程之Java内存模型
  3. Java高效读取大文件
  4. [物理学与PDEs]第2章第2节 粘性流体力学方程组 2.6 一维粘性热传导流体动力学方程组...
  5. FAT32格式和NTFS有什么区别
  6. Java 的Runnable和Callable的区别
  7. 一步步编写操作系统 79 在c代码中内联汇编
  8. HTML简单注册页面
  9. pandas读取csv文件数据并对指定字段使用matplotlib画折线图
  10. javascript的parseInt函数(转)
  11. linux 有道 离线词典,有道词典离线版
  12. opnet安装的问题
  13. MDM数据质量应用说明
  14. Pascal基本教程
  15. 什么是CBR,VBV和CPB
  16. html 阴历阳历转换,本人花了一个星期的时间,使用ASP代码获得公历并转换成农历时间(2021年至2100年)...
  17. 工信部ICP备案管理系统滑动验证码破解
  18. sql server 日期转换为英文格式
  19. sata port multiplier
  20. ORACLE占用大量系统CPU,致使系统宕机

热门文章

  1. 算法详解【快速排序】
  2. 数学建模之熵权法(EWM)matlab实例实现
  3. 2021年山东省安全员B证试题及解析及山东省安全员B证复审模拟考试
  4. LVSKeepalived—集群、负载均衡、企业高可用详解
  5. 人工智能-三连子游戏设计和实现
  6. 计算机高级技师评审述职报告,技师述职报告
  7. 计算机应用基础教案文库,计算机应用基础教案82246
  8. Swift4.0判断本函数是否在其它类有相同的方法
  9. 这些城市都有哪些互联网公司?一文全知道!
  10. ST33三相可控硅触发板 无触点晶闸管调压控制板