Lae/app/Http/Controllers/Api/BalanceController.php

102 lines
2.5 KiB
PHP
Raw Normal View History

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
use App\Exceptions\ChargeException;
2022-11-08 13:01:43 +00:00
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-11-14 10:25:07 +00:00
use Illuminate\Http\JsonResponse;
2022-08-30 09:20:45 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
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;
2022-08-30 09:20:45 +00:00
class BalanceController extends Controller
{
//
public function index(): JsonResponse
2022-08-30 09:20:45 +00:00
{
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 checkAndCharge(Balance $balance, $check = false): JsonResponse|bool
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 $e) {
Log::error($e->getMessage());
2022-11-14 09:47:52 +00:00
return $this->error('无法验证支付结果。');
} catch (ChargeException $e) {
Log::error($e->getMessage());
return $this->error('暂时无法处理充值。');
2022-11-06 13:31:35 +00:00
}
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
*
2022-11-14 10:25:07 +00:00
* @return JsonResponse
2022-11-06 14:57:01 +00:00
*/
2022-11-14 10:25:07 +00:00
public function transactions(Request $request): JsonResponse
2022-09-18 06:28:29 +00:00
{
2022-11-06 14:57:01 +00:00
$transactions = Transaction::where('user_id', auth()->id());
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
*
2022-11-14 10:25:07 +00:00
* @return JsonResponse
2022-11-06 14:57:01 +00:00
*/
2022-11-14 10:25:07 +00:00
public function drops(): JsonResponse
2022-09-09 13:42:12 +00:00
{
$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
}