diff --git a/app/Http/Controllers/Api/SharePayController.php b/app/Http/Controllers/Api/SharePayController.php new file mode 100644 index 0000000..1d0aef9 --- /dev/null +++ b/app/Http/Controllers/Api/SharePayController.php @@ -0,0 +1,61 @@ +input('id'); + if (!$order_id) { + return $this->error('无效的ID'); + } + //用户openid + $openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG + + $order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id); + + $config = config('wechat.payment.default'); + $config = array_merge($config, [ + 'app_id' => 'wxb35ef055a4dd8ad4', + 'mch_id' => 'c782df1e6d3a03bb4a12e9d41c89cf9c', + 'key' => 'lfyyhyz8888888888888888888888888', + ]); + + $app = Factory::payment($config); + try { + $result = $app->order->unify([ + 'body' => $order->title, + 'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付 + 'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分 + 'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址 + 'trade_type' => 'JSAPI', + 'openid' => $openid, +// 'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写 + ]); + } catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) { + return ['error' => $e->getMessage(), 'line' => $e->getLine()]; + } + + if (empty($result['prepay_id'])) { + return $result; + } + + $jssdk = $app->jssdk; + $payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组 + return $this->success($payConfig); + } +} diff --git a/app/Http/Controllers/Api/TestController.php b/app/Http/Controllers/Api/TestController.php index 6cd7188..f52e1d7 100644 --- a/app/Http/Controllers/Api/TestController.php +++ b/app/Http/Controllers/Api/TestController.php @@ -31,4 +31,41 @@ class TestController } return ''; } -} + + public function area() + { + $handle = fopen(Env::get('root_path').'2020-11行政区划(民政部).txt', 'r'); + if ($handle) { + Db::execute('TRUNCATE `aqs_area`;'); + while (($line = fgets($handle, 4096)) !== false) { + $line = trim($line); + + $last2 = substr($line, 4, 2); //区划码后2位 + $front2 = substr($line, 0, 2); //区划码前2位 + $front4 = substr($line, 0, 4); //区划码前4位 + + //判断是否是直辖市,是直辖市则再插入一次上条记录作为二级联动 + if( isset($last4, $arr, $parent_arr[$front2]) && $last4=='0000' && $last2!='00' ) { + $insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_arr[$front2], 'name'=>$arr[1]]); + $parent_arr[$front4] = $insert_id; + //对直辖市不做省直辖县处理,如:重庆市 + $parent_arr[$front2] = $insert_id; + } + + $last4 = substr($line, 2, 4); //区划码后4位 + + $arr = preg_split('/\s+/', $line); + if( $last4 == '0000' ) { + $insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>0, 'name'=>$arr[1]]); + $parent_arr[$front2] = $insert_id; + } else if( $last2 == '00' ) { + $insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_arr[$front2], 'name'=>$arr[1]]); + $parent_arr[$front4] = $insert_id; + } else { + //考虑到省直辖县级市情况,如:海南琼海市、湖北仙桃市等,但重庆市的省辖县除外(已在上面判断直辖市逻辑中做处理) + $parent_id = $parent_arr[$front4] ?? $parent_arr[$front2]; + Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_id, 'name'=>$arr[1]]); + } + } + } + }} diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index ae32b86..74ff3be 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -26,6 +26,9 @@ class Controller extends BaseController } $this->agent_id = Cache::get($appid, 0); + if ($this->agent_id == 3) { + $this->agent_id = 1; + } } protected function success($data = [], $msg = 'success', $code = 0, $status = 200) diff --git a/routes/api.php b/routes/api.php index 80c8ec7..d2bd2b9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -28,6 +28,9 @@ Route::namespace('App\Http\Controllers\Api') Route::post('refund/{agent_id}', 'WxpayController@refund')->name('wxpay_refund'); //退款通知,aid为代理商ID }); +# 跳转支付 +Route::post('share_pay', \App\Http\Controllers\Api\SharePayController::class . '@pay'); + # 仅用于测试 Route::get('t/index', \App\Http\Controllers\Api\TestController::class . '@index');