Browse Source

订单退款--修改完-测试1

master
liangyuyan 5 years ago
parent
commit
472108ba0f
  1. 38
      app/Controller/NotifyPayRefundController.php
  2. 3
      app/Model/FinancialRecord.php
  3. 6
      app/Model/Users.php
  4. 18
      app/Service/FinancialRecordService.php
  5. 23
      app/Service/FinancialRecordServiceInterface.php
  6. 28
      app/Service/OrderService.php
  7. 19
      app/Service/UserService.php
  8. 9
      app/Service/UserServiceInterface.php
  9. 3
      app/Service/WxRefundService.php

38
app/Controller/NotifyPayRefundController.php

@ -6,19 +6,11 @@ use App\Constants\LogLabel;
use App\Model\OrderMain;
use App\Model\Users;
use App\Service\CouponRebateServiceInterface;
use App\Service\DeviceServiceInterface;
use App\Service\FeiePrintServiceInterface;
use App\Service\MiniprogramServiceInterface;
use App\Service\MqttServiceInterface;
use App\Service\OrderServiceInterface;
use App\Service\SeparateAccountsServiceInterface;
use App\Service\UserServiceInterface;
use App\Service\FinancialRecordServiceInterface;
use EasyWeChat\Factory;
use Hyperf\DbConnection\Db;
use Hyperf\Guzzle\CoroutineHandler;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Symfony\Component\HttpFoundation\Request;
@ -30,15 +22,9 @@ class NotifyPayRefundController extends BaseController
/**
* @Inject
* @var UserServiceInterface
* @var FinancialRecordServiceInterface
*/
protected $userService;
/**
* @Inject
* @var CouponRebateServiceInterface
*/
protected $couponRebateService;
protected $financialService;
/**
* @Inject
@ -83,13 +69,17 @@ class NotifyPayRefundController extends BaseController
}
/* --- 退款成功 --- */
// 退款返还优惠券
$this->couponService->orderRefundCoupon($message['out_trade_no']);
// 退款返还优惠券
$this->couponService->orderRefundCoupon($message['out_trade_no']);
// 添加用户的流水
$orderMain = OrderMain::select('id','global_order_id')->where('code',$message['out_trade_no'])->first();
} catch (Exception $e) {
$orderMain = OrderMain::select('id','global_order_id','money','user_id')
->where('global_order_id',$message['out_trade_no'])
->where('state',OrderMain::ORDER_STATE_REFUNDED)
->first();
if(!empty($orderMain)){
$this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
}
} catch (\Exception $e) {
$this->log->event(
LogLabel::PAY_NOTIFY_REFUND,

3
app/Model/FinancialRecord.php

@ -59,8 +59,7 @@ class FinancialRecord extends Model
const MONEY_TYPE_STORE_FIRST_ORDER = 5; // 商户当日首单奖励
const MONEY_TYPE_STORE_OL_ORDER_COMP = 6; // 商户线上订单完成收入
const MONEY_TYPE_STORE_OFL_ORDER_COMP = 7; // 商户线下订单完成收入
const MONEY_TYPE_USER_OL_ORDER_REFUND = 8; // 商户线下订单完成收入
const MONEY_TYPE_USER_OL_ORDER_REFUND = 8; // 用户线上订单退款
const MONEY_TYPE_USER_OFL_ORDER = 100; // 用户线下支付订单
const MONEY_TYPE_USER_OL_ORDER = 101; // 用户线上支付订单

6
app/Model/Users.php

@ -4,6 +4,12 @@ namespace App\Model;
class Users extends Model
{
// 用户账户(钱包)操作 增
const WALLET_TYPE_INC = 1;
// 用户账户(钱包)操作 减
const WALLET_TYPE_DEC = 2;
protected $table = 'ims_cjdc_user';
public $timestamps = false;
}

18
app/Service/FinancialRecordService.php

@ -260,4 +260,22 @@ class FinancialRecordService implements FinancialRecordServiceInterface
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
}
/**
* @inheritDoc
* 订单退款(线上)
*/
public function userByOLOrderRefund(
$user_id,
$source_id,
$money,
$user_type = FinancialRecord::USER_TYPE_USER,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_USER_OL_ORDER_REFUND,
$desc = '线上订单退款',
$comment = '线上订单退款到微信'
)
{
$this->recordAll($user_id, $source_id, $money, $user_type, $source_type, $money_type, $desc, $comment);
}
}

23
app/Service/FinancialRecordServiceInterface.php

@ -226,4 +226,27 @@ interface FinancialRecordServiceInterface
$comment=''
);
/**
* 线上订单退款流水
* @param $user_id
* @param $source_id
* @param $money
* @param int $user_type
* @param int $source_type
* @param int $money_type
* @param string $desc
* @param string $comment
* @return mixed
*/
public function userByOLOrderRefund(
$user_id,
$source_id,
$money,
$user_type = FinancialRecord::USER_TYPE_STORE,
$source_type = FinancialRecord::SOURCE_TYPE_ORDER,
$money_type = FinancialRecord::MONEY_TYPE_STORE_OFL_ORDER_COMP,
$desc='线上订单退款',
$comment=''
);
}

28
app/Service/OrderService.php

@ -20,6 +20,8 @@ use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Di\Annotation\Inject;
use App\Service\WxRefundServiceInterface;
use App\Service\UserServiceInterface;
use App\Model\Users;
class OrderService implements OrderServiceInterface
{
@ -41,6 +43,12 @@ class OrderService implements OrderServiceInterface
*/
protected $wxRefundService;
/**
* @Inject
* @var UserServiceInterface
*/
protected $userService;
/**
* @inheritDoc
*/
@ -731,7 +739,7 @@ class OrderService implements OrderServiceInterface
$time = time();
// 主订单状态更新
$orderMain = OrderMain::query()
->select('id','global_order_id','state','pay_type')
->select('id','global_order_id','state','pay_type','user_id','money')
->where(['global_order_id' => $global_order_id, 'state' => OrderMain::ORDER_STATE_REFUNDING])
->first();
@ -741,7 +749,10 @@ class OrderService implements OrderServiceInterface
}
$orderMain->state = OrderMain::ORDER_STATE_REFUNDED;
$upOrderMain = $orderMain->save();
if(!$orderMain->save()){
Db::rollBack();
return false;
};
// 子订单状态更新
$upChild = Order::query()
@ -755,14 +766,21 @@ class OrderService implements OrderServiceInterface
if($orderMain->pay_type == OrderMain::ORDER_PAY_WX){
// 微信支付 微信退款
$refundRes = $this->wxRefundService->wxPayRefund($global_order_id);
if($this->wxRefundService->wxPayRefund($global_order_id)){
Db::rollBack();
return false;
};
}else if($orderMain->pay_type == OrderMain::ORDER_PAY_BALANCE){
// 余额支付 退款到用户余额
if($this->userService->userWallet($orderMain->user_id,$orderMain->money,Users::WALLET_TYPE_INC)){
Db::rollBack();
return false;
};
// 返还优惠券
$this->couponService->orderRefundCoupon($global_order_id);
// 添加用户流水
$this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
}
Db::commit();
@ -813,7 +831,7 @@ class OrderService implements OrderServiceInterface
return true;
} catch (Exception $e) {
$this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款失败时处理状态9->8','exception' => $e->getMessage()]);
$this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款失败时处理状态9->8 ','exception' => $e->getMessage()]);
Db::rollBack();
return false;
}

19
app/Service/UserService.php

@ -76,4 +76,23 @@ class UserService implements UserServiceInterface
->where('id', '!=', $current_order_id)
->exists();
}
/**
* 用户账户(钱包)
* @inheritDoc
* $type 1 2
*/
public function userWallet($userId,$money,$type)
{
$user = Users::select('id','wallet')->where('id',$userId)->lockForUpdate()->first();
$res = false;
if(!empty($user) && is_numeric($money) && $money > 0){
if($type == Users::WALLET_TYPE_INC){
$res = $user->increment('wallet',$money);
}else if($type == Users::WALLET_TYPE_DEC){
$res = $user->decrement('wallet',$money);
}
}
return $res;
}
}

9
app/Service/UserServiceInterface.php

@ -26,4 +26,13 @@ interface UserServiceInterface
*/
public function isStoreFirstOrderToday($user_id, $store_id, $current_order_id, $limit_amount = 3);
/**
* 用户账户(钱包)
* @inheritDoc
* @param $userId
* @param $money
* @param $type 1 2
* @return mixed
*/
public function userWallet($userId,$money,$type);
}

3
app/Service/WxRefundService.php

@ -27,8 +27,7 @@ class WxRefundService implements WxRefundServiceInterface
->first();
if(empty($orderMain)){
return false;
}
;
};
$result = $app->refund->byOutTradeNumber(
$orderMain->global_order_id,
$orderMain->global_order_id,

Loading…
Cancel
Save