Bailey 的 AI 折腾笔记
中文 · English · Deutsch · 한국어 · 日本語
Tossing records2026-12-0319 min readtasonc

Run small program payment without buying a server: A record of the pitfalls of WeChat cloud development

Mini programWeChat cloud developmentvirtual paymentStep on the trapServerless
Contents

Run small program payment without buying a server: WeChat cloud development pitfall record

Recently, an AI Token price inquiry applet was changed from "purely static" to a version with WeChat login and paid inquiry. The biggest struggle in the process was not writing code, but back-end selection - the server was too expensive, so I finally switched to WeChat cloud development. This article writes down the pitfalls that I actually encountered and the solutions that worked in the end, as a reference for people who also don’t want to run a server for gadgets.

Why give up building your own server?

The initial plan was very conventional: own server + Python/FastAPI + cloud database, login and virtual payment signature are all done on the server side. This set of code has been written, but it is stuck in implementation:

  • Servers cost money, even dozens of dollars a year, which is a long-term cost for a query tool.
  • Buy a domain name, register it, configure HTTPS, and configure Nginx reverse proxy
  • The mini program also needs to fill in the domain name into the request legal domain name whitelist

Later, I switched to WeChat cloud development, and all these things were gone: no server, no registration, no domain name whitelist. Cloud functions come with a registration-free calling channel, and payment callbacks can also be pushed via cloud-developed messages. There is no need to prepare a public network address yourself. There is a free quota for pay-as-you-go. This kind of gadget usually costs a few cents to a few yuan a month.

Cloud development runs payment, what should be changed in the code?

The original Python logic was moved into a Node.js cloud function and ran in the WeChat cloud:

  • AppSecret is no longer required for login: cloud.getWXContext() is directly given to openid, and the user table uses openid as the primary key.
  • The cloud database creates two collections: tp_users stores users and remaining times, and tp_orders stores orders.
  • The payment signature must be calculated in the cloud function: paySig = HMAC-SHA256(AppKey, "requestVirtualPayment&" + signData), user mode signature signature = HMAC-SHA256(session_key, signData). The session_key is replaced on-site with a one-time front-end code and will not be left in the database after use.
  • The front-end callback cannot be trusted when the account is received: after payment, the cloud function actively checks the order once, confirms that the status is paid, and only adds the number of times, and uses the order number to make idempotence to prevent repeated additions.

Several keys are all placed in the environment variables of the cloud function, and the front end cannot get any of them.

Pitfall 1: The cloud function cannot be uploaded, and "Please select a cloud environment" is reported.

After the code is written, right-click xpay to upload and deploy, but it is blocked directly:

请在编辑器云函数根目录(cloudfunctionRoot)选择一个云环境

The reason is not the code, but that although cloudfunctionRoot: "cloudfunctions/" is declared in project.config.json, the developer tools do not specify a specific environment for this directory.

The solution is to right-click the root directory of cloudfunctions in the file tree on the left (not the function folder inside), select "Select Cloud Environment", and select the environment you just opened. Just make the selection and then deploy.

Pit 2: Login report -501000, Env Not Exists

The cloud function has been deployed and the database collection has been created. When I click on login, I get the following message:

cloud.callFunction:fail errCode: -501000 | errMsg: [100003] Env Not Exists

This pit took the longest. The environment ID was entered into config.js from the screenshot of the console character by character. After repeated checking, what was displayed on the console was exactly the same as what was in the code, but it still reported "Environment does not exist".

After checking around, I found that this kind of error is very common in the WeChat community. The root cause is that the explicitly specified env is not recognized at the SDK level, and the request is rejected at the gateway layer and does not enter the cloud function at all.

The final solution is not to "fix the environment ID", but to let the code figure it out for itself:

wx.cloud.init({ traceUser: true });  // 初始化时不写死 env

function call(action, data) {
  return callOnce(action, data, true).catch(function (err) {
    if (isEnvError(err)) {
      return callOnce(action, data, false);  // 自动用默认环境重试
    }
    throw err;
  });
}

function callOnce(action, data, useEnv) {
  var opts = { name: 'xpay', data: Object.assign({ action: action }, data) };
  if (useEnv) opts.config = { env: configModule.config.envId };
  wx.cloud.callFunction(opts);
}

Adjust it once according to the configuration environment. If it hits Env Not Exists or -501000, it will automatically adjust it again without env and use the default environment assigned by WeChat to the current AppID. After making the changes and recompiling, the login was successful immediately.

The lesson is: Even if the environment ID is correct with the naked eye, don't bet all your money on "hard-coding an env" and leave a retreat for the default environment.

Pit 3: I can log in, but when I click to buy, it says the product has not been released.

After logging in, click "Buy Now" and a message will pop up on WeChat:

PRODUCT_ID_NOT_PUBLISH

On the contrary, this shows that the entire technical chain of signatures, cloud functions, and environments is connected - WeChat has received the purchase request, but it just said that this product cannot be sold.

The reason is that virtual payment props are divided into "development version" and "online version". Props are only saved in the development version and are not considered for sale. You must click Publish to send them to the online version. And it will take about 10 minutes after it is released to take effect, and -15014 will be reported if you pay too early.

There are also two field pitfalls that are easy to step on:

  • Prop ID only supports English, numbers, and underscores. The dash in tasonc-1 is illegal. You must use tasonc_1, and it must be exactly the same as the one in the cloud function environment variable.
  • The price unit of props filled in the background is yuan (fill in 9.9), and the unit of goodsPrice in the code is cent (fill in 990). If the amounts on both sides are equal, -15013 will not be reported.

Don’t confuse the two "releases"

It's easy to mix these two things up when debugging:

  • Item release: Make the product available for purchase, wait 10 minutes after ordering, and use the trial version to test payment, without the need for a mini program to be online.
  • Mini Program Release: Click Upload in Developer Tools → Submit for review in MP background → Pass the review → Click Publish. After this step is completed, others can search it in WeChat

First publish the props, test the payment, and finally launch the mini program. On the contrary, if you go online first, the payment will not go through, which is equivalent to waiting for the review in vain.

How to test if you don’t want to spend real money?

In the entire process, only the step of "really deducting the money into the account" cannot bypass payment, and the rest can be verified at zero cost:

  • Click "Buy Now" to bring up the payment checkout, and then cancel directly without entering the password. If the checkout counter can pop up, it means that the signature, props, and environment are all correct, and no money will be deducted.
  • If you want to see the number display and deduction logic, manually change the credits field of the user record directly in the cloud database to 100. Go back to the mini program and cut the page and it will refresh.

A hidden danger that has not been solved yet

For individual mini programs, the official documentation clearly states that virtual payment does not support "information search and query" services. The essence of this tool is to query. If the subject is an individual, it is very likely that the domain name, code, and props are all ready, and finally get stuck on the subject category. It is recommended to go to the MP backend to confirm the subject type and virtual payment activation status before taking action. Don't wait until all the codes are run through to find out that it cannot be enabled.

Summary

The deepest experience from this round is: if a gadget wants to go online, the cost does not necessarily lie in the code. Choosing the right backend (cloud development eliminates the need for servers and registration), leaving the key on the server, and leaving an escape route for environment binding can save most of the pitfalls. The remaining pitfalls are basically in the background configuration - the environment needs to be selected, the props need to be released, the price units need to be aligned, and you have to wait for the 10 minutes after release.

Views 0 次

Comments

加载中…

Comments appear after review.

← Back to Home