From 30a2b6aee57e1a7746722fe63890f9bea69d9605 Mon Sep 17 00:00:00 2001 From: xiaochong0302 Date: Tue, 12 May 2020 16:06:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Tasks/CloseTradeTask.php | 4 +- app/Console/Tasks/SyncChapterCounterTask.php | 36 +++++-- app/Console/Tasks/SyncCommentCounterTask.php | 29 ++++-- app/Console/Tasks/SyncConsultCounterTask.php | 26 +++-- app/Console/Tasks/SyncCourseCounterTask.php | 37 +++++-- app/Console/Tasks/SyncReviewCounterTask.php | 26 +++-- app/Http/Admin/Views/order/show.volt | 2 +- app/Http/Admin/Views/page/list.volt | 40 +------- .../Admin/Views/partials/media_uploader.volt | 2 +- .../Admin/Views/setting/pay_alipay_test.volt | 17 ---- .../Admin/Views/setting/pay_wxpay_test.volt | 17 ---- app/Http/Admin/Views/trade/show.volt | 12 +-- app/Library/Http/Request.php | 12 +-- app/Services/Pay/Alipay.php | 96 ++++--------------- app/Services/Pay/AlipayGateway.php | 69 +++++++++++++ app/Services/Pay/Wxpay.php | 83 ++++------------ app/Services/Pay/WxpayGateway.php | 63 ++++++++++++ public/static/admin/js/common.js | 1 + 18 files changed, 298 insertions(+), 274 deletions(-) create mode 100644 app/Services/Pay/AlipayGateway.php create mode 100644 app/Services/Pay/WxpayGateway.php diff --git a/app/Console/Tasks/CloseTradeTask.php b/app/Console/Tasks/CloseTradeTask.php index a210bb5f..7d4a3bfb 100644 --- a/app/Console/Tasks/CloseTradeTask.php +++ b/app/Console/Tasks/CloseTradeTask.php @@ -34,7 +34,7 @@ class CloseTradeTask extends Task * * @param TradeModel $trade */ - protected function handleAlipayTrade($trade) + protected function handleAlipayTrade(TradeModel $trade) { $allowClosed = true; @@ -66,7 +66,7 @@ class CloseTradeTask extends Task * * @param TradeModel $trade */ - protected function handleWxpayTrade($trade) + protected function handleWxpayTrade(TradeModel $trade) { $allowClosed = true; diff --git a/app/Console/Tasks/SyncChapterCounterTask.php b/app/Console/Tasks/SyncChapterCounterTask.php index 61e79eba..97064fe8 100644 --- a/app/Console/Tasks/SyncChapterCounterTask.php +++ b/app/Console/Tasks/SyncChapterCounterTask.php @@ -46,25 +46,43 @@ class SyncChapterCounterTask extends Task return; } + $counterCache = new ChapterCounterCache(); + $chapterCache = new ChapterCache(); - $chapterCounterCache = new ChapterCounterCache(); + $hour = date('H'); foreach ($chapters as $chapter) { - $counter = $chapterCounterCache->get($chapter->id); + if ($hour % 3 == 0) { - if ($counter) { - - $chapter->user_count = $counter['user_count']; - $chapter->lesson_count = $counter['lesson_count']; - $chapter->comment_count = $counter['comment_count']; - $chapter->agree_count = $counter['agree_count']; - $chapter->oppose_count = $counter['oppose_count']; + $chapter->user_count = $chapterRepo->countUsers($chapter->id); + $chapter->lesson_count = $chapterRepo->countLessons($chapter->id); + $chapter->comment_count = $chapterRepo->countComments($chapter->id); + $chapter->agree_count = $chapterRepo->countAgrees($chapter->id); + $chapter->oppose_count = $chapterRepo->countOpposes($chapter->id); $chapter->update(); + $counterCache->rebuild($chapter->id); $chapterCache->rebuild($chapter->id); + + } else { + + $counter = $counterCache->get($chapter->id); + + if ($counter) { + + $chapter->user_count = $counter['user_count']; + $chapter->lesson_count = $counter['lesson_count']; + $chapter->comment_count = $counter['comment_count']; + $chapter->agree_count = $counter['agree_count']; + $chapter->oppose_count = $counter['oppose_count']; + + $chapter->update(); + + $chapterCache->rebuild($chapter->id); + } } } diff --git a/app/Console/Tasks/SyncCommentCounterTask.php b/app/Console/Tasks/SyncCommentCounterTask.php index dedc4b52..2d558e95 100644 --- a/app/Console/Tasks/SyncCommentCounterTask.php +++ b/app/Console/Tasks/SyncCommentCounterTask.php @@ -45,19 +45,34 @@ class SyncCommentCounterTask extends Task return; } - $cache = new CommentCounterCache(); + $counterCache = new CommentCounterCache(); + + $hour = date('H'); foreach ($comments as $comment) { - $counter = $cache->get($comment->id); + if ($hour % 3 == 0) { - if ($counter) { - - $comment->reply_count = $counter['reply_count']; - $comment->agree_count = $counter['agree_count']; - $comment->oppose_count = $counter['oppose_count']; + $comment->reply_count = $commentRepo->countReplies($comment->id); + $comment->agree_count = $commentRepo->countAgrees($comment->id); + $comment->oppose_count = $commentRepo->countOpposes($comment->id); $comment->update(); + + $counterCache->rebuild($comment->id); + + } else { + + $counter = $counterCache->get($comment->id); + + if ($counter) { + + $comment->reply_count = $counter['reply_count']; + $comment->agree_count = $counter['agree_count']; + $comment->oppose_count = $counter['oppose_count']; + + $comment->update(); + } } } diff --git a/app/Console/Tasks/SyncConsultCounterTask.php b/app/Console/Tasks/SyncConsultCounterTask.php index 1f9caf6f..afc5d021 100644 --- a/app/Console/Tasks/SyncConsultCounterTask.php +++ b/app/Console/Tasks/SyncConsultCounterTask.php @@ -45,18 +45,32 @@ class SyncConsultCounterTask extends Task return; } - $cache = new ConsultCounterCache(); + $counterCache = new ConsultCounterCache(); + + $hour = date('H'); foreach ($consults as $consult) { - $counter = $cache->get($consult->id); + if ($hour % 3 == 0) { - if ($counter) { - - $consult->agree_count = $counter['agree_count']; - $consult->oppose_count = $counter['oppose_count']; + $consult->agree_count = $consultRepo->countAgrees($consult->id); + $consult->oppose_count = $consultRepo->countOpposes($consult->id); $consult->update(); + + $counterCache->rebuild($consult->id); + + } else { + + $counter = $counterCache->get($consult->id); + + if ($counter) { + + $consult->agree_count = $counter['agree_count']; + $consult->oppose_count = $counter['oppose_count']; + + $consult->update(); + } } } diff --git a/app/Console/Tasks/SyncCourseCounterTask.php b/app/Console/Tasks/SyncCourseCounterTask.php index 9cda6066..6202efba 100644 --- a/app/Console/Tasks/SyncCourseCounterTask.php +++ b/app/Console/Tasks/SyncCourseCounterTask.php @@ -46,26 +46,43 @@ class SyncCourseCounterTask extends Task return; } - $courseCounterCache = new CourseCounterCache(); + $counterCache = new CourseCounterCache(); $courseCache = new CourseCache(); + $hour = date('H'); + foreach ($courses as $course) { - $counter = $courseCounterCache->get($course->id); + if ($hour % 3 == 0) { - if ($counter) { - - $course->user_count = $counter['user_count']; - $course->lesson_count = $counter['lesson_count']; - $course->comment_count = $counter['comment_count']; - $course->consult_count = $counter['consult_count']; - $course->review_count = $counter['review_count']; - $course->favorite_count = $counter['favorite_count']; + $course->user_count = $courseRepo->countUsers($course->id); + $course->lesson_count = $courseRepo->countLessons($course->id); + $course->comment_count = $courseRepo->countComments($course->id); + $course->consult_count = $courseRepo->countConsults($course->id); + $course->review_count = $courseRepo->countReviews($course->id); + $course->favorite_count = $courseRepo->countFavorites($course->id); $course->update(); + $counterCache->rebuild($course->id); $courseCache->rebuild($course->id); + + } else { + + $counter = $counterCache->get($course->id); + + if ($counter) { + + $course->user_count = $counter['user_count']; + $course->lesson_count = $counter['lesson_count']; + $course->comment_count = $counter['comment_count']; + $course->consult_count = $counter['consult_count']; + $course->review_count = $counter['review_count']; + $course->favorite_count = $counter['favorite_count']; + + $course->update(); + } } } diff --git a/app/Console/Tasks/SyncReviewCounterTask.php b/app/Console/Tasks/SyncReviewCounterTask.php index 1196d1f6..ead634ba 100644 --- a/app/Console/Tasks/SyncReviewCounterTask.php +++ b/app/Console/Tasks/SyncReviewCounterTask.php @@ -45,18 +45,32 @@ class SyncReviewCounterTask extends Task return; } - $cache = new ReviewCounterCache(); + $counterCache = new ReviewCounterCache(); + + $hour = date('H'); foreach ($reviews as $review) { - $counter = $cache->get($review->id); + if ($hour % 3 == 0) { - if ($counter) { - - $review->agree_count = $counter['agree_count']; - $review->oppose_count = $counter['oppose_count']; + $review->agree_count = $reviewRepo->countAgrees($review->id); + $review->oppose_count = $reviewRepo->countOpposes($review->id); $review->update(); + + $counterCache->rebuild($review->id); + + } else { + + $counter = $counterCache->get($review->id); + + if ($counter) { + + $review->agree_count = $counter['agree_count']; + $review->oppose_count = $counter['oppose_count']; + + $review->update(); + } } } diff --git a/app/Http/Admin/Views/order/show.volt b/app/Http/Admin/Views/order/show.volt index cbcdb8a9..cfbd82c7 100644 --- a/app/Http/Admin/Views/order/show.volt +++ b/app/Http/Admin/Views/order/show.volt @@ -30,7 +30,7 @@
{% if order.status == 'pending' %} - + {% endif %}
diff --git a/app/Http/Admin/Views/page/list.volt b/app/Http/Admin/Views/page/list.volt index db80a6c2..79069c5a 100644 --- a/app/Http/Admin/Views/page/list.volt +++ b/app/Http/Admin/Views/page/list.volt @@ -53,42 +53,4 @@ -{{ partial('partials/pager') }} - - \ No newline at end of file +{{ partial('partials/pager') }} \ No newline at end of file diff --git a/app/Http/Admin/Views/partials/media_uploader.volt b/app/Http/Admin/Views/partials/media_uploader.volt index 3d6caafe..90aefa51 100644 --- a/app/Http/Admin/Views/partials/media_uploader.volt +++ b/app/Http/Admin/Views/partials/media_uploader.volt @@ -39,7 +39,7 @@ uploader.on('media_progress', function (info) { if (!isNaN(info.percent)) { - var percent = parseInt(100 * info.percent); + var percent = Math.ceil(100 * info.percent); element.progress('upload-progress', percent + '%'); } }); diff --git a/app/Http/Admin/Views/setting/pay_alipay_test.volt b/app/Http/Admin/Views/setting/pay_alipay_test.volt index 6f1a19bc..a7ba81e3 100644 --- a/app/Http/Admin/Views/setting/pay_alipay_test.volt +++ b/app/Http/Admin/Views/setting/pay_alipay_test.volt @@ -33,8 +33,6 @@ layui.use(['jquery'], function () { var $ = layui.jquery; - - var loopTime = 0; var tradeSn = $('input[name=trade_sn]').val(); var interval = setInterval(function () { $.ajax({ @@ -54,21 +52,6 @@ clearInterval(interval); } }); - loopTime += 5; - if (loopTime >= 300) { - $.ajax({ - type: 'POST', - url: '/admin/test/alipay/cancel', - data: {trade_sn: tradeSn}, - success: function (res) { - }, - error: function (xhr) { - } - }); - $('#error-tips').removeClass('layui-hide'); - $('#qrcode').addClass('layui-hide'); - clearInterval(interval); - } }, 5000); }); diff --git a/app/Http/Admin/Views/setting/pay_wxpay_test.volt b/app/Http/Admin/Views/setting/pay_wxpay_test.volt index 612b4fa2..5ae7e533 100644 --- a/app/Http/Admin/Views/setting/pay_wxpay_test.volt +++ b/app/Http/Admin/Views/setting/pay_wxpay_test.volt @@ -33,8 +33,6 @@ layui.use(['jquery'], function () { var $ = layui.jquery; - - var loopTime = 0; var tradeSn = $('input[name=trade_sn]').val(); var interval = setInterval(function () { $.ajax({ @@ -54,21 +52,6 @@ clearInterval(interval); } }); - loopTime += 5; - if (loopTime >= 300) { - $.ajax({ - type: 'POST', - url: '/admin/test/wxpay/cancel', - data: {trade_sn: tradeSn}, - success: function (res) { - }, - error: function (xhr) { - } - }); - $('#error-tips').removeClass('layui-hide'); - $('#qrcode').addClass('layui-hide'); - clearInterval(interval); - } }, 5000); }); diff --git a/app/Http/Admin/Views/trade/show.volt b/app/Http/Admin/Views/trade/show.volt index 22fe887b..c0d087b7 100644 --- a/app/Http/Admin/Views/trade/show.volt +++ b/app/Http/Admin/Views/trade/show.volt @@ -27,10 +27,10 @@
{% if trade.status == 'pending' %} - + {% endif %} {% if trade.status == 'finished' %} - + {% endif %}
@@ -110,12 +110,12 @@ var $ = layui.jquery; $('.kg-close').on('click', function () { - var tradeId = $(this).attr('trade-id'); + var url = $(this).attr('data-url'); var tips = '确定要关闭交易吗?'; layer.confirm(tips, function () { $.ajax({ type: 'POST', - url: '/admin/trade/' + tradeId + '/close', + url: url, finished: function (res) { layer.msg(res.msg, {icon: 1}); setTimeout(function () { @@ -133,12 +133,12 @@ }); $('.kg-refund').on('click', function () { - var tradeId = $(this).attr('trade-id'); + var url = $(this).attr('data-url'); var tips = '确定要申请退款吗?'; layer.confirm(tips, function () { $.ajax({ type: 'POST', - url: '/admin/trade/' + tradeId + '/refund', + url: url, success: function (res) { layer.msg(res.msg, {icon: 1}); setTimeout(function () { diff --git a/app/Library/Http/Request.php b/app/Library/Http/Request.php index b626b6ab..6bc88c92 100644 --- a/app/Library/Http/Request.php +++ b/app/Library/Http/Request.php @@ -2,8 +2,6 @@ namespace App\Library\Http; -use App\Exceptions\BadRequest; - class Request extends \Phalcon\Http\Request { @@ -43,13 +41,13 @@ class Request extends \Phalcon\Http\Request { $contentType = $this->getContentType(); - if (stripos($contentType, 'form')) { - return parent::getPost($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive); - } elseif (stripos($contentType, 'json')) { - return $this->getPut($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive); + if (stripos($contentType, 'json')) { + $data = $this->getPut($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive); } else { - throw new BadRequest('sys.invalid_content_type'); + $data = parent::getPost($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive); } + + return $data; } } \ No newline at end of file diff --git a/app/Services/Pay/Alipay.php b/app/Services/Pay/Alipay.php index 55670e32..f9dd7145 100644 --- a/app/Services/Pay/Alipay.php +++ b/app/Services/Pay/Alipay.php @@ -6,33 +6,23 @@ use App\Models\Refund as RefundModel; use App\Models\Trade as TradeModel; use App\Repos\Trade as TradeRepo; use App\Services\Pay as PayService; -use Symfony\Component\HttpFoundation\Response as HttpResponse; -use Yansongda\Pay\Gateways\Alipay as AlipayGateway; +use Symfony\Component\HttpFoundation\Response; use Yansongda\Pay\Log; -use Yansongda\Pay\Pay; use Yansongda\Supports\Collection; class Alipay extends PayService { /** - * @var array + * @var \Yansongda\Pay\Gateways\Alipay */ - protected $settings; + protected $gateway; - public function __construct() + public function __construct($gateway = null) { - $this->settings = $this->getSectionSettings('pay.alipay'); - } + $gateway = $gateway instanceof AlipayGateway ? $gateway : new AlipayGateway(); - public function setReturnUrl($returnUrl) - { - $this->settings['return_url'] = $returnUrl; - } - - public function setNotifyUrl($notifyUrl) - { - $this->settings['notify_url'] = $notifyUrl; + $this->gateway = $gateway->getInstance(); } /** @@ -43,11 +33,9 @@ class Alipay extends PayService */ public function scan(TradeModel $trade) { - $gateway = $this->getGateway(); - try { - $response = $gateway->scan([ + $response = $this->gateway->scan([ 'out_trade_no' => $trade->sn, 'total_amount' => $trade->amount, 'subject' => $trade->subject, @@ -72,15 +60,13 @@ class Alipay extends PayService * 移动端支付 * * @param TradeModel $trade - * @return HttpResponse|bool + * @return Response|bool */ public function wap(TradeModel $trade) { - $gateway = $this->getGateway(); - try { - return $gateway->wap([ + return $this->gateway->wap([ 'out_trade_no' => $trade->sn, 'total_amount' => $trade->amount, 'subject' => $trade->subject, @@ -101,15 +87,13 @@ class Alipay extends PayService /** * 异步通知 - * @return HttpResponse|bool + * @return Response|bool */ public function notify() { - $gateway = $this->getGateway(); - try { - $data = $gateway->verify(); + $data = $this->gateway->verify(); Log::debug('Alipay Verify Data', $data->all()); @@ -127,17 +111,11 @@ class Alipay extends PayService return false; } - if ($data->app_id != $this->settings['app_id']) { - return false; - } - $tradeRepo = new TradeRepo(); $trade = $tradeRepo->findBySn($data->out_trade_no); - if (!$trade) { - return false; - } + if (!$trade) return false; if ($data->total_amount != $trade->amount) { return false; @@ -151,7 +129,7 @@ class Alipay extends PayService $this->eventsManager->fire('pay:afterPay', $this, $trade); - return $gateway->success(); + return $this->gateway->success(); } /** @@ -163,13 +141,11 @@ class Alipay extends PayService */ public function find($outTradeNo, $type = 'wap') { - $gateway = $this->getGateway(); - try { $order = ['out_trade_no' => $outTradeNo]; - $result = $gateway->find($order, $type); + $result = $this->gateway->find($order, $type); } catch (\Exception $e) { @@ -192,11 +168,9 @@ class Alipay extends PayService */ public function close($outTradeNo) { - $gateway = $this->getGateway(); - try { - $response = $gateway->close(['out_trade_no' => $outTradeNo]); + $response = $this->gateway->close(['out_trade_no' => $outTradeNo]); $result = $response->code == '10000'; @@ -221,11 +195,9 @@ class Alipay extends PayService */ public function cancel($outTradeNo) { - $gateway = $this->getGateway(); - try { - $response = $gateway->cancel(['out_trade_no' => $outTradeNo]); + $response = $this->gateway->cancel(['out_trade_no' => $outTradeNo]); $result = $response->code == '10000'; @@ -250,15 +222,13 @@ class Alipay extends PayService */ public function refund(RefundModel $refund) { - $gateway = $this->getGateway(); - try { $tradeRepo = new TradeRepo(); $trade = $tradeRepo->findById($refund->trade_id); - $response = $gateway->refund([ + $response = $this->gateway->refund([ 'out_trade_no' => $trade->sn, 'out_request_no' => $refund->sn, 'refund_amount' => $refund->amount, @@ -279,36 +249,4 @@ class Alipay extends PayService return $result; } - /** - * 获取 Gateway - * - * @return AlipayGateway - */ - public function getGateway() - { - $config = $this->getDI()->get('config'); - - $level = $config->env == ENV_DEV ? 'debug' : 'info'; - - $payConfig = [ - 'app_id' => $this->settings['app_id'], - 'ali_public_key' => $this->settings['public_key'], - 'private_key' => $this->settings['private_key'], - 'return_url' => $this->settings['return_url'], - 'notify_url' => $this->settings['notify_url'], - 'log' => [ - 'file' => log_path('alipay.log'), - 'level' => $level, - 'type' => 'daily', - 'max_file' => 30, - ], - ]; - - if ($config->env == ENV_DEV) { - $payConfig['mode'] = 'dev'; - } - - return Pay::alipay($payConfig); - } - } diff --git a/app/Services/Pay/AlipayGateway.php b/app/Services/Pay/AlipayGateway.php new file mode 100644 index 00000000..a23d5f84 --- /dev/null +++ b/app/Services/Pay/AlipayGateway.php @@ -0,0 +1,69 @@ +getSectionSettings('pay.alipay'); + + $this->settings = array_merge($defaults, $options); + } + + public function getSettings() + { + return $this->settings; + } + + public function setReturnUrl($returnUrl) + { + $this->settings['return_url'] = $returnUrl; + } + + public function setNotifyUrl($notifyUrl) + { + $this->settings['notify_url'] = $notifyUrl; + } + + /** + * @return Alipay + */ + public function getInstance() + { + $config = $this->getDI()->get('config'); + + $level = $config->env == ENV_DEV ? 'debug' : 'info'; + + $payConfig = [ + 'app_id' => $this->settings['app_id'], + 'ali_public_key' => $this->settings['public_key'], + 'private_key' => $this->settings['private_key'], + 'return_url' => $this->settings['return_url'], + 'notify_url' => $this->settings['notify_url'], + 'log' => [ + 'file' => log_path('alipay.log'), + 'level' => $level, + 'type' => 'daily', + 'max_file' => 30, + ], + ]; + + if ($config->env == ENV_DEV) { + $payConfig['mode'] = 'dev'; + } + + return Pay::alipay($payConfig); + } + +} diff --git a/app/Services/Pay/Wxpay.php b/app/Services/Pay/Wxpay.php index a4d359da..45f76307 100644 --- a/app/Services/Pay/Wxpay.php +++ b/app/Services/Pay/Wxpay.php @@ -6,28 +6,24 @@ use App\Models\Refund as RefundModel; use App\Models\Trade as TradeModel; use App\Repos\Trade as TradeRepo; use App\Services\Pay as PayService; -use Symfony\Component\HttpFoundation\Response as HttpResponse; -use Yansongda\Pay\Gateways\Wechat as WechatGateway; +use Symfony\Component\HttpFoundation\Response; +use Yansongda\Pay\Gateways\Wechat; use Yansongda\Pay\Log; -use Yansongda\Pay\Pay; use Yansongda\Supports\Collection; class Wxpay extends PayService { /** - * @var array + * @var Wechat */ - protected $settings; + protected $gateway; - public function __construct() + public function __construct($gateway = null) { - $this->settings = $this->getSectionSettings('pay.wxpay'); - } + $gateway = $gateway instanceof WxpayGateway ? $gateway : new WxpayGateway(); - public function setNotifyUrl($notifyUrl) - { - $this->settings['notify_url'] = $notifyUrl; + $this->gateway = $gateway->getInstance(); } /** @@ -38,11 +34,9 @@ class Wxpay extends PayService */ public function scan(TradeModel $trade) { - $gateway = $this->getGateway(); - try { - $response = $gateway->scan([ + $response = $this->gateway->scan([ 'out_trade_no' => $trade->sn, 'total_fee' => 100 * $trade->amount, 'body' => $trade->subject, @@ -67,15 +61,13 @@ class Wxpay extends PayService * 移动端支付 * * @param TradeModel $trade - * @return HttpResponse|bool + * @return Response|bool */ public function wap(TradeModel $trade) { - $gateway = $this->getGateway(); - try { - return $gateway->wap([ + return $this->gateway->wap([ 'out_trade_no' => $trade->sn, 'total_fee' => 100 * $trade->amount, 'body' => $trade->subject, @@ -97,15 +89,13 @@ class Wxpay extends PayService /** * 异步通知 * - * @return HttpResponse|bool + * @return Response|bool */ public function notify() { - $gateway = $this->getGateway(); - try { - $data = $gateway->verify(); + $data = $this->gateway->verify(); Log::debug('Wxpay Verify Data', $data->all()); @@ -123,10 +113,6 @@ class Wxpay extends PayService return false; } - if ($data->mch_id != $this->settings['mch_id']) { - return false; - } - $tradeRepo = new TradeRepo(); $trade = $tradeRepo->findBySn($data->out_trade_no); @@ -145,7 +131,7 @@ class Wxpay extends PayService $this->eventsManager->fire('pay:afterPay', $this, $trade); - return $gateway->success(); + return $this->gateway->success(); } /** @@ -157,13 +143,11 @@ class Wxpay extends PayService */ public function find($outTradeNo, $type = 'wap') { - $gateway = $this->getGateway(); - try { $order = ['out_trade_no' => $outTradeNo]; - $result = $gateway->find($order, $type); + $result = $this->gateway->find($order, $type); } catch (\Exception $e) { @@ -186,11 +170,9 @@ class Wxpay extends PayService */ public function close($outTradeNo) { - $gateway = $this->getGateway(); - try { - $response = $gateway->close(['out_trade_no' => $outTradeNo]); + $response = $this->gateway->close(['out_trade_no' => $outTradeNo]); $result = $response->result_code == 'SUCCESS'; @@ -226,15 +208,13 @@ class Wxpay extends PayService */ public function refund(RefundModel $refund) { - $gateway = $this->getGateway(); - try { $tradeRepo = new TradeRepo(); $trade = $tradeRepo->findById($refund->trade_id); - $response = $gateway->refund([ + $response = $this->gateway->refund([ 'out_trade_no' => $trade->sn, 'out_refund_no' => $refund->sn, 'total_fee' => 100 * $trade->amount, @@ -256,35 +236,4 @@ class Wxpay extends PayService return $result; } - /** - * 获取 Gateway - * - * @return WechatGateway - */ - public function getGateway() - { - $config = $this->getDI()->get('config'); - - $level = $config->env == ENV_DEV ? 'debug' : 'info'; - - $payConfig = [ - 'app_id' => $this->settings['app_id'], - 'mch_id' => $this->settings['mch_id'], - 'key' => $this->settings['key'], - 'notify_url' => $this->settings['notify_url'], - 'log' => [ - 'file' => log_path('wxpay.log'), - 'level' => $level, - 'type' => 'daily', - 'max_file' => 30, - ], - ]; - - if ($config->env == ENV_DEV) { - $payConfig['mode'] = 'dev'; - } - - return Pay::wechat($payConfig); - } - } diff --git a/app/Services/Pay/WxpayGateway.php b/app/Services/Pay/WxpayGateway.php new file mode 100644 index 00000000..3ecfa1c4 --- /dev/null +++ b/app/Services/Pay/WxpayGateway.php @@ -0,0 +1,63 @@ +getSectionSettings('pay.wxpay'); + + $this->settings = array_merge($defaults, $options); + } + + public function getSettings() + { + return $this->settings; + } + + public function setNotifyUrl($notifyUrl) + { + $this->settings['notify_url'] = $notifyUrl; + } + + /** + * @return Wechat + */ + public function getInstance() + { + $config = $this->getDI()->get('config'); + + $level = $config->env == ENV_DEV ? 'debug' : 'info'; + + $payConfig = [ + 'app_id' => $this->settings['app_id'], + 'mch_id' => $this->settings['mch_id'], + 'key' => $this->settings['key'], + 'notify_url' => $this->settings['notify_url'], + 'log' => [ + 'file' => log_path('wxpay.log'), + 'level' => $level, + 'type' => 'daily', + 'max_file' => 30, + ], + ]; + + if ($config->env == ENV_DEV) { + $payConfig['mode'] = 'dev'; + } + + return Pay::wechat($payConfig); + } + +} diff --git a/public/static/admin/js/common.js b/public/static/admin/js/common.js index f7a47a0b..85335258 100644 --- a/public/static/admin/js/common.js +++ b/public/static/admin/js/common.js @@ -16,6 +16,7 @@ layui.use(['jquery', 'form', 'element', 'layer', 'dropdown'], function () { var csrfTokenValue = $('meta[name="csrf-token-value"]').attr('content'); xhr.setRequestHeader('X-Csrf-Token-Key', csrfTokenKey); xhr.setRequestHeader('X-Csrf-Token-Value', csrfTokenValue); + xhr.setRequestHeader('Content-Type', 'application/json'); } });