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.

251 lines
9.1 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
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. throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL);
  63. }
  64. }
  65. /**
  66. * 线上订单退款,整个订单退,这个是专门用于处理用户的申请退款的同意退款操作
  67. * @param $global_order_id
  68. * @param $user_id
  69. * @return array
  70. */
  71. public function onlineRefund($global_order_id, $user_id)
  72. {
  73. Db::beginTransaction();
  74. try {
  75. $this->orderOnlineService->doRefund($global_order_id, $user_id);
  76. Db::commit();
  77. return [
  78. "status" => 200,
  79. "code" => 0,
  80. "result" => [],
  81. "message" => '处理成功'
  82. ];
  83. } catch (\Exception $e) {
  84. Db::rollBack();
  85. $this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
  86. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  87. }
  88. }
  89. /**
  90. * 线上订单单笔退款,主要用于后台强行操作退单退款
  91. * 支持单商品、单店、整单
  92. * 按比例计算红包进行退款
  93. * 比如:两个子订单和子订单商品,分别是2元,98元,使用了10元优惠券
  94. * 退2元商品时,退款金额为
  95. * 红包 (2/(98+2)*10 = 0.2
  96. * 退款:2-0.2=1.8
  97. * @param $user_id *用户ID
  98. * @param $global_order_id *全局总订单ID
  99. * @param $order_child_id *主订单ID,
  100. * @param $order_goods_id *订单商品ID
  101. * @param $note
  102. * @throws InvalidConfigException
  103. */
  104. public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null)
  105. {
  106. $params = [
  107. 'user_id' => $user_id,
  108. 'note' => $note,
  109. 'global_order_id' => $global_order_id,
  110. 'order_child_id' => $order_child_id,
  111. 'order_goods_id' => $order_goods_id,
  112. ];
  113. if (!$user_id || !$global_order_id || !$note) {
  114. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  115. 'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
  116. 'params' => json_encode($params)
  117. ]);
  118. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  119. }
  120. // 主订单
  121. $orderMain = OrderMain::query()
  122. ->where(['global_order_id' => $global_order_id, 'user_id' => $user_id])
  123. ->whereIn('state', OrderState::CAN_REFUND_DIRECT)
  124. ->first();
  125. if (is_null($orderMain)) {
  126. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  127. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
  128. 'params' => json_encode($params)
  129. ]);
  130. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  131. }
  132. // 子订单
  133. $orderChild = null;
  134. if ($order_child_id) {
  135. $orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first();
  136. }
  137. // 订单商品
  138. $orderGoods = null;
  139. if ($order_goods_id) {
  140. if (!$order_child_id || is_null($orderChild)) {
  141. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  142. 'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]',
  143. 'params' => json_encode($params)
  144. ]);
  145. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  146. }
  147. $orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first();
  148. if (is_null($orderGoods)) {
  149. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  150. 'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]',
  151. 'params' => json_encode($params)
  152. ]);
  153. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  154. }
  155. }
  156. $totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费
  157. $preRefundAmount = 0; // 预退款金额
  158. $refundAmount = 0; // 实际退款金额
  159. $refundType = 'Main'; // 退款类型, Main整单 Sub子单 Goods单品
  160. if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品
  161. $preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2);
  162. $refundType = 'Goods';
  163. } elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单
  164. $preRefundAmount = $orderChild->money;
  165. $refundType = 'Sub';
  166. } else { // 3. 再则如果存在主订单说明退主订单
  167. $preRefundAmount = $orderMain->total_money;
  168. $refundType = 'Main';
  169. }
  170. // 占订单金额的比例
  171. $rate = bcdiv($preRefundAmount, $totalAmount, 6);
  172. // 计算优惠券所占金额
  173. $couponMoney = bcmul($orderMain->coupon_money, $rate, 6);
  174. // 计算本次退款实际退款金额
  175. $refundAmount = bcsub($preRefundAmount, $couponMoney, 2);
  176. if ($refundAmount <= 0) {
  177. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  178. 'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]',
  179. 'params' => json_encode($params)
  180. ]);
  181. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  182. }
  183. // 开始退款
  184. $config = config('wxpay');
  185. $app = Factory::payment($config);
  186. $app['guzzle_handler'] = CoroutineHandler::class;
  187. $result = $app->refund->byOutTradeNumber(
  188. $orderMain->global_order_id,
  189. $orderMain->global_order_id,
  190. bcmul($orderMain->money, 100, 0),
  191. bcmul($refundAmount, 100, 0),
  192. [
  193. 'refund_desc' => '订单协商退款['.$refundType.']',
  194. 'notify_url' => config('wechat.notify_url.refund_single'),
  195. ]
  196. );
  197. if ($result['return_code'] == 'FAIL') {
  198. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  199. 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'].'[微信退款失败]',
  200. 'params' => json_encode($result)
  201. ]);
  202. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  203. }
  204. if ($result['result_code'] == 'FAIL') {
  205. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  206. 'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'].'[微信退款失败]'.$result['err_code'],
  207. 'params' => json_encode($result)
  208. ]);
  209. throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
  210. }
  211. // 退款申请成功,查询退款状态
  212. $refundResult = $app->refund->queryByRefundId($result['refund_id']);
  213. $this->log->event(LogLabel::ORDER_REFUND_LOG, [
  214. 'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]',
  215. 'params' => json_encode($refundResult)
  216. ]);
  217. }
  218. }