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.

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