Android WeChat Pay access notes

We basically encounter payment functions in App mobile development. A few days ago, there was a WeChat payment function in the company's project. This article is developed based on AndroidStudio.

Step 1 Read the official document

WeChat official documents
Through the documents, we need to apply for the WeChat developer platform ( different from the WeChat official account) to verify developer qualifications. There is also an important point to pay! ! !

Step 2 Fill in the basic information of the App and get the APPID

When filling in App information, pay attention to the following points:

  • The application package name must be consistent. Make
    sure to fill in the application package name and the package value declared in the App project configuration file AndroidManifest.xml, and the applicationId in the build.gradle file of the project are consistent.
    build.gradle file
    AndroidManifest.xml file

  • Application signature: 32-bit md5 string ( digital lowercase )
    can use the WeChat signature tool to obtain the md5 value, or use the keytool command to compile and view the md5 value. Important point: fill in the release signature (suffix .jks) here.

Please pay attention to the above 2 points, otherwise, it will cause the payment to be transferred and report errCode=-1.

Step 3 Familiarize yourself with the payment process

Looking specifically at the WeChat business interaction sequence diagram , you can better understand the entire payment process and logic.

Step 4 Pay SDK dependencies

  • In the build.gradle file, add the following dependencies:
//下面依赖根据业务需要,任选其一
dependencies {
   //包含统计功能
   compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
   //不包含统计功能
   compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
}
  • Rights Profile
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Step 5 configure AS project

Add a wxapi package name to the project, and must have the class name WXPayEntryActivity at the same time, otherwise the WeChat payment cannot be called up. Suggestion: WXPayEntryActivity is copied directly from the demo to reduce unnecessary trouble. At the same time, the path of the package name and the WXPayEntryActivity configuration in the AndroidManifest.xml file are shown in the figure:
Path to package name
WXPayEntryActivity configuration

Step 6 Payment request code

  • Register the appId
    in the onCreate() method that needs to call up the payment Activity to register the appId.
 IWXAPI wxApi= WXAPIFactory.createWXAPI(context, null);
 wxApi.registerApp(appId);
  • Initiate payment
public void getWXpayOrderInfo(WXPayBean wxPayBean) {
        PayReq req = new PayReq();
        //wxPayBean是根据服务器返回json数据生成bean实体对象
        req.appId = wxPayBean.getAppid();
        req.partnerId = wxPayBean.getMch_id();
        req.prepayId = wxPayBean.getPrepay_id();
        req.nonceStr = wxPayBean.getNonce_str();
        req.timeStamp = String.valueOf(wxPayBean.getTimestamp());
        req.packageValue ="Sign=WXPay";
        req.sign =CommonUtils.getSign(wxPayBean,req.timeStamp);
        //发起支付请求
        wxApi.sendReq(req);
    }
  • Note: The sign value server side should be encrypted according to the following rules:
    signature algorithm

  • Recall the payment interface parameter description
    Parameter Description

Step 7 WXPayEntryActivity configuration

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    
    
    private IWXAPI api;

      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = App.getWXApi();
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }
    @Override
    public void onReq(BaseReq baseReq) {
    }
    @Override
    public void onResp(BaseResp resp) {
      if(resp.getType()==ConstantsAPI.COMMAND_PAY_BY_WX){
          switch (resp.errCode) {
             case BaseResp.ErrCode.ERR_OK://成功

              break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://取消支付

              break;
             case BaseResp.ErrCode.ERR_COMM:// -1

             break;
          }
      }
   }
}

At this point, the payment access process has been completed.

Pay attention points

  • Problem WeChat open platform signature
    as long as your signature programs on your mobile phone and registered in the micro-channel platform signature can be the same, whether debug version, or release versions can be transferred from the successful payment. If you use the debug signature, remember to go to the WeChat development platform to change the signature of the release version when it is officially launched (it may take a while to take effect). If you use the release signature, there is no need to test the payment function packaging, add the following code to the bulid.gradle file:
    release signature

  • WeChat Pay for the same order will not jump to the payment page for the second time to indicate that the payment has failed. When the same order is paid for the second time, it will return preayid as null when the payment order is requested directly.

  • WeChat Pay can only bind one price to the same order. If it has already been generated, the obtained prepareid is null, and payment cannot be transferred.

Guess you like

Origin blog.csdn.net/xufei5789651/article/details/77968148