Browse Source

Merge branch 'phoenix' of http://120.24.33.109:11081/hyzjshwo/lanzu_api_hyperf into phoenix

master
Lemon 5 years ago
parent
commit
63c0dc10f0
  1. 302
      app/JsonRpc/OrderService.php
  2. 8
      app/Service/v3/Implementations/PaymentService.php

302
app/JsonRpc/OrderService.php

@ -69,7 +69,12 @@ class OrderService implements OrderServiceInterface
Db::rollBack();
$this->log->event(LogLabel::ORDER_COMPLETE_LOG, ['jsonrpc_order_service_exception_onlineComplete' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
throw new ErrorCodeException(ErrorCode::ORDER_COMPLETE_FAIL);
return [
"status" => 200,
"code" => $e->getCode() ?? ErrorCode::ORDER_COMPLETE_FAIL,
"result" => [],
"message" => $e->getMessage()
];
}
}
@ -82,32 +87,21 @@ class OrderService implements OrderServiceInterface
*/
public function onlineRefund($global_order_id, $user_id)
{
Db::beginTransaction();
try {
$result = $this->orderOnlineService->doRefund($global_order_id, $user_id);
Db::commit();
if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") {
return [
"status" => 200,
"code" => 0,
"result" => [],
"message" => '处理成功'
];
} else {
return [
"status" => 200,
"code" => -1,
"result" => [],
"message" => $result['err_code_des']
];
}
$this->orderOnlineService->doRefund($global_order_id, $user_id);
return [
"status" => 200,
"code" => 0,
"result" => [],
"message" => '处理成功'
];
} catch (\Exception $e) {
Db::rollBack();
$this->log->event(LogLabel::ORDER_REFUND_LOG, ['jsonrpc_order_service_exception_onlineRefund' => $e->getMessage(), 'params' => json([$global_order_id, $user_id])]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
return [
"status" => 200,
"code" => $e->getCode(),
"result" => [],
"message" => $e->getMessage()
];
}
}
@ -130,132 +124,186 @@ class OrderService implements OrderServiceInterface
public function onlineSingleRefund($user_id, $note, $global_order_id, $order_child_id=null, $order_goods_id=null)
{
$params = [
'user_id' => $user_id,
'note' => $note,
'global_order_id' => $global_order_id,
'order_child_id' => $order_child_id,
'order_goods_id' => $order_goods_id,
];
if (!$user_id || !$global_order_id || !$note) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
Db::beginTransaction();
try {
// 主订单
$orderMain = OrderMain::query()
->where(['global_order_id' => $global_order_id, 'user_id' => $user_id])
->whereIn('state', OrderState::CAN_REFUND_DIRECT)
->first();
$params = [
'user_id' => $user_id,
'note' => $note,
'global_order_id' => $global_order_id,
'order_child_id' => $order_child_id,
'order_goods_id' => $order_goods_id,
];
if (is_null($orderMain)) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
if (!$user_id || !$global_order_id || !$note) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '参数不对',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 子订单
$orderChild = null;
if ($order_child_id) {
$orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first();
}
// 主订单
$orderMain = OrderMain::query()
->where(['global_order_id' => $global_order_id, 'user_id' => $user_id])
->whereIn('state', OrderState::CAN_REFUND_DIRECT)
->first();
// 订单商品
$orderGoods = null;
if ($order_goods_id) {
if (!$order_child_id || is_null($orderChild)) {
if (is_null($orderMain)) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]',
'jsonrpc_order_service_exception_onlineSingleRefund' => '订单不存在',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
$orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first();
// 子订单
$orderChild = null;
if ($order_child_id) {
$orderChild = Order::query()->where(['order_main_id' => $orderMain->global_order_id, 'id' => $order_child_id])->first();
}
// 订单商品
$orderGoods = null;
if ($order_goods_id) {
if (!$order_child_id || is_null($orderChild)) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '子订单参数异常[单品]',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
$orderGoods = OrderGoods::query()->where(['order_id' => $orderChild->id, 'id' => $order_goods_id])->first();
if (is_null($orderGoods)) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
}
var_dump('ordergoods', $orderGoods);
$totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费
$preRefundAmount = 0; // 预退款金额
$refundAmount = 0; // 实际退款金额
$refundType = 'main'; // 退款类型, Main整单 Sub子单 Goods单品
if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品
$preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2);
$refundType = 'goods';
} elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单
$preRefundAmount = $orderChild->money;
$refundType = 'sub';
} else { // 3. 再则如果存在主订单说明退主订单
$preRefundAmount = $orderMain->total_money;
$refundType = 'main';
}
// 占订单金额的比例
$rate = bcdiv($preRefundAmount, $totalAmount, 6);
// 计算优惠券所占金额
$couponMoney = bcmul($orderMain->coupon_money, $rate, 6);
// 计算本次退款实际退款金额
$refundAmount = bcsub($preRefundAmount, $couponMoney, 2);
if (is_null($orderGoods)) {
if ($refundAmount <= 0) {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '订单商品参数异常[单品]',
'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]',
'params' => json_encode($params)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
}
$totalAmount = $orderMain->total_money; // 订单可退款金额,总订单金额不含配送费和服务费
$preRefundAmount = 0; // 预退款金额
$refundAmount = 0; // 实际退款金额
$refundType = 'Main'; // 退款类型, Main整单 Sub子单 Goods单品
if ($orderGoods) { // 1. 如果订单商品存在则说明要退单品
$preRefundAmount = bcmul($orderGoods->price, $orderGoods->number, 2);
$refundType = 'Goods';
} elseif ($orderChild) { // 2. 否则如果存在子订单说明退子订单
$preRefundAmount = $orderChild->money;
$refundType = 'Sub';
} else { // 3. 再则如果存在主订单说明退主订单
$preRefundAmount = $orderMain->total_money;
$refundType = 'Main';
}
var_dump($orderMain->money, $preRefundAmount, $refundAmount);
// 开始退款
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
$result = $app->refund->byOutTradeNumber(
$orderMain->global_order_id,
$orderMain->global_order_id,
bcmul($orderMain->money, 100, 0),
bcmul($refundAmount, 100, 0),
[
'refund_desc' => '订单协商退款[' . $refundType . ']',
'notify_url' => config('wechat.notify_url.refund_single'),
]
);
if ($result['return_code'] == 'FAIL') {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'] . '[微信退款失败]',
'params' => json_encode($result)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 占订单金额的比例
$rate = bcdiv($preRefundAmount, $totalAmount, 6);
// 计算优惠券所占金额
$couponMoney = bcmul($orderMain->coupon_money, $rate, 6);
// 计算本次退款实际退款金额
$refundAmount = bcsub($preRefundAmount, $couponMoney, 2);
if ($result['result_code'] == 'FAIL') {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'] . '[微信退款失败]' . $result['err_code'],
'params' => json_encode($result)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
if ($refundAmount <= 0) {
// 退款申请成功,查询退款状态,此处暂时无法传递和记录单品退的信息,所以直接查询退款状态处理,不做回调
$refundResult = $app->refund->queryByRefundId($result['refund_id']);
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '没有可退款金额[实际退款金额]',
'params' => json_encode($params)
'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]',
'params' => json_encode($refundResult)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 开始退款
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
$result = $app->refund->byOutTradeNumber(
$orderMain->global_order_id,
$orderMain->global_order_id,
bcmul($orderMain->money, 100, 0),
bcmul($refundAmount, 100, 0),
[
'refund_desc' => '订单协商退款['.$refundType.']',
'notify_url' => config('wechat.notify_url.refund_single'),
]
);
if ($result['return_code'] == 'FAIL') {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => $result['return_msg'].'[微信退款失败]',
'params' => json_encode($result)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
// 退款成功
if (
!($refundResult['return_code'] == 'SUCCESS'
&& isset($refundResult['result_code'])
&& $refundResult['result_code'] == 'SUCCESS')
) {
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
if ($result['result_code'] == 'FAIL') {
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => $result['err_code_des'].'[微信退款失败]'.$result['err_code'],
'params' => json_encode($result)
]);
throw new ErrorCodeException(ErrorCode::ORDER_REFUND_FAIL);
}
$currentTime = time();
// 处理订单状态
$orderMain->state = OrderState::REFUNDED;
$orderMain->total_refund_note = $note;
$orderMain->refund_time = $currentTime;
$orderMain->save();
if ($refundType == 'sub') {
$orderChild->status = 3;
$orderChild->refund_note = $note;
$orderChild->refund_time = $currentTime;
$orderChild->save();
}
if ($refundType == 'goods') {
$orderGoods->status = 3;
$orderGoods->refund_note = $note;
$orderGoods->refund_time = $currentTime;
$orderGoods->save();
}
Db::commit();
return [
"status" => 200,
"code" => 0,
"result" => [],
"message" => '处理成功'
];
// 退款申请成功,查询退款状态
$refundResult = $app->refund->queryByRefundId($result['refund_id']);
$this->log->event(LogLabel::ORDER_REFUND_LOG, [
'jsonrpc_order_service_exception_onlineSingleRefund' => '[微信退款查询]',
'params' => json_encode($refundResult)
]);
} catch (\Exception $e) {
Db::rollBack();
return [
"status" => 200,
"code" => $e->getCode(),
"result" => [],
"message" => '[退款失败]'.$e->getMessage()
];
}
}
}

8
app/Service/v3/Implementations/PaymentService.php

@ -127,6 +127,14 @@ class PaymentService implements PaymentServiceInterface
'notify_url' => config('wechat.notify_url.refund'),
]
);
if ($result['return_code'] == 'SUCCESS' && isset($result['result_code']) && $result['result_code'] == "SUCCESS") {
return true;
} else {
throw new ErrorCodeException(ErrorCode::PAYMENT_FAIL);
}
return $result;
} catch (\Exception $e) {
$this->log->event(LogLabel::ORDER_PAYMENT_LOG, ['payment_do_exception_msg' => $e->getMessage()]);

Loading…
Cancel
Save