Notify.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Models\Order;
  4. use App\Models\UserCourse;
  5. use Illuminate\Support\Facades\Log;
  6. use Yansongda\LaravelPay\Facades\Pay;
  7. class Notify
  8. {
  9. public function notifyWx()
  10. {
  11. $pay = Pay::wechat();
  12. try {
  13. $data = $pay->verify()->toArray();
  14. $tradeState = $data["result_code"];
  15. if ($tradeState != "SUCCESS") {
  16. return $pay->success();
  17. }
  18. $outTradeNo = $data["out_trade_no"];
  19. $orderInfo = Order::getOrderInfoByOutTradeNo($outTradeNo);
  20. if (!$orderInfo) {
  21. return $pay->success();
  22. }
  23. if ($orderInfo["status"] != 0) {//待支付状态
  24. return $pay->success();
  25. }
  26. if ($orderInfo["total_fee"] != $data["total_fee"]) {
  27. return $pay->success();
  28. }
  29. if (Order::updateOrder(["out_trade_no" => $outTradeNo], ["status" => 1])) {
  30. if ($orderInfo["type"] == 0) {
  31. }elseif ($orderInfo["type"] == 1) {
  32. UserCourse::insertData(['user_id'=>$orderInfo['user_id'],'course_id'=>$orderInfo['object_id']]);
  33. }
  34. }
  35. } catch (\Exception $e) {
  36. Log::error("wx_cb", [$e->getTraceAsString()]);
  37. }
  38. return $pay->success();
  39. }
  40. }