diff --git a/app/AdminSettled/Common/SettledPay.php b/app/AdminSettled/Common/SettledPay.php new file mode 100644 index 0000000..0c979da --- /dev/null +++ b/app/AdminSettled/Common/SettledPay.php @@ -0,0 +1,143 @@ +payConfig($model, $cost, ['supplier' => 1, 'agent' => 2, 'guide' => 3][$type]); + + if (empty($pay['code_url'])) { + if (isset($pay['result_code'], $pay['err_code_des']) && $pay['result_code'] != 'SUCCESS') { + $msg = $pay['err_code_des']; + } else { + $msg = $pay['return_msg'] ?? '获取支付信息失败'; + } + $js = "Dcat.swal.info('支付:$msg', null);"; + } else { + $ajax_url = admin_url('is_pay'); + $js = <<

入驻费:¥{$cost}元

', + '请微信扫码支付,请勿关闭页面', + { + type: null, + imageWidth: 240, + imageHeight: 240, + animation: false, + // confirmButtonText: '我已完成支付,刷新', + showConfirmButton: false, + allowOutsideClick: false, + allowEscapeKey: false, + onOpen: function () { + $('#qrcode').qrcode({text:'{$pay["code_url"]}', width:240, height:240}); + if (window.timer) { + clearInterval(window.timer); + } + window.timer = setInterval(function () { + $.ajax({ + url: '$ajax_url', + data: { + username: '{$model->username}', + type: '{$type}', + }, + success: function (res) { + if (res == 1) { + clearInterval(window.timer); + Dcat.swal.success('支付成功,请联系平台审核!', null, { + onClose: function () { + window.location.reload(); + } + }).then(() => { + window.location.reload(); + }); + } + } + }); + }, 1000); + } + }); + JS; + } + return $js; + } + + /** + * 获取支付配置 + * @param $model + * @param $cost + * @param $user_type + * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string + * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException + * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException + * @throws \GuzzleHttp\Exception\GuzzleException + */ + private function PayConfig($model, $cost, $user_type) + { + $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']); + $config = [ + 'app_id' => $setting['payee_appid'], + 'mch_id' => $setting['payee_mchid'], + 'key' => $setting['payee_mchkey'], + 'notify_url' => route('wxpay_settled_notify'), + ]; + $app = Factory::payment($config); + + //生成订单号 + list($micro, $sec) = explode(' ', microtime()); + $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT); + $order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999); + + //保存订单记录 + SettledOrder::insertOrIgnore([ + 'order_no' => $order_no, + 'user_type' => $user_type, + 'username' => $model->username, + 'money' => $cost, + 'status' => 0, + 'created_at' => now(), + 'updated_at' => now(), + ]); + + return $app->order->unify([ + 'product_id' => $model->id, + 'attach' => $model->username, + 'body' => mb_strcut($model->company_name . ' 入驻易游平台', 0, 127), + 'out_trade_no' => $order_no, + 'total_fee' => round($cost * 100), //支付金额单位为分 + 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型 + ]); + } +} diff --git a/app/AdminSettled/Controllers/AgentController.php b/app/AdminSettled/Controllers/AgentController.php index ec8a076..f14d50d 100644 --- a/app/AdminSettled/Controllers/AgentController.php +++ b/app/AdminSettled/Controllers/AgentController.php @@ -3,6 +3,7 @@ namespace App\AdminSettled\Controllers; use App\Admin\Repositories\Agent; +use App\AdminSettled\Common\SettledPay; use App\Common\AgentType; use App\Common\UserStatus; use App\Models\AdminSetting; @@ -137,7 +138,8 @@ class AgentController extends AdminController $form->rate = 5; $form->type = in_array($form->type, AgentType::key()) ? $form->type : AgentType::OPERATOR; })->saved(function (Form $form) { - return $form->response()->success('操作成功,请等待管理员审核')->refresh(); + $js = (new SettledPay)->PayJs($form->repository()->model()); + return $form->response()->success('操作成功,继续支付入驻费完成入驻')->script($js); })->deleting(function (Form $form) { return $form->response()->error('服务器出错了~~'); }); diff --git a/app/AdminSettled/Controllers/GuideController.php b/app/AdminSettled/Controllers/GuideController.php index 4cff3a0..886d8d7 100644 --- a/app/AdminSettled/Controllers/GuideController.php +++ b/app/AdminSettled/Controllers/GuideController.php @@ -3,6 +3,7 @@ namespace App\AdminSettled\Controllers; use App\Admin\Repositories\Guide; +use App\AdminSettled\Common\SettledPay; use App\Common\UserStatus; use App\Models\AdminSetting; use Dcat\Admin\Admin; @@ -113,7 +114,8 @@ class GuideController extends AdminController $form->status = UserStatus::UNAUDITED; $form->rate = 5; })->saved(function (Form $form) { - return $form->response()->success('操作成功,请等待管理员审核')->refresh(); + $js = (new SettledPay)->PayJs($form->repository()->model()); + return $form->response()->success('操作成功,继续支付入驻费完成入驻')->script($js); })->deleting(function (Form $form) { return $form->response()->error('服务器出错了~~'); }); diff --git a/app/AdminSettled/Controllers/IsPayController.php b/app/AdminSettled/Controllers/IsPayController.php new file mode 100644 index 0000000..aaceac8 --- /dev/null +++ b/app/AdminSettled/Controllers/IsPayController.php @@ -0,0 +1,28 @@ +input('username'); + $type = request()->input('type'); + + $user_type = ['supplier' => 1, 'agent' => 2, 'guide' => 3][$type] ?? die('错误的type'); + + $order = SettledOrder::where(['username' => $username, 'user_type' => $user_type])->first(); + if (!$order) { + return 0; + } + return (int)$order->status; + } +} diff --git a/app/AdminSettled/Controllers/SupplierController.php b/app/AdminSettled/Controllers/SupplierController.php index 7d4d573..fb1b7d8 100644 --- a/app/AdminSettled/Controllers/SupplierController.php +++ b/app/AdminSettled/Controllers/SupplierController.php @@ -3,6 +3,7 @@ namespace App\AdminSettled\Controllers; use App\Admin\Repositories\Supplier; +use App\AdminSettled\Common\SettledPay; use App\Common\UserStatus; use App\Models\AdminSetting; use App\Models\SettledOrder; @@ -116,112 +117,10 @@ class SupplierController extends AdminController $form->status = UserStatus::UNAUDITED; $form->rate = 5; })->saved(function (Form $form) { - $pay = (new SupplierController)->payConfig($form->repository()->model()); - - if (empty($pay['code_url'])) { - if (isset($pay['result_code'], $pay['err_code_des']) && $pay['result_code'] != 'SUCCESS') { - $msg = $pay['err_code_des']; - } else { - $msg = $pay['return_msg'] ?? '获取支付信息失败'; - } - $js = "Dcat.swal.info('支付:$msg', null);"; - } else { - $ajax_url = admin_url('is_pay'); - $cost = AdminSetting::val('settled_supplier_cost'); - $js = <<

入驻费:¥{$cost}元

', - '请微信扫码支付,请勿关闭页面', - { - type: null, - imageWidth: 240, - imageHeight: 240, - animation: false, - // confirmButtonText: '我已完成支付,刷新', - showConfirmButton: false, - allowOutsideClick: false, - allowEscapeKey: false, - onOpen: function () { - $('#qrcode').qrcode({text:'{$pay["code_url"]}', width:240, height:240}); - if (window.timer) { - clearInterval(window.timer); - } - window.timer = setInterval(function () { - $.ajax({ - url: '$ajax_url', - data: { - username: '{$form->repository()->model()->username}' - }, - success: function (res) { - if (res == 1) { - clearInterval(window.timer); - Dcat.swal.success('支付成功,请联系平台审核!', null, { - onClose: function () { - window.location.reload(); - } - }).then(() => { - window.location.reload(); - }); - } - } - }); - }, 1000); - } - }); - JS; - } - return $form->response()->success('操作成功,请等待管理员审核')->script($js); + $js = (new SettledPay)->PayJs($form->repository()->model()); + return $form->response()->success('操作成功,继续支付入驻费完成入驻')->script($js); })->deleting(function (Form $form) { return $form->response()->error('服务器出错了~~'); }); } - - //付款 - private function payConfig($supplier) - { - $setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey', 'settled_supplier_cost']); - $config = [ - 'app_id' => $setting['payee_appid'], - 'mch_id' => $setting['payee_mchid'], - 'key' => $setting['payee_mchkey'], - 'notify_url' => route('wxpay_settled_notify'), - ]; - $app = Factory::payment($config); - - //生成订单号 - list($micro, $sec) = explode(' ', microtime()); - $micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT); - $order_no = date('ymdHis', $sec) . $micro . mt_rand(1000, 9999); - - //保存订单记录 - SettledOrder::insertOrIgnore([ - 'order_no' => $order_no, - 'user_type' => 1, - 'username' => $supplier->username, - 'money' => $setting['settled_supplier_cost'], - 'status' => 0, - 'created_at' => now(), - 'updated_at' => now(), - ]); - - return $app->order->unify([ - 'product_id' => $supplier->id, - 'attach' => $supplier->username, - 'body' => mb_strcut($supplier->company_name . ' 供应商入驻', 0, 127), - 'out_trade_no' => $order_no, - 'total_fee' => round($setting['settled_supplier_cost'] * 100), //支付金额单位为分 - 'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型 - ]); - } - - //ajax回调,判断是否已支付 - public function isPay(): int - { - $username = request()->input('username'); - $order = SettledOrder::where(['username' => $username, 'user_type' => 1])->first(); - if (!$order) { - return 0; - } - return (int)$order->status; - } } diff --git a/app/AdminSettled/routes.php b/app/AdminSettled/routes.php index 36d028a..1812982 100644 --- a/app/AdminSettled/routes.php +++ b/app/AdminSettled/routes.php @@ -18,5 +18,5 @@ Route::group([ $router->resource('agent', 'AgentController'); $router->resource('guide', 'GuideController'); - $router->get('is_pay', 'SupplierController@isPay'); //是否支付回调 + $router->get('is_pay', 'IsPayController@index'); //是否支付回调 }); diff --git a/app/Models/Guide.php b/app/Models/Guide.php index 23c8e63..151e2bf 100644 --- a/app/Models/Guide.php +++ b/app/Models/Guide.php @@ -9,6 +9,8 @@ class Guide extends BaseModel { use HasFactory, SoftDeletes; + protected $casts = ['license' => 'json']; + //密码修改 public function setPasswordAttribute($value) {