对安卓端微信、支付宝、银联支付作封装,便于快速集成支付
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.shuaijia:JsEasyPay:v1.1'
}
<!-- 微信支付权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--依赖库中的微信支付广播-->
<receiver android:name="com.jia.jsesaypaylibrary.wxpay.JsWXPayReceiver">
<intent-filter>
<action android:name="com.tencent.mm.plugin.openapi.Intent.ACTION_REFRESH_WXAPP" />
</intent-filter>
</receiver>
@Override
public void create(Bundle savedInstanceState) {
}
@Override
public void paySuccess() {
}
@Override
public void payCancel() {
}
@Override
public void payFail() {
}
将该Activity在清单文件中注册
String prepay_id = "GET PREPAY_ID FROM SERVER";// 下单后将订单信息传给服务器,以获取prepay_id
WXPayConfig.getInstance().setAPI_KEY("YOUR API_KEY");
WXPayConfig.getInstance().setAPP_ID("YOUR APP_ID");
WXPayConfig.getInstance().setMCH_ID("YOUR MCH_ID");
WXPay wxPay = new WXPay(MainActivity.this);
wxPay.pay(prepay_id);
<!-- 支付宝支付权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
AlipayConfig.getInstance().setSELLER("YOUR SELLER"); // 商户收款账号
AlipayConfig.getInstance().setPARTNER("YOUR PARTNER"); // 商户PID
AlipayConfig.getInstance().setRSA_PRIVATE(""); // 商户私钥,pkcs8格式
AlipayConfig.getInstance().setCALL_SERVER_URL("YOUR CALLBACK URL"); // 支付宝请求服务器回调页
AliPay aliPay = new AliPay(MainActivity.this, "GOODS NAME", "GOODS DETAIL", "PRICE");
// 将订单id和info传给服务器,可以支付,则开始支付
if (checkOrderFromServer(aliPay.getOrderId(), aliPay.getOrderInfo())) {
aliPay.pay(new AliPay.AlipayCallBack() {
@Override
public void success() {
Log.e(TAG, "支付宝 success: ");
}
@Override
public void faile() {
Log.e(TAG, "支付宝 faile: ");
}
@Override
public void paying() {
Log.e(TAG, "支付宝 paying: ");
}
});
}
<!--银联支付权限-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc.hce" />
<!--银联支付页-->
<activity
android:name="com.unionpay.uppay.PayActivity"
android:configChanges="orientation|keyboardHidden"
android:excludeFromRecents="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
String tn=getTNfromServer(); // 从服务器获取订单流水号
UPPay upPay=new UPPay(MainActivity.this,tn,"01"); //00表示正式环境,01表示测试环境
upPay.pay();
UPPay.getPayResult(data, new UPPay.UPPayCallback() {
@Override
public void onSuccess() {
Log.e(TAG, "银联 success: ");
}
@Override
public void onFail() {
Log.e(TAG, "银联 onFail: ");
}
@Override
public void onCancel() {
Log.e(TAG, "银联 onCancel: ");
}
});
更多精彩内容,请关注我的微信公众号——Android机动车