QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

小程序

首页 > 小程序 >

快手小程序担保支付php源码封装

 admin  2022-06-30 10:38:44
官方社区论坛里边已经有人整理出来一部分代码了,但是他没有封装,并且没有写获取openid的方法,所以专门做了个整理,并且有担保支付回调源码
PHP 封装:
  1. <?php 
  2.     /** 
  3.      * 快手支付 
  4.      */ 
  5.     class KspayService 
  6.     { 
  7.         protected $appid; 
  8.         protected $appSecret; 
  9.         public $data = null
  10.         public function __construct($appid, $appSecret) 
  11.         { 
  12.             $this->appid = $appid; //小程序APPID 
  13.             $this->appSecret = $appSecret; //小程序的appsecret 
  14.         } 
  15.         /** 
  16.          * 通过跳转获取用户的openid,跳转流程如下: 
  17.          * @return 用户的openid 
  18.          */ 
  19.         public function getOpenid($code) 
  20.         { 
  21.             $url = 'https://open.kuaishou.com/oauth2/mp/code2session'
  22.             $dat = array( 
  23.                 'app_id' => $this->appid, 
  24.                 'app_secret' => $this->appSecret, 
  25.                 'js_code' => $code 
  26.             ); 
  27.             if(isset($code)){ 
  28.                 $res = self::curlPost($url,$dat); 
  29.                 $data = json_decode($res,true); 
  30.                 if($data['result'] == 1){ 
  31.                     $openid = $data['open_id']; 
  32.                     return $openid; 
  33.                 }else
  34.                     echo "获取失败"
  35.                 } 
  36.             }else
  37.                 echo "参数错误"
  38.             } 
  39.         } 
  40.  
  41.         /** 
  42.          * 获取accessToken 
  43.          * @return [type] [description] 
  44.          */ 
  45.         public function _getAccessToken() { 
  46.             $postData['app_id'] = $this->appid; 
  47.             $postData['app_secret'] = $this->appSecret; 
  48.             $postData['grant_type'] = 'client_credentials'
  49.             $res = $this->curlPost('https://open.kuaishou.com/oauth2/access_token', $postData); 
  50.             $res = json_decode($res, 1); 
  51.             return $res['access_token']; 
  52.         } 
  53.  
  54.         /** 
  55.          * 生成签名 
  56.          *  @return 签名 
  57.          */ 
  58.         public function makeSign($query, $postData) { 
  59.             unset($query['access_token']); 
  60.             $arr = array_merge($query, $postData); 
  61.             foreach ($arr as $k => $item) { 
  62.                 if (empty($item)) { 
  63.                     unset($arr[$k]); 
  64.                 } 
  65.             } 
  66.             ksort($arr, 2); 
  67.             $str = ''
  68.             foreach ($arr as $k => $v) { 
  69.                 $str .= $k . '=' . $v . '&'
  70.             } 
  71.             $str = substr($str, 0, strlen($str) - 1); 
  72.             $md5 = $str .$this->appSecret; 
  73.             return md5($md5); 
  74.         } 
  75.         /** 
  76.          * 预下单 
  77.          * @param  [type] $orderid   [description] 
  78.          * @param  [type] $price     [description] 
  79.          * @param  [type] $subject   [description] 
  80.          * @param  [type] $body      [description] 
  81.          * @param  [type] $openid    [description] 
  82.          * @param  [type] $notifyUrl [description] 
  83.          * @return [type]            [description] 
  84.          */ 
  85.         public function createOrder($orderid,$price,$subject,$body,$openid,$notifyUrl){ 
  86.             $time = time(); 
  87.             $price = $price*100; 
  88.             $config = [ 
  89.                 'access_token' => $this->_getAccessToken(), 
  90.                 'app_id' => $this->appid, 
  91.             ]; 
  92.             $data = [ 
  93.                 'open_id' => $openid, 
  94.                 'out_order_no' =>  $orderid, //订单号 
  95.                 'total_amount' => $price,  //金额 单位:分 
  96.                 'detail' => $body,         //支付的内容 
  97.                 'subject' => $subject,     //支付的标题 
  98.                 'type' => 1302, 
  99.                 'expire_time' => 3600, 
  100.                 'notify_url'=> $notifyUrl, 
  101.             ]; 
  102.             $data['sign'] = $this->makeSign($config,$data); 
  103.             $url = 'https://open.kuaishou.com/openapi/mp/developer/epay/create_order?' . http_build_query($config); 
  104.  
  105.             $json = json_encode($data, 320); 
  106.             $res = $this->jsonPost($url, $json); 
  107.             return json_decode($res,true); 
  108.         } 
  109.         /** 
  110.          * 订单结算 
  111.          * @return [type] [description] 
  112.          */ 
  113.         public function settle($orderid,$amount){ 
  114.             $config = [ 
  115.                 'access_token' => $this->_getAccessToken(), 
  116.                 'app_id' => $this->appid, 
  117.             ]; 
  118.             $params = [ 
  119.                 'out_order_no'  => $orderid, 
  120.                 'out_settle_no' => 'js'.$orderid, 
  121.                 'reason' =>'用户申请结算',//退款理由 
  122.                 'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/jiesuan_notify.php' 
  123.             ]; 
  124.             $params['sign'] = $this->makeSign($config,$params); 
  125.             $url = "https://open.kuaishou.com/openapi/mp/developer/epay/settle?". http_build_query($config); 
  126.             $json = json_encode($params, 320); 
  127.             $res = $this->jsonPost($url, $json); 
  128.             return json_decode($res, true); 
  129.         } 
  130.         /** 
  131.          * 订单查询 
  132.          * @return [type] [description] 
  133.          */ 
  134.         public function queryOrder($orderid){ 
  135.             $config = [ 
  136.                 'access_token' => $this->_getAccessToken(), 
  137.                 'app_id' => $this->appid, 
  138.             ]; 
  139.             $params = [ 
  140.                 'out_order_no'  => $orderid, 
  141.             ]; 
  142.             $params['sign'] = $this->makeSign($config,$params); 
  143.             $url = "https://open.kuaishou.com/openapi/mp/developer/epay/query_order?". http_build_query($config); 
  144.             $json = json_encode($params, 320); 
  145.             $res = $this->jsonPost($url, $json); 
  146.             return json_decode($res, true); 
  147.         } 
  148.         /** 
  149.          * 退款 
  150.          * @return [type] [description] 
  151.          */ 
  152.         public function createRefund($orderid,$amount){ 
  153.             $config = [ 
  154.                 'access_token' => $this->_getAccessToken(), 
  155.                 'app_id' => $this->appid, 
  156.             ]; 
  157.             $params = [ 
  158.                 'out_order_no'  => $orderid, 
  159.                 'out_refund_no' => 'tk'.$orderid, 
  160.                 'reason' =>'用户申请退款',//退款理由 
  161.                 'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/tk_notify.php'
  162.                 'refund_amount' => $amount, 
  163.             ]; 
  164.             $params['sign'] = $this->makeSign($config,$params); 
  165.             $url = "https://open.kuaishou.com/openapi/mp/developer/epay/apply_refund?". http_build_query($config); 
  166.             $json = json_encode($params, 320); 
  167.             $res = $this->jsonPost($url, $json); 
  168.             return json_decode($res, true); 
  169.         } 
  170.  
  171.         public function jsonPost($url, $data = NULL, $times = 0) { 
  172.             $curl = curl_init(); 
  173.             curl_setopt($curl, CURLOPT_URL, $url); 
  174.             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
  175.             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
  176.             curl_setopt($curl, CURLOPT_POST, 1); 
  177.             curl_setopt($curl, CURLOPT_TIMEOUT, 2); //超时时间2秒 
  178.             curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
  179.             curl_setopt($curl, CURLOPT_HEADER, 0); 
  180.             curl_setopt($curl, CURLOPT_HTTPHEADER, array( 
  181.                 'Content-Type: application/json; charset=utf-8'
  182.                 'Content-Length:' . strlen($data), 
  183.                 'Cache-Control: no-cache'
  184.                 'Pragma: no-cache' 
  185.             )); 
  186.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  187.             $res = curl_exec($curl); 
  188.             curl_close($curl); 
  189.             return $res; 
  190.         } 
  191.  
  192.         public static function curlGet($url = '', $options = array()) 
  193.         { 
  194.             $ch = curl_init($url); 
  195.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  196.             curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
  197.             if (!empty($options)) { 
  198.                 curl_setopt_array($ch, $options); 
  199.             } 
  200.             //https请求 不验证证书和host 
  201.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  202.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  203.             $data = curl_exec($ch); 
  204.             curl_close($ch); 
  205.             return $data; 
  206.         } 
  207.         public function curlPost($url = '', $postData = '', $options = array()) 
  208.         { 
  209.             if (is_array($postData)) { 
  210.                 $postData = http_build_query($postData); 
  211.             } 
  212.             $ch = curl_init(); 
  213.             curl_setopt($ch, CURLOPT_URL, $url); 
  214.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  215.             curl_setopt($ch, CURLOPT_POST, 1); 
  216.             curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
  217.             curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数 
  218.             if (!empty($options)) { 
  219.                 curl_setopt_array($ch, $options); 
  220.             } 
  221.             //https请求 不验证证书和host 
  222.             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  223.             curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
  224.             $data = curl_exec($ch); 
  225.             curl_close($ch); 
  226.             return $data; 
  227.         } 
  228.     } 
  229. ?> 


使用方式:
  1. <?php 
  2.     $appid = '';  //APPID 
  3.     $appSecret = '';   //appsecret 
  4.     //①、获取用户openid 
  5.     $ksPay = new KspayService($appid,$appSecret); 
  6.     $openId = $ksPay->getOpenid($code);      //获取openid 
  7.     if(!$openId) exit('获取openid失败'); 
  8.     // //②、统一下单 
  9.     $outTradeNo = '';     //你自己的商品订单号 
  10.     $payAmount =  '0.01';         //付款金额,单位:元 
  11.     $orderName = '标题';    //订单标题 
  12.     $body = '描述';//订单描述 
  13.     $notifyUrl = 'https://'.$__SERVER['HTTP_HOST'].'/notify.php';      //付款成功后的回调地址(不要有问号) 
  14.     $payTime = time(); //付款时间 
  15.     $res = $ksPay->createOrder($orderid,$payAmount,$orderName,$body,$openId,$notifyUrl); 
  16.     echo json_encode($res); 
  17. ?> 

回调:
  1. header('Content-type:text/html; Charset=utf-8'); 
  2. $result = file_get_contents('php://input'); 
  3. $result = json_decode($result, true); 
  4. $kwaisign  = isset($__SERVER['HTTP_KWAISIGN']) ? $__SERVER['HTTP_KWAISIGN'] : ''
  5.  
  6. if(true){ 
  7.     //完成你的逻辑 
  8.     $orderid = $result['data']['out_order_no'];//订单号 
  9.     if($result['data']['status'] == 'SUCCESS'){ 
  10.         $appSecret = 'z63W9JP6CG6DfkV_7hpKGg';   //appsecret 
  11.         $result = json_encode($result); 
  12.         $notify = md5($result.$appSecret);//签名 
  13.          
  14.         if($notify == $kwaisign){ 
  15.             //更新你的数据库 
  16.              
  17.             $res = [ 
  18.                 'result'=>1, 
  19.                 'message_id'=>$result['message_id'
  20.             ]; 
  21.             echo json_encode($res); 
  22.         }else
  23.             echo 'Signature error'
  24.         } 
  25.     } 
  26. }else
  27.     echo 'pay error'
  28. ?> 

¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《快手小程序担保支付php源码封装》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/xiaocheng/876.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换
小程序插件

PHPcms小程序插件

phpcms小程序插件万能接口,支持一键授权登录

Pbootcms小程序插件

Pbootcms小程序插件,支持一键授权登录

迅睿cms小程序插件

xunruicms小程序插件,简单易学,专为小白设计

dedecms小程序插件

不需要php基础,只需要div+css就可,方便简单

添加微信好友

添加微信好友

微信小程序

百度小程序