PayController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 思维定制
  5. * Date: 2018/7/16
  6. * Time: 18:09
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\PaymentInfoModel;
  10. use App\Models\UserInfoModel;
  11. use App\Models\WechatAppModel;
  12. use App\User;
  13. use EasyWeChat\Factory;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use Auth,Validator;
  18. use App\Services\Base\ErrorCode;
  19. class PayController extends Controller
  20. {
  21. private $config;
  22. private $wechat;
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. $wechat = WechatAppModel::find(1);
  27. $this->wechat = $wechat;
  28. $this->config = [
  29. // 必要配置
  30. 'app_id' => $wechat->appId,
  31. 'mch_id' => $wechat->mchId,
  32. 'key' => $wechat->key, // API 密钥0
  33. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  34. //'cert_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  35. //'key_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  36. 'notify_url' => '/pay/notify', // 你也可以在下单时单独设置来想覆盖它
  37. ];
  38. }
  39. /**
  40. * @api {post} /api/pay/recharge 充值(recharge)
  41. * @apiDescription 充值(recharge)recharge
  42. * @apiGroup Pay
  43. * @apiPermission none
  44. * @apiVersion 0.1.0
  45. * @apiParam {decimal} price 金额
  46. * @apiSuccessExample {json} Success-Response:
  47. * HTTP/1.1 200 OK
  48. * {
  49. * "status": true,
  50. * "status_code": 0,
  51. * "message": "",
  52. * "data": {
  53. * "json": "{\"appId\":\"wx92066f7587c34617\",\"timeStamp\":\"1532584373\",\"nonceStr\":\"5b5961b537a27\",\"package\":\"prepay_id=wx2613525320101170a0f42d941994171920\",\"signType\":\"MD5\",\"paySign\":\"B4E50A4610F3AC4AE2E12EF2A45D8109\"}"
  54. * }
  55. *}
  56. * @apiErrorExample {json} Error-Response:
  57. * HTTP/1.1 400 Bad Request
  58. * {
  59. * "state": false,
  60. * "code": 1000,
  61. * "message": "传入参数不正确",
  62. * "data": null or []
  63. * }
  64. */
  65. public function recharge(Request $request)
  66. {
  67. $validator = Validator::make($request->all(),
  68. [
  69. 'price' => 'required',
  70. ],[
  71. 'price.required' => 'price不能为空!',
  72. ]
  73. );
  74. if ($validator->fails()) {
  75. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  76. }
  77. $userAuth = Auth('api')->user();
  78. $money = $request->input('price');
  79. $trade_no = 'Pay'.date('YmdHis').rand(1000,9999);
  80. $order['user_id'] = $userAuth->id;
  81. $order['out_trade_no'] = $trade_no;
  82. $order['price'] = $money;
  83. $order['to_user'] = 0;
  84. $order['state'] = 0;
  85. $order['msg_id'] = 0;
  86. $order['poundage'] = 0;
  87. $order['openid'] = $userAuth->openid;
  88. $order['type'] = 0;
  89. PaymentInfoModel::create($order);
  90. $app = Factory::payment($this->config);
  91. $result = $app->order->unify([
  92. 'body' => '知识付费充值余额',
  93. 'out_trade_no' => $trade_no,
  94. 'total_fee' => $money * 100,
  95. 'trade_type' => 'JSAPI',
  96. 'notify_url' => url('/api/home/notify'),
  97. 'openid' => $userAuth->openid
  98. ]);
  99. //dd($result);
  100. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  101. $payment = Factory::payment($this->config);
  102. $jssdk = $payment->jssdk;
  103. $json = $jssdk->bridgeConfig($result['prepay_id']);
  104. //dd($json);
  105. return $this->api(compact('json'));
  106. }else{
  107. $msg = "签名失败,请稍后再试!";
  108. return $this->api(compact('msg'));
  109. }
  110. }
  111. //下面是回调函数
  112. public function notify()
  113. {
  114. $app = Factory::payment($this->config);
  115. \Log::info("wechat notify start!");
  116. $response = $app->handlePaidNotify(function ($notify, $successful) {
  117. \Log::info($notify);
  118. if ($notify['result_code'] == 'SUCCESS') {
  119. $user = UserInfoModel::where('openid',$notify['openid'])->first();
  120. $data['order_no'] = $notify['out_trade_no'];
  121. $data['price'] = $notify['total_fee'] / 100;
  122. $wechat_pay = PaymentInfoModel::where([['user_id',$user->id],['out_trade_no',$notify['out_trade_no']],['state',0]])->first();
  123. if($wechat_pay){
  124. $user->money += $data['price'];
  125. $user->save();
  126. $wechat_pay->state = 1;
  127. $wechat_pay->save();
  128. }
  129. } else {
  130. return $successful('通信失败,请稍后再通知我');
  131. }
  132. return true;
  133. });
  134. return $response->send();
  135. }
  136. /**
  137. * @api {post} /api/pay/cash 提现(cash)
  138. * @apiDescription 提现(cash)cash
  139. * @apiGroup Pay
  140. * @apiPermission none
  141. * @apiVersion 0.1.0
  142. * @apiParam {decimal} price 金额
  143. * @apiParam {string} name 真实姓名
  144. * @apiSuccessExample {json} Success-Response:
  145. * HTTP/1.1 200 OK
  146. * {
  147. * "status": true,
  148. * "status_code": 0,
  149. * "message": "",
  150. * "data": {
  151. * "msg": "已成功发起提现,请等待后台审核"
  152. * }
  153. *}
  154. * @apiErrorExample {json} Error-Response:
  155. * HTTP/1.1 400 Bad Request
  156. * {
  157. * "state": false,
  158. * "code": 1000,
  159. * "message": "传入参数不正确",
  160. * "data": null or []
  161. * }
  162. */
  163. public function cash(Request $request)
  164. {
  165. $validator = Validator::make($request->all(),
  166. [
  167. 'price' => 'required',
  168. 'name' => 'required',
  169. ],[
  170. 'price.required' => 'price不能为空!',
  171. 'name.required' => 'name不能为空!'
  172. ]
  173. );
  174. if ($validator->fails()) {
  175. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  176. }
  177. $data = $request->input();
  178. $userAuth = Auth('api')->user();
  179. $trade_no = 'Cash'.date('YmdHis').rand(1000,9999);
  180. $poundage = $this->wechat->poundage * $data['price'];
  181. $total = $data['price'] + $poundage;
  182. if($userAuth->money < $total){
  183. $msg = '您的余额不足';
  184. return $this->api($msg);
  185. }
  186. DB::beginTransaction();
  187. $user = UserInfoModel::find($userAuth->id);
  188. $user->money -= $total;
  189. $res = $user->save();
  190. if($res){
  191. $order['user_id'] = 0;
  192. $order['out_trade_no'] = $trade_no;
  193. $order['price'] = $data['price'];
  194. $order['to_user'] = $userAuth->id;
  195. $order['state'] = 0;
  196. $order['name'] = $data['name'];
  197. $order['msg_id'] = 0;
  198. $order['poundage'] = 0;
  199. $order['openid'] = $userAuth->openid;
  200. $order['type'] = 1;
  201. $query = PaymentInfoModel::create($order);
  202. if($query){
  203. $msg = '已成功发起提现,请等待后台审核';
  204. DB::commit();
  205. } else {
  206. $msg = '提现失败';
  207. DB::rollback();
  208. }
  209. } else {
  210. $msg = '提现失败';
  211. DB::rollback();
  212. }
  213. return $this->api($msg);
  214. }
  215. }