204 lines
5.8 KiB
PHP
204 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
use App\Models\Good;
|
|
use App\Models\Merchant;
|
|
use App\Models\NotifyMoneys;
|
|
use App\Models\Order;
|
|
use App\ServicePay\ApiOrderServices;
|
|
use App\ServicePay\LocalOrderServices;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use DB;
|
|
|
|
|
|
class OrderController extends BaseController
|
|
{
|
|
|
|
public $order;
|
|
|
|
public function debugLog($str = '', $arr = [])
|
|
{
|
|
|
|
$arr = is_array($arr) ? $arr : [$arr];
|
|
Log::channel('pay_order')->info($str, $arr);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
|
|
$id = $request->input('id');
|
|
$goods = Good::find($id);
|
|
$data = [
|
|
'title' => '立即下单',
|
|
'good' => $goods
|
|
];
|
|
if (is_mobile_client()) {
|
|
$this->setViewPath('', 'mindex');
|
|
}
|
|
return $this->display($data);
|
|
}
|
|
|
|
|
|
public function orderPost(Request $request)
|
|
{
|
|
if ($request->input('app_id')) {
|
|
return $this->apiOrderPost($request);
|
|
} else {
|
|
return $this->localOrderPost($request);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 检查订单
|
|
* @param Request $request
|
|
* @return mixed
|
|
*/
|
|
public function orderCheck(Request $request){
|
|
$order_sn = $request->input('order_sn');
|
|
$out_trade_sn = $request->input('out_trade_sn');
|
|
$key = $request->input('app_id');
|
|
$time=$request->input('time');
|
|
if (empty($time) || $time < time() - 60) {
|
|
$data = [
|
|
'error' => 1,
|
|
'msg' => '请求错误',
|
|
'order' => [],
|
|
'backurl' => '',
|
|
];
|
|
return response()->json($data);
|
|
}
|
|
Log::channel('pay_order')->info('merch查询订单', $request->input());
|
|
|
|
if ((!$order_sn && empty($out_trade_sn)) || empty($key)) {
|
|
$data = [
|
|
'error' => 1,
|
|
'msg' => '订单不存在',
|
|
'order'=>[],
|
|
'backurl' => '',
|
|
];
|
|
}else{
|
|
// $merchent=Merchant::where('app_key',$key)->first(["token"]);
|
|
$merchent = get_merchant($key);//取得商户
|
|
if(empty($merchent)){
|
|
$data = [
|
|
'error' => 2,
|
|
'msg' => '商户不存在',
|
|
'order'=>[],
|
|
'backurl' => '',
|
|
];
|
|
return response()->json($data);
|
|
}
|
|
$params=$request->input();
|
|
|
|
ksort($params);
|
|
$secret='';
|
|
$paramsign=$params['sign']??"";
|
|
unset($params['sign']);
|
|
foreach ($params as $k => $d) {
|
|
if (!empty($d)) {
|
|
$secret .= $k . '=' . $d . '&';
|
|
}
|
|
}
|
|
$token=$merchent['token'];
|
|
$secret .= 'token=' . $token;
|
|
$sign = md5(strtolower(trim($secret, '&')));
|
|
if($sign!=$paramsign){
|
|
$data = [
|
|
'error' => 3,
|
|
'msg' => '签名不正确',
|
|
'order'=>[],
|
|
'backurl' => '',
|
|
];
|
|
return response()->json($data);
|
|
}
|
|
//查询这个订单是否已经支付
|
|
if (!empty($order_sn)) {
|
|
$order = Order::where('order_sn', $order_sn)->first();
|
|
} else {
|
|
$order = Order::where('out_trade_sn', $out_trade_sn)->first();
|
|
}
|
|
if (is_null($order['order_sn'])) {
|
|
$data = [
|
|
'error' => 1,
|
|
'msg' => '订单不存在',
|
|
'order'=>[],
|
|
'backurl' => '',
|
|
];
|
|
return response()->json($data);
|
|
}
|
|
$orderData = [
|
|
'order_sn' => $order['order_sn'],
|
|
'order_money' => $order['order_money'],
|
|
'pay_status' => $order['pay_status'],
|
|
'pay_money' => $order['pay_money'],
|
|
'order_at' => $order['order_at']
|
|
];
|
|
ksort($orderData);
|
|
$secret = '';
|
|
foreach ($orderData as $k => $d) {
|
|
$secret .= $k . '=' . $d . '&';
|
|
}
|
|
$secret .= 'token=' . $token;
|
|
$sign = md5(strtolower(trim($secret, '&')));
|
|
$orderData['sign']=$sign;
|
|
$data = [
|
|
'error' => 0,
|
|
'msg' => '',
|
|
'order' =>$orderData
|
|
];
|
|
}
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function localOrderPost($request)
|
|
{
|
|
$pay = new LocalOrderServices();
|
|
return $pay->order($request);
|
|
}
|
|
|
|
public function apiOrderPost($request)
|
|
{
|
|
$pay = new ApiOrderServices();
|
|
return $pay->order($request);
|
|
}
|
|
|
|
public function notify(Request $request){
|
|
$insert = [];
|
|
if (!isset($request['title'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['title'] = $request['title'];
|
|
if (!isset($request['money'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['money'] = $request['money'];
|
|
if (!isset($request['from'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['from'] = $request['from'];
|
|
if (!isset($request['to'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['to'] = $request['to'];
|
|
if (!isset($request['orderid'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['orderid'] = $request['orderid'];
|
|
if (!isset($request['time'])) {
|
|
$insert['receive_time'] = time();
|
|
} else {
|
|
$insert['receive_time'] = $request['time'];
|
|
}
|
|
try {
|
|
$money = new NotifyMoneys();
|
|
$money->insert($insert);
|
|
} catch (\Exception $e) {
|
|
return 'fail';
|
|
}
|
|
return 'success';
|
|
}
|
|
}
|