You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
6.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\API\H5;
use App\Http\Controllers\API\AspNetZhuanController;
use App\Http\Controllers\API\XCXApiController;
use App\Http\Controllers\Controller;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PayController extends Controller
{
//支付回调.弃用!!!!
public function Notify()
{
$order_num = request('order_num');
$status = request('status');
$pay_info = request('pay_info');
if (!isset($order_num)) return \Yz::echoError1("订单号不能为空");
$order = DB::table('orders')->where(['order_number' => $order_num])->first();
if (!$order) return \Yz::echoError1("订单不存在");
if ($status == 'SUCCESS') {
$u = DB::table('orders')->where(['order_number' => $order_num])->update([
'status' => 2
]);
if ($u) {
return \Yz::Return(true, "更新成功", ['order_num' => $order_num]);
}
}
}
//对参数进行签名,返回前端需要的参数,前端携带参数跳转小程序支付
public function StartPay()
{
$openid = request('openid');
$id = request('id');
if(!isset($id)) return \Yz::echoError1("id不能为空");
$orderInfo = DB::table('orders')->where(['id' => $id,])->first();
if (!$orderInfo) return \Yz::echoError1("未找到有效订单");
if ($orderInfo->status !== 1) return \Yz::echoError1("订单不是待支付状态不能支付。当前状态:" . $orderInfo->status);
$personInfo = DB::table('web_user_person')->where(['id' => $orderInfo->person_id, 'is_del' => 0])->first();
if (!$personInfo) return \Yz::echoError1("就诊人异常");
$sub_org_code = 0; //小程序端医院代码
if ($orderInfo->hospital_id == 1) {
$sub_org_code = 6;
}
if ($orderInfo->hospital_id == 2) {
$sub_org_code = 2;
}
if ($sub_org_code == 0) return \Yz::echoError1("医院id异常");
//每次支付前更新订单号为1个新订单号码
$randomPart = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6);
$new_ordernumber=substr($orderInfo->order_number, 0, -6);
$new_ordernumber=$new_ordernumber.$randomPart;
$u=DB::table('orders')->where(['id' => $id,])->update(['order_number'=>$new_ordernumber]);
if(!$u) return \Yz::echoError1("更新订单号失败");
$XCXApi = new XCXApiController();
$params = [
'appid' => $XCXApi::$appid,
'sub_org_code' => $sub_org_code,
'ghzid' => $personInfo->ghzid,
'orderid' => $new_ordernumber,
'order_desc' => $orderInfo->title,
'amount' => (int)($orderInfo->true_price * 100),
'notify_url' => config('app.globals.PayNotifyUrl')
];
$nonce = self::nonce();
$timestamp = (string)time();
//签名
$pay_path = "/jeecg-boot/hospital/openapi/order/create";
$base64Signature = $XCXApi::Sign($pay_path, $params, $nonce, $timestamp, true);
$params['timestamp'] = $timestamp;
$params['nonce'] = $nonce;
$params['signature'] = $base64Signature;
return \Yz::Return(true, "操作完成", ['info' => $params]);
}
//订单支付查询
public function CheckPay()
{
$openid = request('openid');
$order_number = request('order_number');
$XCX = new XCXApiController();
$res = $XCX::Post('订单查询', ['orderid' => $order_number]);
//判断如果支付成功,更改订单状态
if ($res['data']['trade_state'] === 'SUCCESS') {
$order = DB::table('orders')->where(['order_number' => $order_number])->first();
if($order->status == 1){
DB::table('orders')->where(['id' => $order->id])->update([
'status' => 2,
'pay_time' => date('Y-m-d H:i:s'),
]);
$is_yuyue=false;//是否已经勾选号源
if(!!$order->plan_id){
$is_yuyue=true;
$Finish = new OrderController();//预约体检
$Finish->Finish($order_number);
}
}
return \Yz::Return(true, "支付成功", ['id' => $order->id,'is_yuyue'=>$is_yuyue]);
} else {
return \Yz::echoError1("支付失败" . $res['data']['trade_state']);
}
}
//退款
public function Refund()
{
//先调用思信取消,恢复号源,然后再退款
//判断订单状态是否是已经支付,判断是否到检,
$openid = request('openid');
$id = request('id');
if(!isset($id)) return \Yz::echoError1("id不能为空");
$orderInfo = DB::table('orders')->where(['id' => $id,])->first();
if(!$orderInfo) return \Yz::echoError1("未找到有效订单");
$userInfo = DB::table('web_users')->where(['id' => $orderInfo->web_user_id])->first();
if($openid != $userInfo->openid) return \Yz::echoError1("无权操作此订单");
$service = new OrderService();
$res=$service->Refund($id);
if(!$res['status']) return \Yz::echoError1($res['msg']);
if($res['status']) return \Yz::Return(true, "退款成功", []);
}
//机器人退款
public function AutoRefund(){
$id = request('id');
$key = request('key');
if($key != "dfsd2Ajd256SDI02") return \Yz::echoError1("权限校验错误");
if(!isset($id)) return \Yz::echoError1("id不能为空");
$orderInfo = DB::table('orders')->where(['id' => $id])->first();
$person=DB::table('web_user_person')->where(['id' => $orderInfo->person_id])->first();
$service = new OrderService();
$res=$service->Refund($id);
if(!$res['status']) return \Yz::echoError1($res['msg']);
if($res['status']){
//婚检发送短信提醒
$keshi="健康管理中心";
if($person->sex==1) $keshi="健康管理中心1楼男宾区";
if($person->sex==2) $keshi="健康管理中心2楼女宾区";
$data = [
"ghzid" => $person->ghzid,
"yyid" => 6,
"type" => "15",
"msg1" => $person->name,
"msg2" => $keshi,
"msg3" => $orderInfo->doctor,
"msg4" => $orderInfo->appointment_date.' '.$orderInfo->appointment_time,
"msg5" => "",
"msg6" => "",
"url" => ""
];
$dnet = new AspNetZhuanController();
$dnet->WeiXinSend($data);
return \Yz::Return(true, "自动退款成功", []);
}
}
public static function nonce($l = 16)
{
$charts = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789";
$max = strlen($charts) - 1;
$noncestr = "";
for ($i = 0; $i < $l; $i++) {
$noncestr .= $charts[rand(0, $max)];
}
return $noncestr;
}
}