当前位置:网站首页>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
边栏推荐
- QT build with built-in application framework -- Hello World -- use min GW 32bit
- mysql/mariadb怎样生成core文件
- ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
- Sword finger offer II 041 Average value of sliding window
- 电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
- break net
- 快速熟知XML解析
- List of top ten domestic industrial 3D visual guidance enterprises in 2022
- Working principle of stm32gpio port
- ANSI / NEMA- MW- 1000-2020 磁铁线标准。. 最新原版
猜你喜欢
ClickHouse原理解析与应用实践》读书笔记(8)
快手小程序担保支付php源码封装
2022国内十大工业级三维视觉引导企业一览
Nacos microservice gateway component +swagger2 interface generation
为什么更新了 DNS 记录不生效?
PB9.0 insert OLE control error repair tool
Introduction à l'outil nmap et aux commandes communes
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
Anaconda3 download address Tsinghua University open source software mirror station
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
随机推荐
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
How to make enterprise recruitment QR code?
What kind of MES system is a good system
Node JS maintains a long connection
Tapdata 的 2.0 版 ,開源的 Live Data Platform 現已發布
快手小程序担保支付php源码封装
nmap工具介紹及常用命令
Reading notes of Clickhouse principle analysis and Application Practice (7)
Introduction à l'outil nmap et aux commandes communes
If time is a river
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
【目标跟踪】|atom
《ClickHouse原理解析与应用实践》读书笔记(7)
What are the types of system tests? Let me introduce them to you
break net
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
List of top ten domestic industrial 3D visual guidance enterprises in 2022
用户之声 | 对于GBase 8a数据库学习的感悟
Codeforces Round #649 (Div. 2)——A. XXXXX