PayController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. use Auth;
  15. class PayController extends Controller
  16. {
  17. private $config;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $wechat = WechatAppModel::find(1);
  22. $this->config = [
  23. // 必要配置
  24. 'app_id' => $wechat->appId,
  25. 'mch_id' => $wechat->mchId,
  26. 'key' => $wechat->key, // API 密钥
  27. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  28. //'cert_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  29. //'key_path' => public_path().'/pem/', // XXX: 绝对路径!!!!
  30. 'notify_url' => '/pay/notify', // 你也可以在下单时单独设置来想覆盖它
  31. ];
  32. }
  33. public function recharge(Request $request)
  34. {
  35. $validator = Validator::make($request->all(),
  36. [
  37. 'price' => 'required',
  38. ],[
  39. 'price.required' => 'price不能为空!',
  40. ]
  41. );
  42. if ($validator->fails()) {
  43. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '传入参数不正确!', $validator->messages());
  44. }
  45. $userAuth = Auth('api')->user();
  46. $money = $request->input('price');
  47. $trade_no = 'Pay'.date('YmdHis').rand(1000,9999);
  48. $app = Factory::payment($this->config);
  49. $result = $app->order->unify([
  50. 'body' => '知识付费充值余额',
  51. 'out_trade_no' => $trade_no,
  52. 'total_fee' => $money * 100,
  53. 'trade_type' => 'JSAPI',
  54. 'notify_url' => url('/api/home/notify'),
  55. 'openid' => $userAuth->openid
  56. ]);
  57. dd($result);
  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. }