Android安卓原生接入微信支付sdk客户端

minSdkVersion必须大于15

 

配置微信sdk所需权限

<!--    微信SDK所需权限-->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- for mta statistics, not necessary-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

 

引入微信sdk加okhttp

 

 

api 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'

 

 

添加一个button用于实现点击支付

 

 

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="点击微信支付"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 

新建okhttp类用于发送网络请求

 

 

import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtil {
    public  static void sendOkHttp(String address,okhttp3.Callback callback){


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(address).build();
        client.newCall(request).enqueue(callback);
    }

}

 

修改MainActivity内逻辑代码

 

 

 

MainActivity内全部代码

 

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.com.gzyingyu.utli.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private static final String APP_ID = "填写APPID";
    private IWXAPI api;
    String address=null;//请求地址
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 通过WXAPIFactory工厂,获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, APP_ID, true);

        // 将应用的appId注册到微信
        api.registerApp(APP_ID);

        Button button=(Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                address = "填写后台写的接口地址";
                HttpUtil.sendOkHttp(address, new Callback() {

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String  orderInfo=response.body().string();
                        try {
                            JSONObject object1 = new JSONObject(orderInfo);


                            PayReq request = new PayReq();
                            request.appId = object1.getString("appid");
                            request.partnerId = object1.getString("partnerid");
                            request.prepayId= object1.getString("prepayid");
                            request.packageValue = "Sign=WXPay";
                            request.nonceStr= object1.getString("noncestr");
                            request.timeStamp= object1.getString("timestamp");
                            request.sign= object1.getString("si");
                            api.sendReq(request);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }



                    }
                    @Override
                    public void onFailure(Call call, IOException e) {

                        //回到主线程
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                            }

                        });

                    }
                });


            }
        });

//        //建议动态监听微信启动广播进行注册到微信
//        registerReceiver(new BroadcastReceiver() {
//            @Override
//            public void onReceive(Context context, Intent intent) {
//                // 将该app注册到微信
//                api.registerApp(APP_ID);
//            }
//        },new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));



    }


    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {

    }
}

 

到这里sdk已经接入完成了,现在打包,打包后安装到手机,然后下载微信:签名生成工具

 

下载地址:https://developers.weixin.qq.com/doc/oplatform/Downloads/Android_Resource.html

 

 

下载签名工具后,安装到安卓手机上,输入包名,然后点击Get Sign

 

 

 

获取到签名之后,将签名填到微信开放平台“应用签名”内

 

 

现在客户端开发已经全部完成,我们来看看效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值