ApiSharetowelorder.php
<?php
namespace app\api\controller;
use app\model\Sharetowel;
use app\model\Sharetowelmachine;
use app\model\Sharetowelorder;
use app\model\Store;
use app\model\User;
use app\model\Userbalancerecord;
use think\Db;
class ApiSharetowelorder extends Api
{
/**
* 获取下单信息
*/
public function getInfo()
{
$id = input('request.id');
$data = Sharetowel::get_curr();
$m = Sharetowelmachine::find($id);
$data["price"] = $m["price"];
success_withimg_json($data);
}
/**
* 下单
*/
public function setOrder()
{
global $_W;
$user_id = input('request.user_id');
$sharetowelmachine_id = input('request.id');
$num = input('request.num');
$m = Sharetowelmachine::find($sharetowelmachine_id);
$total_price = $sharetowelmachine_id["price"] * $num;
$order_data = [
'uniacid' => $_W['uniacid'],
'sharetowelmachine_id' => $sharetowelmachine_id,
'user_id' => $user_id,
'order_amount' => $total_price,
'total_price' => $total_price,
'order_no' => date("YmdHis") . rand(11111, 99999),
'order_status' => 10,
'create_time' => time(),
];
$order_id = Db::name('sharetowelorder')->insertGetId($order_data);
success_json(["order_id"=>$order_id]);
}
//获取微信支付参数
public function getPayParam($param)
{
global $_W;
$orderModel = new Sharetowelorder();
$orderModel->allowField(true)->save(['pay_type' => $param['pay_type']], ['id' => $param['order_id']]);
$order = $orderModel::get($param['order_id']);
$order['attach'] = json_encode(array(
'type' => $param["type"],
'uniacid' => $_W['uniacid'],
'order_id' => $param['order_id'],
));
if ($param['pay_type'] == 1) {
if ($order['from'] == 2 || $param['app'] == 1) {
// $data=$this->setAppPayParam_app($order);
$from = 2;
} else if ($order['from'] == 3) {
// $data=$this->setWxPayParam($order);
$from = 3;
} else if ($order['from'] == 5) {
// $data=$this->setH5WxPayParam($order);
$from = 5;
} else {
// $data=$this->setPayParam($order);
$from = 1;
}
$_type = input('request._type');
$_sub_type = input('request._sub_type');
if ($_type == "wechat") {
if ($_sub_type == "h5") {
$from = 5;
} else {
$from = 3;
}
} else if ($_type == "app") {
$from = 2;
}
$wx = new ApiWx();
$data = $wx->pay($order['user_id'], $order['order_amount'], $order['attach'], $order['order_no'], 0, $from, 0, 1);
$data['order_id'] = $param['order_id'];
success_json($data);
} else {
//余额支付
$userinfo = User::get($order['user_id']);
if ($userinfo['balance'] >= $order['order_amount']) {
$record = new Userbalancerecord();
$remark = "购买共享毛巾余额支付";
$record->addBalanceRecord($order['user_id'], $order['uniacid'], 2, $send_money = '0.00', -$order['order_amount'], $param['order_id'], $order['order_no'],
$remark);
$orderModel = new Sharetowelorder();
$order = $orderModel->where(['id' => $param['order_id']])->find();
$paydata['transaction_id'] = '';
$paydata['out_trade_no'] = '';
$this->successdo($order, $paydata);
success_json(["order_id" => $order["id"]]);
} else {
error_json('余额不足无法支付');
}
}
}
//立即支付使用 根据订单id获取微信支付参数
public function getWxParamByOrderId()
{
$order_id = input('request.order_id');
$pay_type = input('post.pay_type', 1);
$app = input('request.app', 0);
if (input('get._type') == 'wechat') {
if (input('get._sub_type') == 'wechat') {
$app = 2;//公众号
} else if (input('get._sub_type') == 'h5') {
$app = 4;
}
}
//获取微信支付参数
$param = array(
'type' => 'SharetowelOrder',
'order_id' => $order_id,
'app' => $app,
'pay_type' => $pay_type,
);
$this->getPayParam($param);
}
//普通订单支付回调处理
public function payNotify($data)
{
logging_run("SharetowelOrder notify");
logging_run($data);
global $_W;
$attach = json_decode($data['attach'], 1);
$_W['uniacid'] = $attach['uniacid'];
$order = Sharetowelorder::get($attach['order_id']);
if (!$order || $order['pay_status'] == 1) {
echo 'SUCCESS';
exit;
}
$this->successdo($order, $data);
echo 'SUCCESS';
}
public function successdo(Sharetowelorder $order, $data)
{
$order->allowField(true)->save(["order_status" => 20, 'pay_status' => 1, 'pay_time' => time(), 'transaction_id' => $data["transaction_id"], 'out_trade_no' => $data["out_trade_no"]]);
}
/**
* 订单列表
*/
public function getOrderlist()
{
$user_id=input('request.user_id');
$model=new Sharetowelorder();
$model->fill_order_limit_length();
$data=$model->where("order_status","<>",10)->where("user_id",$user_id)->order('id desc')->select();
foreach ($data as &$item){
$item["status_str"] = Sharetowelorder::Orderstatus[$item['order_status']];
$m = Sharetowelmachine::find($item["sharetowelmachine_id"]);
$item["pic"] =$m["pic"];
$item["name"] =$m["name"];
}
success_withimg_json($data);
}
}
Notify.php
else if($attach['type']=='SharetowelOrder'){
//共享毛巾订单
$order=new ApiSharetowelorder();
$order->payNotify($data);
}