Browse Source

特价商品--迁移线上订单退款

master
liangyuyan 5 years ago
parent
commit
19b4636e0e
  1. 12
      app/JsonRpc/OrderService.php
  2. 8
      app/Model/CouponUserRec.php
  3. 6
      app/Model/CouponUserUse.php
  4. 20
      app/Service/CouponService.php
  5. 2
      app/Service/CouponServiceInterface.php
  6. 62
      app/Service/OrderService.php
  7. 47
      app/Service/PayRefundService.php
  8. 8
      app/Service/PayRefundServiceInterface.php

12
app/JsonRpc/OrderService.php

@ -65,6 +65,8 @@ class OrderService implements OrderServiceInterface
/**
* 线上订单退款
* 申请退款 state = 8
* 退款成功 state = 9
*/
public function onlineRefund($global_order_id){
Db::beginTransaction();
@ -75,15 +77,15 @@ class OrderService implements OrderServiceInterface
"code" => 0,
"result" => $this->orderService->onlineRefund($global_order_id),
// 'result' => $global_order_id,
"message" => ''
"message" => '退款成功'
];
} catch (\Exception $e){
Db::rollBack();
return [
"status" => 200,
"code" => 0,
"result" => $this->orderService->onlineRefund($global_order_id),
// 'result' => $global_order_id,
"message" => ''
"code" => ErrorCode::ORDER_FAILURE,
"result" => [],
"message" => $e->getMessage()
];
}
}

8
app/Model/CouponUserRec.php

@ -6,5 +6,13 @@ namespace App\Model;
class CouponUserRec extends Model
{
/* 状态 */
// 未使用
const STATE_UNUSED = 0;
// 使用部分
const STATE_SOME = 1;
// 已用完
const STATE_FINISH = 2;
protected $table = 'ims_system_coupon_user_receive';
}

6
app/Model/CouponUserUse.php

@ -6,5 +6,11 @@ namespace App\Model;
class CouponUserUse extends Model
{
/* 状态 */
// 正常使用
const STATE_USE = 1;
// 已退回用户
const STATE_REFUND = 2;
protected $table = 'ims_system_coupon_user_use';
}

20
app/Service/CouponService.php

@ -220,4 +220,24 @@ class CouponService implements CouponServiceInterface
return $setRes&&$expireRes;
}
/**
* 删除-优惠券今日使用的缓存
* @param $userId
* @param $couponId
* @param $couponRecId
* @return bool
*/
function clearTodayCouponUsed($userId, $couponId)
{
$redis = ApplicationContext::getContainer()->get(Redis::class);
$res = $redis->sRem(
'coupon_'.date('Ymd').'_used_'.$userId,
$couponId
);
return $res;
}
}

2
app/Service/CouponServiceInterface.php

@ -27,4 +27,6 @@ interface CouponServiceInterface
*/
public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]);
function clearTodayCouponUsed($userId, $couponId);
}

62
app/Service/OrderService.php

@ -711,6 +711,66 @@ class OrderService implements OrderServiceInterface
* @inheritDoc
*/
public function onlineRefund($global_order_id){
return 123;
Db::beginTransaction();
try {
$time = time();
// 主订单状态更新
$orderMain = OrderMain::query()
->where(['global_order_id' => $global_order_id, 'state' => OrderMain::ORDER_STATE_DELIVERY])
->first();
if (empty($orderMain)) {
Db::rollBack();
return false;
}
$orderMain->state = OrderMain::ORDER_STATE_REFUNDED;
$upOrderMain = $orderMain->save();
// 子订单状态更新
$upChild = Order::query()
->where(['order_main_id' => $orderMain->id])
->update(['state' => OrderMain::ORDER_STATE_REFUNDED]);
/* 退还优惠券 */
// 先查询是否正常使用优惠券
// 修改状态,退还领取记录库存,删除ssdb缓存
$couponUses = CouponUserUse::query()
->select('id','system_coupon_id','user_id','number','user_receive_id')
->where('order_main_id',$orderMain->id)
->where('status',CouponUserUse::STATE_USE)
->select();
if(!empty($couponUse)){
foreach($couponUses as $use){
$use->status = CouponUserUse::STATE_USE;
$use->return_time = $time;
$use->update_time = $time;
$use->save();
$couponReceive = CouponUserRec::query()
->where('id',$use->user_receive_id)
->whereRaw('number >= number_remain+'.$use->number)
->update([
'number_remain' => Db::raw('number_remain+'.$use->number),
'status' => Db::raw('IF(number=number_remain,' . CouponUserRec::STATE_UNUSED . ',' . CouponUserRec::STATE_SOME . ')'),
'update_time' => $time
]);
$clearUseRedis = $this->couponService->clearTodayCouponUsed($use->user_id,$use->system_coupon_id);
}
}
// 退还订单金额到用户微信余额
Db::commit();
return true;
} catch (Exception $e) {
$this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款','exception' => $e->getMessage()]);
Db::rollBack();
return false;
}
}
}

47
app/Service/PayRefundService.php

@ -0,0 +1,47 @@
<?php
namespace App\Service;
use App\Model\OrderMain;
use EasyWeChat\Factory;
use Hyperf\DbConnection\Db;
use App\Constants\ErrorCode;
class PayRefundService implements PayRefundServiceInterface
{
/**
* 微信支付退款
*/
public function wxPayRefund($global_order_id)
{
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
$result = [
'status' => 0,
'msg' => '退款成功'
];
// 查询订单
$orderMain = OrderMain::query()
->select('id','code','order_num','money','state')
->where('global_order_id',$global_order_id)
->where('pay_type',OrderMain::ORDER_PAY_WX)
->where(Db::raw('refund_time is null'))
->first();
if(empty($orderMain)){
return ['status'=>1, 'msg'=>'订单不存在或已退款'];
}
$optional = [];
$result = $app->refund->byOutTradeNumber(
$orderMain->code,
$orderMain->code,
$orderMain->money * 100,
$orderMain->money * 100,
$optional
);
return $result;
}
}

8
app/Service/PayRefundServiceInterface.php

@ -0,0 +1,8 @@
<?php
namespace App\Service;
interface PayRefundServiceInterface
{
public function wxPayRefund($global_order_id);
}
Loading…
Cancel
Save