android third-party payment WeChat payment

android mobile payment is one of the necessary functions of most apps. As a popular instant messaging app, it is necessary to access a more convenient payment port. WeChat payment is the easiest and most popular in terms of the difficulty of payment operation. It is also the most extensive.
Let 's talk about the steps of WeChat payment integration.
First, you need to create a project
https://open.weixin.qq.com/WeChat open platform address. After
registration or WeChat scanning, you need to create a project and fill in the project name Introduce the logo.
Second , fill in the development information ios This piece fills in the download address BundleID and the beta version BundleID android This piece only needs to fill in the package name and signature (if it has not been released, fill in the signature of the beta version and fill in the signature of the release version after it goes online. About the method of obtaining the signature, the WeChat official website tells you that if you obtain the signature),
then submit your project and wait for the approval of WeChat. During this period, you can download the sdk to integrate the WeChat payment environment.

Payment environment configuration

The environment is divided into two types: one is eclipse and the other is android studio


The environment configuration of as is very simple. Add dependencies {
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
} directly under the app's build.gradle and you're done.
For the
eclipse integrated environment, you need to download the sdk Then import it into the libs package and add it to the project association

Then configure the permissions of this project in the AndroidManifast.xml file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Then you need to create a new folder wxapi under your package and create WXPayEntryActivity under the wxapi package.
Note that if the package name of your project is com.baidu.xxx, the path of your WXPayEntryActivity should be com.baidu.xxx.wxapi. WXPayEntryActivity must be seen clearly, it only needs to be in the next level of your package name, otherwise it will fail to call the payment

WXPayEntryActivity configuration

<activity
            android:name=".wxapi.WXPayEntryActivity"
            android:exported="true"
            android:launchMode="singleTop" />
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    String APP_ID = "YOUR APPID";
    private IWXAPI api;
    private String sign = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        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 baseResp) {
        String result = "";
        switch (baseResp.errCode){
            case BaseResp.ErrCode.ERR_OK:
                result = "支付成功";
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "取消支付";
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "支付失败";
                break;
            default:
               // result = R.string.errcode_unknown;
                break;
        }

      //  Toast.makeText(this, result, Toast.LENGTH_LONG).show();

        onceToast(this,result+"");
    }
    //支付结果提示
    public  void onceToast(final Context context, String contentText){
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.oncetoast_layout, null);
        builder.setView(view);
        Button onesure = (Button) view.findViewById(R.id.oncetoast_button);
        TextView oncecontent = (TextView) view.findViewById(R.id.oncetoast_content);
        oncecontent.setText(contentText);
        final AlertDialog dialog = builder.create();
        onesure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();

                if (sign!=null&&sign.equals(Constant.PASSENGER_PUBLISH_PAY)||sign.equals(Constant.HOST_PUBLISH_PAY)||sign.equals(Constant.CHANGE_PAY)||sign.equals(Constant.PASSENGER_ROB_PAY)||sign.equals(Constant.HOST_ROB_PAY)){
                    Intent intent = new Intent(WXPayEntryActivity.this, CompleteActivity.class);
                    intent.putExtra("type",BookPayActivity.completeType);
                    intent.putExtra("text_1",BookPayActivity.text_1);
                    intent.putExtra("text_2",BookPayActivity.text_2);
                    startActivity(intent);
                    AppManager.getAppManager().finishActivity(BookPayActivity.class);
                }
                finish();
            }
        });
        dialog.show();

    }
}

// WeChat payment method order generation and signature should be placed in the background service to generate

  private void wXPay() {
        final String appId = "YOUR APPID";
        iwxapi = WXAPIFactory.createWXAPI(this, null); //初始化微信api
        iwxapi.registerApp(appId); //注册appid  appid可以在开发平台获取

        Runnable payRunnable = new Runnable() {  //这里注意要放在子线程
            @Override
            public void run() {
                PayReq request = new PayReq(); //调起微信APP的对象
                //下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
                request.appId = appId;
                request.partnerId = wxpartnerid;
                request.prepayId = wxprepayid;
                request.packageValue = "Sign=WXPay";
                request.nonceStr = wxnoncestr;
                request.timeStamp = wxtimestamp;
                request.sign = wxsign;
                iwxapi.sendReq(request);//发送调起微信的请求
            }
        };
        Thread payThread = new Thread(payRunnable);
        payThread.start();
       // finish();
    }

At this point, the WeChat payment WXPayEntryActivity is completed. Especially note that this is a callback processing class formulated by WeChat, which means that you can have a lot of payment calls, but all the results can only be obtained here. The path problem about this class should be your WeChat You must pay attention to wxapi.WXPayEntryActivity under the package name on the open platform. Another thing to pay attention to is your appid. If you think your appid is also given to you by the WeChat official, it means your app is synchronized with your signature. It is not safe to put your appid in the project. You can also put it in the background. Finally, when you package the official version and go online, you should pay attention to your signature configuration on the WeChat open platform. You must change it to the signature of the release version in time. Otherwise, call The payment will fail
. Okay, that's it for WeChat payment. If you have any questions, you can leave me a message and let's discuss together. Let's make progress together.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325530496&siteId=291194637