| xqd
@@ -9,10 +9,13 @@
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
|
|
|
|
|
+use App\Models\PaymentInfoModel;
|
|
|
use App\Models\UserInfoModel;
|
|
|
use App\Models\WechatAppModel;
|
|
|
+use App\User;
|
|
|
use EasyWeChat\Factory;
|
|
|
use Illuminate\Http\Request;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Auth,Validator;
|
|
|
use App\Services\Base\ErrorCode;
|
| xqd
@@ -20,10 +23,12 @@ use App\Services\Base\ErrorCode;
|
|
|
class PayController extends Controller
|
|
|
{
|
|
|
private $config;
|
|
|
+ private $wechat;
|
|
|
public function __construct()
|
|
|
{
|
|
|
- //parent::__construct();
|
|
|
+ parent::__construct();
|
|
|
$wechat = WechatAppModel::find(1);
|
|
|
+ $this->wechat = $wechat;
|
|
|
$this->config = [
|
|
|
// 必要配置
|
|
|
'app_id' => $wechat->appId,
|
| xqd
@@ -38,6 +43,33 @@ class PayController extends Controller
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @api {post} /api/pay/recharge 充值(recharge)
|
|
|
+ * @apiDescription 充值(recharge)recharge
|
|
|
+ * @apiGroup Pay
|
|
|
+ * @apiPermission none
|
|
|
+ * @apiVersion 0.1.0
|
|
|
+ * @apiParam {decimal} price 内容
|
|
|
+ * @apiSuccessExample {json} Success-Response:
|
|
|
+ * HTTP/1.1 200 OK
|
|
|
+ * {
|
|
|
+ * "status": true,
|
|
|
+ * "status_code": 0,
|
|
|
+ * "message": "",
|
|
|
+ * "data": {
|
|
|
+ * "msg": "已成功发起提现,请等待后台审核"
|
|
|
+ * }
|
|
|
+ *}
|
|
|
+ * @apiErrorExample {json} Error-Response:
|
|
|
+ * HTTP/1.1 400 Bad Request
|
|
|
+ * {
|
|
|
+ * "state": false,
|
|
|
+ * "code": 1000,
|
|
|
+ * "message": "传入参数不正确",
|
|
|
+ * "data": null or []
|
|
|
+ * }
|
|
|
+ */
|
|
|
public function recharge(Request $request)
|
|
|
{
|
|
|
$validator = Validator::make($request->all(),
|
| xqd
@@ -54,7 +86,16 @@ class PayController extends Controller
|
|
|
$userAuth = Auth('api')->user();
|
|
|
$money = $request->input('price');
|
|
|
$trade_no = 'Pay'.date('YmdHis').rand(1000,9999);
|
|
|
-
|
|
|
+ $order['user_id'] = $userAuth->id;
|
|
|
+ $order['out_trade_no'] = $trade_no;
|
|
|
+ $order['price'] = $money;
|
|
|
+ $order['to_user'] = 0;
|
|
|
+ $order['state'] = 0;
|
|
|
+ $order['msg_id'] = 0;
|
|
|
+ $order['poundage'] = 0;
|
|
|
+ $order['openid'] = $userAuth->openid;
|
|
|
+ $order['type'] = 0;
|
|
|
+ PaymentInfoModel::create($order);
|
|
|
$app = Factory::payment($this->config);
|
|
|
|
|
|
$result = $app->order->unify([
|
| xqd
@@ -73,7 +114,7 @@ class PayController extends Controller
|
|
|
$jssdk = $payment->jssdk;
|
|
|
|
|
|
$json = $jssdk->bridgeConfig($result['prepay_id']);
|
|
|
- dd($json);
|
|
|
+ //dd($json);
|
|
|
return $this->api(compact('json'));
|
|
|
}else{
|
|
|
$msg = "签名失败,请稍后再试!";
|
| xqd
@@ -86,27 +127,109 @@ class PayController extends Controller
|
|
|
//下面是回调函数
|
|
|
public function notify()
|
|
|
{
|
|
|
- $app = Factory::payment($this->options());
|
|
|
+ $app = Factory::payment($this->config);
|
|
|
\Log::info("wechat notify start!");
|
|
|
- return $app->handlePaidNotify(function ($notify, $successful) {
|
|
|
+ $response = $app->handlePaidNotify(function ($notify, $successful) {
|
|
|
\Log::info($notify);
|
|
|
if ($notify['result_code'] == 'SUCCESS') {
|
|
|
$user = UserInfoModel::where('openid',$notify['openid'])->first();
|
|
|
$data['order_no'] = $notify['out_trade_no'];
|
|
|
$data['price'] = $notify['total_fee'] / 100;
|
|
|
- $data['user_id'] = $user->id;
|
|
|
- $data['code'] = $user->code;
|
|
|
- \Log::info($data);
|
|
|
- $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
|
|
|
- $this->createQueryInfo($user,1);
|
|
|
-
|
|
|
- OrderInfoModel::create($data);
|
|
|
- $user->save();
|
|
|
+ $wechat_pay = PaymentInfoModel::where([['user_id',$user->id],['out_trade_no',$notify['out_trade_no']],['state',0]])->first();
|
|
|
+ if($wechat_pay){
|
|
|
+ $user->money += $data['price'];
|
|
|
+ $user->save();
|
|
|
+ $wechat_pay->state = 1;
|
|
|
+ $wechat_pay->save();
|
|
|
+ }
|
|
|
} else {
|
|
|
return $successful('通信失败,请稍后再通知我');
|
|
|
}
|
|
|
return true;
|
|
|
});
|
|
|
+ return $response->send();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @api {post} /api/pay/cash 提现(cash)
|
|
|
+ * @apiDescription 提现(cash)cash
|
|
|
+ * @apiGroup Pay
|
|
|
+ * @apiPermission none
|
|
|
+ * @apiVersion 0.1.0
|
|
|
+ * @apiParam {decimal} price 内容
|
|
|
+ * @apiParam {string} name 真实姓名
|
|
|
+ * @apiSuccessExample {json} Success-Response:
|
|
|
+ * HTTP/1.1 200 OK
|
|
|
+ * {
|
|
|
+ * "status": true,
|
|
|
+ * "status_code": 0,
|
|
|
+ * "message": "",
|
|
|
+ * "data": {
|
|
|
+ * "msg": "已成功发起提现,请等待后台审核"
|
|
|
+ * }
|
|
|
+ *}
|
|
|
+ * @apiErrorExample {json} Error-Response:
|
|
|
+ * HTTP/1.1 400 Bad Request
|
|
|
+ * {
|
|
|
+ * "state": false,
|
|
|
+ * "code": 1000,
|
|
|
+ * "message": "传入参数不正确",
|
|
|
+ * "data": null or []
|
|
|
+ * }
|
|
|
+ */
|
|
|
+
|
|
|
+ public function cash(Request $request)
|
|
|
+ {
|
|
|
+ $validator = Validator::make($request->all(),
|
|
|
+ [
|
|
|
+ 'price' => 'required',
|
|
|
+ 'name' => 'required',
|
|
|
+ ],[
|
|
|
+ 'price.required' => 'price不能为空!',
|
|
|
+ 'name.required' => 'name不能为空!'
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ if ($validator->fails()) {
|
|
|
+ return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
|
|
|
+ }
|
|
|
+ $data = $request->input();
|
|
|
+ $userAuth = Auth('api')->user();
|
|
|
+ $trade_no = 'Cash'.date('YmdHis').rand(1000,9999);
|
|
|
+ $poundage = $this->wechat->poundage * $data['price'];
|
|
|
+ $total = $data['price'] + $poundage;
|
|
|
+ if($userAuth->money < $total){
|
|
|
+ $msg = '您的余额不足';
|
|
|
+ return $this->api($msg);
|
|
|
+ }
|
|
|
+ DB::beginTransaction();
|
|
|
+ $user = UserInfoModel::find($userAuth->id);
|
|
|
+ $user->money -= $total;
|
|
|
+ $res = $user->save();
|
|
|
+ if($res){
|
|
|
+ $order['user_id'] = 0;
|
|
|
+ $order['out_trade_no'] = $trade_no;
|
|
|
+ $order['price'] = $data['price'];
|
|
|
+ $order['to_user'] = $userAuth->id;
|
|
|
+ $order['state'] = 0;
|
|
|
+ $order['name'] = $data['name'];
|
|
|
+ $order['msg_id'] = 0;
|
|
|
+ $order['poundage'] = 0;
|
|
|
+ $order['openid'] = $userAuth->openid;
|
|
|
+ $order['type'] = 1;
|
|
|
+ $query = PaymentInfoModel::create($order);
|
|
|
+ if($query){
|
|
|
+ $msg = '已成功发起提现,请等待后台审核';
|
|
|
+ DB::commit();
|
|
|
+ } else {
|
|
|
+ $msg = '提现失败';
|
|
|
+ DB::rollback();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $msg = '提现失败';
|
|
|
+ DB::rollback();
|
|
|
+ }
|
|
|
+ return $this->api($msg);
|
|
|
|
|
|
}
|
|
|
|