Commit 477a3111 by LiuJunYi

1

parent 4c6949fc
......@@ -9,6 +9,7 @@
namespace app\api\controller;
use think\Db;
use think\Exception;
use think\Loader;
use think\Validate;
use app\common\model\Project as ProjectModel;
......@@ -16,6 +17,7 @@ use app\common\model\Order as OrderModel;
use app\common\model\User as UserModel;
use app\common\model\Refund as RefundModel;
Loader::import('wxpay.Api');
class Order extends Base
......@@ -73,12 +75,10 @@ class Order extends Base
//查看是否为会员
$user = UserModel::get($this->userinfo[0]);
//活动总价
$price = $user['is_vip'] ? $project['price'] : $project['vip_price'];
$price = $user['is_vip'] ? $project['vip_price'] : $project['price'];
$order->total_fee = $price * 100 * input('post.sign_limits') / 100;
//TODO::支付状态应该是noPay,测试换成pay
$order->status = config('order_status.pay');
// $order->status = config('order_status.no_pay');
$order->extras = [
$order->status = config('order_status.no_pay');
$order->extras = [
'nickname' => input('post.nickname'),
'phone' => input('post.phone'),
'adult_number' => input('post.adult_number'),
......@@ -94,44 +94,39 @@ class Order extends Base
$data['order_id'] = intval($order->id);
$data['total_fee'] = doubleval($order->total_fee);
//生成订单
//TODO::关闭支付接口
// 更新订单号-防止签名失败
// $order->out_trade_no = (new \WxPayConfig())->GetMerchantId() . time() . $order->id;
// $input = new \WxPayUnifiedOrder();
// $input->SetBody($order->title);
// $input->SetAttach($order->id);
// $input->SetOut_trade_no($order->out_trade_no);
// $input->SetTotal_fee(floatval($order->total_fee) * 100); // 单位为分
// $input->SetTime_start(date("YmdHis"));
// $input->SetTime_expire(date("YmdHis", time() + 600));
// //$input->SetGoods_tag("wx-xcx");
// $input->SetNotify_url(config('wx_pay.notify_url'));
// $input->SetTrade_type("JSAPI");
// $input->SetOpenid($this->userinfo[1]);
// $unifiedOrder = \WxPayApi::unifiedOrder($input);
// if ($unifiedOrder["return_code"] == 'FAIL') {
// return ['code' => 1, 'msg' => $unifiedOrder['return_msg']];
// }
// // 保存 prepay_id 用于发送模版消息
// $order->prepay_id = $unifiedOrder['prepay_id'];
// $order->save();
// $jsApiParameters = $this->GetJsApiParameters($unifiedOrder);
// if ($jsApiParameters) {
// return ['code' => 0, 'msg' => '支付参数', 'data' => $data, 'result' => json_decode($jsApiParameters)];
// } else {
// throw new \Exception('订单签名失败');
// }
Db::commit();
$order->out_trade_no = (new \WxPayConfig())->GetMerchantId() . time() . $order->id;
$config = new \WxPayConfig();
$input = new \WxPayUnifiedOrder();
$input->SetBody($order->title);
$input->SetAttach($order->id);
$input->SetOut_trade_no($order->out_trade_no);
$input->SetTotal_fee(floatval($order->total_fee) * 100); // 单位为分
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
//$input->SetGoods_tag("wx-xcx");
$input->SetNotify_url(config('wx_pay.notify_url'));
$input->SetTrade_type("JSAPI");
$input->SetOpenid($this->userinfo[2]);
$unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
if ($unifiedOrder["return_code"] == 'FAIL') {
return ['code' => 1, 'msg' => $unifiedOrder['return_msg']];
}
// 保存 prepay_id 用于发送模版消息
$order->prepay_id = $unifiedOrder['prepay_id'];
$order->save();
$jsApiParameters = $this->GetJsApiParameters($unifiedOrder);
if ($jsApiParameters) {
Db::commit();
return ['code' => 0, 'msg' => '支付参数', 'data' => $data, 'result' => json_decode($jsApiParameters)];
} else {
throw new \Exception('订单签名失败');
}
} catch (\Exception $e) {
Db::rollback();
return ['code' => 1, 'msg' => $e->getMessage()];
}
//TODO::删除伪成功
return ['code' => 0, 'msg' => '报名成功'];
}
/**
......@@ -173,10 +168,8 @@ class Order extends Base
} else {
$vip_endtime = strtotime("+{$year} year");
}
$vip_endtime = date('Y-m-d H:i:s', $vip_endtime);
//TODO::支付状态应该是noPay,测试换成pay
$order->status = config('order_status.pay');
// $order->status = config('order_status.no_pay');
$vip_endtime = date('Y-m-d H:i:s', $vip_endtime);
$order->status = config('order_status.no_pay');
$order->extras = [
'year' => $year,
'vip_endtime' => $vip_endtime,
......@@ -189,11 +182,10 @@ class Order extends Base
$data['order_id'] = intval($order->id);
$data['total_fee'] = doubleval($order->total_fee);
// 生成订单
// TODO::关闭支付接口
// // 更新订单号-防止签名失败
$order->out_trade_no = (new \WxPayConfig())->GetMerchantId() . time() . $order->id;
$input = new \WxPayUnifiedOrder();
$config = new \WxPayConfig();
$input = new \WxPayUnifiedOrder();
$input->SetBody($order->title);
$input->SetAttach($order->id);
$input->SetOut_trade_no($order->out_trade_no);
......@@ -203,8 +195,8 @@ class Order extends Base
// $input->SetGoods_tag("wx-xcx");
$input->SetNotify_url(config('wx_pay.notify_url'));
$input->SetTrade_type("JSAPI");
$input->SetOpenid($this->userinfo[1]);
$unifiedOrder = \WxPayApi::unifiedOrder($input);
$input->SetOpenid($this->userinfo[2]);
$unifiedOrder = \WxPayApi::unifiedOrder($config, $input);
if ($unifiedOrder["return_code"] == 'FAIL') {
return ['code' => 1, 'msg' => $unifiedOrder['return_msg']];
}
......@@ -213,19 +205,16 @@ class Order extends Base
$order->save();
$jsApiParameters = $this->GetJsApiParameters($unifiedOrder);
if ($jsApiParameters) {
Db::commit();
return ['code' => 0, 'msg' => '支付参数', 'data' => $data, 'result' => json_decode($jsApiParameters)];
} else {
throw new \Exception('订单签名失败');
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return ['code' => 1, 'msg' => $e->getMessage()];
}
//TODO::删除伪成功
return ['code' => 0, 'msg' => '开通成功'];
}
/**
......@@ -242,14 +231,16 @@ class Order extends Base
|| $UnifiedOrderResult['prepay_id'] == "") {
return null;
}
$jsapi = new \WxPayJsApiPay();
$jsapi->SetAppid($UnifiedOrderResult["appid"]);
$timeStamp = time();
$jsapi->SetTimeStamp("$timeStamp");
$jsapi->SetNonceStr(\WxPayApi::getNonceStr());
$jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
$jsapi->SetSignType("MD5");
$jsapi->SetPaySign($jsapi->MakeSign());
$config = new \WxPayConfig();
$jsapi->SetPaySign($jsapi->MakeSign($config));
$parameters = json_encode($jsapi->GetValues());
return $parameters;
}
......@@ -260,47 +251,57 @@ class Order extends Base
*/
public function callback()
{
$xml = file_get_contents("php://input");
$data = \WxPayResults::Init($xml);
if (!array_key_exists("transaction_id", $data)) {
$data = simplexml_load_string($xml);//将文件转换成 对象
if (empty($data->transaction_id)) {
echo $this->toXML(['return_code' => 'FAIL', 'return_msg' => '参数错误']);
exit;
}
// 查询订单,判断订单真实性
if (!$data["out_trade_no"] || !$this->Queryorder($data["transaction_id"])) {
if (!$data->out_trade_no || !$this->Queryorder($data->transaction_id)) {
echo $this->toXML(['return_code' => 'FAIL', 'return_msg' => '订单真实性存疑']);
exit;
}
$order = OrderModel::where(['out_trade_no' => $data["out_trade_no"]])->find();
$order = OrderModel::where(['out_trade_no' => $data->out_trade_no])->find();
if (!$order) {
echo $this->toXML(['return_code' => 'FAIL', 'return_msg' => '订单不存在']);
exit;
}
$succeed = false;
Db::startTrans();
try {
// 订单状态更新 - 已支付
$order->status = config("order_status.pay");
$order->trade_status = 'SUCCESS';
$order->save();
if ($order->type == config('order_type.vip')) {
//给user表加数据
$user = UserModel::get($this->userinfo[0]);
$order = OrderModel::get($data->attach);
$user = UserModel::get($order->uid);
$user->is_vip = 1;
$user->vip_endtime = $order['extras']['end_time'];
$user->vip_endtime = $order['extras']['vip_endtime'];
$user->save();
}
// 提交事务
Db::commit();
$succeed = true;
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
$succeed = false;
}
if ($succeed) {
echo $this->toXML(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
} else {
......@@ -316,9 +317,10 @@ class Order extends Base
*/
private function Queryorder($transaction_id)
{
$input = new \WxPayOrderQuery();
$input = new \WxPayOrderQuery();
$config = new \WxPayConfig();
$input->SetTransaction_id($transaction_id);
$result = \WxPayApi::orderQuery($input);
$result = \WxPayApi::orderQuery($config, $input);
if (array_key_exists("return_code", $result)
&& array_key_exists("result_code", $result)
&& $result["return_code"] == "SUCCESS"
......@@ -335,7 +337,7 @@ class Order extends Base
*/
public function getVipPrice()
{
return ['code' => 1, 'msg' => 'success', 'price' => number_format(config('wx_pay.vip_price'), 2)];
return ['code' => 0, 'msg' => 'success', 'price' => number_format(config('wx_pay.vip_price'), 2)];
}
/**
......@@ -350,13 +352,13 @@ class Order extends Base
$p = input('post.p/d', 1);
$page = input('post.page/d', 8);
$status = input('post.status/d', 1);
$uid = $this->userinfo[0];
$uid = $this->userinfo[0];
$order = OrderModel::with('project')
->where('status', $status)
->where('uid', $uid)
->where('type', 1)
->page($p, $page)
->order('id','desc')
->order('id', 'desc')
->select();
$order = collection($order);
if ($order->isEmpty()) {
......@@ -379,7 +381,7 @@ class Order extends Base
->where('uid', $this->userinfo[0])
->where('type', config('order_type.project'))
->where('status', config('order_status.pay'))
->where('is_refund','in', [config('refund_status.no'), config('refund_status.no_pass')])
->where('is_refund', 'in', [config('refund_status.no'), config('refund_status.no_pass')])
->find();
if (!$order) return ['code' => 1, 'msg' => '订单错误'];
$order->is_refund = config('refund_status.to_examine');
......@@ -391,4 +393,46 @@ class Order extends Base
]);
return ['code' => 0, 'msg' => '提交成功'];
}
/**
* array转xml
*
* @param [type] $value
* @return void
*/
private function toXML($values)
{
if (!is_array($values) || count($values) <= 0) {
return "<xml></xml>";
}
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
/**
* 发送模板消息
* @url /api/Order/sendTemplate
*
*/
public function sendTemplate()
{
//查看订单是否存在
// $id = input('order_id',0);
// $order = OrderModel::get($id);
// if(!$order){
// return ['code'=>1,'msg'=>'订单不存在'];
// }
//发送消息模板
return ['code' => 0, 'msg' => '支付成功','data'=>['order'=>input('order_id')]];
}
}
\ No newline at end of file
......@@ -296,7 +296,7 @@ class Project extends Base
// 正常
$project['status'] = 0;
$project['status_text'] = '立即报名';
if (strtotime($project['sign_endtime']) > time()) {
if (strtotime($project['sign_endtime']) < time()) {
// 已过期
$project['status'] = 1;
$project['status_text'] = '预约下次';
......
......@@ -156,7 +156,17 @@ class User extends Base
{
//用户UID
$uid = $this->userinfo[0];
$user = UserModel::get($uid);
$user = UserModel::get($uid)->toArray();
//过期时间
if($user['is_vip']){
if(strtotime($user['vip_endtime'])>time()){
$user['vip_endtime_str'] = '剩余'.ceil((strtotime($user['vip_endtime'])-time())/86400).'天';
}else{
$user['vip_endtime_str'] = '已到期';
}
}else{
$user['vip_endtime_str'] = '';
}
$user['hidephone'] = substr_replace($user['phone'], '****', 3, 4);
return ['code' => 0, 'msg' => 'success', 'data' => $user];
}
......
......@@ -71,20 +71,8 @@ class Order extends BaseModel
public static function getGroupNumberByProject($pid, $sign_limits)
{
//获取家庭组限制
$project = ProjectModel::get($pid);
$order = self::lock(true)
->where(['status' => config('order_status.no_pay'), 'created_at' => ['> time', date('Y-m-d H:i:s', time() - 300)]])
->whereOr('status' , 'in', [config('order_status.pay'), config('order_status.wait_comment'), config('order_status.complete')])
->select();
$count = 0;
if ($order) {
foreach ($order as $k => $v) {
if($v['type']==config('order_type.project')){
$count += $v['extras']['sign_limits'];
}
}
}
if ($count + $sign_limits > $project['sign_limits']) {
$project = ProjectModel::lock(true)->find($pid);
if ($project['sign_limits'] >= intval($project['sign_num'])+$sign_limits) {
return true;
}
return false;
......
......@@ -322,9 +322,16 @@ $config = [
// +----------------------------------------------------------------------
'wx_pay' => [
'notify_url' => '域名/api/Order/callback',
'vip_title' => '美行者会员VIP',
'vip_price' => 49.00
'notify_url' => 'https://app.maveler.com/api/Order/callback',
'vip_title' => '美行者会员VIP',
'vip_price' => 00.01,
'app_id' => 'wx1b834552a7d49bb3',
'merchant_id' => '1515838821',
'sign_type' => 'MD5',
'key' => 'meixingzheqinziyou2018jqtechgzgy',
'app_secret' => '9708059f5f0a280bea0779df44389231',
'ssl_cert_path' => '',
'ssl_key_path' => ''
],
];
......
......@@ -2,7 +2,7 @@
require_once "WxPay.Exception.php";
require_once "WxPay.Config.Interface.php";
require_once "WxPay.Data.php";
require_once "WxPay.Config.php";
require_once "Config.php";
/**
*
......
......@@ -33,7 +33,7 @@ class WxPayConfig extends WxPayConfigInterface
}
public function GetMerchantId()
{
return config('wx_pay.erchant_id');
return config('wx_pay.merchant_id');
}
//=======【支付相关配置:支付成功回调地址/签名方式】===================================
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment