android微信和支付寶支付
1 支付寶支付 相對於微信來說支付寶官方demo已經很好了,但是支付寶demo有個缺點就是在demo裏面演示的時候把後臺和前段做得混到一起了,
雖然說已經說明了,但是對於剛開始集成的我們來說真的頭大,下面我總結一下支付寶客戶端要做得事情。
1 確認訂單接口
/**
* 確認訂單接口
* 必填
* 1107001:微信
* 1107002:支付寶
*/
private void VerifyOrderinInfosInterface(String mOrderinfosBean, String payfor) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
}
mProgressDialog.show();
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
map.put(GsonUtils.orderCode, mOrderinfosBean);//訂單編號
map.put(GsonUtils.payWay, payfor);//支付方式
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
if (mPayWayBeans.getStatus() == 0) {//請求成功
Log.i(response, "onResponse: " + response);
//得到訂單編號
// String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
//獲取簽名後的訂單信息 ,需要訂單編號和訂單金額
if (orderCode != null) {
getAlipayOderInfo(orderCode, mPriceTotal);
//得到簽名訂單信息後調用支付寶pay方法支付
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
2 獲取簽名後的訂單信息
/**
* 獲取訂單簽名後的信息
*
* @param payFororderCode
* @param mPriceTotal
*/
private void getAlipayOderInfo(String payFororderCode, double mPriceTotal) {
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//訂單編號
}
map.put(GsonUtils.out_trade_no, payFororderCode);//訂單編號
map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.APLPAYAPP, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
Log.i("onError", "onResponse: " + e);
}
@Override
public void onResponse(String response, int id) {
Log.i("responseres", "onResponse: " + response);
// alipay(fffff);
mPayForApp = GsonUtils.GsonToBean(response, PayForApp.class);
if (mPayForApp != null) {
if (mPayForApp.getStatus() == 0) {//請求成功
String detail = mPayForApp.getDetail();
alipay(detail);
mProgressDialog.dismiss();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 同步通知自己的app支付結果;(注意異步的已經交給後臺了,後臺那邊支付結果會相應的修改訂單狀態的)
private void alipay(final String orderInfo) {
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(SelectOrderInfoActivity.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();
}
@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")) {
// 該筆訂單是否真實支付成功,需要依賴服務端的異步通知。
Toast.makeText(SelectOrderInfoActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
finish();
} else {
// 該筆訂單真實的支付結果,需要依賴服務端的異步通知。
Toast.makeText(SelectOrderInfoActivity.this, "支付失敗", Toast.LENGTH_SHORT).show();
}
break;
}
case SDK_AUTH_FLAG:
default:
break;
}
}
;
};
支付寶支付很簡單沒有像微信那樣有很多坑,加密,私鑰,商戶id等都在後臺做,前端只需要傳入訂單賬號和支付的選擇方式即可
2 微信支付
微信支付坑太多,主要總結一下和對應的解決方法
1 WXpayEntryActivity 包名和類名必須一致;坑------微信demo裏面的是WXEntryActivity導致支付不了
2 清單文件
<activity
android:name="com.dplustours.b2c.wxapi.WXPayEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="你自己的appid" />
</intent-filter>
</activity>
<receiver
android:name="com.dplustours.b2c.View.receiver.AppRegister"
android:permission="com.tencent.mm.plugin.permission.SEND">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
</intent-filter>
</receiver>
3 微信支付沒有網頁支付,ios那邊有時候會跳轉出來,反正android沒有 支付寶有網頁支付,多以支付前要判斷微信客戶端安裝了沒有
4 打包簽名,這個是最重要的,簽名的keystore要和平臺申請的一致,微信測試必須要簽名打包去驗證平臺的key,驗證成功纔可以打開app裏面的微信支付頁面,要是 打不開就去找申請負責的人,去驗證keystore的MD5字符串是否正確
/**
* 判斷微信客戶端是否存在
*
* @param appid
* @return
*/
private boolean isWXAppInstalledAndSupported(String appid) {
IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
msgApi.registerApp(appid);
boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()
return sIsWXAppInstalledAndSupported;
}
下面是微信支付流程,客戶端走流程
/**
* 微信支付
*
* @param orderCode
* @param s
*/
private void VerifyOrderinInfosInterfaceWeixin(final String orderCode, String s) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
}
mProgressDialog.show();
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
map.put(GsonUtils.orderCode, orderCode);//訂單編號
map.put(GsonUtils.payWay, s);//支付方式
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
if (mPayWayBeans.getStatus() == 0) {//請求成功
Log.i(response, "onResponse: " + response);
//得到訂單編號
// String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
//獲取簽名後的訂單信息 ,需要訂單編號和訂單金額
if (orderCode != null) {
getAlipayOderInfoWeixing(orderCode);
//得到簽名訂單信息後調用支付寶pay方法支付
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 微信zhifu
*
* @param orderCode
*/
private void getAlipayOderInfoWeixing(String orderCode) {
Map map = new TreeMap();
// map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
// if (MyApplication.getInstance().getPreferencesUtils().getUserid() != null && MyApplication.getInstance().getPreferencesUtils().getSessionid() != null) {
// map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid().toString());
// map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid().toString());
// }
// map.put(GsonUtils.out_trade_no, orderCode);//訂單編號
// Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
// String sn = GsonUtils.buildSN(resultMap);
//
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//訂單編號
}
map.put(GsonUtils.out_trade_no, orderCode);//訂單編號
map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.PAYWEIXIN, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
Log.i(response, "onResponse: " + response);
mProgressDialog.dismiss();
try {
PayWayWeixinBeans mPayWayWeixinBeans = GsonUtils.GsonToBean(response, PayWayWeixinBeans.class);
if (mPayWayWeixinBeans != null) {
if (mPayWayWeixinBeans.getDetail() != null) {
//開始跳轉到微信界面進行支付
String appid = mPayWayWeixinBeans.getDetail().getAppid();
String timestamp = mPayWayWeixinBeans.getDetail().getTimestamp();
String noncestr = mPayWayWeixinBeans.getDetail().getNoncestr();
String packageX = mPayWayWeixinBeans.getDetail().getPackageX();
String partnerid = mPayWayWeixinBeans.getDetail().getPartnerid();
String sign = mPayWayWeixinBeans.getDetail().getSign();
String prepayid = mPayWayWeixinBeans.getDetail().getPrepayid();
//開始調用微信支付
StartWinxinPayFor(appid, timestamp, noncestr, packageX, partnerid, sign, prepayid);
}
}
} catch (Exception e) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "數據格式錯誤");
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* app微信支付參數
*
* @param appid
* @param timestamp
* @param noncestr
* @param packageX
* @param partnerid
* @param sign
* @param prepayid
*/
private void StartWinxinPayFor(String appid, String timestamp, String noncestr, String packageX, String partnerid, String sign, String prepayid) {
// 將該app註冊到微信
if (isWXAppInstalledAndSupported(appid)) {
MyApplication.getInstance().getPreferencesUtils().clearOdercode();
MyApplication.getInstance().getPreferencesUtils().clearselectWay();
MyApplication.getInstance().getPreferencesUtils().setOrderCode(orderCode);
MyApplication.getInstance().getPreferencesUtils().setselectWay(selectWay);
msgApi = WXAPIFactory.createWXAPI(SelectOrderInfoActivity.this, null);
msgApi.registerApp(appid);
PayReq request = new PayReq();
request.appId = appid;
request.partnerId = partnerid;
request.prepayId = prepayid;
request.packageValue = packageX;
request.nonceStr = noncestr;
request.timeStamp = timestamp;
request.sign = sign;
msgApi.sendReq(request);
} else {
Toast.makeText(this, "請安裝微信客戶端", Toast.LENGTH_LONG).show();
}
}
/**
* 判斷微信客戶端是否存在
*
* @param appid
* @return
*/
private boolean isWXAppInstalledAndSupported(String appid) {
IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
msgApi.registerApp(appid);
boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()
return sIsWXAppInstalledAndSupported;
}
雖然說已經說明了,但是對於剛開始集成的我們來說真的頭大,下面我總結一下支付寶客戶端要做得事情。
1 確認訂單接口
/**
* 確認訂單接口
* 必填
* 1107001:微信
* 1107002:支付寶
*/
private void VerifyOrderinInfosInterface(String mOrderinfosBean, String payfor) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
}
mProgressDialog.show();
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
map.put(GsonUtils.orderCode, mOrderinfosBean);//訂單編號
map.put(GsonUtils.payWay, payfor);//支付方式
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
if (mPayWayBeans.getStatus() == 0) {//請求成功
Log.i(response, "onResponse: " + response);
//得到訂單編號
// String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
//獲取簽名後的訂單信息 ,需要訂單編號和訂單金額
if (orderCode != null) {
getAlipayOderInfo(orderCode, mPriceTotal);
//得到簽名訂單信息後調用支付寶pay方法支付
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
2 獲取簽名後的訂單信息
/**
* 獲取訂單簽名後的信息
*
* @param payFororderCode
* @param mPriceTotal
*/
private void getAlipayOderInfo(String payFororderCode, double mPriceTotal) {
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//訂單編號
}
map.put(GsonUtils.out_trade_no, payFororderCode);//訂單編號
map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.APLPAYAPP, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
Log.i("onError", "onResponse: " + e);
}
@Override
public void onResponse(String response, int id) {
Log.i("responseres", "onResponse: " + response);
// alipay(fffff);
mPayForApp = GsonUtils.GsonToBean(response, PayForApp.class);
if (mPayForApp != null) {
if (mPayForApp.getStatus() == 0) {//請求成功
String detail = mPayForApp.getDetail();
alipay(detail);
mProgressDialog.dismiss();
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
3 同步通知自己的app支付結果;(注意異步的已經交給後臺了,後臺那邊支付結果會相應的修改訂單狀態的)
private void alipay(final String orderInfo) {
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(SelectOrderInfoActivity.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();
}
@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")) {
// 該筆訂單是否真實支付成功,需要依賴服務端的異步通知。
Toast.makeText(SelectOrderInfoActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
finish();
} else {
// 該筆訂單真實的支付結果,需要依賴服務端的異步通知。
Toast.makeText(SelectOrderInfoActivity.this, "支付失敗", Toast.LENGTH_SHORT).show();
}
break;
}
case SDK_AUTH_FLAG:
default:
break;
}
}
;
};
支付寶支付很簡單沒有像微信那樣有很多坑,加密,私鑰,商戶id等都在後臺做,前端只需要傳入訂單賬號和支付的選擇方式即可
2 微信支付
微信支付坑太多,主要總結一下和對應的解決方法
1 WXpayEntryActivity 包名和類名必須一致;坑------微信demo裏面的是WXEntryActivity導致支付不了
2 清單文件
<activity
android:name="com.dplustours.b2c.wxapi.WXPayEntryActivity"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="你自己的appid" />
</intent-filter>
</activity>
<receiver
android:name="com.dplustours.b2c.View.receiver.AppRegister"
android:permission="com.tencent.mm.plugin.permission.SEND">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
</intent-filter>
</receiver>
3 微信支付沒有網頁支付,ios那邊有時候會跳轉出來,反正android沒有 支付寶有網頁支付,多以支付前要判斷微信客戶端安裝了沒有
4 打包簽名,這個是最重要的,簽名的keystore要和平臺申請的一致,微信測試必須要簽名打包去驗證平臺的key,驗證成功纔可以打開app裏面的微信支付頁面,要是 打不開就去找申請負責的人,去驗證keystore的MD5字符串是否正確
/**
* 判斷微信客戶端是否存在
*
* @param appid
* @return
*/
private boolean isWXAppInstalledAndSupported(String appid) {
IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
msgApi.registerApp(appid);
boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()
return sIsWXAppInstalledAndSupported;
}
下面是微信支付流程,客戶端走流程
/**
* 微信支付
*
* @param orderCode
* @param s
*/
private void VerifyOrderinInfosInterfaceWeixin(final String orderCode, String s) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(SelectOrderInfoActivity.this);
}
mProgressDialog.show();
Map map = new TreeMap();
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
map.put(GsonUtils.orderCode, orderCode);//訂單編號
map.put(GsonUtils.payWay, s);//支付方式
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.CAPTIONINITOKINFO, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
mPayWayBeans = GsonUtils.GsonToBean(response, PayWayBeans.class);
if (mPayWayBeans.getStatus() == 0) {//請求成功
Log.i(response, "onResponse: " + response);
//得到訂單編號
// String ppayFororderCode = mPayWayBeans.getDetail().getOrderCode();
//獲取簽名後的訂單信息 ,需要訂單編號和訂單金額
if (orderCode != null) {
getAlipayOderInfoWeixing(orderCode);
//得到簽名訂單信息後調用支付寶pay方法支付
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 微信zhifu
*
* @param orderCode
*/
private void getAlipayOderInfoWeixing(String orderCode) {
Map map = new TreeMap();
// map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
// if (MyApplication.getInstance().getPreferencesUtils().getUserid() != null && MyApplication.getInstance().getPreferencesUtils().getSessionid() != null) {
// map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid().toString());
// map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid().toString());
// }
// map.put(GsonUtils.out_trade_no, orderCode);//訂單編號
// Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
// String sn = GsonUtils.buildSN(resultMap);
//
map.put(GsonUtils.snTime, String.valueOf(System.currentTimeMillis()));
if (!MyApplication.getInstance().getPreferencesUtils().getUserid().equals("")) {
map.put(GsonUtils.userId, MyApplication.getInstance().getPreferencesUtils().getUserid());
map.put(GsonUtils.sessionId, MyApplication.getInstance().getPreferencesUtils().getSessionid());//訂單編號
}
map.put(GsonUtils.out_trade_no, orderCode);//訂單編號
map.put(GsonUtils.total_amount, String.valueOf(mPriceTotal));
Map<String, String> resultMap = GsonUtils.sortMapByKey(map); // 按Key進行排序
String sn = GsonUtils.buildSN(resultMap);
try {
sn = URLEncoder.encode(sn, "UTF-8");
sn = MD5Coder.getMD5Code(sn);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
map.put(GsonUtils.sn, sn);
String json = GsonUtils.buildJson(map);
try {
NetUtils.getmInstance().postJsons(NetGlobalParams.PAYWEIXIN, json, new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "網絡連接超時");
mProgressDialog.dismiss();
}
@Override
public void onResponse(String response, int id) {
Log.i(response, "onResponse: " + response);
mProgressDialog.dismiss();
try {
PayWayWeixinBeans mPayWayWeixinBeans = GsonUtils.GsonToBean(response, PayWayWeixinBeans.class);
if (mPayWayWeixinBeans != null) {
if (mPayWayWeixinBeans.getDetail() != null) {
//開始跳轉到微信界面進行支付
String appid = mPayWayWeixinBeans.getDetail().getAppid();
String timestamp = mPayWayWeixinBeans.getDetail().getTimestamp();
String noncestr = mPayWayWeixinBeans.getDetail().getNoncestr();
String packageX = mPayWayWeixinBeans.getDetail().getPackageX();
String partnerid = mPayWayWeixinBeans.getDetail().getPartnerid();
String sign = mPayWayWeixinBeans.getDetail().getSign();
String prepayid = mPayWayWeixinBeans.getDetail().getPrepayid();
//開始調用微信支付
StartWinxinPayFor(appid, timestamp, noncestr, packageX, partnerid, sign, prepayid);
}
}
} catch (Exception e) {
UIHelper.ToastMessage(SelectOrderInfoActivity.this, "數據格式錯誤");
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* app微信支付參數
*
* @param appid
* @param timestamp
* @param noncestr
* @param packageX
* @param partnerid
* @param sign
* @param prepayid
*/
private void StartWinxinPayFor(String appid, String timestamp, String noncestr, String packageX, String partnerid, String sign, String prepayid) {
// 將該app註冊到微信
if (isWXAppInstalledAndSupported(appid)) {
MyApplication.getInstance().getPreferencesUtils().clearOdercode();
MyApplication.getInstance().getPreferencesUtils().clearselectWay();
MyApplication.getInstance().getPreferencesUtils().setOrderCode(orderCode);
MyApplication.getInstance().getPreferencesUtils().setselectWay(selectWay);
msgApi = WXAPIFactory.createWXAPI(SelectOrderInfoActivity.this, null);
msgApi.registerApp(appid);
PayReq request = new PayReq();
request.appId = appid;
request.partnerId = partnerid;
request.prepayId = prepayid;
request.packageValue = packageX;
request.nonceStr = noncestr;
request.timeStamp = timestamp;
request.sign = sign;
msgApi.sendReq(request);
} else {
Toast.makeText(this, "請安裝微信客戶端", Toast.LENGTH_LONG).show();
}
}
/**
* 判斷微信客戶端是否存在
*
* @param appid
* @return
*/
private boolean isWXAppInstalledAndSupported(String appid) {
IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
msgApi.registerApp(appid);
boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() ;//&& msgApi.isWXAppSupportAPI()
return sIsWXAppInstalledAndSupported;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.