sifangpay/app/Http/Controllers/Merchant/OrderHandleApiController.php

202 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers\Merchant;
use App\Models\Order;
use App\Models\OrderHandleLog;
use App\Models\PayForLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Log;
use Ixudra\Curl\Facades\Curl;
use PayServices;
use function PHPSTORM_META\type;
class OrderHandleApiController extends BaseController
{
public $input_status;
public $order_id;
public $orderObj;
public $userObj;
public $serverObj;
public function __construct(Request $request)
{
parent::__construct();
//通过之后再获取
$this->middleware(function ($request, $next) {
$this->input_status=$request->status;
$this->order_id=$request->order_id;
$this->orderObj=Order::find($this->order_id);
if($this->orderObj->merchant_id!=0)
{
$this->serverObj=$this->orderObj->merchants;
}
view()->share('order_id',$this->order_id);
view()->share('order',$this->orderObj);
return $next($request);
});
}
public function handle($order_id,$type,Request $request){
return $this->$type($request);
}
public function handlePost($order_id,$type,Request $request){
return $this->$type($request);
}
public function __call($method, $parameters)
{
return response()->json(['error'=>1,'msg'=>'该方法不存在']);
}
public function msg($arr)
{
return response()->json($arr);
}
public function notifyPost(Request $request){
//发送curl过去
$order_info=$this->orderObj->infos;
$agent=$request->input('agent',0);
if($agent==1)
{
//如果不存在
if(!in_array($order_info->merchant_id,merchant()->mySubMerchant()))
{
return $this->errorJosn('不能操作其他人的订单');
}
}else
{
if($order_info->merchant_id!=$this->getMerchantId())
{
return $this->errorJosn('不能操作其他人的订单');
}
}
if($order_info)
{
$notify_url=$order_info->notify_url;//回调地址
//签名计算
$data=[
'app_id'=>$this->serverObj->app_key,
'token'=>$this->serverObj->token,
'money'=>$this->orderObj->order_money,
'return_url'=>$order_info->return_url,
'notify_url'=>$order_info->notify_url,
'out_trade_sn'=>$this->orderObj->out_trade_sn
];
//dump($data);
//回调携带参数回去
//返回付款金额,原来充值金额,本站订单号,支付流水号,原网站订单号,附加消息原样返回
$notify_data=[
'pay_money'=>$this->orderObj->pay_money,
'money'=>$this->orderObj->order_money,
'order_sn'=>$this->orderObj->order_sn,
'out_trade_sn'=>$this->orderObj->out_trade_sn,
'pay_order_sn'=>$this->orderObj->pay_order_sn,
// 'sign'=>PayServices::sign($data,1),
'sign' => md5($this->serverObj->token . $order_info->sign . 'pay_money=' . $this->orderObj->pay_money . 'pay_status=' . $this->orderObj->pay_status),
'attch'=>$order_info->attach,
'pay_status' => $this->orderObj->pay_status,
'return_url'=>$order_info->return_url,
'notify_url'=>$order_info->notify_url
];
Log::channel('pay_order')->info('merchant手动回调信息', $notify_data);
//dd($notify_data);
if(!$notify_url)
{
return response(['error'=>1,'msg'=>'回调失败,缺少回调地址','back'=>'']);
}
//发送curl
$r=Curl::to($notify_url)->withData($notify_data)->post();
//返回success表示回调成功
//dump(gettype($r));
if($r=='success')
{
DB::beginTransaction();
//更新订单
$this->orderObj->out_notify_status=1;
$this->orderObj->status=1;
$or=$this->orderObj->save();
//
$order_info->notify_data=json_encode($notify_data);
$order_info->notify_status=$r;
$lr=$order_info->save();
if($lr && $or)
{
//写入日志
$this->orderHandleLog(['from_status' => 2, 'to_status' => 1,'name'=>'外站回调成功'], $this->order_id);
DB::commit();
return response(['error'=>0,'msg'=>'回调成功','back'=>$r]);
}
DB::rollBack();
$this->orderHandleLog(['from_status' => 2, 'to_status' => 1,'name'=>'外站回调失败'], $this->order_id);
return response(['error'=>1,'msg'=>'对方回调成功,本站更新失败','back'=>$r]);
}else
{
return response(['error'=>1,'msg'=>'回调失败,外部没有返回success','back'=>$r,'sign_data'=>$data,'url'=>$notify_url,'notify_data'=>$notify_data]);
}
}
return response(['error'=>1,'msg'=>'回调失败,缺少回调地址','back'=>'']);
}
//结算日志
public function payForLog($config){
$log=new PayForLog();
$log->order_id = $this->order_id;
foreach ($config as $k => $v) {
$log->$k = $v;
}
$log->admin_id=admin('id');
$log->admin_name=admin('account');
$r = $log->save();
if ($r) {
return true;
}
return false;
}
//订单操作日志
public function orderHandleLog($config){
$log = new OrderHandleLog();
foreach ($config as $k => $v) {
$log->$k = $v;
}
$log->order_id = $this->order_id;
$log->handle_user_id = merchant('id');//
$log->handle_user_name =merchant('name');//
$log->handle_user_type ='merchant';//
$r = $log->save();
$this->insertLog('操作订单,行为是'.$config['name']);
if ($r) {
return true;
}
return false;
}
}