1.当我们点击支付按钮支付
public static final String PARTNER = "";
public static final String SELLER = "";
public static final String RSA_PRIVATE = "";
public static final String RSA_PUBLIC = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64b" +
"fQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+i" +
"lIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
private static final int SDK_PAY_FLAG = 1;
/**
* call alipay sdk pay. 调用SDK支付
*/
public void pay() {
if (TextUtils.isEmpty(PARTNER) || TextUtils.isEmpty(RSA_PRIVATE) || TextUtils.isEmpty(SELLER)) {
new AlertDialog.Builder(this).setTitle("警告").setMessage("需要配置PARTNER | RSA_PRIVATE| SELLER")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
finish();
}
}).show();
return;
}
String orderInfo = genOrderInfo(title,title,price);
/**
* 特别注意,这里的签名逻辑需要放在服务端,切勿将私钥泄露在代码中!
*/
String sign = sign(orderInfo);
try {
/**
* 仅需对sign 做URL编码
*/
sign = URLEncoder.encode(sign, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/**
* 完整的符合支付宝参数规范的订单信息
*/
final String payInfo = orderInfo + "&sign=\"" + sign + "\"&" + getSignType();
Runnable payRunnable = new Runnable() {
@Override
public void run() {
PayTask alipay = new PayTask(PayActivity.this);
String result = alipay.pay(payInfo, true);
Message msg = mHandler.obtainMessage();
msg.what = SDK_PAY_FLAG;
msg.obj = result;
mHandler.sendMessage(msg);
}
};
getThreadPoolProxy().execute(payRunnable);
}
/**
* create the order info. 创建订单信息
*/
private String genOrderInfo(String subject, String body, String price) {
String orderInfo = "partner=" + "\"" + PARTNER + "\"";
orderInfo += "&seller_id=" + "\"" + SELLER + "\"";
orderInfo += "&out_trade_no=" + "\"" + getOutTradeNo() + "\"";
orderInfo += "&subject=" + "\"" + subject + "\"";
orderInfo += "&body=" + "\"" + body + "\"";
orderInfo += "&total_fee=" + "\"" + price + "\"";
orderInfo += "¬ify_url=" + "\"" +"服务器给你的回调地址" + "\"";
orderInfo += "&service=\"mobile.securitypay.pay\"";
orderInfo += "&payment_type=\"1\"";
orderInfo += "&_input_charset=\"utf-8\"";
orderInfo += "&it_b_pay=\"30m\"";
orderInfo += "&return_url=\"m.alipay.com\"";
return orderInfo;
}
/**
* get the out_trade_no for an order. 生成商户订单号,该值在商户端应保持唯一(可自定义格式规范)
*/
public static String getOutTradeNo() {
return orderNum;
}
/**
* sign the order info. 对订单信息进行签名
*
* @param content 待签名订单信息
*/
private String sign(String content) {
return SignUtils.sign(content, RSA_PRIVATE);
}
/**
* get the sign type we use. 获取签名方式
*/
private String getSignType() {
return "sign_type=\"RSA\"";
}
}
2.通过handler发送msg接收的回调
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
@SuppressWarnings("unused")
public void handleMessage(Message msg) {
switch (msg.what) {
case SDK_PAY_FLAG: {
PayResult payResult = new PayResult((String) msg.obj);
/**
* 同步返回的结果必须放置到服务端进行验证(验证的规则请看https://doc.open.alipay.com/doc2/
detail.htm?spm=0.0.0.0.xdvAU6&treeId=59&articleId=103665&
* docType=1) 建议商户依赖异步通知
*/
String resultInfo = payResult.getResult();
String resultStatus = payResult.getResultStatus();
if (TextUtils.equals(resultStatus, "9000")) {
mBtPay.setClickable(true);
Toast.makeText(PayActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
} else {
if (TextUtils.equals(resultStatus, "8000")) {
Toast.makeText(PayActivity.this, "支付结果确认中", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
}
}
break;
}
default:
break;
}
}
};
3.清单文件配置
<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>
<activity
android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind"/>
4.特别注意以下几点
5.最后
case R.id.id_bt_submit:
if(price.equals("0")){
UIUtils.showToast(this,"支付成功..");
return;
}
if(index==1){
mBtPay.setClickable(false);
new WebChatPay(this).execute(orderBean);
}else{
mBtPay.setClickable(false);
pay();
}
break;