最近模板兔在给客户开发抖音小程序,大概功能就是付费下载与查看,使用的是wordpress的Modown主题+erphpdown插件来对接头条小程序,下面给出一些核心代码供参考:
小程序端js代码:
tt.request({
url: app.globalData.appUrl,
data: {
erphp_app: app.globalData.appToken,
action: 'user',
page: "***",
userid: tt.getStorageSync('UserId'),
ice_money: that.data.inputRechargeMoney
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
tt.hideLoading();
//console.log(res.data);
if (res.data.error == '0') {
tt.pay({
orderInfo: {
order_id: res.data.ddata.order_id,
order_token:res.data.ddata.order_token
},
service: 5,
success(res) {
if (res.code == 0) {
app.globalData.userBackFlush = 1;
tt.showModal({
title: '温馨提示',
content: '充值成功!',
confirmText: '好的',
showCancel: false,
success(res) {
if (res.confirm) {
tt.navigateBack({
delta: 1
});
}
}
});
}else{
tt.showToast({
title: '支付失败',
icon: 'none',
duration: 2000
});
}
},
fail(res) {
tt.showToast({
title: '支付失败',
icon: 'none',
duration: 2000
});
}
});
} else {
tt.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
},
fail: function () {
tt.showToast({
title: '提交失败',
icon: 'none',
duration: 2000
});
}
});
网站服务端php代码:
$price = esc_sql(trim($_POST['ice_money']));
if($price){
require_once ERPHPAPP_PATH."/pay/douyin/api.php";
$out_trade_no = date("ymdhis").mt_rand(100,999).mt_rand(100,999);
$subject = get_bloginfo('name').'订单['.get_the_author_meta( 'user_login', $userid ).']';
$erphp_order_title = get_option('erphp_order_title');
if($erphp_order_title){
$subject = $erphp_order_title;
}
$sql="INSERT INTO $wpdb->icemoney (ice_money,ice_num,ice_user_id,ice_user_type,ice_post_id,ice_post_index,ice_time,ice_success,ice_note,ice_success_time,ice_alipay,ice_aff,ice_ip,ice_data) VALUES ('$price','$out_trade_no','".$userid."','0','0','','".date("Y-m-d H:i:s")."',0,'0','".date("Y-m-d H:i:s")."','dymini','','".erphpGetIP()."','')";
$wpdb->query($sql);
$url = 'https://developer.toutiao.com/api/apps/ecpay/v1/create_order';
$data = [
"app_id" => get_option("erphpapp_dyappid"),
"out_order_no" =>$out_trade_no,
"total_amount" => $price*100,
"subject" => $subject,
"body" => 'erphpdown',
"valid_time" => 3600,
"notify_url" => ERPHPAPP_URL.'***.php'
];
$data['sign']= erphpapp_dyapp_sign($data,get_option("erphpapp_dyappsalt"));
$res= erphpapp_http($url,json_encode($data),'POST');
$res=json_decode($res,true);
$ddata = array();
if(!is_array($res)){
$error = 1;
$msg = $res;
}elseif($res['err_no']!=0){
$error = 1;
$msg = $res['err_tips'];
}else{
$error = 0;
$msg = '';
$ddata=$res['data'];
}
echo json_encode(array("error"=>$error,"msg"=>$msg,"ddata"=>$ddata));
}
function erphpapp_dyapp_sign($map,$salt) {
$rList = [];
foreach($map as $k =>$v) {
if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
continue;
$value = trim(strval($v));
if (is_array($v)) {
$value = arrayToStr($v);
}
$len = strlen($value);
if ($len > 1 && substr($value, 0,1)=="\"" && substr($value, $len-1)=="\"")
$value = substr($value,1, $len-1);
$value = trim($value);
if ($value == "" || $value == "null")
continue;
$rList[] = $value;
}
$rList[] =$salt;
sort($rList, SORT_STRING);
return md5(implode('&', $rList));
}
function erphpapp_dyapp_backsign(array $params) {
$data = [
$params['timestamp'],
(string) $params['nonce'],
(string) $params['msg'],
(string) $params['token'],
];
sort($data, SORT_STRING);
return hash('sha1', join('', $data));
}
回调通知php代码:
<?php
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once('../../../../../wp-load.php');
require_once('api.php');
$post = file_get_contents('php://input');
$post=json_decode($post,true);
$post['token']=get_option("erphpapp_dyapptoken");
$sign=erphpapp_dyapp_backsign($post);
if($sign!=$post['msg_signature']){
$return=json_encode(["err_no"=>400,"err_tips"=>'business fail']);
echo $return;exit;
}elseif($post['type'] == 'payment'){
$msg = json_decode($post['msg'],true);
$total_fee=$msg['total_amount']*0.01;
$out_trade_no = $msg['cp_orderno'];
if(strstr($out_trade_no,'MD') || strstr($out_trade_no,'FK')){
epd_set_wppay_success($out_trade_no,$total_fee,'dymini');
}else{
epd_set_order_success($out_trade_no,$total_fee,'dymini');
}
$return=json_encode(["err_no"=>0,"err_tips"=>'success']);
echo $return;exit;
}


0 个评论