sifangpay/app/Http/Controllers/Api/OrderApiController.php

182 lines
6.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Models\Order;
use App\Models\OrderInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
class OrderApiController extends BaseController
{
//
public function checkPay(Request $request)
{
$order_sn = $request->input('order_sn');
Log::channel('pay_order')->info('查询订单', $request->input());
if (!$order_sn) {
$data = [
'error' => 1,
'msg' => '订单不存在',
'backurl' => '',
];
} else {
//查询这个订单是否已经支付
$order = Order::where('order_sn', $order_sn)->first();
if ($order['pay_status'] == 1) {
$backurl = route('web.user.index');
//判断是否是商户id
if ($order->merchant_id == 0) {
$backurl = route('web.user.index');
} else {
//判断外站
//取得订单信息
$order_info = OrderInfo::where('order_id', $order['id'])->first();
$return_url = $order_info->return_url;
$merchant = get_merchant($order_info->app_id);//取得商户
//发送回调
//发送签名过去,组装信息
$notify_data = [
'pay_money' => $order->pay_money,
'money' => $order->order_money,
'order_sn' => $order->order_sn,
'out_trade_sn' => $order->out_trade_sn,
'pay_order_sn' => $order->pay_order_sn,
// 'sign' => md5($merchant['token'] . $order_info->sign),
'sign' => md5($merchant['token'] . $order_info->sign . 'pay_money=' . $order->pay_money . 'pay_status=' . $order->pay_status),
'attch' => $order_info->attach,
'pay_status' => $order->pay_status,
'return_url' => $order_info->return_url,
'notify_url' => $order_info->notify_url,
];
$backurl = arr_to_url($return_url, $notify_data);
}
$data = [
'error' => 0,
'msg' => '支付成功',
'backurl' => $backurl,
];
} else {
$data = [
'error' => 1,
'msg' => '等待支付',
'backurl' => '',
];
}
}
return $this->json($data);
}
public function cancleOrder(Request $request)
{
$order_sn = $request->input('order_sn');
if (!$order_sn) {
$data = [
'error' => 1,
'msg' => '订单不存在',
'backurl' => '',
];
} else {
//查询这个订单是否已经支付
//只能取消30分钟前的订单
$date_at = date('Y-m-d H:i:s', strtotime('-30 minute'));
$order = Order::where('order_sn', $order_sn)->where('order_at', '>=', $date_at)->first();
if (empty($order)) {
$data = [
'error' => 1,
'msg' => '订单不存在',
'backurl' => '',
];
return $this->json($data);
}
if ($order['pay_status'] == 1) {
$data = [
'error' => 1,
'msg' => '支付成功',
'backurl' => route('web.pay.scan.return', ['order_sn' => $order->order_sn]),
];
} else {
//进行取消订单
$order->status = 4;//取消订单
$r = $order->save();
if ($r) {
$order_info = OrderInfo::where('order_id', $order->id)->first();
$url_info = parse_url($order_info->return_url);
$backurl = '';
if (isset($url_info['scheme'])) {
$backurl = $url_info['scheme'] . '://';
}
if (isset($url_info['host'])) {
$backurl .= $url_info['host'];
}
$data = [
'error' => 0,
'msg' => '取消订单成功',
'backurl' => $backurl,
];
}
}
}
return $this->json($data);
}
public function updateOrder(Request $request)
{
$mark = $request->input('pay_mark');
$pay_account = $request->input('pay_account');
$order_sn = $request->input('order_sn');
if (!$order_sn) {
$data = [
'error' => 1,
'msg' => '订单不存在',
'backurl' => '',
];
return $this->json($data);
}
$date_at = date('Y-m-d H:i:s', strtotime('-30 minute'));
$order = Order::where('order_sn', $order_sn)->where('order_at', '>=', $date_at)->first();
if (empty($order)) {
$data = [
'error' => 1,
'msg' => '订单不存在',
'backurl' => '',
];
return $this->json($data);
}
if($order->ewm_mark || $order->ewm_account)
{
$data = [
'error' => 1,
'msg' => '你已经提交过',
'backurl' => '',
];
return $this->json($data);
}
//xieru
$order->ewm_mark = $mark;
$order->ewm_account = $pay_account;
$r = $order->save();
if ($r) {
$data = [
'error' => 0,
'msg' => '提交成功,请等待系统确认',
'backurl' => '',
];
return $this->json($data);
}
$data = [
'error' => 1,
'msg' => '提交失败,请重试',
'backurl' => '',
];
return $this->json($data);
}
}