You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 lines
12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Commons\Log;
  4. use App\Constants\v3\ErrorCode;
  5. use App\Constants\v3\LogLabel;
  6. use App\Constants\v3\OrderState;
  7. use App\Exception\ErrorCodeException;
  8. use App\Model\v3\Order;
  9. use App\Model\v3\OrderGoods;
  10. use App\Model\v3\OrderMain;
  11. use App\Service\v3\Interfaces\OrderOnlineServiceInterface;
  12. use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;
  13. use EasyWeChat\Factory;
  14. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  15. use Hyperf\DbConnection\Db;
  16. use Hyperf\Guzzle\CoroutineHandler;
  17. use Hyperf\RpcServer\Annotation\RpcService;
  18. use Hyperf\Di\Annotation\Inject;
  19. use function AlibabaCloud\Client\json;
  20. /**
  21. * @RpcService(name="OrderService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="")
  22. */
  23. class OrderService implements OrderServiceInterface
  24. {
  25. /**
  26. * @Inject
  27. * @var Log
  28. */
  29. protected $log;
  30. /**
  31. * @Inject
  32. * @var OrderOnlineServiceInterface
  33. */
  34. protected $orderOnlineService;
  35. /**
  36. * @Inject
  37. * @var SeparateAccountsServiceInterface
  38. */
  39. protected $separateAccountsService;
  40. /**
  41. * 订单完成
  42. * @param $global_order_id
  43. * @param $user_id
  44. * @return array
  45. */
  46. public function onlineComplete($global_order_id, $user_id)
  47. {
  48. Db::beginTransaction();
  49. try {
  50. $this->orderOnlineService->doComplete($global_order_id, $user_id);
  51. $this->separateAccountsService->orderOnlineCompleted($global_order_id, $user_id);
  52. Db::commit();
  53. return [
  54. "status" => 200,
  55. "code" => 0,
  56. "result" => [],
  57. "message" => '处理成功'
  58. ];
  59. } catch (\Exception $e) {
  60. Db::rollBack();
  61. $this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
  62. return [
  63. "status" => 200,
  64. "code" => $e->getCode() ?? ErrorCode::ORDER_COMPLETE_FAIL,
  65. "result" => [],
  66. "message" => $e->getMessage()
  67. ];
  68. }
  69. }
  70. /**
  71. * 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作
  72. * @param $global_order_id
  73. * @param $user_id
  74. * @return array
  75. */
  76. public function onlineRefund($global_order_id, $user_id)
  77. {
  78. Db::beginTransaction();
  79. try {
  80. $result = $this->orderOnlineService->doRefund($global_order_id, $user_id);
  81. Db::commit();
  82. if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") {
  83. return [
  84. "status" => 200,
  85. "code" => 0,
  86. "result" => [],
  87. "message" => '处理成功'
  88. ];
  89. } else {
  90. return [
  91. "status" => 200,
  92. "code" => -1,
  93. "result" => [],
  94. "message" => $result['err_code_des']
  95. ];
  96. }
  97. } catch (\Exception $e) {
  98. Db::rollBack();
  99. $this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
  100. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  101. }
  102. }
  103. /**
  104. * 线上订单单笔退款,主要用于后台强行操作退单退款
  105. * 支持单商品、单店、整单
  106. * 按比例计算红包进行退款
  107. * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券
  108. * 退2元商品时,退款金额为
  109. * 红包 (2/(98+2)*10 = 0.2
  110. * 退款:2-0.2=1.8
  111. * @param $user_id *用户ID
  112. * @param $global_order_id *全局总订单ID
  113. * @param $order_child_id *主订单ID,
  114. * @param $order_goods_id *订单商品ID
  115. * @param $note
  116. * @throws InvalidConfigException
  117. */
  118. public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null)
  119. {
  120. Db::beginTransaction();
  121. try {
  122. $params = [
  123. 'user_id' => $user_id,
  124. 'note' => $note,
  125. 'global_order_id' => $global_order_id,
  126. 'order_child_id' => $order_child_id,
  127. 'order_goods_id' => $order_goods_id,
  128. ];
  129. if (!$user_id || !$global_order_id || !$note) {
  130. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  131. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
  132. 'params' => json_encode($params)
  133. ]);
  134. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  135. }
  136. // 主订单
  137. $orderMain = OrderMain::query()
  138. ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id])
  139. ->whereIn('state', OrderState::CAN_REFUND_DIRECT)
  140. ->first();
  141. if (is_null($orderMain)) {
  142. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  143. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
  144. 'params' => json_encode($params)
  145. ]);
  146. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  147. }
  148. // 子订单
  149. $orderChild = null;
  150. if ($order_child_id) {
  151. $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first();
  152. }
  153. // 订单商品
  154. $orderGoods = null;
  155. if ($order_goods_id) {
  156. if (!$order_child_id || is_null($orderChild)) {
  157. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  158. 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]',
  159. 'params' => json_encode($params)
  160. ]);
  161. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  162. }
  163. $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first();
  164. if (is_null($orderGoods)) {
  165. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  166. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]',
  167. 'params' => json_encode($params)
  168. ]);
  169. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  170. }
  171. }
  172. var_dump('ordergoods', $orderGoods);
  173. $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费
  174. $preRefundAmount = 0; // 预退款金额
  175. $refundAmount = 0; // 实际退款金额
  176. $refundType = 'main'; // 退款类型, Main整单 Sub子单 Goods单品
  177. if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品
  178. $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2);
  179. $refundType = 'goods';
  180. } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单
  181. $preRefundAmount = $orderChild->money;
  182. $refundType = 'sub';
  183. } else { // 3. 再则如果存在主订单说明退主订单
  184. $preRefundAmount = $orderMain->total_money;
  185. $refundType = 'main';
  186. }
  187. // 占订单金额的比例
  188. $rate = bcdiv($preRefundAmount, $totalAmount, 6);
  189. // 计算优惠券所占金额
  190. $couponMoney = bcmul($orderMain->coupon_money, $rate, 6);
  191. // 计算本次退款实际退款金额
  192. $refundAmount = bcsub($preRefundAmount, $couponMoney, 2);
  193. if ($refundAmount <= 0) {
  194. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  195. 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]',
  196. 'params' => json_encode($params)
  197. ]);
  198. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  199. }
  200. var_dump($orderMain->money, $preRefundAmount, $refundAmount);
  201. // 开始退款
  202. $config = config('wxpay');
  203. $app = Factory::payment($config);
  204. $app['guzzle_handler'] = CoroutineHandler::class;
  205. $result = $app->refund->byOutTradeNumber(
  206. $orderMain->global_order_id,
  207. $orderMain->global_order_id,
  208. bcmul($orderMain->money, 100, 0),
  209. bcmul($refundAmount, 100, 0),
  210. [
  211. 'refund_desc' => '订单协商退款[' . $refundType . ']',
  212. 'notify_url' => config('wechat.notify_url.refund_single'),
  213. ]
  214. );
  215. if ($result['return_code'] == 'FAIL') {
  216. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  217. 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'] . '[微信退款失败]',
  218. 'params' => json_encode($result)
  219. ]);
  220. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  221. }
  222. if ($result['result_code'] == 'FAIL') {
  223. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  224. 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'] . '[微信退款失败]' . $result['err_code'],
  225. 'params' => json_encode($result)
  226. ]);
  227. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  228. }
  229. // 退款申请成功,查询退款状态,此处暂时无法传递和记录单品退的信息,所以直接查询退款状态处理,不做回调
  230. $refundResult = $app->refund->queryByRefundId($result['refund_id']);
  231. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  232. 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]',
  233. 'params' => json_encode($refundResult)
  234. ]);
  235. // 退款成功
  236. if (
  237. !($refundResult['return_code'] == 'SUCCESS'
  238. && isset($refundResult['result_code'])
  239. && $refundResult['result_code'] == 'SUCCESS')
  240. ) {
  241. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  242. }
  243. $currentTime = time();
  244. // 处理订单状态
  245. $orderMain->state = OrderState::REFUNDED;
  246. $orderMain->total_refund_note = $note;
  247. $orderMain->refund_time = $currentTime;
  248. $orderMain->save();
  249. if ($refundType == 'sub') {
  250. $orderChild->status = 3;
  251. $orderChild->refund_note = $note;
  252. $orderChild->refund_time = $currentTime;
  253. $orderChild->save();
  254. }
  255. if ($refundType == 'goods') {
  256. $orderGoods->status = 3;
  257. $orderGoods->refund_note = $note;
  258. $orderGoods->refund_time = $currentTime;
  259. $orderGoods->save();
  260. }
  261. Db::commit();
  262. return [
  263. "status" => 200,
  264. "code" => 0,
  265. "result" => [],
  266. "message" => '处理成功'
  267. ];
  268. } catch (\Exception $e) {
  269. Db::rollBack();
  270. return [
  271. "status" => 200,
  272. "code" => $e->getCode(),
  273. "result" => [],
  274. "message" => '[退款失败]'.$e->getMessage()
  275. ];
  276. }
  277. }
  278. }