305 lines
9.4 KiB
PHP
305 lines
9.4 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\Models\UploadQrcode;
|
|
use App\ServicePay\ApiOrderServices;
|
|
use App\ServicePay\LocalOrderServices;
|
|
use App\ServicePay\ZhanXinPayServices;
|
|
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 \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\View\View|void
|
|
*
|
|
*/
|
|
public function showOrder(Request $request){
|
|
if($request->input('out_trade_sn')){
|
|
$pay = new ApiOrderServices();
|
|
return $pay->showOrder($request);
|
|
}else{
|
|
return abort(403,'缺少参数');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查订单
|
|
* @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' => '',
|
|
];
|
|
Log::channel('pay_order')->info('查询订单返回', $data);
|
|
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' => '',
|
|
];
|
|
Log::channel('pay_order')->info('查询订单返回', $data);
|
|
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' => '',
|
|
];
|
|
Log::channel('pay_order')->info('查询订单返回', $data);
|
|
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
|
|
];
|
|
}
|
|
Log::channel('pay_order')->info('查询订单返回', $data);
|
|
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);
|
|
}
|
|
|
|
//回调的时候 查询订单
|
|
private function queryOrder($orderid,$uid){
|
|
$chanxinconfig = config('qrcodeurl.zhanxin');
|
|
$data=[
|
|
'uid'=>$uid,
|
|
'ts'=>time(),
|
|
];
|
|
$data['key']=md5(http_build_query(array_merge($data,['key'=>$chanxinconfig['secret']])));
|
|
|
|
$curl=$this->curlGet($chanxinconfig['baseurl'].$chanxinconfig['query'].'?'.http_build_query($data));
|
|
}
|
|
|
|
private function curlGet($url){
|
|
$curl = curl_init();
|
|
$headerArray = array("Content-Type: application/json;", "Accept: application/json");
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL =>$url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_ENCODING => '',
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => 'GET',
|
|
CURLOPT_HTTPHEADER => $headerArray,
|
|
));
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
$response = curl_exec($curl);
|
|
|
|
curl_close($curl);
|
|
return $response;
|
|
}
|
|
|
|
//接收回调展信回调
|
|
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['ts'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['ts'] = $request['ts'];
|
|
|
|
if (!isset($request['orderid'])) {
|
|
return 'fail';
|
|
}
|
|
$insert['orderid'] = $request['orderid'];
|
|
if (!isset($request['ts'])) {
|
|
return 'fail';
|
|
}
|
|
|
|
$insert['receive_time'] = $request['ts'];
|
|
$chanxinconfig = config('qrcodeurl.zhanxin');
|
|
$sinpa = [
|
|
'ts' => $insert['ts'],
|
|
'key' => $chanxinconfig['secret'],
|
|
'money' => $insert['money'],
|
|
'from' => $insert['from'],
|
|
'to' => $insert['to'],
|
|
'orderid' => $insert['orderid'],
|
|
];
|
|
if (isset($request['serialnumber'])) {
|
|
$insert['serialnumber'] = $request['serialnumber'];
|
|
}
|
|
$str = '';
|
|
foreach ($sinpa as $k => $v) {
|
|
$str .= '&' . $k . '=' . $v;
|
|
}
|
|
$str = trim($str, '&');
|
|
$sign = strtolower(md5($str));
|
|
if (!isset($request['key']) || $request['key'] != $sign) {
|
|
return 'fail8';
|
|
}
|
|
try {
|
|
//检查有没有发过这个
|
|
$money = new NotifyMoneys();
|
|
$isset = $money->findByOrder($request['orderid']);
|
|
if ($isset) {
|
|
return 'isset';
|
|
}
|
|
$insert['create_at'] = time();
|
|
$qrcode = new UploadQrcode();
|
|
$info = $qrcode->updateExpireTime($insert['to']);
|
|
if (empty($info)) {
|
|
return 'faili';
|
|
}
|
|
$insert['out_trade_sn'] = $info->session_id;
|
|
|
|
unset($insert['key'], $insert['ts']);
|
|
$res = $money->insert($insert);
|
|
$service = new ZhanXinPayServices();
|
|
$service->afterPay($insert);
|
|
} catch (\Exception $e) {
|
|
Log::channel('zhanxin')->info('ineed 给我回调=>exception' . $e->getMessage(), []);
|
|
return 'faile';
|
|
}
|
|
Log::channel('zhanxin')->info('ineed 给我回调=>success', []);
|
|
return 'success';
|
|
}
|
|
}
|