微信支付,和支付宝支付流程大体都是一样的
1.向自己的服务器请求订单号
2.获取订单信息后,把信息传给微信或支付宝sdk,掉起支付
3.微信或支付宝sdk返给支付成功或失败的回调
4.若回调成功请求服务器二次确认支付是否成功
一.支付宝
我们先说程序员应该做的事情,后全面再说申请账号,创建应用(产品干的)
1.下载支付宝jar包,目前只提供jar包,放入到我们libs目录
下载目录
https://docs.open.alipay.com/54/104509/
image.png
2.清单配置
<!-- 支付宝支付 苏艳 -->
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" />
<activity
android:name="com.alipay.sdk.app.H5AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" />
<!-- 支付宝支付结束 苏艳 -->
3.这是我项目中用到的支付宝调用的utils,支付宝的回调是通过handler完成的,所以要传入一个handler
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import com.alipay.sdk.app.PayTask;
import java.util.Map;
/**
* Created by Lenovo on 2017/10/19.
* <p>
* by:苏艳
* <p>
* 支付宝支付,集成
*/
public class PayZhifubaoUtils {
Activity mActivity;
Handler mHandler;
private static PayZhifubaoUtils instance;
private PayZhifubaoUtils(Activity mActivity, Handler mHandler) {
this.mActivity = mActivity;
this.mHandler = mHandler;
}
public static PayZhifubaoUtils getInstance(Activity mActivity, Handler mHandler) {
if (instance == null) {
synchronized (PayZhifubaoUtils.class) {
if (instance == null) {
instance = new PayZhifubaoUtils(mActivity,mHandler);
}
}
}
return instance;
}
/**
* 支付宝支付业务 ,服务器调好后这些重要数据都不可以放在客户端
*/
public void payZhibaoMoney(String order_string) {
// EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX); //
// 沙箱环境测试,生产环境是需要注释掉,沙箱指南中居然没有提到
final String orderInfo = order_string;
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(mActivity);
LogUtils.d("suyan", "=============当前的orderString" + orderInfo);
Map<String, String> result = alipay.payV2(orderInfo, true);
LogUtils.d("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();
}
}
4.在activity中创建回调的handler,拿到支付状态
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
PayResult payResult = new PayResult((Map<String, String>) msg.obj);
String resultInfo = payResult.getResult();
String resultStatus = payResult.getResultStatus();
if (TextUtils.equals(resultStatus, "9000")) {//支付成功,请求接口确定成功
//进行成功的操作(一般请求自己的服务器确定支付成功)
} else if (TextUtils.equals(resultStatus, "6001")) {//支付取消
//进行取消操作
}
} else { //支付失败
//进行失败操作
}
}
LogUtils.d("suyan", "========信息:" + resultInfo + "=======状态码" + resultStatus);
}
};
5.在Activity中请求自己服务器拿到订单信息调用支付宝sdk
if (payZhifubaoUtils == null) {
payZhifubaoUtils = PayZhifubaoUtils.getInstance(PaymentOptionsActivity.this, mHandler);
}
payZhifubaoUtils.payZhibaoMoney(entity.getContent().getKey());
参数entity.getContent().getKey()是服务器给的,也就是说掉起支付宝支付的sdk的是从服务器获取的,不要写在本地,(支付宝和微信支付的参数不太一样,支付宝是把所有的参数都拼接成一个串给sdk,微信是分开给的)
微信支付
微信支付相对于支付宝支付,有点麻烦,还要申请开发者资质,(需要交钱,否则支付跑不通)
1.下载微信的sdk,放入到自己的libs目录
image.png
sdk下载地址
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
2.清单配置
<!-- 微信支付 -->
<activity
android:name=".wxapi.WXPayEntryActivity"
android:exported="true"
android:launchMode="singleTop" />
3.封装微信支付的utils
import com.jyjt.ydyl.application.MyApplication;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
/**
* Created by Lenovo on 2017/10/20.
* by:suyan
* 微信支付开发平台集成 苏艳
*/
public class PayWeixinUtils {
/**
* 微信支付请求
*/
private PayReq req;
/**
* 注册商户号到微信平台
*/
private final IWXAPI msgApi = WXAPIFactory.createWXAPI(MyApplication.getmApplication(), null);
private static PayWeixinUtils instance;
private PayWeixinUtils() {
req = new PayReq();
}
public static PayWeixinUtils getInstance() {
if (instance == null) {
synchronized (PayZhifubaoUtils.class) {
if (instance == null) {
instance = new PayWeixinUtils();
}
}
}
return instance;
}
/**
* 拼接微信请求参数
*/
public void genPayReqAndSend(String APP_ID, String MCH_ID, String prepayId, String packageValue, String nonceStr, String timeStamp, String sign) {
msgApi.registerApp(APP_ID);
req.appId = APP_ID;
req.partnerId = MCH_ID;
req.prepayId = prepayId;
req.packageValue = packageValue;
req.nonceStr = nonceStr;
req.timeStamp = timeStamp;
req.sign = sign;
msgApi.sendReq(req); // 发送请求参数
}
}
微信支付的沙箱环境配置复杂,不在这里说了
4.在activity中,请求自己的服务器,获取到订单信息调用微信sdk
if (mPayWeixinUtils == null) {
mPayWeixinUtils = PayWeixinUtils.getInstance();
}
mPayWeixinUtils.genPayReqAndSend(wechat_key.getAppid(), wechat_key.getPartnerid(), wechat_key.getPrepayid(), wechat_key.getPack_age(), wechat_key.getNoncestr(), wechat_key.getTimestamp(), wechat_key.getSign());
实体类wechat_key是服务器给的,也就是说掉起微信支付的sdk的参数都是从服务器获取的,不要写在本地。
5.微信支付的回调
image.png
根据文档在项目的根目录下创建wxapi包
在包下创建 WXPayEntryActivity页面
/**
* Created by Lenovo on 2017/10/20.
* <p>
* by:苏艳
* 微信支付回掉页面
*/
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
private IWXAPI api;
@BindView(R.id.bt_back_app)
Button bt_back_app;
String order_id = "";
@BindView(R.id.tv_suc)
TextView tv_suc;
@BindView(R.id.ll_err)
LinearLayout ll_err;
@BindView(R.id.iv_pay_zhifubao)
ImageView iv_pay_zhifubao;
@BindView(R.id.iv_back)
ImageView iv_back;
String urlNow = "";
@Override
public void onReq(BaseReq baseReq) {
}
@Override
public void onResp(BaseResp baseResp) {
if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
if (baseResp.errCode == 0) {//支付成功
//进行请求自己的服务器,二次确认
}
} else if (baseResp.errCode == -2) {//取消
} else {//失败
}
} else {//失败
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void showLoading() {
setShowLoading(true);
}
@Override
public void hideLoading() {
setShowLoading(false);
}
@Override
protected PaymentWXpayActivityPresenter loadPresenter() {
return new PaymentWXpayActivityPresenter();
}
@Override
protected void initData() {
//微信相关判断
api = WXAPIFactory.createWXAPI(this, Constans.WEICHAT_APP_ID);
api.handleIntent(getIntent(), this);
}
@Override
protected void initListener() {
}
@Override
protected void initView() {
}
@Override
protected int getLayoutId() {
return R.layout.activity_pay_suc_zhifu_bao;
}
}
网友评论