JPay 简易而不简单的支付 SDK
对微信App支付、支付宝App支付、银联App支付的二次封装,对外提供一个相对简单的接口以及支付结果的回调
-
GitHub: https://github.com/Javen205/JPay
联系方式
使用方法
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
implementation 'com.github.javen205:JPay:AliPay:latest.release.here'
implementation 'com.github.javen205:JPay:WxPay:latest.release.here'
例如:版本号为0.0.5
implementation 'com.github.javen205.JPay:AliPay:0.0.5'
implementation 'com.github.javen205.JPay:WxPay:0.0.5'
// 最新版本无需特殊配置,已配置到 Library
application
节点添加如下类容
// 最新版本无需特殊配置,已配置到 Library
com.jpay.wxpay.JPay.getIntance(mContext).toWxPay(appId, partnerId, prepayId, nonceStr, timeStamp, sign, new com.jpay.wxpay.JPay.WxPayListener() {
@Override
public void onPaySuccess() {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayError(int error_code, String message) {
Toast.makeText(mContext, "支付失败>" + error_code + " " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPayCancel() {
Toast.makeText(mContext, "取消了支付", Toast.LENGTH_SHORT).show();
}
});
或者
com.jpay.wxpay.JPay.getIntance(mContext).toWxPay(payParameters, new com.jpay.wxpay.JPay.WxPayListener() {
@Override
public void onPaySuccess() {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayError(int error_code, String message) {
Toast.makeText(mContext, "支付失败>" + error_code + " " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPayCancel() {
Toast.makeText(mContext, "取消了支付", Toast.LENGTH_SHORT).show();
}
});
payParameters
为JSON字符串格式如下:
{
"appId": "",
"partnerId": "",
"prepayId": "",
"sign": "",
"nonceStr" : "",
"timeStamp": ""
}
或者
JPay.getIntance(mContext).toWxPay(appId, partnerId, prepayId, nonceStr, timeStamp, sign, new JPay.JPayListener() {
@Override
public void onPaySuccess() {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayError(int error_code, String message) {
Toast.makeText(mContext, "支付失败>"+error_code+" "+ message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPayCancel() {
Toast.makeText(mContext, "取消了支付", Toast.LENGTH_SHORT).show();
}
});
JPay.getIntance(mContext).toAliPay(orderInfo, new JPay.AliPayListener() {
@Override
public void onPaySuccess() {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayError(int error_code, String message) {
Toast.makeText(mContext, "支付失败>" + error_code + " " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPayCancel() {
Toast.makeText(mContext, "取消了支付", Toast.LENGTH_SHORT).show();
}
});
com.jpay.unionpay.JPay.getIntance(mContext).toUnionPay("01", tn, new com.jpay.unionpay.JPay.UnionPayListener() {
@Override
public void onPaySuccess() {
Toast.makeText(mContext, "支付成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onPayError(int error_code, String message) {
Toast.makeText(mContext, "支付失败>" + error_code + " " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPayCancel() {
Toast.makeText(mContext, "取消了支付", Toast.LENGTH_SHORT).show();
}
@Override
public void onUnionPay(String dataOrg, String sign, String mode) {
Log.d("onUnionPay", "支付成功>需要后台查询订单确认>dataOrg" + dataOrg + " sign>" + sign + " mode>" + mode);
Toast.makeText(mContext, "支付成功>需要后台查询订单确认>" + dataOrg + " " + mode, Toast.LENGTH_SHORT).show();
}
});
Activity 中 重写 onActivityResult 监听回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("UnionPay",requestCode+" "+resultCode);
try {
UnionPay.getInstance(this).onUnionPayResult(data);
} catch (JSONException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
appId 以及相关的 key 我们都从服务端获取
- 将
AndroidManifest.xml
的包名修改为申请应用的包名 - 将应用中的
build.gradle
的applicationId
修改为申请应用的包名 - 测试的时候修改默认的签名 key
将key复制到项目的根目录(app)中并修改
buildTypes
配置如下
signingConfigs {
release {
storeFile file("wxkey")
storePassword '123456'
keyAlias '1'
keyPassword '123456'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
}
}
微信、支付宝APP支付详细介绍参考资料 博客地址
10分钟搭建属于自己的ngork服务器,实现内网穿透 Android版-微信APP支付
安利