intraCityExpress.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. <?php
  2. namespace common\components;
  3. use yii\helpers\Json;
  4. use linslin\yii2\curl;
  5. use bizHd\wx\classes\WxOpenClass;
  6. class IntraCityExpress
  7. {
  8. /**
  9. * 获取 access_token
  10. * @param $merchant
  11. * @param int $ptStyle
  12. * @return string
  13. */
  14. public static function getAccessToken($merchant, $ptStyle = 0)
  15. {
  16. if ($ptStyle == 0) {
  17. $ptStyle = dict::getDict('ptStyle', 'hd');
  18. }
  19. // 直接复用 miniUtil 的获取小程序 access_token 方法
  20. return miniUtil::getMiniProgramAccessToken($merchant, $ptStyle);
  21. }
  22. public static function getMerchant()
  23. {
  24. $merchant = WxOpenClass::getWxInfo();
  25. return $merchant;
  26. }
  27. private static function generateSign($method, $url, $timestamp, $nonce, $body, $merchant)
  28. {
  29. $appId = $merchant['miniAppId'];
  30. $serialNo = $merchant['serial_no'];
  31. $privateKey = $merchant['private_key']; // Ensure this is the raw private key content
  32. $parsedUrl = parse_url($url);
  33. $path = $parsedUrl['path'] . (isset($parsedUrl['query']) ? '?' . $parsedUrl['query'] : '');
  34. $message = "{$method}\n{$path}\n{$timestamp}\n{$nonce}\n{$body}\n";
  35. openssl_sign($message, $signature, $privateKey, 'sha256WithRSAEncryption');
  36. $base64Signature = base64_encode($signature);
  37. return sprintf(
  38. 'WECHATPAY2-SHA256-RSA2048 appid=%s,timestamp="%d",nonce_str="%s",signature="%s",serial_no="%s"',
  39. $appId,
  40. $timestamp,
  41. $nonce,
  42. $base64Signature,
  43. $serialNo
  44. );
  45. }
  46. /**
  47. * 发送HTTP请求
  48. * @param string $url 请求URL
  49. * @param array $data 请求数据
  50. * @param string $accessToken access_token
  51. * @return array
  52. */
  53. public static function sendRequest($url, $data, $accessToken, $merchant = null)
  54. {
  55. if ($merchant === null) {
  56. $merchant = self::getMerchant();
  57. }
  58. $curl = new curl\Curl();
  59. // 添加access_token到URL
  60. $urlWithToken = $url . (strpos($url, '?') !== false ? '&' : '?') . 'access_token=' . $accessToken;
  61. $timestamp = time();
  62. $nonce = \Yii::$app->security->generateRandomString();
  63. $body = Json::encode($data);
  64. if ($body === '[]') {
  65. $body = '{}';
  66. }
  67. //$signature = self::generateSign('POST', $urlWithToken, $timestamp, $nonce, $body, $merchant);
  68. $response = $curl->setOption(CURLOPT_POSTFIELDS, $body)
  69. ->setOption(CURLOPT_HTTPHEADER, [
  70. 'Content-Type: application/json',
  71. 'Accept: application/json',
  72. //'Authorization: ' . $signature
  73. ])
  74. ->post($urlWithToken);
  75. // 确保响应是字符串格式再进行JSON解码
  76. if (is_array($response)) {
  77. return $response;
  78. }
  79. return Json::decode($response, true);
  80. }
  81. public static function emptyRequest($url, $accessToken)
  82. {
  83. $curl = new curl\Curl();
  84. // 添加access_token到URL
  85. $urlWithToken = $url . (strpos($url, '?') !== false ? '&' : '?') . 'access_token=' . $accessToken;
  86. $response = $curl->setOption(CURLOPT_POSTFIELDS, Json::encode(new \stdClass()))
  87. ->setOption(CURLOPT_HTTPHEADER, ['Content-Type: application/json'])
  88. ->post($urlWithToken);
  89. // 确保响应是字符串格式再进行JSON解码
  90. if (is_array($response)) {
  91. return $response;
  92. }
  93. return Json::decode($response, true);
  94. }
  95. // ==================== API URL常量 ====================
  96. const API_BASE_URL = 'https://api.weixin.qq.com/cgi-bin/express/intracity';
  97. // 门店管理
  98. const API_CREATE_STORE = self::API_BASE_URL . '/createstore';
  99. const API_UPDATE_STORE = self::API_BASE_URL . '/updatestore';
  100. const API_QUERY_STORE = self::API_BASE_URL . '/querystore';
  101. // 开通门店权限
  102. const API_APPLY_STORE = self::API_BASE_URL . '/apply';
  103. // 订单管理
  104. const API_ADD_ORDER = self::API_BASE_URL . '/addorder';
  105. const API_CANCEL_ORDER = self::API_BASE_URL . '/cancelorder';
  106. const API_GET_ORDER = self::API_BASE_URL . '/queryorder';
  107. // 预下单
  108. const API_PRE_ADD_ORDER = self::API_BASE_URL . '/preaddorder';
  109. // 资金管理
  110. const API_GET_BALANCE = self::API_BASE_URL . '/getbalance';
  111. const API_CHARGE_STORE = self::API_BASE_URL . '/chargestore';
  112. const API_REFUND_STORE = self::API_BASE_URL . '/refundstore';
  113. const API_GET_CHARGE_RECORD = self::API_BASE_URL . '/getchargerecord';
  114. // 测试接口
  115. const API_MOCK_NOTIFY = self::API_BASE_URL . '/mocknotify';
  116. // ==================== 门店管理接口 ====================
  117. /**
  118. * 创建门店
  119. * @param array $storeData 门店信息
  120. * @param string $merchant 商户信息
  121. * @param int $ptStyle
  122. * @return array
  123. */
  124. public static function createStore($storeData, $merchant = null, $ptStyle = 0)
  125. {
  126. if ($merchant === null) {
  127. $merchant = self::getMerchant();
  128. }
  129. // $re = self::applyStore($merchant, $ptStyle);
  130. // if (!is_array($re)) {
  131. // $re = Json::decode($re, true);
  132. // }
  133. // if ($re['errcode'] != 0) {
  134. // return $re;
  135. // }
  136. $accessToken = self::getAccessToken($merchant, $ptStyle);
  137. $data = [
  138. 'out_store_id' => $storeData['out_store_id'],
  139. 'store_name' => $storeData['store_name'],
  140. 'store_address' => $storeData['store_address'],
  141. 'store_longitude' => $storeData['store_longitude'],
  142. 'store_latitude' => $storeData['store_latitude'],
  143. 'store_phone' => $storeData['store_phone'],
  144. 'service_type' => $storeData['service_type'] ?? 1, // 默认同城配送
  145. ];
  146. return self::sendRequest(self::API_CREATE_STORE, $data, $accessToken, $merchant);
  147. }
  148. /**
  149. * 更新门店
  150. * @param array $storeData 门店信息
  151. * @param string $merchant 商户信息
  152. * @param int $ptStyle
  153. * @return array
  154. */
  155. public static function updateStore($storeData, $merchant = null, $ptStyle = 0)
  156. {
  157. if ($merchant === null) {
  158. $merchant = self::getMerchant();
  159. }
  160. $accessToken = self::getAccessToken($merchant, $ptStyle);
  161. $data = [
  162. 'out_store_id' => $storeData['out_store_id'],
  163. 'store_name' => $storeData['store_name'] ?? null,
  164. 'store_address' => $storeData['store_address'] ?? null,
  165. 'store_longitude' => $storeData['store_longitude'] ?? null,
  166. 'store_latitude' => $storeData['store_latitude'] ?? null,
  167. 'store_phone' => $storeData['store_phone'] ?? null,
  168. ];
  169. return self::sendRequest(self::API_UPDATE_STORE, $data, $accessToken, $merchant);
  170. }
  171. /**
  172. * 查询门店
  173. * @param string $outStoreId 门店ID
  174. * @param string $merchant 商户信息
  175. * @param int $ptStyle
  176. * @return array
  177. */
  178. public static function getStore($outStoreId, $merchant = null, $ptStyle = 0)
  179. {
  180. if ($merchant === null) {
  181. $merchant = self::getMerchant();
  182. }
  183. $accessToken = self::getAccessToken($merchant, $ptStyle);
  184. $data = [
  185. 'out_store_id' => $outStoreId,
  186. ];
  187. return self::sendRequest(self::API_QUERY_STORE, $data, $accessToken, $merchant);
  188. }
  189. /**
  190. * 开通门店权限
  191. * @param null $merchant
  192. * @param int $ptStyle
  193. * @return array
  194. */
  195. public static function applyStore($merchant = null, $ptStyle = 0)
  196. {
  197. if ($merchant === null) {
  198. $merchant = self::getMerchant();
  199. }
  200. $accessToken = self::getAccessToken($merchant, $ptStyle);
  201. return self::sendRequest(self::API_APPLY_STORE, new \stdClass(), $accessToken, $merchant);
  202. //return self::getRequest(self::API_APPLY_STORE, $accessToken);
  203. }
  204. // ==================== 订单管理接口 ====================
  205. /**
  206. * 创建订单
  207. * @param array $orderData 订单信息
  208. * @param string $merchant 商户信息
  209. * @param int $ptStyle
  210. * @return array
  211. */
  212. public static function createOrder($orderData, $merchant = null, $ptStyle = 0)
  213. {
  214. if ($merchant === null) {
  215. $merchant = self::getMerchant();
  216. }
  217. $accessToken = self::getAccessToken($merchant, $ptStyle);
  218. $data = [
  219. 'out_store_id' => $orderData['out_store_id'],
  220. 'store_order_id' => $orderData['store_order_id'], // 注意:文档是 store_order_id,不是 out_order_id
  221. 'delivery_service_code' => $orderData['delivery_service_code'],
  222. 'receiver' => [
  223. 'name' => $orderData['to_user_name'],
  224. 'phone' => $orderData['to_user_phone'],
  225. 'address' => $orderData['to_user_address'],
  226. 'lng' => $orderData['to_user_longitude'],
  227. 'lat' => $orderData['to_user_latitude'],
  228. ],
  229. 'cargo' => [
  230. 'goods_value' => $orderData['goods_value'],
  231. 'goods_weight' => $orderData['goods_weight'],
  232. 'goods_pickup_info' => $orderData['goods_pickup_info'],
  233. 'goods_delivery_info' => $orderData['goods_delivery_info'],
  234. 'cargo_first_class' => $orderData['cargo_first_class'] ?? '',
  235. 'cargo_second_class' => $orderData['cargo_second_class'] ?? '',
  236. 'goods_type' => $orderData['goods_type'] ?? 99,
  237. ],
  238. 'order' => [
  239. 'order_type' => $orderData['order_type'] ?? 0,
  240. 'expected_pickup_time' => $orderData['expected_pickup_time'] ?? 0,
  241. 'expected_delivery_time' => $orderData['expected_delivery_time'] ?? 0,
  242. 'tips' => $orderData['tips'] ?? 0,
  243. 'is_insured' => $orderData['is_insured'] ?? 0,
  244. 'declared_value' => $orderData['declared_value'] ?? 0,
  245. 'cash_on_delivery' => $orderData['cash_on_delivery'] ?? 0,
  246. 'cash_on_delivery_value' => $orderData['cash_on_delivery_value'] ?? 0,
  247. ],
  248. 'sub_biz_id' => $orderData['sub_biz_id'] ?? '',
  249. ];
  250. return self::sendRequest(self::API_ADD_ORDER, $data, $accessToken, $merchant);
  251. }
  252. /**
  253. * 预下单(查询运费)
  254. * @param array $orderData
  255. * @param null $merchant
  256. * @param int $ptStyle
  257. * @return array
  258. */
  259. public static function preAddOrder($orderData, $merchant = null, $ptStyle = 0)
  260. {
  261. if ($merchant === null) {
  262. $merchant = self::getMerchant();
  263. }
  264. $accessToken = self::getAccessToken($merchant, $ptStyle);
  265. // 构建与 createOrder 相同的请求体
  266. $data = [
  267. 'out_store_id' => $orderData['out_store_id'],
  268. 'store_order_id' => $orderData['store_order_id'],
  269. 'delivery_service_code' => $orderData['delivery_service_code'],
  270. 'receiver' => [
  271. 'name' => $orderData['to_user_name'],
  272. 'phone' => $orderData['to_user_phone'],
  273. 'address' => $orderData['to_user_address'],
  274. 'lng' => $orderData['to_user_longitude'],
  275. 'lat' => $orderData['to_user_latitude'],
  276. ],
  277. 'cargo' => [
  278. 'goods_value' => $orderData['goods_value'],
  279. 'goods_weight' => $orderData['goods_weight'],
  280. 'goods_pickup_info' => $orderData['goods_pickup_info'],
  281. 'goods_delivery_info' => $orderData['goods_delivery_info'],
  282. 'cargo_first_class' => $orderData['cargo_first_class'] ?? '',
  283. 'cargo_second_class' => $orderData['cargo_second_class'] ?? '',
  284. 'goods_type' => $orderData['goods_type'] ?? 99,
  285. ],
  286. 'order' => [
  287. 'order_type' => $orderData['order_type'] ?? 0,
  288. 'expected_pickup_time' => $orderData['expected_pickup_time'] ?? 0,
  289. 'expected_delivery_time' => $orderData['expected_delivery_time'] ?? 0,
  290. 'tips' => $orderData['tips'] ?? 0,
  291. 'is_insured' => $orderData['is_insured'] ?? 0,
  292. 'declared_value' => $orderData['declared_value'] ?? 0,
  293. 'cash_on_delivery' => $orderData['cash_on_delivery'] ?? 0,
  294. 'cash_on_delivery_value' => $orderData['cash_on_delivery_value'] ?? 0,
  295. ],
  296. 'sub_biz_id' => $orderData['sub_biz_id'] ?? '',
  297. ];
  298. return self::sendRequest(self::API_PRE_ADD_ORDER, $data, $accessToken, $merchant);
  299. }
  300. /**
  301. * 取消订单
  302. * @param string $wxOrderId 微信订单号
  303. * @param string $outOrderId 商户订单号
  304. * @param string $outStoreId 门店ID
  305. * @param string $merchant 商户信息
  306. * @param int $ptStyle
  307. * @return array
  308. */
  309. public static function cancelOrder($wxOrderId = '', $outOrderId = '', $outStoreId = '', $merchant = null, $ptStyle = 0)
  310. {
  311. if ($merchant === null) {
  312. $merchant = self::getMerchant();
  313. }
  314. $accessToken = self::getAccessToken($merchant, $ptStyle);
  315. $data = [];
  316. if (!empty($wxOrderId)) {
  317. $data['wx_order_id'] = $wxOrderId;
  318. }
  319. if (!empty($outOrderId)) {
  320. $data['store_order_id'] = $outOrderId;
  321. }
  322. if (!empty($outStoreId)) {
  323. $data['out_store_id'] = $outStoreId;
  324. }
  325. return self::sendRequest(self::API_CANCEL_ORDER, $data, $accessToken, $merchant);
  326. }
  327. /**
  328. * 查询订单
  329. * @param string $wxOrderId 微信订单号
  330. * @param string $outOrderId 商户订单号
  331. * @param string $outStoreId 门店ID
  332. * @param string $merchant 商户信息
  333. * @param int $ptStyle
  334. * @return array
  335. */
  336. public static function getOrder($wxOrderId = '', $outOrderId = '', $outStoreId = '', $merchant = null, $ptStyle = 0)
  337. {
  338. if ($merchant === null) {
  339. $merchant = self::getMerchant();
  340. }
  341. $accessToken = self::getAccessToken($merchant, $ptStyle);
  342. $data = [];
  343. if (!empty($wxOrderId)) {
  344. $data['wx_order_id'] = $wxOrderId;
  345. }
  346. if (!empty($outOrderId)) {
  347. $data['store_order_id'] = $outOrderId;
  348. }
  349. if (!empty($outStoreId)) {
  350. $data['out_store_id'] = $outStoreId;
  351. }
  352. return self::sendRequest(self::API_GET_ORDER, $data, $accessToken, $merchant);
  353. }
  354. /**
  355. * 获取订单列表
  356. * @param int $offset 偏移量
  357. * @param int $limit 限制数量
  358. * @param string $merchant 商户信息
  359. * @param int $ptStyle
  360. * @return array
  361. */
  362. public static function getOrderList($offset = 0, $limit = 20, $merchant = null, $ptStyle = 0)
  363. {
  364. if ($merchant === null) {
  365. $merchant = self::getMerchant();
  366. }
  367. $accessToken = self::getAccessToken($merchant, $ptStyle);
  368. $url = 'https://api.weixin.qq.com/cgi-bin/express/intracity/order/list';
  369. $data = [
  370. 'offset' => $offset,
  371. 'limit' => $limit,
  372. ];
  373. return self::sendRequest($url, $data, $accessToken, $merchant);
  374. }
  375. // ==================== 运力管理接口 ====================
  376. /**
  377. * 获取运力列表
  378. * @param string $merchant 商户信息
  379. * @param int $ptStyle
  380. * @return array
  381. */
  382. public static function getDeliveryList($merchant = null, $ptStyle = 0)
  383. {
  384. if ($merchant === null) {
  385. $merchant = self::getMerchant();
  386. }
  387. $accessToken = self::getAccessToken($merchant, $ptStyle);
  388. $url = 'https://api.weixin.qq.com/cgi-bin/express/intracity/delivery/list';
  389. return self::sendRequest($url, new \stdClass(), $accessToken, $merchant);
  390. }
  391. /**
  392. * 获取运力服务范围
  393. * @param string $deliveryId 运力ID
  394. * @param string $merchant 商户信息
  395. * @param int $ptStyle
  396. * @return array
  397. */
  398. public static function getDeliveryServiceArea($deliveryId, $merchant = null, $ptStyle = 0)
  399. {
  400. if ($merchant === null) {
  401. $merchant = self::getMerchant();
  402. }
  403. $accessToken = self::getAccessToken($merchant, $ptStyle);
  404. $url = 'https://api.weixin.qq.com/cgi-bin/express/intracity/delivery/service_area';
  405. $data = [
  406. 'delivery_id' => $deliveryId,
  407. ];
  408. return self::sendRequest($url, $data, $accessToken, $merchant);
  409. }
  410. // ==================== 资金管理接口 ====================
  411. /**
  412. * 查询门店余额
  413. * @param string $outStoreId
  414. * @param null $merchant
  415. * @param int $ptStyle
  416. * @return array
  417. */
  418. public static function getBalance($outStoreId, $merchant = null, $ptStyle = 0)
  419. {
  420. if ($merchant === null) {
  421. $merchant = self::getMerchant();
  422. }
  423. $accessToken = self::getAccessToken($merchant, $ptStyle);
  424. $data = ['out_store_id' => $outStoreId];
  425. return self::sendRequest(self::API_GET_BALANCE, $data, $accessToken, $merchant);
  426. }
  427. /**
  428. * 门店充值
  429. * @param string $outStoreId
  430. * @param string $outChargeId
  431. * @param int $amount
  432. * @param null $merchant
  433. * @param int $ptStyle
  434. * @return array
  435. */
  436. public static function chargeStore($outStoreId, $outChargeId, $amount, $merchant = null, $ptStyle = 0)
  437. {
  438. if ($merchant === null) {
  439. $merchant = self::getMerchant();
  440. }
  441. $accessToken = self::getAccessToken($merchant, $ptStyle);
  442. $data = [
  443. 'out_store_id' => $outStoreId,
  444. 'out_charge_id' => $outChargeId,
  445. 'amount' => $amount,
  446. ];
  447. return self::sendRequest(self::API_CHARGE_STORE, $data, $accessToken, $merchant);
  448. }
  449. /**
  450. * 门店退款
  451. * @param string $outStoreId
  452. * @param string $outRefundId
  453. * @param string $outChargeId
  454. * @param null $merchant
  455. * @param int $ptStyle
  456. * @return array
  457. */
  458. public static function refundStore($outStoreId, $outRefundId, $outChargeId, $merchant = null, $ptStyle = 0)
  459. {
  460. if ($merchant === null) {
  461. $merchant = self::getMerchant();
  462. }
  463. $accessToken = self::getAccessToken($merchant, $ptStyle);
  464. $data = [
  465. 'out_store_id' => $outStoreId,
  466. 'out_refund_id' => $outRefundId,
  467. 'out_charge_id' => $outChargeId,
  468. ];
  469. return self::sendRequest(self::API_REFUND_STORE, $data, $accessToken, $merchant);
  470. }
  471. /**
  472. * 查询充值记录
  473. * @param string $outChargeId
  474. * @param null $merchant
  475. * @param int $ptStyle
  476. * @return array
  477. */
  478. public static function getChargeRecord($outChargeId, $merchant = null, $ptStyle = 0)
  479. {
  480. if ($merchant === null) {
  481. $merchant = self::getMerchant();
  482. }
  483. $accessToken = self::getAccessToken($merchant, $ptStyle);
  484. $data = ['out_charge_id' => $outChargeId];
  485. return self::sendRequest(self::API_GET_CHARGE_RECORD, $data, $accessToken, $merchant);
  486. }
  487. // ==================== 测试接口 ====================
  488. /**
  489. * 模拟回调接口
  490. * @param string $wxOrderId 微信订单号
  491. * @param string $outStoreId 门店ID
  492. * @param string $outOrderId 商户订单号
  493. * @param int $orderStatus 订单状态
  494. * @param string $merchant 商户信息
  495. * @param int $ptStyle
  496. * @return array
  497. */
  498. public static function mockNotify($orderStatus, $wxOrderId = '', $outStoreId = '', $outOrderId = '', $merchant = null, $ptStyle = 0)
  499. {
  500. if ($merchant === null) {
  501. $merchant = self::getMerchant();
  502. }
  503. $accessToken = self::getAccessToken($merchant, $ptStyle);
  504. $data = [
  505. 'order_status' => $orderStatus,
  506. ];
  507. if (!empty($wxOrderId)) {
  508. $data['wx_order_id'] = $wxOrderId;
  509. }
  510. // 注意:文档中模拟回调使用的是 wx_store_id 和 store_order_id
  511. if (!empty($outStoreId) && !empty($outOrderId)) {
  512. $data['wx_store_id'] = $outStoreId;
  513. $data['store_order_id'] = $outOrderId;
  514. }
  515. return self::sendRequest(self::API_MOCK_NOTIFY, $data, $accessToken, $merchant);
  516. }
  517. /**
  518. * 生成回调签名
  519. * @param array $params 回调参数
  520. * @param string $token 安全token
  521. * @return string
  522. */
  523. public static function generateCallbackSignature($params, $token)
  524. {
  525. // 1. 筛选出参与签名的字段
  526. $signParams = [];
  527. $fieldsToSign = ['appid', 'order_status', 'service_trans_id', 'status_change_time', 'store_order_id', 'timestamp', 'wx_order_id', 'wx_store_id'];
  528. foreach ($fieldsToSign as $field) {
  529. if (isset($params[$field])) {
  530. $signParams[$field] = $params[$field];
  531. }
  532. }
  533. // 2. 按字典序排序参数
  534. ksort($signParams);
  535. // 3. 拼接参数字符串
  536. $signStr = '';
  537. foreach ($signParams as $key => $value) {
  538. $signStr .= $key . '=' . $value . '&';
  539. }
  540. $signStr .= 'token=' . $token;
  541. // 4. 计算MD5并转为小写
  542. return strtolower(md5($signStr));
  543. }
  544. // ==================== 回调验证接口 ====================
  545. /**
  546. * 验证回调签名
  547. * @param array $params 回调参数
  548. * @param string $token 安全token
  549. * @return bool
  550. */
  551. public static function verifyCallback($params, $token)
  552. {
  553. if (!isset($params['sign'])) {
  554. return false;
  555. }
  556. $receivedSign = $params['sign'];
  557. unset($params['sign']);
  558. $calculatedSign = self::generateCallbackSignature($params, $token);
  559. return $receivedSign === $calculatedSign;
  560. }
  561. /**
  562. * 处理订单状态回调
  563. * @param array $callbackData 回调数据
  564. * @param string $token 安全token
  565. * @return array
  566. */
  567. public static function handleOrderCallback($callbackData, $token)
  568. {
  569. // 验证签名
  570. if (!self::verifyCallback($callbackData, $token)) {
  571. return [
  572. 'return_code' => 1,
  573. 'return_msg' => '签名验证失败'
  574. ];
  575. }
  576. // 处理订单状态变化
  577. $orderStatus = $callbackData['order_status'];
  578. $wxOrderId = $callbackData['wx_order_id'];
  579. $outOrderId = $callbackData['store_order_id'] ?? '';
  580. $outStoreId = $callbackData['wx_store_id'] ?? '';
  581. // 这里可以添加具体的业务逻辑处理
  582. // 例如:更新数据库中的订单状态、发送通知等
  583. // 伪代码示例:
  584. /*
  585. switch ($orderStatus) {
  586. case 10000: // 订单创建成功
  587. // 处理订单创建成功逻辑
  588. break;
  589. case 30000: // 配送员接单
  590. // 处理配送员接单逻辑
  591. break;
  592. case 40000: // 配送员到店
  593. // 处理配送员到店逻辑
  594. break;
  595. case 50000: // 配送中
  596. // 处理配送中逻辑
  597. break;
  598. case 70000: // 配送完成
  599. // 处理配送完成逻辑
  600. break;
  601. case 20000: // 商家取消订单
  602. case 20001: // 配送方取消订单
  603. case 60000: // 配送员撤单
  604. // 处理订单取消逻辑
  605. break;
  606. case 90000: // 配送异常
  607. // 处理配送异常逻辑
  608. break;
  609. }
  610. */
  611. return [
  612. 'return_code' => 0,
  613. 'return_msg' => 'OK'
  614. ];
  615. }
  616. // ==================== 常量定义 ====================
  617. /**
  618. * 订单状态常量
  619. */
  620. const ORDER_STATUS_CREATED = 10000; // 订单创建成功
  621. const ORDER_STATUS_CANCELED_BY_MERCHANT = 20000; // 商家取消订单
  622. const ORDER_STATUS_CANCELED_BY_DELIVERY = 20001; // 配送方取消订单
  623. const ORDER_STATUS_ACCEPTED = 30000; // 配送员接单
  624. const ORDER_STATUS_ARRIVED = 40000; // 配送员到店
  625. const ORDER_STATUS_DELIVERING = 50000; // 配送中
  626. const ORDER_STATUS_WITHDRAWN = 60000; // 配送员撤单
  627. const ORDER_STATUS_COMPLETED = 70000; // 配送完成
  628. const ORDER_STATUS_EXCEPTION = 90000; // 配送异常
  629. /**
  630. * 物品类型常量
  631. */
  632. const GOODS_TYPE_FAST_FOOD = 1; // 快餐
  633. const GOODS_TYPE_MEDICINE = 2; // 药品
  634. const GOODS_TYPE_GENERAL = 3; // 百货
  635. const GOODS_TYPE_FRESH = 6; // 生鲜
  636. const GOODS_TYPE_WINE = 8; // 酒品
  637. const GOODS_TYPE_DOCUMENT = 12; // 文件
  638. const GOODS_TYPE_CAKE = 13; // 蛋糕
  639. const GOODS_TYPE_FLOWER = 14; // 鲜花
  640. const GOODS_TYPE_DIGITAL = 15; // 数码
  641. const GOODS_TYPE_CLOTHING = 16; // 服装
  642. const GOODS_TYPE_AUTO_PARTS = 17; // 汽配
  643. const GOODS_TYPE_JEWELRY = 18; // 珠宝
  644. const GOODS_TYPE_DRINK = 32; // 饮料
  645. const GOODS_TYPE_LICENSE = 36; // 证照
  646. const GOODS_TYPE_PET = 55; // 宠物用品
  647. const GOODS_TYPE_MATERNITY = 56; // 母婴用品
  648. const GOODS_TYPE_COSMETICS = 57; // 美妆用品
  649. const GOODS_TYPE_HOME = 58; // 家居建材
  650. const GOODS_TYPE_OTHER = 99; // 其他
  651. /**
  652. * 运力常量
  653. */
  654. const DELIVERY_DADA = 'DADA'; // 达达
  655. const DELIVERY_SFTC = 'SFTC'; // 顺丰同城
  656. }