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.

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