PayController.php 3.5 KB

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