PayController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Auth;
  4. use App\libs\helpers\Helper;
  5. use App\libs\helpers\LogHelper;
  6. use App\libs\helpers\Response;
  7. use App\libs\wechat\pay\Wechat;
  8. use App\Models\Order;
  9. use App\Models\Pay;
  10. use App\Models\PaymentConfig;
  11. use App\Models\User;
  12. use Illuminate\Http\Request;
  13. class PayController extends Controller
  14. {
  15. public function query($payId)
  16. {
  17. $pay = Pay::find($payId);
  18. if (!$pay || $pay->status) {
  19. return $this->success();
  20. }
  21. return $this->error();
  22. }
  23. public function pay(Request $request)
  24. {
  25. try {
  26. $id = $this->request->post('id', 1);
  27. $payConfig = PaymentConfig::query()->where('state', 1)->find($id);
  28. var_dump($payConfig->amount);
  29. echo 1111;
  30. exit;
  31. if (!$payConfig) {
  32. return $this->error('非法请求');
  33. }
  34. $order = [
  35. 'user_id' => $this->request->user->id,
  36. 'order' => Helper::generateNonceStr(32),
  37. 'amount' => $payConfig->amount,
  38. 'diamond' => $payConfig->diamond,
  39. ];
  40. var_dump($payConfig->amount * 100);
  41. exit;
  42. $result = Order::query()->create($order);
  43. if (!$result) {
  44. return $this->error('订单提交失败');
  45. }
  46. User::query()->where('id', Auth::$userId)->increment('diamond', $payConfig->diamond);
  47. $ret = Wechat::addOrder($result, Auth::$user);
  48. $result->pay = json_encode($ret);
  49. $result->save();
  50. $config = [];
  51. if (isset($ret['return_code'], $ret['result_code'])) {
  52. if ('SUCCESS' == $ret['result_code'] && 'SUCCESS' == $ret['return_code']) {
  53. $app = Wechat::pay();
  54. $utils = $app->getUtils();
  55. $appId = $ret['appid'];
  56. $signType = 'MD5'; // 默认RSA,v2要传MD5
  57. $config = $utils->buildMiniAppConfig($ret['prepay_id'], $appId, $signType); // 返回数组
  58. } else {
  59. return $this->error($ret['err_code_des']);
  60. }
  61. } else {
  62. return $this->error('参数异常,请稍后再试' . json_encode($ret, 256));
  63. }
  64. $code = 0;
  65. return $this->success(compact('config', 'code'));
  66. } catch (\Exception $exception) {
  67. LogHelper::exceptionLog($exception, $this->code);
  68. return Response::fail($exception->getMessage());
  69. }
  70. }
  71. }