|
|
@@ -1,6 +1,7 @@
|
|
|
<?php
|
|
|
namespace common\components\delivery\platform\shunfeng;
|
|
|
|
|
|
+use bizGhs\express\classes\DeliveryAuthTokenClass;
|
|
|
use bizGhs\express\classes\GhsDeliveryOrderClass;
|
|
|
use bizGhs\order\classes\OrderClass;
|
|
|
use Yii;
|
|
|
@@ -56,6 +57,14 @@ class CallBackHandler
|
|
|
case 'rider_recall':
|
|
|
// 骑士撤单回调
|
|
|
return $this->handleRiderRecall($data);
|
|
|
+
|
|
|
+ case 'bindnotify':
|
|
|
+ // 授权状态回调
|
|
|
+ return $this->handleBindNotify($data);
|
|
|
+
|
|
|
+ case 'cancelbindnotify':
|
|
|
+ // 取消授权状态回调
|
|
|
+ return $this->handleCancelBindNotify($data);
|
|
|
|
|
|
default:
|
|
|
Yii::warning('未知的顺丰回调类型: ' . $urlIndex, 'shunfeng-callback');
|
|
|
@@ -320,6 +329,99 @@ class CallBackHandler
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理顺丰授权成功回调
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ private function handleBindNotify($data)
|
|
|
+ {
|
|
|
+ $sign = Yii::$app->request->get('sign');
|
|
|
+ Yii::info('顺丰授权状态回调(sign=' . ($sign ?? '') . '): ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+
|
|
|
+ $mainId = isset($data['out_shop_id']) ? (int)$data['out_shop_id'] : 0;
|
|
|
+ $shopId = isset($data['shop_id']) ? (string)$data['shop_id'] : '';
|
|
|
+
|
|
|
+ if ($mainId <= 0 || $shopId === '') {
|
|
|
+ Yii::error('顺丰授权状态回调参数缺失: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $payload = [
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'platform' => 'shunfeng',
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'authType' => 'all_store',
|
|
|
+ 'accessToken' => $data['access_token'] ?? '',
|
|
|
+ 'refreshToken' => $data['refresh_token'] ?? '',
|
|
|
+ 'expiresAt' => isset($data['expires_at']) ? (int)$data['expires_at'] : 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $existing = DeliveryAuthTokenClass::getByCondition(
|
|
|
+ ['mainId' => $mainId, 'platform' => 'shunfeng'],
|
|
|
+ false,
|
|
|
+ false,
|
|
|
+ 'id'
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($existing && isset($existing['id'])) {
|
|
|
+ DeliveryAuthTokenClass::updateById($existing['id'], [
|
|
|
+ 'shopId' => $payload['shopId'],
|
|
|
+ 'authType' => $payload['authType'],
|
|
|
+ 'accessToken' => $payload['accessToken'],
|
|
|
+ 'refreshToken' => $payload['refreshToken'],
|
|
|
+ 'expiresAt' => $payload['expiresAt'],
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ DeliveryAuthTokenClass::add($payload);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理顺丰取消授权回调
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ private function handleCancelBindNotify($data)
|
|
|
+ {
|
|
|
+ $sign = Yii::$app->request->get('sign');
|
|
|
+ Yii::info('顺丰取消授权状态回调(sign=' . ($sign ?? '') . '): ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+
|
|
|
+ $mainId = isset($data['out_shop_id']) ? (int)$data['out_shop_id'] : 0;
|
|
|
+ if ($mainId <= 0) {
|
|
|
+ Yii::error('顺丰取消授权回调缺少 out_shop_id: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!empty($data['app_id'])) {
|
|
|
+ $auth = new Auth();
|
|
|
+ if ((string)$auth->getAppKey() !== (string)$data['app_id']) {
|
|
|
+ Yii::warning('顺丰取消授权回调 app_id 不匹配: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $condition = [
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'platform' => 'shunfeng',
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!empty($data['shop_id'])) {
|
|
|
+ $condition['shopId'] = (string)$data['shop_id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $deleted = DeliveryAuthTokenClass::deleteByCondition($condition);
|
|
|
+ if ($deleted === 0) {
|
|
|
+ Yii::warning('顺丰取消授权回调未删除任何记录: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'shunfeng-callback');
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 转换顺丰订单状态到系统配送状态
|