WechatMiniExpressSendSyncServer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\common\server;
  3. use EasyWeChat\Factory;
  4. /**
  5. * @notes 小程序 发货信息录入
  6. * @notes https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
  7. * author lbzy
  8. * @datetime 2023-07-14 14:51:23
  9. * @class WechatMiniExpressSendSyncService
  10. * @package app\common\service
  11. */
  12. class WechatMiniExpressSendSyncServer
  13. {
  14. static private $miniApp;
  15. private static function getMiniApp()
  16. {
  17. static::$miniApp = static::$miniApp ? : Factory::miniProgram(WeChatServer::getMnpConfig());
  18. return static::$miniApp;
  19. }
  20. private static function getToken($app)
  21. {
  22. return $app->access_token->getToken()['access_token'];
  23. }
  24. static function wechatSyncCheck($order)
  25. {
  26. $app = static::getMiniApp();
  27. $data = [
  28. 'transaction_id' => $order['pay_transaction_id'] ?? '',
  29. ];
  30. $token = static::getToken($app);
  31. $result_content = $app->http_client->post("/wxa/sec/order/get_order?access_token={$token}", [
  32. 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
  33. ])->getBody()->getContents();
  34. $result = json_decode($result_content, true);
  35. if (! isset($result['errcode']) || $result['errcode'] != 0) {
  36. // token失效 不标记失败 等下次执行
  37. if (isset($result['errcode']) && $result['errcode'] == 40001) {
  38. // Log::write("等待下次执行" . ($result_content ? : "发货录入发生错误"), 'wechat_mini_express_sync_check');
  39. return false;
  40. }
  41. // Log::write($result_content ? : "发货录入发生错误", 'wechat_mini_express_sync_check');
  42. return [];
  43. }
  44. return $result;
  45. }
  46. }