当前位置:网站首页>快手小程序担保支付php源码封装
快手小程序担保支付php源码封装
2022-07-08 00:16:00 【cms小程序插件【官方】】
抖音担保支付,虽然官方社区论坛已经有人整理分享了部分源码,但是并不全,我这边重新封装了一下,并且整合了入账,退款、查询等方法
<?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请求 不验证证书和host
curl_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请求 不验证证书和host
curl_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';
}
小程序端的支付代码不再发,比较简单,有需要可以联系我,需要注意的是,快手小程序的入账需要根据订单号一笔一笔的处理,这点比较扯淡,不知道后期会不会调整
如果帮助到了您,别忘了支持一下
边栏推荐
- Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
- Anaconda3 tutorial on installing and adding Tsinghua image files
- 滑环使用如何固定
- The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
- npm 内部拆分模块
- qt--將程序打包--不要安裝qt-可以直接運行
- 用户之声 | 对于GBase 8a数据库学习的感悟
- 如何让导电滑环信号更好
- Codeforces Round #643 (Div. 2)——B. Young Explorers
- break algorithm---刷题map
猜你喜欢
Running OFDM in gnuradio_ RX error: gr:: Log: info: packet_ headerparser_ b0 - Detected an invalid packet at item ××
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
About snake equation (1)
3. Multi agent reinforcement learning
Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
How to make enterprise recruitment QR code?
Qt - - Packaging Programs - - Don't install Qt - can run directly
从cmath文件看名字是怎样被添加到命名空间std中的
pb9.0 insert ole control 错误的修复工具
Android 创建的sqlite3数据存放位置
随机推荐
Chapter 7 behavior level modeling
STM32GPIO口的工作原理
Understanding of maximum likelihood estimation
npm 内部拆分模块
Remote Sensing投稿经验分享
Qml 字体使用pixelSize来自适应界面
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
What kind of MES system is a good system
Qt - - Packaging Programs - - Don't install Qt - can run directly
Version 2.0 of tapdata, the open source live data platform, has been released
nacos-微服务网关Gateway组件 +Swagger2接口生成
MySQL查询为什么没走索引?这篇文章带你全面解析
为什么更新了 DNS 记录不生效?
How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
common commands
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
Voice of users | understanding of gbase 8A database learning
After modifying the background of jupyter notebook and adding jupyterthemes, enter 'JT -l' and the error 'JT' is not an internal or external command, nor a runnable program
About snake equation (5)