第三方配置不做介紹,APP支付流程:
調(diào)用項(xiàng)目后臺(tái)接口生成訂單
通過訂單id調(diào)用項(xiàng)目后臺(tái)接口獲取訂單信息(為了安全当悔,訂單信息一般交由后臺(tái)處理提供)
調(diào)用第三方(微信蚓曼、支付寶)api通過訂單信息進(jìn)行支付
微信支付:payInfoWechatBean對(duì)象就是通過第二步獲取到的訂單信息
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);
}
然后在微信規(guī)定的回調(diào)類進(jìn)行結(jié)果處理
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("微信支付回調(diào)", "" + 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);
}
};
// 必須異步調(diào)用
Thread payThread =new Thread(payRunnable);
payThread.start();
}
}
然后通過 Handler 處理支付回調(diào)
@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);
/**
對(duì)于支付結(jié)果绩聘,請(qǐng)商戶依賴服務(wù)端的異步通知結(jié)果沥割。同步通知結(jié)果,僅作為支付結(jié)束的通知凿菩。
*/
String resultInfo = payResult.getResult();// 同步返回需要驗(yàn)證的信息
String resultStatus = payResult.getResultStatus();
// 判斷resultStatus 為9000則代表支付成功
if (TextUtils.equals(resultStatus, "9000")) {
// 該筆訂單是否真實(shí)支付成功机杜,需要依賴服務(wù)端的異步通知。
// Toast.makeText(PayDemoActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PayEvent(true, payType));
}else {
// 該筆訂單真實(shí)的支付結(jié)果衅谷,需要依賴服務(wù)端的異步通知椒拗。
// Toast.makeText(PayDemoActivity.this, "支付失敗", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new PayEvent(false, payType));
}
break;
}
default:
break;
}
}
};