当前位置:网站首页>Kwai applet guaranteed payment PHP source code packaging
Kwai applet guaranteed payment PHP source code packaging
2022-07-08 01:54:00 【CMS applet plug-in [official]】
Tiktok guarantees payment , Although some people in the official community forum have sorted out and shared some of the source code , But not all , I resealed it here , And integrate the entry , refund 、 Query methods
<?php
/**
* Kwai payment
*/
class KspayService
{
protected $appid;
protected $appSecret;
public $data = null;
public function __construct($appid, $appSecret)
{
$this->appid = $appid; // Applet APPID
$this->appSecret = $appSecret; // Applet appsecret
}
/**
* Get user's information through Jump openid, The jump process is as follows :
* @return User 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 " Acquisition failure ";
}
}else{
echo " Parameter error ";
}
}
/**
* obtain 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'];
}
/**
* Generate signature
* @return Signature
*/
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);
}
/**
* Advance order
* @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, // The order number
'total_amount' => $price, // amount of money Company : branch
'detail' => $body, // Content of payment
'subject' => $subject, // Title of payment
'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);
}
/**
* Order and settlement
* @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' =>' The user applies for settlement ',// Reason for refund
'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);
}
/**
* Order inquiry
* @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);
}
/**
* refund
* @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' =>' Users apply for a refund ',// Reason for refund
'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); // Timeout time 2 second
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 request Do not verify certificates and 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); // Set up cURL The maximum number of seconds allowed to execute
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https request Do not verify certificates and host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
?>
Callback :( The callback of Kwai payment does not need to do any processing on the returned parameters , That is, there is no need to sort 、 assemble , Directly joining together appsecret then md5 that will do )
$result = file_get_contents('php://input');
$result = json_decode($result, true);
$kwaisign = isset($_SERVER['HTTP_KWAISIGN']) ? $_SERVER['HTTP_KWAISIGN'] : '';
if(true){
// Complete your logic
$orderid = $result['data']['out_order_no'];
if($result['data']['status'] == 'SUCCESS'){
$appSecret = ''; // Your appsecret
$resulta = json_encode($result);
$notify = md5($resulta.$appSecret);
if($notify == $kwaisign){
// Check success , Update your database
// Below is the fixed return format
$res = [
'result'=>1,
'message_id'=>$result['message_id']
];
echo json_encode($res);
}else{
echo 'Signature error';
}
}
}else{
echo 'pay error';
}
The payment code on the applet side is no longer sent , Relatively simple , You can contact me if you need , It should be noted that , The entry of Kwai applet needs to be processed one by one according to the order number , This is bullshit , I don't know if it will be adjusted later
If it helps you , Don't forget to support
边栏推荐
- Matlab r2021b installing libsvm
- Sum of submatrix
- Remote Sensing投稿经验分享
- ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
- pb9.0 insert ole control 错误的修复工具
- 软件测试笔试题你会吗?
- Redisson分布式锁解锁异常
- QT -- package the program -- don't install qt- you can run it directly
- Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
- Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
猜你喜欢
随机推荐
php 获取音频时长等信息
Anaconda3 download address Tsinghua University open source software mirror station
Android 创建的sqlite3数据存放位置
How mysql/mariadb generates core files
快速熟知XML解析
node js 保持长连接
进程和线程的退出
神经网络与深度学习-5- 感知机-PyTorch
用户之声 | 对于GBase 8a数据库学习的感悟
Nacos microservice gateway component +swagger2 interface generation
Qml 字体使用pixelSize来自适应界面
Exit of processes and threads
Is it safe to open an account on your mobile phone for small amount of stock speculation?
不算不知道,花呗分期的真实利率居然这么高
[SolidWorks] modify the drawing format
Voice of users | understanding of gbase 8A database learning
burpsuite
How to fix the slip ring
Introduction à l'outil nmap et aux commandes communes
Redux使用