Skip to content

Commit

Permalink
改用laravel自带uuid + 新支付:易支付(待测试)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZBrettonYe committed Jul 20, 2020
1 parent 1532619 commit 01ed70d
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 54 deletions.
3 changes: 2 additions & 1 deletion app/Components/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\UserCreditLog;
use App\Models\UserSubscribe;
use App\Models\UserTrafficModifyLog;
use Str;

class Helpers {
// 不生成的端口
Expand Down Expand Up @@ -82,7 +83,7 @@ public static function addUser($email, $password, $transfer_enable, $data, $refe
// 生成一个可用端口
$user->port = self::systemConfig()['is_rand_port']? self::getRandPort() : self::getOnlyPort();
$user->passwd = makeRandStr();
$user->vmess_id = createGuid();
$user->vmess_id = Str::uuid();
$user->enable = 1;
$user->method = self::getDefaultMethod();
$user->protocol = self::getDefaultProtocol();
Expand Down
10 changes: 8 additions & 2 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Redirect;
use Response;
use Session;
use Str;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Validator;

Expand Down Expand Up @@ -246,7 +247,7 @@ public function addUser(Request $request) {
$user->password = Hash::make($request->input('password')?: makeRandStr());
$user->port = $request->input('port')?: $this->makePort();
$user->passwd = $request->input('passwd')?: makeRandStr();
$user->vmess_id = $request->input('uuid')?: createGuid();
$user->vmess_id = $request->input('uuid')?: Str::uuid();
$user->transfer_enable = toGB($request->input('transfer_enable')?: 0);
$user->enable = $request->input('enable')?: 0;
$user->method = $request->input('method');
Expand Down Expand Up @@ -367,7 +368,7 @@ public function editUser(Request $request, $id) {
'email' => $email,
'port' => $port,
'passwd' => $request->input('passwd')?: makeRandStr(),
'vmess_id' => $request->input('uuid')?: createGuid(),
'vmess_id' => $request->input('uuid')?: Str::uuid(),
'transfer_enable' => toGB($transfer_enable?: 0),
'enable' => $status < 0? 0 : $request->input('enable'),
'method' => $request->input('method'),
Expand Down Expand Up @@ -1152,6 +1153,11 @@ public function setConfig(Request $request): JsonResponse {
return Response::json(['status' => 'fail', 'message' => '请先设置【码支付】必要参数']);
}
break;
case 'epay':
if(!self::$systemConfig['epay_url'] || !self::$systemConfig['epay_mch_id'] || !self::$systemConfig['epay_key']){
return Response::json(['status' => 'fail', 'message' => '请先设置【易支付】必要参数']);
}
break;
case 'payjs':
if(!self::$systemConfig['payjs_mch_id'] || !self::$systemConfig['payjs_key']){
return Response::json(['status' => 'fail', 'message' => '请先设置【PayJs】必要参数']);
Expand Down
13 changes: 2 additions & 11 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Routing\Controller as BaseController;
use RuntimeException;
use Str;

class Controller extends BaseController {
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

// 生成随机密码
public function makePasswd() {
return makeRandStr();
}

// 生成UUID
public function makeUUID() {
return createGuid();
}

// 生成网站安全码
public function makeSecurityCode() {
public function makeSecurityCode(): string {
return strtolower(makeRandStr(8));
}

Expand Down
25 changes: 5 additions & 20 deletions app/Http/Controllers/Gateway/AbstractPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Log;
use Str;

abstract class AbstractPayment {
protected static $systemConfig;
Expand All @@ -26,7 +27,7 @@ abstract public function notify(Request $request): void;

protected function postPayment($data, $method): int {
// 获取需要的信息
$payment = Payment::whereTradeNo($data)->first();
$payment = Payment::whereTradeNo($data)->latest()->first();
// 是否为余额购买套餐
if($payment){
Payment::whereTradeNo($data)->update(['status' => 1]);
Expand Down Expand Up @@ -120,9 +121,7 @@ protected function postPayment($data, $method): int {
User::query()->whereId($order->user->referral_uid)->increment('invite_num', 1);
}
//按照返利模式进行返利判断
if(self::$systemConfig['referral_type'] == 2
|| (self::$systemConfig['referral_type'] == 1
&& !$referral)){
if(self::$systemConfig['referral_type'] == 2 || (self::$systemConfig['referral_type'] == 1 && !$referral)){
$this->addReferralLog($order->user_id, $order->user->referral_uid, $order->oid, $order->amount,
$order->amount * self::$systemConfig['referral_percent']);
}
Expand Down Expand Up @@ -155,14 +154,13 @@ private function addReferralLog($userId, $refUserId, $oid, $amount, $refAmount):
$log->amount = $amount;
$log->ref_amount = $refAmount;
$log->status = 0;
$log->save();

return 0;
return $log->save();
}

protected function creatNewPayment($uid, $oid, $amount): Payment {
$payment = new Payment();
$payment->trade_no = self::generateGuid();
$payment->trade_no = substr(str_replace('-', '', Str::uuid()), 0, 8);
$payment->user_id = $uid;
$payment->oid = $oid;
$payment->amount = $amount;
Expand All @@ -171,19 +169,6 @@ protected function creatNewPayment($uid, $oid, $amount): Payment {
return $payment;
}

public static function generateGuid(): string {
mt_srand((double) microtime() * 10000);
$charId = strtoupper(md5(uniqid(mt_rand() + time(), true)));
$hyphen = chr(45);
$uuid = chr(123).substr($charId, 0, 8).$hyphen.substr($charId, 8, 4).$hyphen.substr($charId, 12,
4).$hyphen.substr($charId, 16, 4).$hyphen.substr($charId, 20, 12).chr(125);

$uuid = str_replace(['}', '{', '-'], '', $uuid);
$uuid = substr($uuid, 0, 8);

return $uuid;
}

/**
* @param string $trade_no 本地订单号
* @param string $out_trade_no 外部订单号
Expand Down
76 changes: 76 additions & 0 deletions app/Http/Controllers/Gateway/EPay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Http\Controllers\Gateway;

use App\Components\Curl;
use App\Models\Payment;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Response;

class EPay extends AbstractPayment {
public function purchase(Request $request): JsonResponse {
$payment = $this->creatNewPayment(Auth::id(), $request->input('oid'), $request->input('amount'));

switch($request->input('type')){
case 2:
$type = 'qqpay';
break;
case 3:
$type = 'wxpay';
break;
case 1:
default:
$type = 'alipay';
break;
}


$data = [
'pid' => self::$systemConfig['epay_mch_id'],
'type' => $type,
'out_trade_no' => $payment->trade_no,
'notify_url' => (self::$systemConfig['website_callback_url']?: self::$systemConfig['website_url']).'/callback/notify?method=epay',
'return_url' => self::$systemConfig['website_url'].'/invoices',
'name' => self::$systemConfig['subject_name']?: self::$systemConfig['website_name'],
'money' => $payment->amount,

];
$data['sign'] = $this->sign($this->prepareSign($data));

$result = json_decode(Curl::send(self::$systemConfig['epay_url'].'/submit.php', $data), true);
if(!$result){
return Response::json(['status' => 'fail', 'message' => '支付处理失败!']);
}

Payment::whereId($payment->id)->update(['qr_code' => 1, 'url' => $result['pay_url']]);

return Response::json(['status' => 'success', 'data' => $payment->trade_no, 'message' => '创建订单成功!']);
}

// 签名字符串
private function sign($data): string {
return strtolower(md5($data.self::$systemConfig['epay_key']));
}

private function prepareSign($data): string {
ksort($data);
return http_build_query($data);
}

public function notify(Request $request): void {

if(!$this->verify($request->all(), $request->input('sign'))){
die('FAIL');
}
$this->postPayment($request->input('out_trade_no'), 'EPay');
die('SUCCESS');
}

// 验证签名
private function verify($data, $signature): bool {
unset($data['sign']);
return $this->sign($this->prepareSign($data)) === $signature;
}
}
5 changes: 4 additions & 1 deletion app/Http/Controllers/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Components\Helpers;
use App\Http\Controllers\Gateway\BitpayX;
use App\Http\Controllers\Gateway\CodePay;
use App\Http\Controllers\Gateway\EPay;
use App\Http\Controllers\Gateway\F2Fpay;
use App\Http\Controllers\Gateway\Local;
use App\Http\Controllers\Gateway\PayJs;
Expand Down Expand Up @@ -53,6 +54,8 @@ public static function getClient() {
return new BitpayX();
case 'paypal':
return new PayPal();
case 'epay':
return new EPay();
default:
Log::error("未知支付:".self::$method);

Expand Down Expand Up @@ -189,7 +192,7 @@ public function purchase(Request $request) {
Helpers::addCouponLog($coupon->id, $goods_id, $order->oid, '订单支付使用');
}

$request->merge(['oid' => $order->oid, 'amount' => $amount, 'type' => $request->input('pay_type')]);
$request->merge(['oid' => $order->oid, 'amount' => $amount, 'type' => $pay_type]);

// 生成支付单
return self::getClient()->purchase($request);
Expand Down
13 changes: 0 additions & 13 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,6 @@ function getIPv6($ip) {
}
}

// 随机UUID
if(!function_exists('createGuid')){
function createGuid() {
mt_srand((double) microtime() * 10000);
$charid = strtoupper(md5(uniqid(mt_rand(), true)));
$hyphen = chr(45);
$uuid = substr($charid, 0, 8).$hyphen.substr($charid, 8, 4).$hyphen.substr($charid, 12,
4).$hyphen.substr($charid, 16, 4).$hyphen.substr($charid, 20, 12);

return strtolower($uuid);
}
}

// 过滤emoji表情
if(!function_exists('filterEmoji')){
function filterEmoji($str) {
Expand Down
52 changes: 52 additions & 0 deletions resources/views/admin/config/system.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ class="col-md-3 col-form-label">流量警告阈值</label>
<option value="">关闭</option>
<option value="f2fpay">F2F</option>
<option value="codepay">码支付</option>
<option value="epay">易支付</option>
</select>
</div>
</div>
Expand All @@ -1121,6 +1122,7 @@ class="col-md-3 col-form-label">流量警告阈值</label>
<select class="col-md-3" id="is_QQPay" data-plugin="selectpicker" data-style="btn-outline btn-primary" onchange="updateFromOther('select','is_QQPay')">
<option value="">关闭</option>
<option value="codepay">码支付</option>
<option value="epay">易支付</option>
</select>
</div>
</div>
Expand All @@ -1131,6 +1133,7 @@ class="col-md-3 col-form-label">流量警告阈值</label>
<option value="">关闭</option>
<option value="codepay">码支付</option>
<option value="payjs">PayJS</option>
<option value="epay">易支付</option>
</select>
</div>
</div>
Expand Down Expand Up @@ -1274,6 +1277,55 @@ class="col-md-3 col-form-label">流量警告阈值</label>
</div>
</div>
</div>
<div class="row pb-70">
<div class="form-group col-lg-6">
<div class="row">
<label class="col-md-3 col-form-label">易支付</label>
<div class="col-md-7">
{{-- 请到 <a href="https://codepay.fateqq.com/i/377289" target="_blank">码支付</a>申请账号,然后下载登录其挂机软件--}}
</div>
</div>
</div>
<div class="form-group col-lg-6">
<div class="row">
<label class="col-md-3 col-form-label" for="epay_url">请求URL</label>
<div class="col-md-7">
<div class="input-group">
<input type="text" class="form-control" id="epay_url" value="{{$epay_url}}" placeholder="https://www.example.com"/>
<span class="input-group-append">
<button class="btn btn-primary" type="button" onclick="update('epay_url')">修改</button>
</span>
</div>
</div>
</div>
</div>
<div class="form-group col-lg-6">
<div class="row">
<label class="col-md-3 col-form-label" for="epay_mch_id">商户ID</label>
<div class="col-md-7">
<div class="input-group">
<input type="text" class="form-control" id="epay_mch_id" value="{{$epay_mch_id}}"/>
<span class="input-group-append">
<button class="btn btn-primary" type="button" onclick="update('epay_mch_id')">修改</button>
</span>
</div>
</div>
</div>
</div>
<div class="form-group col-lg-6">
<div class="row">
<label class="col-md-3 col-form-label" for="epay_key">商户签名</label>
<div class="col-md-7">
<div class="input-group">
<input type="text" class="form-control" id="epay_key" value="{{$epay_key}}"/>
<span class="input-group-append">
<button class="btn btn-primary" type="button" onclick="update('epay_key')">修改</button>
</span>
</div>
</div>
</div>
</div>
</div>
<div class="row pb-70">
<div class="form-group col-lg-6">
<div class="row">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/admin/node/nodeInfo.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class="col-md-5 form-control" name="country_code" id="country_code">
$('#passwd').val('{{$node->passwd}}');
$("input[name='type'][value='{{$node->type}}']").click();
@if($node->type==1)
@if($node->type == 1 || $node->type == 4)
// ShadowsocksR
$('#method').selectpicker('val', '{{$node->method}}');
$('#protocol').selectpicker('val', '{{$node->protocol}}');
Expand All @@ -444,7 +444,7 @@ class="col-md-5 form-control" name="country_code" id="country_code">
@endif
@endif
@if($node->type==2)
@if($node->type == 2)
//V2Ray
$('#v2_alter_id').val('{{$node->v2_alter_id}}');
$('#v2_port').val('{{$node->v2_port}}');
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/node/nodeList.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@if($node->is_relay)
中转
@else
{{$node->type == 2 ? 'V2' : 'SSR'}}
{{$node->type_label}}
@endif
</td>
<td> {{$node->name}} </td>
Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
Route::get('active/{token}', 'AuthController@active'); // 激活账号
Route::post('sendCode', 'AuthController@sendCode'); // 发送注册验证码
Route::get('free', 'AuthController@free'); // 免费邀请码
Route::get('makePasswd', 'Controller@makePasswd'); // 生成密码
Route::get('makeUUID', 'Controller@makeUUID'); // 生成UUID
Route::get('makePasswd', makeRandStr()); // 生成随机密码
Route::get('makeUUID', Str::uuid()); // 生成UUID
Route::get('makeSecurityCode', 'Controller@makeSecurityCode'); // 生成网站安全码
});
Route::any('admin/login', 'AuthController@login')->middleware('isForbidden', 'isSecurity'); // 登录
Expand Down
5 changes: 4 additions & 1 deletion sql/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,10 @@ VALUES ('1', 'is_rand_port', 0),
('109', 'paypal_secret', ''),
('110', 'paypal_certificate', ''),
('111', 'paypal_app_id', ''),
('112', 'redirect_url', '');
('112', 'redirect_url', ''),
('113', 'epay_url', ''),
('114', 'epay_mch_id', ''),
('115', 'epay_key', '');

-- ----------------------------
-- Table structure for article
Expand Down
Loading

0 comments on commit 01ed70d

Please sign in to comment.