PayController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\UserInfoModel;
  10. use App\Models\WechatAppModel;
  11. use EasyWeChat\Factory;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Log;
  14. class PayController extends Controller
  15. {
  16. private $config;
  17. public function __construct()
  18. {
  19. $wechat = WechatAppModel::find(1);
  20. $this->config = [
  21. // 必要配置
  22. 'app_id' => $wechat->appId,
  23. 'mch_id' => $wechat->mchId,
  24. 'key' => $wechat->key, // API 密钥
  25. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  26. //'cert_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  27. //'key_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  28. 'notify_url' => '/pay/notify', // 你也可以在下单时单独设置来想覆盖它
  29. ];
  30. }
  31. public function recharge(Request $request)
  32. {
  33. $validator = Validator::make($request->all(),
  34. [
  35. 'userid' => 'required',
  36. 'price' => 'required',
  37. ],
  38. [
  39. 'userid.required' => 'userid不能为空!',
  40. 'price.required' => 'price不能为空!',
  41. ]
  42. );
  43. if ($validator->fails()) {
  44. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  45. }
  46. $money = $request->input('price');
  47. $user = UserInfoModel::find($this->getUserId());
  48. $trade_no = 'Pay'.date('YmdHis').rand(1000,9999);
  49. $app = Factory::payment($this->config);
  50. $result = $app->order->unify([
  51. 'body' => '知识付费充值余额',
  52. 'out_trade_no' => $trade_no,
  53. 'total_fee' => $money * 100,
  54. 'trade_type' => 'JSAPI',
  55. 'notify_url' => url('/api/home/notify'),
  56. 'openid' => $user->openid
  57. ]);
  58. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  59. $payment = Factory::payment($this->options());
  60. $jssdk = $payment->jssdk;
  61. $json = $jssdk->bridgeConfig($result['prepay_id']);
  62. return $this->api(compact('json'));
  63. }else{
  64. $msg = "签名失败,请稍后再试!";
  65. return $this->api(compact('msg'));
  66. }
  67. }
  68. //下面是回调函数
  69. public function notify()
  70. {
  71. $app = Factory::payment($this->options());
  72. \Log::info("wechat notify start!");
  73. return $app->handlePaidNotify(function ($notify, $successful) {
  74. \Log::info($notify);
  75. if ($notify['result_code'] == 'SUCCESS') {
  76. $user = UserInfoModel::where('openid',$notify['openid'])->first();
  77. $data['order_no'] = $notify['out_trade_no'];
  78. $data['price'] = $notify['total_fee'] / 100;
  79. $data['user_id'] = $user->id;
  80. $data['code'] = $user->code;
  81. \Log::info($data);
  82. $user->paid_end_time = Carbon::parse("+1 year")->toDateTimeString();
  83. $this->createQueryInfo($user,1);
  84. OrderInfoModel::create($data);
  85. $user->save();
  86. } else {
  87. return $successful('通信失败,请稍后再通知我');
  88. }
  89. return true;
  90. });
  91. }
  92. }