Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wechat Bundle Laravel Package

jean553/wechat-bundle

A Symfony/PHP bundle for integrating WeChat (Weixin) services into applications. It provides configuration and service wiring for common WeChat API, authentication, and messaging tasks, helping developers add WeChat support without building integration plumbing from scratch.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install with composer require the package, then publish its config (php artisan vendor:publish --tag=wechat-config or the tag shown in the package README) so credentials live outside vendor code.
  • Add WECHAT_APPID, WECHAT_SECRET, and any token/cache settings to .env. Start by reading the published config file — it is the fastest map of what the package can do.
  • The first real use case is the OAuth2 redirect flow: send the user to the package-generated authorization URL with a redirect_uri and scope, handle the callback route, exchange code for an OAuth2 token, then fetch user information.
  • If you only need the JS SDK, start with the JS API token/signature helper and wire it into a Blade view that calls wx.config.

Implementation Patterns

  • OAuth2 login flow: build an authorize URL with snsapi_base (silent, openid only) or snsapi_userinfo (prompts for profile). Store a random state in the session before redirecting and verify it on callback.
  • Callback handling: in the callback controller, exchange the code for an OAuth2 token, read openid, then either look up a local user or call the user-info endpoint and create/update one. Keep this in a dedicated service or action class rather than the controller.
  • Token strategy: treat the app-level access token and JS API token as shared, cached resources. Prefer the package's cache-backed token retrieval and avoid calling the raw "get token" endpoint on every request.
  • JS API signing: generate the signature server-side per page URL, pass appId, timestamp, nonceStr, signature to the view, then call wx.config. Cache the JS ticket, not the signature (signatures are URL-specific).
  • Middleware: attach a middleware to routes that require a WeChat identity; it can ensure an openid is present in session and redirect through OAuth when missing.
  • Integration tips: wrap package calls behind a small application-level service so you can swap fakes in tests, and log the raw error code/message from WeChat API responses — they are the primary debugging signal.

Gotchas and Tips

  • Token expiry and races: access tokens and JS tickets are short-lived and scoped per app. Under concurrent requests, naive refresh logic can invalidate a token another process just fetched. Use cache locks or a single refresh path.
  • Invalid-token errors: WeChat returns specific error codes (e.g. 40001/42001) when a cached token is stale or was invalidated elsewhere. Build a one-time retry that clears the cache and refetches.
  • Scope mismatch: snsapi_base does not return profile fields. If you call the user-info endpoint after a base-scope flow, expect failure — choose scope deliberately at redirect time.
  • Callback domain and IP allowlist: the OAuth callback domain must be configured in the WeChat console, and server IPs may need allowlisting for API calls. These are console-side issues that look like code bugs.
  • state and open redirects: always validate state, and never redirect to an unvalidated redirect_uri.
  • URL signing: JS API signatures are tied to the exact page URL (including query string, excluding the fragment). Signing the wrong URL produces invalid signature in wx.config with little detail.
  • Caching across nodes: use a shared cache store (Redis/DB) for tokens if you run multiple app servers; the default file cache will cause each node to fetch independently.
  • Testing: fake HTTP responses at the Laravel HTTP client layer if the package uses it, and assert your own callback/controller behaviour rather than the package internals.
  • Extension points: the published config and the container binding/facade are the main seams — prefer resolving through the container so you can bind a stub implementation in tests.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package