Those special pits of WeChat Pay

Scenes

Recently, the new app needs to integrate WeChat payment , because we know that there are many pits in WeChat payment. Although the previous App has integrated WeChat payment, it is still cautious when it is copied and copied, but the result is still not satisfactory. ios prompts a signature error , Android calls WeChat white screen and errCode=-1 . According to the official WeChat interpretation of the code, we have checked the signature , APPID and background order parameters back and forth on both ends of Android, ios and backend . There is still no clue about the processing. After more than a day of various search tests, the back-end colleagues shouted and found that the judgment on the sign encryption type processing in the copied WeChat official java file WXPAY.java was the root cause. ,code show as below:

if (useSandbox) {

    this.signType = SignType.MD5; 

} else {

    //第一次调用统一下单接口虽然用HMACSHA256也可以成功,但是二次签名返给手机端后,手机端调微信会报签名错误
    this.signType = SignType.HMACSHA256;

}

The pits of WeChat payment commonly mentioned in other blog posts

  1. Whether the APPID is consistent with the application on the WeChat open platform.
  2. The signature set in the background is the key set by the merchant platform.
  3. Is the location of the WXPayEntryActivity.java and wxapi packages copied to the project folder correct?
  4. Is the WXPayEntryActivity class declared in the Manifest, such as the code:
<activity
    android:name=".wxapi.WXPayEntryActivity"
    android:exported="true"
    android:launchMode="singleTop"/>

The judgment of the client calling WeChat

Regarding the method for the client to call WeChat, the official WeChat document does not explain, most of the blog posts are also covered in one stroke, as follows:

final String APPID = "wxd930ea5d5a258f4f";
IWXAPI msgApi = WXAPIFactory.createWXAPI(context, APPID, false);
msgApi.registerApp(APPID);

if (!msgApi.isWXAppInstalled()) {

    Toast.makeText(this, "未安装微信",Toast.LENGTH_SHORT).show();

} else if (!msgApi.isWXAppSupportAPI()) {

    Toast.makeText(this, "当前微信版本不支持支付",Toast.LENGTH_SHORT).show();

} else {

    //调用微信支付
}

When I tested the payment, I found that on WeChat that is not the latest version, although the isWXAppSupportAPI() of the API indicates that the current WeChat version does not support payment , it can actually call WeChat for payment, so I only used the isWXAppInstalled() method to determine , If installed, directly call WeChat to pay.

On IOS, most manufacturers’ apps use the openWXApp() method that calls the API first , and then calls WeChat payment.

Guess you like

Origin blog.csdn.net/nsacer/article/details/78014811