OrderController.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Modules\Api\Controllers;
  3. use App\Events\PayTotalEvent;
  4. use App\Exceptions\ApiException;
  5. use App\Models\OrderModel;
  6. use App\Models\PaymentModel;
  7. use App\Repositories\OrderRepository;
  8. use App\Repositories\Service\Payment\PaymentInterface;
  9. use Illuminate\Support\Facades\Cache;
  10. class OrderController
  11. {
  12. public function notify($orderId, $payCode)
  13. {
  14. logger(json_encode(request()->all()) . '发送回调信息!');
  15. // 写回调记录
  16. if (empty($orderId) || empty($payCode)) {
  17. throw new ApiException('参数错误');
  18. }
  19. $orderId = intval($orderId);
  20. $payCode = strval($payCode);
  21. $orderInfo = OrderModel::query()->where('id', $orderId)->first();
  22. if (empty($orderInfo)) {
  23. throw new ApiException('订单异常!');
  24. }
  25. $payInterface = PaymentInterface::factory($payCode);
  26. if ($orderInfo->status == 2) {
  27. return $payInterface->stopNotify();
  28. }
  29. $payment = PaymentModel::query()->where('pay_code', $payCode)->first(['partner_id']);
  30. $result = $payInterface->notify($orderSn, $tradeNo, $money, $payment->partner_id);
  31. if ($result) {
  32. $orderRes = app(OrderRepository::class)->doBuy($orderInfo->toArray(), $tradeNo, $money);
  33. if ($orderRes) {
  34. event(new PayTotalEvent($orderInfo->merchant_uid, $orderInfo->payment_uid, $orderInfo->payment_attr_uid, $orderInfo->real_amount, 2));
  35. return $payInterface->stopNotify();
  36. }
  37. }
  38. return 'nook';
  39. }
  40. }