123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 思维定制
- * Date: 2018/7/16
- * Time: 18:09
- */
- 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;
- class PayController extends Controller
- {
- private $config;
- private $wechat;
- public function __construct()
- {
- parent::__construct();
- $wechat = WechatAppModel::find(1);
- $this->wechat = $wechat;
- $this->config = [
- // 必要配置
- 'app_id' => $wechat->appId,
- 'mch_id' => $wechat->mchId,
- 'key' => $wechat->key, // API 密钥0
- // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
- //'cert_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
- //'key_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
- 'notify_url' => '/pay/notify', // 你也可以在下单时单独设置来想覆盖它
- ];
- }
- /**
- * @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(),
- [
- 'price' => 'required',
- ],[
- 'price.required' => 'price不能为空!',
- ]
- );
- if ($validator->fails()) {
- return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
- }
- $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([
- 'body' => '知识付费充值余额',
- 'out_trade_no' => $trade_no,
- 'total_fee' => $money * 100,
- 'trade_type' => 'JSAPI',
- 'notify_url' => url('/api/home/notify'),
- 'openid' => $userAuth->openid
- ]);
- //dd($result);
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- $payment = Factory::payment($this->config);
- $jssdk = $payment->jssdk;
- $json = $jssdk->bridgeConfig($result['prepay_id']);
- //dd($json);
- return $this->api(compact('json'));
- }else{
- $msg = "签名失败,请稍后再试!";
- return $this->api(compact('msg'));
- }
- }
- //下面是回调函数
- public function notify()
- {
- $app = Factory::payment($this->config);
- \Log::info("wechat notify start!");
- $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;
- $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);
- }
- }
|