Browse Source

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

master
Lemon 5 years ago
parent
commit
12fa4fc61d
  1. 2
      .gitignore
  2. 7
      app/Constants/LogLabel.php
  3. 10
      app/Controller/NotifyController.php
  4. 47
      app/Controller/NotifyPayRefundController.php
  5. 35
      app/JsonRpc/OrderService.php
  6. 2
      app/Service/CouponService.php
  7. 81
      app/Service/OrderService.php
  8. 2
      app/Service/PurchaseLimitService.php
  9. 14
      app/Service/WxRefundService.php

2
.gitignore

@ -13,3 +13,5 @@ vendor/
.phpunit*
/watch
.vscode/
config/cert/apiclient_cert_2.pem
config/cert/apiclient_key_2.pem

7
app/Constants/LogLabel.php

@ -57,7 +57,12 @@ class LogLabel extends AbstractConstants
/**
* @Message("Pay refund Log Label")
*/
const PAY_NOTIFY_REFUND = 'notify_refund';
const WX_NOTIFY_REFUND = 'wx_notify_refund';
/**
* @Message("Pay refund Log Label")
*/
const WX_PAY_REFUND = 'wx_pay_refund';
const AUTO_CANCEL_USER_ORDER = 'auto_cancel_user_order_log';

10
app/Controller/NotifyController.php

@ -115,7 +115,7 @@ class NotifyController extends BaseController
$message
);
Db::rollBack();
$fail('Unknown error but FAIL');
return $fail('Unknown error but FAIL');
}
// 查询订单
@ -127,7 +127,7 @@ class NotifyController extends BaseController
->first();
// 订单不存在
if (empty($orderMain)) {
if (empty($orderMain) || $orderMain->state == OrderMain::ORDER_STATE_DELIVERY) {
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
['global_order_id_fail' => $message['out_trade_no']]
@ -222,7 +222,7 @@ class NotifyController extends BaseController
['exception_fail' => $e->getMessage()]
);
Db::rollBack();
$fail('Exception');
return $fail('Exception');
}
});
@ -267,7 +267,7 @@ class NotifyController extends BaseController
$message
);
Db::rollBack();
$fail('Unknown error but FAIL');
return $fail('Unknown error but FAIL');
}
// 查询订单
@ -397,7 +397,7 @@ class NotifyController extends BaseController
['exception_fail' => $e->getMessage()]
);
Db::rollBack();
$fail('Exception');
return $fail('Exception');
}
});

47
app/Controller/NotifyPayRefundController.php

@ -1,12 +1,11 @@
<?php
/**
* 本回调用不到 20200826
*/
namespace App\Controller;
use App\Constants\LogLabel;
use App\Model\OrderMain;
use App\Service\OrderServiceInterface;
use App\Service\FinancialRecordServiceInterface;
use EasyWeChat\Factory;
@ -15,6 +14,7 @@ use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Symfony\Component\HttpFoundation\Request;
use App\Service\PurchaseLimitServiceInterface;
use Hyperf\DbConnection\Db;
class NotifyPayRefundController extends BaseController
{
@ -44,6 +44,10 @@ class NotifyPayRefundController extends BaseController
*/
public function wxPayRefund()
{
$this->log->event(
LogLabel::WX_NOTIFY_REFUND,
'进入回调'
);
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
@ -59,7 +63,10 @@ class NotifyPayRefundController extends BaseController
/* 通知回调,进行业务处理 */
$response = $app->handleRefundedNotify(function ($message, $fail) use ($app) {
$this->log->event(
LogLabel::WX_NOTIFY_REFUND,
$message
);
try {
/* --- 退款失败 --- */
if (
@ -69,41 +76,47 @@ class NotifyPayRefundController extends BaseController
) {
// 错误日志
$this->log->event(
LogLabel::PAY_NOTIFY_REFUND,
LogLabel::WX_NOTIFY_REFUND,
$message
);
$fail('Unknown error but FAIL');
return false;
}
/* --- 退款成功 --- */
$orderMain = OrderMain::select('id','global_order_id','money','user_id')
->where('global_order_id',$message['out_trade_no'])
->where('state',OrderMain::ORDER_STATE_REFUNDED)
->where(Db::raw('refund_time is null'))
->first();
// 退款返还优惠券
$this->couponService->orderRefundCoupon($message['out_trade_no']);
if(!empty($orderMain)){
// 添加退款时间
$orderMain->refund_time = time();
$orderMain->save();
// 退款返还优惠券
$this->couponService->orderRefundCoupons($orderMain->global_order_id);
// 删除特价商品缓存
$this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
// 删除特价商品缓存
$this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
// 添加用户的流水
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,
LogLabel::WX_NOTIFY_REFUND,
['exception_fail' => $e->getMessage()]
);
$fail('Exception');
}
});
// return $this->response
// ->withHeader('Content-Type', 'text/xml')
// ->withStatus(200)
// ->withBody(new SwooleStream($response->getContent()));
return $this->response
->withHeader('Content-Type', 'text/xml')
->withStatus(200)
->withBody(new SwooleStream($response->getContent()));
}
}

35
app/JsonRpc/OrderService.php

@ -69,24 +69,23 @@ class OrderService implements OrderServiceInterface
* 退款成功 state = 9
*/
public function onlineRefund($global_order_id){
Db::beginTransaction();
try{
$result = [
"status" => 200,
"code" => ErrorCode::ORDER_FAILURE,
"result" => [],
"message" => ''
];
return [
"status" => 200,
"code" => 0,
"result" => $this->orderService->onlineRefund($global_order_id),
// 'result' => $global_order_id,
"message" => '退款成功'
];
} catch (\Exception $e){
Db::rollBack();
return [
"status" => 200,
"code" => ErrorCode::ORDER_FAILURE,
"result" => [],
"message" => $e->getMessage()
];
}
$res = $this->orderService->onlineRefund($global_order_id);
if($res['code'] > 0){
$result['result'] = $res;
$result['message'] = '退款失败';
}else{
$result['code'] = 0;
$result['result'] = $res;
$result['message'] = '退款成功';
};
return $result;
}
}

2
app/Service/CouponService.php

@ -294,7 +294,7 @@ class CouponService implements CouponServiceInterface
return $setRes&&$expireRes;
}
/* 删除-优惠券今日使用的缓存
/* 删除-优惠券今日使用的缓存
* @param $userId
* @param $couponId
* @return bool

81
app/Service/OrderService.php

@ -24,6 +24,7 @@ use App\Service\WxRefundServiceInterface;
use App\Service\UserServiceInterface;
use App\Model\Users;
use App\Constants\SsdbKeysPrefix;
use App\Service\PurchaseLimitServiceInterface;
class OrderService implements OrderServiceInterface
{
@ -57,6 +58,12 @@ class OrderService implements OrderServiceInterface
*/
protected $purchaseLimitService;
/**
* @Inject
* @var FinancialRecordServiceInterface
*/
protected $financialService;
/**
* @inheritDoc
*/
@ -746,8 +753,6 @@ class OrderService implements OrderServiceInterface
{
Db::beginTransaction();
try {
$time = time();
// 主订单状态更新
$orderMain = OrderMain::query()
->select('id','global_order_id','state','pay_type','user_id','money')
@ -756,13 +761,20 @@ class OrderService implements OrderServiceInterface
if (empty($orderMain)) {
Db::rollBack();
return false;
return [
'code' => 1,
'msg' =>'查询不到订单'
];
}
$orderMain->state = OrderMain::ORDER_STATE_REFUNDED;
if(!$orderMain->save()){
Db::rollBack();
return false;
return [
'code' => 2,
'msg' =>'更新主订单失败'
];
};
// 子订单状态更新
@ -772,37 +784,68 @@ class OrderService implements OrderServiceInterface
->update(['state' => OrderMain::ORDER_STATE_REFUNDED]);
if(empty($upChild)){
Db::rollBack();
return false;
return [
'code' => 3,
'msg' =>'更新子订单失败'
];
}
if($orderMain->pay_type == OrderMain::ORDER_PAY_WX){
// 微信支付 微信退款
if(!$this->wxRefundService->wxPayRefund($global_order_id)){
$refundRes = $this->wxRefundService->wxPayRefund($orderMain->global_order_id);
var_dump($refundRes);
if(
empty($refundRes)
|| !$refundRes
|| !isset($refundRes['result_code'])
|| $refundRes['result_code'] != 'SUCCESS'
){
Db::rollBack();
return false;
return $refundRes;
};
}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;
// };
if($this->userService->userWallet($orderMain->user_id,$orderMain->money,Users::WALLET_TYPE_INC)){
Db::rollBack();
return [
'code' => 4,
'msg' =>'退款到用户余额失败'
];
};
}
/* --- 退款成功 --- */
$orderMain = $orderMain->fresh();
if(empty($orderMain->refund_time)){
// 添加退款时间
$orderMain->refund_time = time();
$orderMain->save();
// 退款返还优惠券
$this->couponService->orderRefundCoupons($orderMain->global_order_id);
// 返还优惠券
// $this->couponService->orderRefundCoupon($global_order_id);
// 删除特价商品缓存
// $this->orderService->clearTodayGoodPurchase($orderMain->user_id,1);
// 添加用户流水
// $this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
$this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->global_order_id);
// 添加用户的流水
$this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
}
Db::commit();
return true;
} catch (Exception $e) {
return [
'code' => 0,
'msg' =>'退款成功'
];
} catch (\Exception $e) {
$this->log->event(LogLabel::ORDER_LOG, ['msg'=> '订单退款','exception' => $e->getMessage()]);
Db::rollBack();
return false;
return [
'code' => 5,
'msg' => $e->getMessage()
];
}
}

2
app/Service/PurchaseLimitService.php

@ -52,7 +52,9 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
$order_main = OrderMain::where('global_order_id',$global_order_id)
->select('id','user_id')
->first();
$order_id = $order_main->id;
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$order = Order::query()
->where('order_main_id',$order_id)

14
app/Service/WxRefundService.php

@ -5,9 +5,10 @@ namespace App\Service;
use App\Model\OrderMain;
use EasyWeChat\Factory;
use Hyperf\DbConnection\Db;
use App\Constants\ErrorCode;
use App\Commons\Log;
use App\Constants\LogLabel;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\CoroutineHandler;
class WxRefundService implements WxRefundServiceInterface
{
@ -32,13 +33,15 @@ class WxRefundService implements WxRefundServiceInterface
->where('global_order_id',$global_order_id)
->where('pay_type',OrderMain::ORDER_PAY_WX)
->where(Db::raw('refund_time is null'))
->first();
->first()->toArray();
if(empty($orderMain)){
return false;
};
$options = [
'refund_desc' => '',
'notify_url' => config('site_host') . '/wechat/notify/wxpayrefund'
'refund_desc' => '线上订单退款',
// 'notify_url' => config('site_host') . '/wechat/notify/wxpayrefund'
];
$result = $app->refund->byOutTradeNumber(
$orderMain->global_order_id,
@ -47,8 +50,7 @@ class WxRefundService implements WxRefundServiceInterface
$orderMain->money * 100,
$options
);
$this->log->event('wx_pay_refund',$result);
$this->log->event(LogLabel::WX_PAY_REFUND,$result);
return $result;
}
Loading…
Cancel
Save