PayController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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);echo 1111;exit();
  29. if (!$payConfig) {
  30. return $this->error('非法请求');
  31. }
  32. $order = [
  33. 'user_id' => $this->request->user->id,
  34. 'order' => Helper::generateNonceStr(32),
  35. 'amount' => $payConfig->amount,
  36. 'diamond' => $payConfig->diamond,
  37. ];
  38. var_dump(($payConfig->amount * 100));exit();
  39. $result = Order::query()->create($order);
  40. if (!$result) {
  41. return $this->error('订单提交失败');
  42. }
  43. User::query()->where('id', Auth::$userId)->increment('diamond', $payConfig->diamond);
  44. $ret = Wechat::addOrder($result,Auth::$user);
  45. $result->pay = json_encode($ret);
  46. $result->save();
  47. $config = [];
  48. if (isset($ret['return_code'], $ret['result_code'])) {
  49. if ('SUCCESS' == $ret['result_code'] && 'SUCCESS' == $ret['return_code']) {
  50. $app = Wechat::pay();
  51. $utils = $app->getUtils();
  52. $appId = $ret['appid'];
  53. $signType = 'MD5'; // 默认RSA,v2要传MD5
  54. $config = $utils->buildMiniAppConfig($ret['prepay_id'], $appId, $signType); // 返回数组
  55. } else {
  56. return $this->error($ret['err_code_des']);
  57. }
  58. } else {
  59. return $this->error('参数异常,请稍后再试' . json_encode($ret, 256));
  60. }
  61. $code = 0;
  62. return $this->success(compact('config', 'code'));
  63. } catch (\Exception $exception) {
  64. LogHelper::exceptionLog($exception, $this->code);
  65. return Response::fail($exception->getMessage());
  66. }
  67. }
  68. }