Android新手快速上手APP支付
2020-01-06 本文已影响0人
AndroidLazy
第三方配置不做介绍,APP支付流程:
-
调用项目后台接口生成订单
-
通过订单id调用项目后台接口获取订单信息(为了安全,订单信息一般交由后台处理提供)
-
调用第三方(微信、支付宝)api通过订单信息进行支付
微信支付:payInfoWechatBean对象就是通过第二步获取到的订单信息
private void callWechatToPay(PayInfoWechatBean payInfoWechatBean, String extData) {
IWXAPI iwxapi = WXAPIFactory.createWXAPI(this, WechatConstants.getWXAppId());
iwxapi.registerApp(WechatConstants.getWXAppId());
PayReq req =new PayReq();
req.appId = WechatConstants.getWXAppId();
req.partnerId = payInfoWechatBean.getPartner();
req.prepayId = payInfoWechatBean.getPrepay_id();
req.nonceStr = payInfoWechatBean.getNonceStr();
req.timeStamp = payInfoWechatBean.getTimeStamp();
req.packageValue = payInfoWechatBean.getPackages();
req.sign = payInfoWechatBean.getFinalsign();
req.extData = extData; // optional
iwxapi.sendReq(req);
btnSurepayPayorder.setEnabled(true);
}
然后在微信规定的回调类进行结果处理
public class WXPayEntryActivity extends Activityimplements IWXAPIEventHandler {
private IWXAPI api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, WechatConstants.getWXAppId());
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) {
LLog.d("微信支付回调", "" + resp.toString() +"\n" + resp.getType()
+"\n" + ((PayResp) resp).extData +"\n" + resp.errCode);
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
EventBus.getDefault().post(new PayEvent(true, ((PayResp) resp).extData));
}else {
EventBus.getDefault().post(new PayEvent(false, ((PayResp) resp).extData));
}
finish();
}
}
}
支付宝支付: orderInfo 就是第二步获取的订单信息
private void callAlipayToPay(String orderInfo) {
if (!TextUtils.isEmpty(orderInfo)) {
Runnable payRunnable =new Runnable() {
@Override
public void run() {
PayTask alipay =new PayTask(PayOrderActivity.this);
Map result = alipay.payV2(orderInfo, true);
Message msg =new Message();
msg.what =SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
// 必须异步调用
Thread payThread =new Thread(payRunnable);
payThread.start();
}
}
然后通过 Handler 处理支付回调
@SuppressLint("HandlerLeak")
private HandlermHandler =new Handler() {
@SuppressWarnings("unused")
public void handleMessage(Message msg) {
switch (msg.what) {
case SDK_PAY_FLAG: {
@SuppressWarnings("unchecked")
PayResult payResult =new PayResult((Map) msg.obj);
/**
对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
*/
String resultInfo = payResult.getResult();// 同步返回需要验证的信息
String resultStatus = payResult.getResultStatus();
// 判断resultStatus 为9000则代表支付成功
if (TextUtils.equals(resultStatus, "9000")) {
// 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
// Toast.makeText(PayDemoActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PayEvent(true, payType));
}else {
// 该笔订单真实的支付结果,需要依赖服务端的异步通知。
// Toast.makeText(PayDemoActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PayEvent(false, payType));
}
break;
}
default:
break;
}
}
};