MicroPayOrderJob.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\user;
  12. use app\services\order\OtherOrderServices;
  13. use app\services\order\StoreCartServices;
  14. use app\services\order\StoreOrderCreateServices;
  15. use app\services\order\StoreOrderSuccessServices;
  16. use app\services\pay\PayServices;
  17. use app\services\user\UserRechargeServices;
  18. use crmeb\basic\BaseJobs;
  19. use crmeb\services\wechat\Payment;
  20. use crmeb\traits\QueueTrait;
  21. use think\facade\Log;
  22. use app\webscoket\SocketPush;
  23. /**
  24. * 付款码支付
  25. * Class MicroPayOrderJob
  26. * @package app\jobs\user
  27. */
  28. class MicroPayOrderJob extends BaseJobs
  29. {
  30. use QueueTrait;
  31. public function doJob(string $outTradeNo, int $type = 1, int $num = 1)
  32. {
  33. if ($type == 2) {
  34. /** @var OtherOrderServices $OtherOrderServices */
  35. $make = app()->make(OtherOrderServices::class);
  36. } else if ($type == 1) {
  37. /** @var UserRechargeServices $make */
  38. $make = app()->make(UserRechargeServices::class);
  39. } else {
  40. /** @var StoreOrderSuccessServices $make */
  41. $make = app()->make(StoreOrderSuccessServices::class);
  42. }
  43. $orderInfo = $make->get(['order_id' => $outTradeNo]);
  44. if (!$orderInfo) {
  45. return true;
  46. }
  47. if ($orderInfo->paid) {
  48. return true;
  49. }
  50. if (!$type && $orderInfo->is_del) {
  51. return true;
  52. }
  53. try {
  54. //查询订单支付状态
  55. $respones = Payment::queryOrder($outTradeNo);
  56. if ($respones['paid'] && ($respones['payInfo']['trade_state'] ?? '') == 'SUCCESS') {
  57. if ($type == 2) {
  58. $orderInfo = $orderInfo->toArray();
  59. $make->paySuccess($orderInfo, $orderInfo['pay_type']);
  60. } else if ($type == 1) {
  61. $make->rechargeSuccess($outTradeNo);
  62. } else {
  63. //删除购物车
  64. /** @var StoreCartServices $cartServices */
  65. $cartServices = app()->make(StoreCartServices::class);
  66. $cartServices->deleteCartStatus($orderInfo['cart_id'] ?? []);
  67. //修改支付状态
  68. $make->paySuccess($orderInfo->toArray(), PayServices::WEIXIN_PAY, [
  69. 'trade_no' => $respones['payInfo']['transaction_id'] ?? ''
  70. ]);
  71. if ($orderInfo->staff_id) {
  72. //发送消息
  73. try {
  74. SocketPush::instance()->to($orderInfo->staff_id)->setUserType('cashier')->type('changSuccess')->push();
  75. } catch (\Throwable $e) {
  76. }
  77. }
  78. }
  79. } else {
  80. //15秒后还是状态异常直接取消订单
  81. if ($num >= 3) {
  82. try {
  83. $respones = Payment::reverseOrder($outTradeNo);
  84. if ($type) {
  85. /** @var StoreOrderCreateServices $service */
  86. $service = app()->make(StoreOrderCreateServices::class);
  87. $make->update(
  88. [
  89. 'order_id' => $outTradeNo
  90. ],
  91. [
  92. $type ? 'remarks' : 'remark' => '支付状态异常自动撤销订单,并从新生成订单号',
  93. 'order_id' => $type ? $make->getOrderId() : ($type == 2 ? $service->getNewOrderId('hy') : $service->getNewOrderId()),
  94. ]
  95. );
  96. }
  97. } catch (\Throwable $e) {
  98. Log::error([
  99. 'message' => '撤销订单失败,订单号:' . $outTradeNo . ';错误原因:' . $e->getMessage(),
  100. 'file' => $e->getFile(),
  101. 'line' => $e->getLine()
  102. ]);
  103. }
  104. return true;
  105. }
  106. $secs = 5;
  107. if (isset($respones['payInfo']['err_code']) && $respones['payInfo']['err_code'] === 'USERPAYING') {
  108. $secs = 10;
  109. }
  110. self::dispatchSece($secs, [$outTradeNo, $type, $num + 1]);
  111. }
  112. } catch (\Throwable $e) {
  113. }
  114. return true;
  115. }
  116. }