220 lines
7.7 KiB
Java
220 lines
7.7 KiB
Java
package com.huanmeng.talk;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.alipay.sdk.app.PayTask;
|
|
import com.tencent.mm.opensdk.modelpay.PayReq;
|
|
import com.tencent.mm.opensdk.openapi.IWXAPI;
|
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.simple.eventbus.EventBus;
|
|
import org.simple.eventbus.Subscriber;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import io.flutter.embedding.android.FlutterActivity;
|
|
import io.flutter.embedding.engine.FlutterEngine;
|
|
import io.flutter.plugin.common.MethodCall;
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
import io.flutter.plugin.common.StandardMethodCodec;
|
|
|
|
public class MainActivity extends FlutterActivity {
|
|
private static final String CHANNEL = "samples.flutter.dev/battery";
|
|
private MethodChannel nativeChannel;
|
|
|
|
private final int SDK_PAY_FLAG = 1;
|
|
|
|
private final String APP_ID = "wxa4009a51b6438a06";
|
|
|
|
|
|
@Override
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
EventBus.getDefault().register(this);
|
|
Log.d("TAG", "onCreate: EventBus");
|
|
}
|
|
|
|
@Override
|
|
protected void onNewIntent(@NonNull Intent intent) {
|
|
super.onNewIntent(intent);
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
EventBus.getDefault().unregister(this);
|
|
super.onDestroy();
|
|
}
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
@Subscriber(tag = EventBusParams.MAIN)
|
|
public void Ev(HashMap<String, String> TagMap) {
|
|
final String tag = TagMap.get("tag");
|
|
if (!TextUtils.isEmpty(tag)) {
|
|
switch (tag) {
|
|
case "wxPaySuccess":
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("wxPaySuccess", "wxPaySuccess");
|
|
nativeChannel.invokeMethod("wxPaySuccess", map);
|
|
break;
|
|
|
|
case "payError":
|
|
Map<String, String> mapError = new HashMap<>();
|
|
mapError.put("payError", "payError");
|
|
nativeChannel.invokeMethod("payError", mapError);
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
|
|
super.configureFlutterEngine(flutterEngine);
|
|
nativeChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL, StandardMethodCodec.INSTANCE);
|
|
|
|
nativeChannel.setMethodCallHandler(new MethodChannel.MethodCallHandler() {
|
|
@Override
|
|
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
|
|
switch (call.method) {
|
|
case "test":
|
|
Log.d("TAG", "onMethodCall: 66666666666");
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("test", "ADSuccess");
|
|
nativeChannel.invokeMethod("test", map);
|
|
break;
|
|
case "WxPay"://微信支付
|
|
String orderInfoWx = call.argument("orderInfoWx");
|
|
Log.d("TAG", "onMethodCall: orderInfoWx===" + orderInfoWx);
|
|
|
|
WxPay(orderInfoWx);
|
|
|
|
|
|
break;
|
|
|
|
case "Alipay"://支付宝支付
|
|
|
|
Log.d("TAG", "onMethodCall: Alipay");
|
|
|
|
String orderInfoZfb = call.argument("orderInfoZfb");
|
|
Alipay(orderInfoZfb);
|
|
|
|
break;
|
|
|
|
case "playAd"://看广告
|
|
|
|
break;
|
|
case "test1":
|
|
Log.d("TAG1", "onMethodCall: 66666666666");
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@SuppressLint("HandlerLeak")
|
|
private Handler mHandler = new Handler() {
|
|
@SuppressWarnings("unused")
|
|
public void handleMessage(Message msg) {
|
|
switch (msg.what) {
|
|
case SDK_PAY_FLAG:
|
|
//这里接收支付宝的回调信息
|
|
//需要注意的是,支付结果一定要调用自己的服务端来确定,不能通过支付宝的回调结果来判断
|
|
@SuppressWarnings("unchecked")
|
|
PayResult payResult = new PayResult((Map<String, String>) msg.obj);
|
|
/**
|
|
* 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
|
|
*/
|
|
String resultInfo = payResult.getResult();// 同步返回需要验证的信息
|
|
String resultStatus = payResult.getResultStatus();
|
|
// 判断resultStatus 为9000则代表支付成功
|
|
if (TextUtils.equals(resultStatus, "9000")) {
|
|
// 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
|
|
Log.d("TAG", "handleMessage9000: payResult==" + payResult);
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("AlipaySuccess", "AlipaySuccess");
|
|
nativeChannel.invokeMethod("AlipaySuccess", map);
|
|
|
|
} else {
|
|
// 该笔订单真实的支付结果,需要依赖服务端的异步通知。
|
|
Log.d("TAG", "handleMessage: payResult==" + payResult);
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("payError", "payError");
|
|
nativeChannel.invokeMethod("payError", map);
|
|
|
|
}
|
|
break;
|
|
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
private void WxPay(String orderInfo) {
|
|
|
|
IWXAPI api = WXAPIFactory.createWXAPI(MainActivity.this, APP_ID);
|
|
|
|
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(orderInfo);
|
|
|
|
String appid = jsonObject.getString("appid");
|
|
String partnerId = jsonObject.getString("partnerid");
|
|
String prepayId = jsonObject.getString("prepayid");
|
|
String packageValue = jsonObject.getString("package");
|
|
String nonceStr = jsonObject.getString("noncestr");
|
|
String timeStamp = jsonObject.getString("timestamp");
|
|
String sign = jsonObject.getString("sign");
|
|
|
|
PayReq request = new PayReq();
|
|
request.appId = appid;
|
|
request.partnerId = partnerId;
|
|
request.prepayId = prepayId;
|
|
request.packageValue = packageValue;
|
|
request.nonceStr = nonceStr;
|
|
request.timeStamp = timeStamp;
|
|
request.sign = sign;
|
|
api.sendReq(request);
|
|
|
|
Log.d("TAG", "onMethodCall: appid===" + appid);
|
|
|
|
} catch (JSONException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
//支付宝支付
|
|
private void Alipay(String orderInfo) {
|
|
final Runnable payRunnable = () -> {
|
|
PayTask alipay = new PayTask(MainActivity.this);
|
|
Map<String, String> result = alipay.payV2(orderInfo, true);
|
|
Log.i("msp", result.toString());
|
|
|
|
Message msg = new Message();
|
|
msg.what = SDK_PAY_FLAG;
|
|
msg.obj = result;
|
|
mHandler.sendMessage(msg);
|
|
};
|
|
|
|
// 必须异步调用
|
|
Thread payThread = new Thread(payRunnable);
|
|
payThread.start();
|
|
}
|
|
}
|