sifangpay/app/Models/UploadQrcode.php

67 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UploadQrcode extends BaseModel
{
//
// use SoftDeletes;
public function msg($data)
{
if (request()->ajax()) {
return response()->json($data);
}
return $this->htmlAlert($data);
}
public function htmlAlert($data)
{
echo '<script>alert("' . $data['msg'] . '");window.history.back()</script>';
}
public function findOneQrcode($request){
$time = time();
$token = $request->input('_token');
$old = $this::where([['session_id', '=', $token], ['expire_time', '>', intval($time)]])->orderBy('order_count', 'asc')->first();
if ($old) {
return $old;
}
$has_order = $this::where([['expire_time', '<', intval($time)]])->orderBy('order_count', 'asc')->first();
if ($has_order) {
$this::where('id', $has_order->id)->update(['session_id' => $token, 'expire_time' => $time + 1800, 'order_count' => $has_order->order_count + 1]);
} else {
return $this->msg(['error' => 1, 'msg' => '无可用二维码']);
}
return $has_order;
}
//ineed支付使用
public function findOneQrCodeByPay($orderData){
$time = time();
$old = $this::where([['session_id', '=', $orderData['out_trade_sn']], ['expire_time', '>', intval($time)], ['money', '=', $orderData['order_money']]])
->orderBy('order_count', 'asc')->first();
if ($old) {
return $old;
}
$has_order = $this::where([['expire_time', '<', intval($time)], ['money', '=', $orderData['order_money']]])->orderBy('order_count', 'asc')->first();
if ($has_order) {
$this::where('id', $has_order->id)->update(['session_id' => $orderData['out_trade_sn'], 'expire_time' => $time + 1800, 'order_count' => $has_order->order_count + 1]);
} else {
return $this->msg(['error' => 1, 'msg' => '无可用二维码']);
}
$has_order = $this::where('id', $has_order->id)->first();
return $has_order;
}
//接收到ineed回调
public function updateExpireTime($uid){
$has_order = $this::where([['uid', '=', $uid], ['expire_time', '>', intval(time())]])->orderBy('order_count', 'asc')->first();
if ($has_order) {
// $this::where('id', $has_order->id)->orderBy('order_count', 'asc')->update(['expire_time' => time()]);
}
return $has_order;
}
}