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.
|
|
<?php
namespace App\Controller;
use App\Constants\LogLabel;
use App\Model\OrderMain;
use App\Service\OrderServiceInterface;use App\Service\FinancialRecordServiceInterface;use EasyWeChat\Factory;use Hyperf\Guzzle\CoroutineHandler;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{
const AWARD_LIMIT_AMOUNT = 3;
/** * @Inject * @var FinancialRecordServiceInterface */ protected $financialService;
/** * @Inject * @var OrderServiceInterface */ protected $orderService;
/** * @Inject * @var PurchaseLimitServiceInterface */ protected $purchaseLimitService;
/** * 微信退款回调 */ public function wxPayRefund() { $this->log->event( LogLabel::WX_NOTIFY_REFUND, '进入回调' ); $config = config('wxpay'); $app = Factory::payment($config); $app['guzzle_handler'] = CoroutineHandler::class;
$get = $this->request->getQueryParams(); $post = $this->request->getParsedBody(); $cookie = $this->request->getCookieParams(); $files = $this->request->getUploadedFiles(); $server = $this->request->getServerParams(); $xml = $this->request->getBody()->getContents();
$app['request'] = new Request($get,$post,[],$cookie,$files,$server,$xml);
/* 通知回调,进行业务处理 */ $response = $app->handleRefundedNotify(function ($message, $fail) use ($app) { $this->log->event( LogLabel::WX_NOTIFY_REFUND, $message ); try { /* --- 退款失败 --- */ if ( empty($message) || !isset($message['result_code']) || $message['result_code'] != 'SUCCESS' ) { // 错误日志
$this->log->event( 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();
if(!empty($orderMain)){ // 添加退款时间
$orderMain->refund_time = time(); $orderMain->save();
// 退款返还优惠券
$this->couponService->orderRefundCoupons($orderMain->global_order_id); // 删除特价商品缓存
$this->purchaseLimitService->delSsdbPurchaseRecord($orderMain->id);
// 添加用户的流水
$this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money); } } catch (\Exception $e) {
$this->log->event( 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())); }}
|