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

294 lines
8.1 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
2022-09-01 09:48:29 +00:00
use Exception;
2022-11-06 11:28:22 +00:00
use function app;
use function env;
use function now;
use function auth;
use function view;
use function route;
use function config;
use App\Models\Balance;
use function storage_path;
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-10-07 05:15:08 +00:00
use App\Exceptions\ChargeException;
2022-09-09 13:42:12 +00:00
use Illuminate\Support\Facades\Log;
2022-09-01 09:48:29 +00:00
use App\Http\Controllers\Controller;
2022-11-06 11:28:22 +00:00
use Yansongda\LaravelPay\Facades\Pay;
2022-10-30 07:07:26 +00:00
use Illuminate\Support\Facades\Storage;
2022-11-06 13:31:35 +00:00
use Yansongda\Pay\Exception\InvalidResponseException;
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-11-06 11:28:22 +00:00
$pay = Pay::alipay()->web([
'out_trade_no' => 'lae-' . time(),
'total_amount' => $request->amount,
'subject' => '在莱云上充值 ' . $request->amount . ' 元',
]);
return $pay;
2022-11-06 13:31:35 +00:00
2022-11-06 11:28:22 +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-08 16:12:02 +00:00
// if (env('APP_ENV') == 'local') {
// $user->increment('balance', $request->amount);
// return $this->success($balance);
// }
2022-09-01 13:15:13 +00:00
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);
}
public function show(Request $request, Balance $balance)
{
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-08-30 09:20:45 +00:00
try {
2022-09-01 09:48:29 +00:00
2022-11-06 11:28:22 +00:00
return;
// if ($responseChecker->success($result)) {
// $html = $result->body;
// return view('pay', compact('html'));
// }
2022-09-01 09:48:29 +00:00
} catch (Exception $e) {
2022-09-09 13:17:31 +00:00
Log::error($e);
2022-09-01 09:48:29 +00:00
echo "调用失败," . $e->getMessage() . PHP_EOL;;
2022-08-30 09:20:45 +00:00
}
2022-09-01 09:48:29 +00:00
2022-08-30 09:20:45 +00:00
return $this->success($balance);
}
2022-09-06 06:51:48 +00:00
public function return(Request $request)
{
2022-09-09 15:17:59 +00:00
$this->validate($request, [
'out_trade_no' => 'required',
]);
2022-11-06 13:31:35 +00:00
$alipay = Pay::alipay();
$data = $alipay->callback();
Log::debug($data);
dd($data);
return;
2022-09-06 06:51:48 +00:00
// 检测订单是否存在
2022-11-06 13:31:35 +00:00
// $balance = Balance::where('order_id', $request->out_trade_no)->with('user')->first();
// if (!$balance) {
// return $this->notFound('balance not found');
// }
2022-09-06 06:51:48 +00:00
// 检测订单是否已支付
if ($balance->paid_at !== null) {
return $this->success('订单已支付');
}
2022-09-18 06:28:29 +00:00
if ($this->checkAndCharge($balance)) {
2022-09-06 07:12:30 +00:00
return view('pay_success');
2022-09-06 06:51:48 +00:00
} else {
2022-09-06 07:12:30 +00:00
return view('pay_error');
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) {
return $this->notFound('balance not found');
}
// 检测订单是否已支付
if ($balance->paid_at !== null) {
return $this->success('订单已支付');
}
2022-09-18 06:28:29 +00:00
if ($this->checkAndCharge($balance)) {
2022-09-06 06:51:48 +00:00
return $this->success();
} else {
return $this->error();
}
}
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-06 13:31:35 +00:00
try {
2022-09-01 09:48:29 +00:00
2022-11-06 13:31:35 +00:00
// 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
// 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号
// 2、判断total_amount是否确实为该订单的实际金额即商户订单创建时的金额
// 3、校验通知中的seller_id或者seller_email) 是否为out_trade_no这笔单据的对应的操作方有的时候一个商户可能有多个seller_id/seller_email
// 4、验证app_id是否为该商户本身。
// 5、其它业务逻辑情况
2022-09-01 09:48:29 +00:00
2022-11-06 13:31:35 +00:00
// 验证 商户
// if ($data['app_id'] != config('pay.alipay.app_id')) {
// throw new ChargeException('商户不匹配');
// }
// if ((int) $data->total_amount != (int) $balance->amount) {
// throw new ChargeException('金额不一致');
// }
$balance->update([
'paid_at' => now()
]);
(new Transaction)->addAmount($balance->user_id, 'alipay', $data->totalAmount);
} catch (InvalidResponseException) {
return $this->error('无法验证支付结果');
}
return $alipay->success();
2022-09-01 09:48:29 +00:00
}
2022-08-30 09:20:45 +00:00
// // 转换为 drops
// public function transfer($amount = 1)
// {
2022-11-06 11:28:22 +00:00
// $balance = auth()->user();
2022-08-30 09:20:45 +00:00
// $balance->decrement('amount', $request->amount);
// return $this->success($balance);
// }
public function transactions(Request $request)
2022-09-18 06:28:29 +00:00
{
$transactions = Transaction::thisUser();
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-09-09 13:42:12 +00:00
public function drops()
{
2022-10-03 04:11:06 +00:00
// $month = now()->month;
2022-09-09 13:42:12 +00:00
$user_id = auth()->id();
2022-10-03 04:11:06 +00:00
// $cache_key = 'user_' . $user_id . '_month_' . $month . '_drops';
2022-09-09 13:42:12 +00:00
2022-09-15 04:13:37 +00:00
$transactions = new Transaction();
2022-09-09 13:42:12 +00:00
$resp = [
2022-09-15 04:13:37 +00:00
'drops' => $transactions->getDrops($user_id),
2022-10-03 04:11:06 +00:00
// 'monthly_usages' => (double) Cache::get($cache_key, 0),
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-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// private function alipayOptions()
// {
// $options = new AlipayConfig();
// $options->protocol = 'https';
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// // if local
// if (app()->environment() == 'local') {
// $options->gatewayHost = 'openapi.alipaydev.com';
// } else {
// $options->gatewayHost = 'openapi.alipay.com';
// }
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// $options->signType = 'RSA2';
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// $options->appId = config('payment.alipay.app_id');
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
// $options->merchantPrivateKey = trim(Storage::get('alipayAppPriv.key'));
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// $options->alipayCertPath = storage_path('app/alipayCertPublicKey_RSA2.crt');
// $options->alipayRootCertPath = storage_path('app/alipayRootCert.crt');
// $options->merchantCertPath = storage_path('app/appCertPublicKey.crt');
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// $options->notifyUrl = route('balances.notify');
2022-10-30 07:07:26 +00:00
2022-11-06 11:28:22 +00:00
// return $options;
// }
2022-08-30 09:20:45 +00:00
}