From 22138a3d9dfeab296cff97799fb55ee574993cf0 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 13 Sep 2022 15:49:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=8C=E6=88=90=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Check.php | 9 ++++++++- app/controller/Wechat.php | 15 ++++++++++++++- app/model/Payment.php | 14 ++++++++++++++ composer.json | 3 ++- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/controller/Check.php b/app/controller/Check.php index 46b7750..6fce550 100644 --- a/app/controller/Check.php +++ b/app/controller/Check.php @@ -4,11 +4,18 @@ namespace app\controller; use EasyWeChat\Factory; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use think\facade\Config; class Check { - + /** + * Notes : 微信被封检测 + * + * @Date : 2022/9/13 13:39 + * @Author : + * @throws GuzzleException + */ public function index() { $url = $GLOBALS['data']['data']['url']; diff --git a/app/controller/Wechat.php b/app/controller/Wechat.php index 0b4049d..0963d02 100644 --- a/app/controller/Wechat.php +++ b/app/controller/Wechat.php @@ -7,7 +7,6 @@ use app\model\Payment; use EasyWeChat\Factory; use EasyWeChat\OfficialAccount\Application; use think\facade\Config; -use think\facade\Db; use think\facade\Request; use think\facade\Route; use think\facade\View; @@ -128,7 +127,21 @@ class Wechat */ public function paid() { + $this->initWechat(); + return $this->app->handlePaidNotify(function ($message, $fail) { + $order = Payment::where('trade_no', $message['out_trade_no'])->find(); + + if (! $order || $order->paid_at) { + return true; + } + + if (key_exists('result_code', $message) && $message['result_code'] === 'SUCCESS') { + $order->paid(); + } else { + $fail('Order not exists.'); + } + }); } } \ No newline at end of file diff --git a/app/model/Payment.php b/app/model/Payment.php index 993ebc1..7868cf9 100644 --- a/app/model/Payment.php +++ b/app/model/Payment.php @@ -2,6 +2,7 @@ namespace app\model; +use Carbon\Carbon; use think\Model; use think\model\relation\BelongsTo; @@ -13,4 +14,17 @@ class Payment extends Model { return $this->belongsTo(Order::class); } + + /** + * Notes : 订单支付完成后续操作 + * + * @Date : 2022/9/13 15:41 + * @Author : + */ + public function paid() + { + $this->paid_at = Carbon::now(); // 更新支付时间为当前时间 + $this->status = 1; + $this->save(); + } } \ No newline at end of file diff --git a/composer.json b/composer.json index 7aaaab4..43a99ae 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "aliyuncs/oss-sdk-php": "^2.6", "topthink/think-view": "^1.0", "overtrue/wechat": "~5.0", - "guzzlehttp/guzzle": "^7.5" + "guzzlehttp/guzzle": "^7.5", + "nesbot/carbon": "^2.62" }, "require-dev": { "symfony/var-dumper": "^4.2",