2022-08-30 09:20:45 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
namespace App\Http\Controllers\Api;
|
2022-08-30 09:20:45 +00:00
|
|
|
|
2022-11-08 13:01:43 +00:00
|
|
|
use App\Exceptions\ChargeException;
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Models\Balance;
|
2022-10-07 05:15:08 +00:00
|
|
|
use App\Models\Transaction;
|
2022-08-30 09:20:45 +00:00
|
|
|
use Illuminate\Http\Request;
|
2022-11-06 11:28:22 +00:00
|
|
|
use Yansongda\LaravelPay\Facades\Pay;
|
2022-11-06 13:31:35 +00:00
|
|
|
use Yansongda\Pay\Exception\InvalidResponseException;
|
2022-11-08 13:01:43 +00:00
|
|
|
use function auth;
|
|
|
|
use function config;
|
|
|
|
use function now;
|
|
|
|
use function route;
|
|
|
|
use function view;
|
2022-08-30 09:20:45 +00:00
|
|
|
|
|
|
|
class BalanceController extends Controller
|
|
|
|
{
|
|
|
|
//
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2022-09-01 13:15:13 +00:00
|
|
|
$balances = Balance::thisUser()->simplePaginate(30);
|
|
|
|
return $this->success($balances);
|
2022-08-30 09:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
// 充值
|
2022-09-08 16:12:02 +00:00
|
|
|
$this->validate($request, [
|
2022-09-01 10:28:23 +00:00
|
|
|
'amount' => 'required|integer|min:1|max:10000',
|
2022-08-30 09:20:45 +00:00
|
|
|
]);
|
|
|
|
|
2022-09-01 09:48:29 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$balance = new Balance();
|
|
|
|
|
|
|
|
|
2022-09-01 13:15:13 +00:00
|
|
|
$data = [
|
2022-09-01 09:48:29 +00:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'amount' => $request->amount,
|
|
|
|
'payment' => 'alipay',
|
2022-09-01 13:15:13 +00:00
|
|
|
];
|
|
|
|
|
2022-10-31 11:26:30 +00:00
|
|
|
// if local
|
2022-11-06 11:28:22 +00:00
|
|
|
// if (env('APP_ENV') == 'local') {
|
|
|
|
// $data['payment'] = null;
|
|
|
|
// $data['paid_at'] = now();
|
|
|
|
// }
|
2022-09-01 13:15:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
$balance = $balance->create($data);
|
|
|
|
|
2022-09-01 09:48:29 +00:00
|
|
|
// 生成 18 位订单号
|
|
|
|
$order_id = date('YmdHis') . $balance->id . rand(1000, 9999);
|
|
|
|
$balance->order_id = $order_id;
|
|
|
|
|
|
|
|
$balance->save();
|
|
|
|
|
|
|
|
$balance = $balance->toArray();
|
|
|
|
$balance['pay_url'] = route('balances.pay.show', ['balance' => $balance['order_id']]);
|
|
|
|
|
|
|
|
return $this->success($balance);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-06 14:57:01 +00:00
|
|
|
public function show(Balance $balance)
|
2022-09-01 09:48:29 +00:00
|
|
|
{
|
|
|
|
if ($balance->paid_at !== null) {
|
|
|
|
return $this->error('订单已支付');
|
|
|
|
}
|
2022-08-30 09:20:45 +00:00
|
|
|
|
2022-09-18 06:28:29 +00:00
|
|
|
if (now()->diffInDays($balance->created_at) > 1) {
|
|
|
|
return $this->error('订单已失效');
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:57:01 +00:00
|
|
|
$pay = Pay::alipay()->web([
|
|
|
|
'out_trade_no' => $balance->order_id,
|
2022-11-07 04:03:46 +00:00
|
|
|
'total_amount' => $balance->amount,
|
2022-11-06 14:57:01 +00:00
|
|
|
'subject' => config('app.display_name') . ' 充值',
|
2022-09-09 15:17:59 +00:00
|
|
|
]);
|
|
|
|
|
2022-11-06 14:57:01 +00:00
|
|
|
return $pay;
|
2022-09-06 06:51:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 09:48:29 +00:00
|
|
|
public function notify(Request $request)
|
|
|
|
{
|
2022-09-09 15:17:59 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'out_trade_no' => 'required',
|
|
|
|
]);
|
|
|
|
|
2022-09-01 09:48:29 +00:00
|
|
|
// 检测订单是否存在
|
|
|
|
$balance = Balance::where('order_id', $request->out_trade_no)->with('user')->first();
|
|
|
|
if (!$balance) {
|
2022-11-06 14:57:01 +00:00
|
|
|
return $this->notFound('找不到对应的订单');
|
2022-09-01 09:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 检测订单是否已支付
|
|
|
|
if ($balance->paid_at !== null) {
|
2022-11-06 14:57:01 +00:00
|
|
|
// return $this->success('订单已支付');
|
2022-11-06 15:05:40 +00:00
|
|
|
return view('pay_process');
|
2022-11-06 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = Pay::alipay()->callback();
|
|
|
|
|
|
|
|
// 检测 out_trade_no 是否为商户系统中创建的订单号
|
|
|
|
if ($data->out_trade_no != $balance->order_id) {
|
|
|
|
return $this->error('订单号不一致');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int) $data->total_amount != (int) $balance->amount) {
|
|
|
|
throw new ChargeException('金额不一致');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 验证 商户
|
|
|
|
if ($data['app_id'] != config('pay.alipay.default.app_id')) {
|
|
|
|
throw new ChargeException('商户不匹配');
|
2022-09-01 09:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 15:05:40 +00:00
|
|
|
$this->checkAndCharge($balance);
|
|
|
|
|
|
|
|
return view('pay_process');
|
2022-09-06 06:51:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 13:31:35 +00:00
|
|
|
public function checkAndCharge(Balance $balance, $check = false)
|
2022-09-06 06:51:48 +00:00
|
|
|
{
|
2022-10-30 07:07:26 +00:00
|
|
|
|
2022-11-06 13:31:35 +00:00
|
|
|
if ($check) {
|
|
|
|
$alipay = Pay::alipay()->find(['out_trade_no' => $balance->order_id,]);
|
2022-09-01 09:48:29 +00:00
|
|
|
|
2022-11-06 13:31:35 +00:00
|
|
|
if ($alipay->trade_status !== 'TRADE_SUCCESS') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-09-01 09:48:29 +00:00
|
|
|
|
2022-11-07 04:03:46 +00:00
|
|
|
if ($balance->paid_at !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-06 13:31:35 +00:00
|
|
|
try {
|
2022-11-06 14:57:01 +00:00
|
|
|
(new Transaction)->addAmount($balance->user_id, 'alipay', $balance->amount);
|
2022-11-06 13:31:35 +00:00
|
|
|
|
|
|
|
$balance->update([
|
|
|
|
'paid_at' => now()
|
|
|
|
]);
|
|
|
|
} catch (InvalidResponseException) {
|
|
|
|
return $this->error('无法验证支付结果');
|
|
|
|
}
|
|
|
|
|
2022-11-06 15:05:40 +00:00
|
|
|
return true;
|
2022-09-01 09:48:29 +00:00
|
|
|
}
|
2022-08-30 09:20:45 +00:00
|
|
|
|
2022-11-06 14:57:01 +00:00
|
|
|
/**
|
|
|
|
* 获取交易记录
|
|
|
|
*
|
|
|
|
* @param mixed $request
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-10-08 04:24:04 +00:00
|
|
|
public function transactions(Request $request)
|
2022-09-18 06:28:29 +00:00
|
|
|
{
|
2022-11-06 14:57:01 +00:00
|
|
|
$transactions = Transaction::where('user_id', auth()->id());
|
2022-10-08 04:24:04 +00:00
|
|
|
|
|
|
|
if ($request->has('type')) {
|
|
|
|
$transactions = $transactions->where('type', $request->type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('payment')) {
|
|
|
|
$transactions = $transactions->where('payment', $request->payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$transactions = $transactions->latest()->simplePaginate(30);
|
2022-09-15 04:13:37 +00:00
|
|
|
|
|
|
|
return $this->success($transactions);
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:57:01 +00:00
|
|
|
/**
|
|
|
|
* 获取 Drops
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-09-09 13:42:12 +00:00
|
|
|
public function drops()
|
|
|
|
{
|
|
|
|
$user_id = auth()->id();
|
|
|
|
|
|
|
|
$resp = [
|
2022-11-06 14:57:01 +00:00
|
|
|
'drops' => (new Transaction())->getDrops($user_id),
|
2022-09-09 13:53:30 +00:00
|
|
|
'rate' => config('drops.rate'),
|
2022-09-09 13:42:12 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->success($resp);
|
|
|
|
}
|
2022-08-30 09:20:45 +00:00
|
|
|
}
|