What the errors mean
Code already used or expired means the same code was submitted twice, typically from double taps or an uncontrolled server retry.
Invalid appid or secret usually comes from a copied value with trailing whitespace, or from mixing in an official account appid. Official account and mini program appids are not interchangeable.
The correct single-use flow
The client calls wx.login, sends the code to your backend, and the backend calls jscode2session with code, appid and secret to obtain openid and session_key.
session_key stays on the server for decrypting sensitive data. Never send it to the client or log it in full.
Maintaining the session
Do not fetch openid on every request. After the first login, issue your own token and identify users with it.
session_key expires. When decryption fails, have the client call wx.login again for a new code. That is normal flow, not an exception.
FAQ
How long is a code valid?
Minutes at most, and only once. Use it immediately and never cache it.
Can the client call jscode2session directly?
No. It requires the secret and must only be called server-side. Client-side calls expose the secret.
Does openid change for the same user?
Within one mini program, openid is stable. A different mini program or entity produces a different openid; use unionid to link them.
Related questions
Further reading
- WeChat Pay Merchant Setup Service details
- Debugging WeChat Pay Callbacks: Did the Request Arrive
- What Are the Common API Configuration Errors in Official Account Development? A Checklist Approach
- What to Do When Alipay Merchant Setup Documents Are Rejected? Correction Tips and Document Checklist
- Why WeChat Pay Settlements Get Delayed: Checking Order, Records and Bank Side
Sources & References
The following are public sources. Always check the latest requirements on the official authority or platform page.
