├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── yi2580 │ │ └── easypay │ │ └── demo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── yi2580 │ │ │ └── easypay │ │ │ └── demo │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── yi2580 │ └── easypay │ └── demo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── paylibrary ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── yi2580 │ │ └── easypay │ │ ├── EasyPay.java │ │ ├── ali │ │ ├── AliPayAPI.java │ │ ├── AliPayReq.java │ │ └── AliPayResultCallBack.java │ │ ├── bean │ │ ├── AliBizContent.java │ │ └── AliPayResult.java │ │ ├── utils │ │ ├── ALiPayUtils.java │ │ ├── Base64.java │ │ └── WeChatPayUtils.java │ │ └── wechat │ │ ├── WechatPayAPI.java │ │ ├── WechatPayCallbackActivity.java │ │ ├── WechatPayReq.java │ │ └── WechatPayResultCallBack.java │ └── res │ ├── layout │ └── activity_wechat_pay_callback.xml │ └── values │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.iml 3 | .gradle 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | *.jks 10 | /captures 11 | .externalNativeBuild 12 | # built application files 13 | *.apk 14 | *.ap_ 15 | 16 | # files for the dex VM 17 | *.dex 18 | 19 | # Java class files 20 | *.class 21 | 22 | # generated files 23 | bin/ 24 | gen/ 25 | 26 | # Local configuration file (sdk path, etc) 27 | local.properties 28 | 29 | # Eclipse project files 30 | .classpath 31 | .project 32 | 33 | # Proguard folder generated by Eclipse 34 | proguard/ 35 | 36 | # Intellij project files 37 | # *.iml 38 | *.ipr 39 | *.iws 40 | .idea/ 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EasyPay 2 | 3 | [ [![](https://jitpack.io/v/ZQ7/EasyPay.svg)](https://jitpack.io/#ZQ7/EasyPay) ](https://jitpack.io/#ZQ7/EasyPay) 4 | 5 | > 对微信支付和支付宝支付的App端SDK进行二次封装,对外提供一个较为简单的接口和支付结果回调 6 | 7 | 8 | ## 相关文档 9 | 1. 支付宝APP支付文档: 10 | https://docs.open.alipay.com/204 11 | 12 | 2. 微信APP支付文档: 13 | https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1 14 | 15 | ## 1. 如何添加 16 | 17 | #### 在app目录下的build.gradle中添加依赖(微信支付宝SDK版本号请查询文档自行添加) 18 | 19 | ```gradle 20 | implementation 'com.github.ZQ7:EasyPay::1.1.0' 21 | ``` 22 | 23 | ## 2. Android Manifest配置 24 | 25 | **注册activity** 26 | 27 | ```xml 28 | 29 | 34 | ``` 35 | 36 | **全局初始化AppId及相关(也可以在发起支付的时候设置)** 37 | 38 | ```java 39 | //支付宝AppId和通知回调地址 40 | EasyPay.ALI_PAY_APP_ID = ConstantValue.ALI_APP_ID; 41 | EasyPay.ALI_PAY_NOTIFY_URL = ConstantValue.ALI_NOTIFY_URL; 42 | //微信AppId和商户号 43 | EasyPay.WECHAT_PAY_APP_ID = ConstantValue.WX_APP_ID; 44 | EasyPay.WECHAT_PAY_PARTNER_ID = ConstantValue.WX_MCH_ID; 45 | ``` 46 | 47 | 48 | ## 3. 发起支付 49 | 50 | ### 3.1 微信支付(包含部分参数简介) 51 | 52 | ```java 53 | //微信请求参数文档https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2 54 | WechatPayReq req = new WechatPayReq.Builder() 55 | .with(mActivity) 56 | .setPrepayId("预支付交易会话ID") 57 | //如果没有设置签名后的字符串,可以设置私钥,自动完成签名(签名一般由服务端完成) 58 | .setSign("") 59 | .setPrivateKey(ConstantValue.WX_API_KEY) 60 | .create(); 61 | req.setWechatPayResultCallBack(new WechatPayResultCallBack() { 62 | @Override 63 | public void onSuccess() { 64 | Toast.makeText(getApplication(), "支付成功", Toast.LENGTH_SHORT).show(); 65 | } 66 | 67 | @Override 68 | public void onError(int errorCode, String message) { 69 | //错误码参考:https://docs.open.alipay.com/204/105301/ 70 | Toast.makeText(getApplication(), "支付失败", Toast.LENGTH_SHORT).show(); 71 | } 72 | 73 | @Override 74 | public void onCancel() { 75 | Toast.makeText(getApplication(), "取消支付", Toast.LENGTH_SHORT).show(); 76 | } 77 | }); 78 | EasyPay.getInstance().sendPayRequest(req); 79 | ``` 80 | 81 | ```java 82 | { 83 | //微信支付AppID 84 | private String appId = EasyPay.WECHAT_PAY_APP_ID; 85 | //微信支付商户号 86 | private String partnerId = EasyPay.WECHAT_PAY_PARTNER_ID; 87 | //预支付码(重要) 88 | private String prepayId; 89 | //"Sign=WXPay" 90 | private String packageValue = "Sign=WXPay"; 91 | private String nonceStr; 92 | //时间戳 93 | private String timeStamp; 94 | //签名 95 | private String sign; 96 | //API密钥,在商户平台设置 97 | private String privateKey; 98 | } 99 | 100 | ``` 101 | 102 | ### 3.2 支付宝支付(包含部分参数简介) 103 | 104 | ```java 105 | //支付宝请求参数文档https://docs.open.alipay.com/204/105465/ 106 | AliBizContent bizContent = new AliBizContent("订单号", "价格", "商品名称", "交易具体描述信息"); 107 | //PayReq中可根据自己需求设置字段 108 | AliPayReq req = new AliPayReq.Builder() 109 | .with(mActivity) 110 | .setBizContent(bizContent) 111 | //如果没有设置签名后的字符串,可以设置私钥,自动完成签名(签名一般由服务端完成) 112 | .setSign("") 113 | .setPrivateKey(ConstantValue.ALI_PRIVATE_KEY) 114 | .create(); 115 | req.setResultCallBack(new AliPayResultCallBack() { 116 | @Override 117 | public void onSuccess() { 118 | Toast.makeText(getApplication(), "支付成功", Toast.LENGTH_SHORT).show(); 119 | } 120 | 121 | @Override 122 | public void onDealing() { 123 | Toast.makeText(getApplication(), "支付结果确认中", Toast.LENGTH_SHORT).show(); 124 | } 125 | 126 | @Override 127 | public void onError(String resultStatus, String message) { 128 | //错误码参考:https://docs.open.alipay.com/204/105301/ 129 | Toast.makeText(getApplication(), "支付失败", Toast.LENGTH_SHORT).show(); 130 | } 131 | 132 | @Override 133 | public void onCancel() { 134 | Toast.makeText(getApplication(), "取消支付", Toast.LENGTH_SHORT).show(); 135 | } 136 | }); 137 | EasyPay.getInstance().sendPayRequest(req); 138 | ``` 139 | 140 | 141 | ```java 142 | { 143 | //支付宝分配给开发者的应用ID 144 | private String appId = EasyPay.ALI_PAY_APP_ID; 145 | //支付宝服务器主动通知商户服务器里指定的页面http/https路径。建议商户使用https 146 | private String notifyUrl = EasyPay.ALI_PAY_NOTIFY_URL; 147 | //发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 148 | private String timestamp; 149 | 150 | //接口名称 151 | private String method = "alipay.trade.app.pay"; 152 | 153 | //商户请求参数的签名串 154 | private String sign; 155 | //pkcs8 格式的商户私钥。 156 | private String privateKey; 157 | //商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 158 | private String signType = "RSA2"; 159 | 160 | //仅支持JSON 161 | private String format = "JSON"; 162 | //调用的接口版本,固定为:1.0 163 | private String version = "1.0"; 164 | //请求使用的编码格式,如utf-8,gbk,gb2312等 165 | //private String charset = ALiPayUtils.DEFAULT_CHARSET; 166 | private String charset = "utf-8"; 167 | 168 | private AliBizContent bizContent; 169 | private String bizContentStr; 170 | 171 | 172 | public class AliBizContent { 173 | 174 | /** 175 | * 商户网站唯一订单号 176 | */ 177 | private String out_trade_no; 178 | 179 | /** 180 | * 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] 181 | */ 182 | private String total_amount; 183 | 184 | /** 185 | * 商品的标题/交易标题/订单标题/订单关键字等。 186 | */ 187 | private String subject; 188 | 189 | /** 190 | * 对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。 191 | */ 192 | private String body; 193 | 194 | /** 195 | * 该笔订单允许的最晚付款时间,逾期将关闭交易。 196 | * 取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 197 | * 该参数数值不接受小数点, 如 1.5h,可转换为 90m。 198 | * 注:若为空,则默认为15d。 199 | */ 200 | private String timeout_express = "15d"; 201 | 202 | /** 203 | * 销售产品码,商家和支付宝签约的产品码,为固定值QUICK_MSECURITY_PAY 204 | */ 205 | private String product_code = "QUICK_MSECURITY_PAY"; 206 | 207 | /** 208 | * 商品主类型:0—虚拟类商品,1—实物类商品 209 | * 注:虚拟类商品不支持使用花呗渠道 210 | */ 211 | private int goods_type; 212 | 213 | } 214 | } 215 | 216 | ``` 217 | 218 | 219 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION) 5 | defaultConfig { 6 | applicationId "com.yi2580.easypay.demo" 7 | minSdkVersion Integer.parseInt(project.ANDROID_MIN_SDK_VERSION) 8 | targetSdkVersion Integer.parseInt(project.ANDROID_TARGET_SDK_VERSION) 9 | versionCode Integer.parseInt(project.VERSION_CODE) 10 | versionName project.VERSION_NAME 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | 20 | repositories { 21 | flatDir { 22 | dirs '../paylibrary/libs','libs' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | implementation 'com.android.support:appcompat-v7:28.0.0' 30 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 34 | 35 | implementation project(':paylibrary') 36 | 37 | implementation 'com.github.ZQ7:AliPaySDK:15.8.03release' 38 | implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' 39 | } 40 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yi2580/easypay/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.demo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.yi2580.easypay.demo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/yi2580/easypay/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.demo; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | EasyPay 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/yi2580/easypay/demo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.demo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | mavenCentral() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.4.2' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | google() 18 | mavenCentral() 19 | maven { url 'https://jitpack.io' } 20 | 21 | } 22 | } 23 | 24 | task clean(type: Delete) { 25 | delete rootProject.buildDir 26 | } 27 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | 16 | VERSION_NAME=1.1.0 17 | VERSION_CODE=9 18 | 19 | ANDROID_COMPILE_SDK_VERSION=28 20 | 21 | ANDROID_MIN_SDK_VERSION=16 22 | ANDROID_TARGET_SDK_VERSION=28 23 | 24 | ANDROID_SUPPORT_VERSION = 28.0.0 25 | 26 | #对应网址后面的https://bintray.com/yi2580/ 27 | #Org=yi2580 28 | Org=zhangqi 29 | #(library的唯一ID) 30 | GROUP=com.yi2580.easypay 31 | #项目名称(bintray.com上面的项目名称) 32 | ARTIFACT=EasyPay 33 | #项目描述 34 | DESCRIPTION=Android Pay 35 | #站点 36 | WEBSITE=https://yi2580.com 37 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZQ7/EasyPay/19bcb6cca1c83584f8446658b6591be754a81485/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Aug 14 14:39:47 CST 2019 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /paylibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /paylibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion Integer.parseInt(project.ANDROID_COMPILE_SDK_VERSION) 5 | 6 | defaultConfig { 7 | minSdkVersion Integer.parseInt(project.ANDROID_MIN_SDK_VERSION) 8 | targetSdkVersion Integer.parseInt(project.ANDROID_TARGET_SDK_VERSION) 9 | versionCode Integer.parseInt(project.VERSION_CODE) 10 | versionName project.VERSION_NAME 11 | 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | 14 | } 15 | 16 | repositories { 17 | flatDir { 18 | dirs 'libs' 19 | } 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | 29 | } 30 | 31 | dependencies { 32 | implementation fileTree(dir: 'libs', include: ['*.jar']) 33 | 34 | //https://docs.open.alipay.com/204/ 35 | //api(name: 'alipaySdk-15.6.0-20190226104053', ext: 'aar') 36 | //api 'cn.ieclipse.aar-ref:alipaySdk:15.5.9' 37 | compileOnly 'com.github.ZQ7:AliPaySDK:15.8.03release' 38 | //https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&token=&lang=zh_CN 39 | compileOnly 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+' 40 | } 41 | -------------------------------------------------------------------------------- /paylibrary/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /paylibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/EasyPay.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay; 2 | 3 | import com.yi2580.easypay.ali.AliPayAPI; 4 | import com.yi2580.easypay.ali.AliPayReq; 5 | import com.yi2580.easypay.wechat.WechatPayAPI; 6 | import com.yi2580.easypay.wechat.WechatPayReq; 7 | 8 | /** 9 | * @author zhangqi 10 | * Date:2019/3/8 11 | * Description: 12 | */ 13 | public class EasyPay { 14 | 15 | /** 16 | * 微信AppId和商户Id 17 | */ 18 | public static String WECHAT_PAY_APP_ID = ""; 19 | public static String WECHAT_PAY_PARTNER_ID = ""; 20 | 21 | /** 22 | * 支付宝AppId和支付回调服务器地址 23 | */ 24 | public static String ALI_PAY_APP_ID = ""; 25 | public static String ALI_PAY_NOTIFY_URL = ""; 26 | 27 | private static final Object mLock = new Object(); 28 | private static EasyPay mInstance; 29 | 30 | public static EasyPay getInstance() { 31 | if (mInstance == null) { 32 | synchronized (mLock) { 33 | if (mInstance == null) { 34 | mInstance = new EasyPay(); 35 | } 36 | } 37 | } 38 | return mInstance; 39 | } 40 | 41 | /** 42 | * 微信支付请求 43 | * 44 | * @param wechatPayReq 45 | */ 46 | public void sendPayRequest(WechatPayReq wechatPayReq) { 47 | WechatPayAPI.getInstance().sendPayReq(wechatPayReq); 48 | } 49 | 50 | /** 51 | * 支付宝支付请求 52 | * 53 | * @param aliPayReq 54 | */ 55 | public void sendPayRequest(AliPayReq aliPayReq) { 56 | AliPayAPI.getInstance().sendPayReq(aliPayReq); 57 | } 58 | 59 | public void release() { 60 | AliPayAPI.getInstance().release(); 61 | WechatPayAPI.getInstance().release(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/ali/AliPayAPI.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.ali; 2 | 3 | /** 4 | * 支付宝支付API 5 | */ 6 | public class AliPayAPI { 7 | 8 | public static final String ALI_PAY_SUCCESS = "9000"; 9 | public static final String ALI_PAY_DEALING = "8000"; 10 | public static final String ALI_PAY_CANCEL = "6001"; 11 | 12 | private AliPayReq mPayReq; 13 | /** 14 | * 获取支付宝支付API 15 | */ 16 | private static final Object mLock = new Object(); 17 | private static AliPayAPI mInstance; 18 | 19 | public static AliPayAPI getInstance() { 20 | if (mInstance == null) { 21 | synchronized (mLock) { 22 | if (mInstance == null) { 23 | mInstance = new AliPayAPI(); 24 | } 25 | } 26 | } 27 | return mInstance; 28 | } 29 | 30 | 31 | /** 32 | * 发送支付宝支付请求 33 | * 34 | * @param aliPayReq 35 | */ 36 | public void sendPayReq(AliPayReq aliPayReq) { 37 | if (this.mPayReq!=null){ 38 | this.mPayReq.release(); 39 | this.mPayReq = null; 40 | } 41 | this.mPayReq = aliPayReq; 42 | aliPayReq.send(); 43 | } 44 | 45 | public void release() { 46 | if (this.mPayReq != null) { 47 | this.mPayReq.release(); 48 | this.mPayReq = null; 49 | mInstance = null; 50 | } 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/ali/AliPayReq.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.ali; 2 | 3 | import android.app.Activity; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.text.TextUtils; 7 | 8 | import com.alipay.sdk.app.PayTask; 9 | import com.yi2580.easypay.EasyPay; 10 | import com.yi2580.easypay.bean.AliBizContent; 11 | import com.yi2580.easypay.bean.AliPayResult; 12 | import com.yi2580.easypay.utils.ALiPayUtils; 13 | 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | /** 18 | * 支付宝支付请求 19 | * 支付宝APP支付文档 https://docs.open.alipay.com/204 20 | */ 21 | public class AliPayReq { 22 | 23 | private static final int SDK_PAY_FLAG = 1; 24 | private static final int SDK_AUTH_FLAG = 2; 25 | 26 | private AliPayResultCallBack mCallback; 27 | 28 | private Activity mActivity; 29 | 30 | //订单信息 31 | private String orderInfo; 32 | 33 | private Handler mHandler; 34 | 35 | public AliPayReq() { 36 | super(); 37 | mHandler = new MyHandler(); 38 | } 39 | 40 | private class MyHandler extends Handler { 41 | @Override 42 | public void handleMessage(Message msg) { 43 | switch (msg.what) { 44 | case SDK_PAY_FLAG: { 45 | if (mCallback != null) { 46 | AliPayResult payResult = new AliPayResult((Map) msg.obj); 47 | /** 48 | * 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。 49 | */ 50 | String resultInfo = payResult.getResult(); 51 | String resultStatus = payResult.getResultStatus(); 52 | String memo = payResult.getMemo(); 53 | // 判断resultStatus 为9000则代表支付成功 54 | if (TextUtils.equals(resultStatus, AliPayAPI.ALI_PAY_SUCCESS)) { 55 | // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。 56 | mCallback.onSuccess(resultInfo); 57 | } else if (TextUtils.equals(resultStatus, AliPayAPI.ALI_PAY_DEALING)) { 58 | //正在处理中,支付结果未知 59 | mCallback.onDealing(resultInfo); 60 | } else if (TextUtils.equals(resultStatus, AliPayAPI.ALI_PAY_CANCEL)) { 61 | //用户取消支付 62 | mCallback.onCancel(); 63 | } else { 64 | // 该笔订单真实的支付结果,需要依赖服务端的异步通知。 65 | mCallback.onError(resultStatus, memo); 66 | } 67 | } 68 | } 69 | break; 70 | default: 71 | } 72 | } 73 | } 74 | 75 | 76 | /** 77 | * 发送支付宝支付请求 78 | */ 79 | public void send() { 80 | if (mActivity == null || mHandler == null) 81 | return; 82 | Runnable payRunnable = new Runnable() { 83 | 84 | @Override 85 | public void run() { 86 | PayTask alipay = new PayTask(mActivity); 87 | Map result = alipay.payV2(orderInfo, true); 88 | Message msg = new Message(); 89 | msg.what = SDK_PAY_FLAG; 90 | msg.obj = result; 91 | mHandler.sendMessage(msg); 92 | } 93 | }; 94 | 95 | // 必须异步调用 96 | Thread payThread = new Thread(payRunnable); 97 | payThread.start(); 98 | } 99 | 100 | /** 101 | * 设置回调事件 102 | * 103 | * @param callBack 回调 104 | * @return 105 | */ 106 | public AliPayReq setResultCallBack(AliPayResultCallBack callBack) { 107 | this.mCallback = callBack; 108 | return this; 109 | } 110 | 111 | public static class Builder { 112 | //上下文 113 | private Activity activity; 114 | private String orderInfo; 115 | //支付宝分配给开发者的应用ID 116 | private String appId = EasyPay.ALI_PAY_APP_ID; 117 | //支付宝服务器主动通知商户服务器里指定的页面http/https路径。建议商户使用https 118 | private String notifyUrl = EasyPay.ALI_PAY_NOTIFY_URL; 119 | //发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 120 | private String timestamp; 121 | 122 | //接口名称 123 | private String method = "alipay.trade.app.pay"; 124 | 125 | //商户请求参数的签名串 126 | private String sign; 127 | //pkcs8 格式的商户私钥。 128 | private String privateKey; 129 | //商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 130 | private String signType = "RSA2"; 131 | 132 | //仅支持JSON 133 | private String format = "JSON"; 134 | //调用的接口版本,固定为:1.0 135 | private String version = "1.0"; 136 | //请求使用的编码格式,如utf-8,gbk,gb2312等 137 | //private String charset = ALiPayUtils.DEFAULT_CHARSET; 138 | private String charset = "utf-8"; 139 | 140 | 141 | private AliBizContent bizContent; 142 | private String bizContentStr; 143 | 144 | public Builder() { 145 | super(); 146 | } 147 | 148 | public Builder with(Activity activity) { 149 | this.activity = activity; 150 | return this; 151 | } 152 | 153 | public Builder with(Activity activity, String orderInfo) { 154 | this.activity = activity; 155 | this.orderInfo = orderInfo; 156 | return this; 157 | } 158 | 159 | /** 160 | * 设置支付宝支付AppID 161 | * 162 | * @param appId AppId 163 | * @return 164 | */ 165 | public Builder setAppId(String appId) { 166 | this.appId = appId; 167 | return this; 168 | } 169 | 170 | /** 171 | * 商户请求参数的签名串 172 | * 173 | * @param sign https://docs.open.alipay.com/291/105974 174 | * @return 175 | */ 176 | public Builder setSign(String sign) { 177 | this.sign = sign; 178 | return this; 179 | } 180 | 181 | /** 182 | * RSA2或者RSA 183 | * 184 | * @param signType 185 | * @return 186 | */ 187 | public Builder setSignType(String signType) { 188 | this.signType = signType; 189 | return this; 190 | } 191 | 192 | /** 193 | * 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 194 | * 195 | * @param timestamp 196 | * @return 197 | */ 198 | public Builder setTimestamp(String timestamp) { 199 | this.timestamp = timestamp; 200 | return this; 201 | } 202 | 203 | /** 204 | * 业务请求参数的集合 205 | * 206 | * @param bizContent 207 | * @return 208 | */ 209 | public Builder setBizContent(AliBizContent bizContent) { 210 | this.bizContent = bizContent; 211 | return this; 212 | } 213 | 214 | public Builder setBizContentStr(String bizContentStr) { 215 | this.bizContentStr = bizContentStr; 216 | return this; 217 | } 218 | 219 | /** 220 | * 支付宝服务器主动通知商户服务器里指定的页面 221 | * 222 | * @param notifyUrl 223 | * @return 224 | */ 225 | public Builder setNotifyUrl(String notifyUrl) { 226 | this.notifyUrl = notifyUrl; 227 | return this; 228 | } 229 | 230 | /** 231 | * 设置方法名 232 | * 233 | * @param method 234 | * @return 235 | */ 236 | public Builder setMethod(String method) { 237 | this.method = method; 238 | return this; 239 | } 240 | 241 | /** 242 | * 使用私钥客户端完成签名 243 | * 244 | * @param privateKey 245 | * @return 246 | */ 247 | public Builder setPrivateKey(String privateKey) { 248 | this.privateKey = privateKey; 249 | return this; 250 | } 251 | 252 | public AliPayReq create() { 253 | if (TextUtils.isEmpty(this.orderInfo)) { 254 | //如果签名和私钥同时为空,抛出异常 255 | if (TextUtils.isEmpty(sign) && TextUtils.isEmpty(privateKey)) 256 | throw new NullPointerException("sign is null"); 257 | 258 | if (bizContent == null && TextUtils.isEmpty(bizContentStr)) 259 | throw new NullPointerException("AliBizContent is null"); 260 | 261 | if (TextUtils.isEmpty(timestamp)) { 262 | timestamp = ALiPayUtils.getTimestamp(); 263 | } 264 | 265 | //判断签名方式 266 | boolean rsa2 = TextUtils.equals(signType, "RSA2"); 267 | 268 | Map keyValues = new HashMap(); 269 | keyValues.put("app_id", appId); 270 | keyValues.put("method", method); 271 | keyValues.put("format", format); 272 | keyValues.put("charset", charset); 273 | keyValues.put("sign_type", signType); 274 | keyValues.put("timestamp", timestamp); 275 | keyValues.put("version", version); 276 | keyValues.put("notify_url", notifyUrl); 277 | if (!TextUtils.isEmpty(bizContentStr)) { 278 | keyValues.put("biz_content", bizContentStr); 279 | } else { 280 | keyValues.put("biz_content", bizContent.toJson()); 281 | } 282 | 283 | String orderParam = ALiPayUtils.buildOrderParam(keyValues); 284 | 285 | //签名为空,私钥不为空,本地生成签名 286 | if (TextUtils.isEmpty(sign) && !TextUtils.isEmpty(privateKey)) { 287 | sign = ALiPayUtils.getSign(keyValues, privateKey, rsa2); 288 | } else { 289 | sign = ALiPayUtils.encodeUTF8(sign); 290 | } 291 | this.orderInfo = orderParam + "&sign=" + sign; 292 | } 293 | 294 | 295 | AliPayReq aliPayReq = new AliPayReq(); 296 | aliPayReq.mActivity = this.activity; 297 | aliPayReq.orderInfo = this.orderInfo; 298 | 299 | return aliPayReq; 300 | } 301 | 302 | } 303 | 304 | protected void release() { 305 | mHandler = null; 306 | this.mCallback = null; 307 | this.mActivity = null; 308 | } 309 | 310 | } 311 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/ali/AliPayResultCallBack.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.ali; 2 | 3 | /** 4 | * @author zhangqi 5 | * Date:2019/3/8 6 | * Description:支付宝支付结果回调 7 | */ 8 | public interface AliPayResultCallBack { 9 | /** 10 | * 支付成功 11 | */ 12 | void onSuccess(String result); 13 | 14 | /** 15 | * 正在处理中,支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态 16 | */ 17 | void onDealing(String result); 18 | 19 | /** 20 | * 支付失败 21 | * https://docs.open.alipay.com/204/105301/ 22 | * 23 | * @param resultStatus 24 | */ 25 | void onError(String resultStatus, String message); 26 | 27 | /** 28 | * 支付取消 29 | */ 30 | void onCancel(); 31 | } 32 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/bean/AliBizContent.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.bean; 2 | 3 | /** 4 | * @author zhangqi 5 | * Date:2019/3/9 6 | * Description:支付宝支付业务参数 7 | */ 8 | public class AliBizContent { 9 | 10 | /** 11 | * 商户网站唯一订单号 12 | */ 13 | private String out_trade_no; 14 | 15 | /** 16 | * 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] 17 | */ 18 | private String total_amount; 19 | 20 | /** 21 | * 商品的标题/交易标题/订单标题/订单关键字等。 22 | */ 23 | private String subject; 24 | 25 | /** 26 | * 对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。 27 | */ 28 | private String body; 29 | 30 | /** 31 | * 该笔订单允许的最晚付款时间,逾期将关闭交易。 32 | * 取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 33 | * 该参数数值不接受小数点, 如 1.5h,可转换为 90m。 34 | * 注:若为空,则默认为15d。 35 | */ 36 | private String timeout_express = "15d"; 37 | 38 | /** 39 | * 销售产品码,商家和支付宝签约的产品码,为固定值QUICK_MSECURITY_PAY 40 | */ 41 | private String product_code = "QUICK_MSECURITY_PAY"; 42 | 43 | /** 44 | * 商品主类型:0—虚拟类商品,1—实物类商品 45 | * 注:虚拟类商品不支持使用花呗渠道 46 | */ 47 | private int goods_type; 48 | 49 | public AliBizContent() { 50 | } 51 | 52 | public String getOut_trade_no() { 53 | return out_trade_no; 54 | } 55 | 56 | public AliBizContent(String out_trade_no,String total_amount, String subject, String body) { 57 | this.out_trade_no = out_trade_no; 58 | this.total_amount = total_amount; 59 | this.subject = subject; 60 | this.body = body; 61 | } 62 | 63 | 64 | public void setOut_trade_no(String out_trade_no) { 65 | this.out_trade_no = out_trade_no; 66 | } 67 | 68 | public String getTotal_amount() { 69 | return total_amount; 70 | } 71 | 72 | public void setTotal_amount(String total_amount) { 73 | this.total_amount = total_amount; 74 | } 75 | 76 | public String getSubject() { 77 | return subject; 78 | } 79 | 80 | public void setSubject(String subject) { 81 | this.subject = subject; 82 | } 83 | 84 | public String getBody() { 85 | return body; 86 | } 87 | 88 | public void setBody(String body) { 89 | this.body = body; 90 | } 91 | 92 | public String getTimeout_express() { 93 | return timeout_express; 94 | } 95 | 96 | public void setTimeout_express(String timeout_express) { 97 | this.timeout_express = timeout_express; 98 | } 99 | 100 | 101 | public String getGoods_type() { 102 | return String.valueOf(goods_type); 103 | } 104 | 105 | public void setGoods_type(int goods_type) { 106 | this.goods_type = goods_type; 107 | } 108 | 109 | @Override 110 | public String toString() { 111 | final StringBuffer sb = new StringBuffer("AliBizContent{"); 112 | sb.append("out_trade_no='").append(out_trade_no).append('\''); 113 | sb.append(", total_amount='").append(total_amount).append('\''); 114 | sb.append(", subject='").append(subject).append('\''); 115 | sb.append(", body='").append(body).append('\''); 116 | sb.append(", timeout_express='").append(timeout_express).append('\''); 117 | sb.append(", product_code='").append(product_code).append('\''); 118 | sb.append(", goods_type=").append(goods_type); 119 | sb.append('}'); 120 | return sb.toString(); 121 | } 122 | 123 | public String toJson(){ 124 | final StringBuffer sb = new StringBuffer("{"); 125 | sb.append("\"out_trade_no\"").append(":\"").append(out_trade_no).append("\","); 126 | sb.append("\"total_amount\"").append(":\"").append(total_amount).append("\","); 127 | sb.append("\"subject\"").append(":\"").append(subject).append("\","); 128 | sb.append("\"body\"").append(":\"").append(body).append("\","); 129 | sb.append("\"timeout_express\"").append(":\"").append(timeout_express).append("\","); 130 | sb.append("\"product_code\"").append(":\"").append(product_code).append("\","); 131 | sb.append("\"goods_type\"").append(":\"").append(goods_type).append("\""); 132 | sb.append('}'); 133 | return sb.toString(); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/bean/AliPayResult.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.bean; import android.text.TextUtils; import java.util.Map; /** * 参数说明 * https://docs.open.alipay.com/204/105301 */ public class AliPayResult { private String resultStatus; private String result; private String memo; public AliPayResult(Map rawResult) { if (rawResult == null) { return; } for (String key : rawResult.keySet()) { if (TextUtils.equals(key, "resultStatus")) { resultStatus = rawResult.get(key); } else if (TextUtils.equals(key, "result")) { result = rawResult.get(key); } else if (TextUtils.equals(key, "memo")) { memo = rawResult.get(key); } } } @Override public String toString() { final StringBuffer sb = new StringBuffer("AliPayResult{"); sb.append("resultStatus='").append(resultStatus).append('\''); sb.append(", result='").append(result).append('\''); sb.append(", memo='").append(memo).append('\''); sb.append('}'); return sb.toString(); } /** * @return the resultStatus */ public String getResultStatus() { return resultStatus; } /** * @return the memo */ public String getMemo() { return memo; } /** * @return the result */ public String getResult() { return result; } /** * { * "memo" : "xxxxx", * "result" : "{ * \"alipay_trade_app_pay_response\":{ * \"code\":\"10000\", * \"msg\":\"Success\", * \"app_id\":\"2014072300007148\", * \"out_trade_no\":\"081622560194853\", * \"trade_no\":\"2016081621001004400236957647\", * \"total_amount\":\"0.01\", * \"seller_id\":\"2088702849871851\", * \"charset\":\"utf-8\", * \"timestamp\":\"2016-10-11 17:43:36\" * }, * \"sign\":\"NGfStJf3i3ooWBuCDIQSumOpaGBcQz+aoAqyGh3W6EqA/gmyPYwLJ2REFijY9XPTApI9YglZyMw+ZMhd3kb0mh4RAXMrb6mekX4Zu8Nf6geOwIa9kLOnw0IMCjxi4abDIfXhxrXyj********\", * \"sign_type\":\"RSA2\" * }", * "resultStatus" : "9000" * } */ } -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/utils/ALiPayUtils.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.utils; 2 | 3 | 4 | import java.io.UnsupportedEncodingException; 5 | import java.net.URLEncoder; 6 | import java.security.KeyFactory; 7 | import java.security.PrivateKey; 8 | import java.security.spec.PKCS8EncodedKeySpec; 9 | import java.text.DateFormat; 10 | import java.text.SimpleDateFormat; 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.Date; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | * @author zhangqi 19 | * Date:2019/3/9 20 | * Description: 21 | */ 22 | public class ALiPayUtils { 23 | 24 | //非对称密钥算法 25 | public static final String KEY_ALGORITHM = "RSA"; 26 | 27 | private static final String SIGN_ALGORITHMS = "SHA1WithRSA"; 28 | 29 | private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA"; 30 | 31 | public static final String DEFAULT_CHARSET = "UTF-8"; 32 | 33 | /** 34 | * 构造支付订单参数信息 35 | * 36 | * @param map 支付订单参数 37 | * @return 38 | */ 39 | public static String buildOrderParam(Map map) { 40 | List keys = new ArrayList(map.keySet()); 41 | 42 | StringBuilder sb = new StringBuilder(); 43 | for (int i = 0; i < keys.size(); i++) { 44 | String key = keys.get(i); 45 | String value = map.get(key); 46 | sb.append(buildKeyValue(key, value, true)); 47 | //最后一个不添加&字符 48 | if (i != keys.size() - 1) { 49 | sb.append("&"); 50 | } 51 | } 52 | 53 | return sb.toString(); 54 | } 55 | 56 | /** 57 | * 获取签名串 58 | * 59 | * @param map 订单参数集合 60 | * @param rsaKey 私钥 61 | * @param rsa2 是否使用rsa2加密 62 | * @return 63 | */ 64 | public static String getSign(Map map, String rsaKey, boolean rsa2) { 65 | List keys = new ArrayList(map.keySet()); 66 | // key排序 67 | Collections.sort(keys); 68 | int keySize = keys.size(); 69 | 70 | StringBuilder orderInfo = new StringBuilder(); 71 | for (int i = 0; i < keySize; i++) { 72 | String key = keys.get(i); 73 | String value = map.get(key); 74 | 75 | orderInfo.append(buildKeyValue(key, value, false)); 76 | //最后一个不添加&字符 77 | if (i != keys.size() - 1) { 78 | orderInfo.append("&"); 79 | } 80 | } 81 | 82 | String oriSign = sign(orderInfo.toString(), rsaKey, rsa2); 83 | return encodeUTF8(oriSign); 84 | } 85 | 86 | /** 87 | * 签名 88 | * 89 | * @param content 90 | * @param privateKey 91 | * @param rsa2 92 | * @return 93 | */ 94 | private static String sign(String content, String privateKey, boolean rsa2) { 95 | try { 96 | PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( 97 | Base64.decode(privateKey)); 98 | KeyFactory keyf = KeyFactory.getInstance(KEY_ALGORITHM); 99 | PrivateKey priKey = keyf.generatePrivate(priPKCS8); 100 | 101 | java.security.Signature signature = java.security.Signature 102 | .getInstance(rsa2 ? SIGN_SHA256RSA_ALGORITHMS : SIGN_ALGORITHMS); 103 | 104 | signature.initSign(priKey); 105 | signature.update(content.getBytes(DEFAULT_CHARSET)); 106 | 107 | byte[] signed = signature.sign(); 108 | 109 | return Base64.encode(signed); 110 | } catch (Exception e) { 111 | e.printStackTrace(); 112 | } 113 | 114 | return null; 115 | } 116 | 117 | /** 118 | * 拼接键值对 119 | * 120 | * @param key 121 | * @param value 122 | * @param isEncode 123 | * @return 124 | */ 125 | private static String buildKeyValue(String key, String value, boolean isEncode) { 126 | StringBuilder sb = new StringBuilder(); 127 | sb.append(key); 128 | sb.append("="); 129 | if (isEncode) { 130 | sb.append(encodeUTF8(value)); 131 | } else { 132 | sb.append(value); 133 | } 134 | return sb.toString(); 135 | } 136 | 137 | public static String encodeUTF8(String value) { 138 | try { 139 | return URLEncoder.encode(value, DEFAULT_CHARSET); 140 | } catch (UnsupportedEncodingException e) { 141 | return value; 142 | } 143 | } 144 | 145 | /** 146 | * 根据系统时间生成时间戳 147 | * 148 | * @return 149 | */ 150 | public static String getTimestamp() { 151 | DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 152 | return dateFormat.format(new Date()); 153 | } 154 | 155 | } 156 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/utils/Base64.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.utils; 2 | 3 | public final class Base64 { 4 | 5 | private static final int BASELENGTH = 128; 6 | private static final int LOOKUPLENGTH = 64; 7 | private static final int TWENTYFOURBITGROUP = 24; 8 | private static final int EIGHTBIT = 8; 9 | private static final int SIXTEENBIT = 16; 10 | private static final int FOURBYTE = 4; 11 | private static final int SIGN = -128; 12 | private static char PAD = '='; 13 | private static byte[] base64Alphabet = new byte[BASELENGTH]; 14 | private static char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; 15 | 16 | static { 17 | for (int i = 0; i < BASELENGTH; ++i) { 18 | base64Alphabet[i] = -1; 19 | } 20 | for (int i = 'Z'; i >= 'A'; i--) { 21 | base64Alphabet[i] = (byte) (i - 'A'); 22 | } 23 | for (int i = 'z'; i >= 'a'; i--) { 24 | base64Alphabet[i] = (byte) (i - 'a' + 26); 25 | } 26 | 27 | for (int i = '9'; i >= '0'; i--) { 28 | base64Alphabet[i] = (byte) (i - '0' + 52); 29 | } 30 | 31 | base64Alphabet['+'] = 62; 32 | base64Alphabet['/'] = 63; 33 | 34 | for (int i = 0; i <= 25; i++) { 35 | lookUpBase64Alphabet[i] = (char) ('A' + i); 36 | } 37 | 38 | for (int i = 26, j = 0; i <= 51; i++, j++) { 39 | lookUpBase64Alphabet[i] = (char) ('a' + j); 40 | } 41 | 42 | for (int i = 52, j = 0; i <= 61; i++, j++) { 43 | lookUpBase64Alphabet[i] = (char) ('0' + j); 44 | } 45 | lookUpBase64Alphabet[62] = (char) '+'; 46 | lookUpBase64Alphabet[63] = (char) '/'; 47 | 48 | } 49 | 50 | private static boolean isWhiteSpace(char octect) { 51 | return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); 52 | } 53 | 54 | private static boolean isPad(char octect) { 55 | return (octect == PAD); 56 | } 57 | 58 | private static boolean isData(char octect) { 59 | return (octect < BASELENGTH && base64Alphabet[octect] != -1); 60 | } 61 | 62 | /** 63 | * Encodes hex octects into Base64 64 | * 65 | * @param binaryData 66 | * Array containing binaryData 67 | * @return Encoded Base64 array 68 | */ 69 | public static String encode(byte[] binaryData) { 70 | 71 | if (binaryData == null) { 72 | return null; 73 | } 74 | 75 | int lengthDataBits = binaryData.length * EIGHTBIT; 76 | if (lengthDataBits == 0) { 77 | return ""; 78 | } 79 | 80 | int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; 81 | int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; 82 | int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 83 | : numberTriplets; 84 | char encodedData[] = null; 85 | 86 | encodedData = new char[numberQuartet * 4]; 87 | 88 | byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; 89 | 90 | int encodedIndex = 0; 91 | int dataIndex = 0; 92 | 93 | for (int i = 0; i < numberTriplets; i++) { 94 | b1 = binaryData[dataIndex++]; 95 | b2 = binaryData[dataIndex++]; 96 | b3 = binaryData[dataIndex++]; 97 | 98 | l = (byte) (b2 & 0x0f); 99 | k = (byte) (b1 & 0x03); 100 | 101 | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 102 | : (byte) ((b1) >> 2 ^ 0xc0); 103 | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) 104 | : (byte) ((b2) >> 4 ^ 0xf0); 105 | byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) 106 | : (byte) ((b3) >> 6 ^ 0xfc); 107 | 108 | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 109 | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; 110 | encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; 111 | encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; 112 | } 113 | 114 | // form integral number of 6-bit groups 115 | if (fewerThan24bits == EIGHTBIT) { 116 | b1 = binaryData[dataIndex]; 117 | k = (byte) (b1 & 0x03); 118 | 119 | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 120 | : (byte) ((b1) >> 2 ^ 0xc0); 121 | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 122 | encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; 123 | encodedData[encodedIndex++] = PAD; 124 | encodedData[encodedIndex++] = PAD; 125 | } else if (fewerThan24bits == SIXTEENBIT) { 126 | b1 = binaryData[dataIndex]; 127 | b2 = binaryData[dataIndex + 1]; 128 | l = (byte) (b2 & 0x0f); 129 | k = (byte) (b1 & 0x03); 130 | 131 | byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) 132 | : (byte) ((b1) >> 2 ^ 0xc0); 133 | byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) 134 | : (byte) ((b2) >> 4 ^ 0xf0); 135 | 136 | encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; 137 | encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; 138 | encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; 139 | encodedData[encodedIndex++] = PAD; 140 | } 141 | 142 | return new String(encodedData); 143 | } 144 | 145 | /** 146 | * Decodes Base64 data into octects 147 | * 148 | * @param encoded 149 | * string containing Base64 data 150 | * @return Array containind decoded data. 151 | */ 152 | public static byte[] decode(String encoded) { 153 | 154 | if (encoded == null) { 155 | return null; 156 | } 157 | 158 | char[] base64Data = encoded.toCharArray(); 159 | // remove white spaces 160 | int len = removeWhiteSpace(base64Data); 161 | 162 | if (len % FOURBYTE != 0) { 163 | return null;// should be divisible by four 164 | } 165 | 166 | int numberQuadruple = (len / FOURBYTE); 167 | 168 | if (numberQuadruple == 0) { 169 | return new byte[0]; 170 | } 171 | 172 | byte decodedData[] = null; 173 | byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; 174 | char d1 = 0, d2 = 0, d3 = 0, d4 = 0; 175 | 176 | int i = 0; 177 | int encodedIndex = 0; 178 | int dataIndex = 0; 179 | decodedData = new byte[(numberQuadruple) * 3]; 180 | 181 | for (; i < numberQuadruple - 1; i++) { 182 | 183 | if (!isData((d1 = base64Data[dataIndex++])) 184 | || !isData((d2 = base64Data[dataIndex++])) 185 | || !isData((d3 = base64Data[dataIndex++])) 186 | || !isData((d4 = base64Data[dataIndex++]))) { 187 | return null; 188 | }// if found "no data" just return null 189 | 190 | b1 = base64Alphabet[d1]; 191 | b2 = base64Alphabet[d2]; 192 | b3 = base64Alphabet[d3]; 193 | b4 = base64Alphabet[d4]; 194 | 195 | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 196 | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 197 | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); 198 | } 199 | 200 | if (!isData((d1 = base64Data[dataIndex++])) 201 | || !isData((d2 = base64Data[dataIndex++]))) { 202 | return null;// if found "no data" just return null 203 | } 204 | 205 | b1 = base64Alphabet[d1]; 206 | b2 = base64Alphabet[d2]; 207 | 208 | d3 = base64Data[dataIndex++]; 209 | d4 = base64Data[dataIndex++]; 210 | if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters 211 | if (isPad(d3) && isPad(d4)) { 212 | if ((b2 & 0xf) != 0)// last 4 bits should be zero 213 | { 214 | return null; 215 | } 216 | byte[] tmp = new byte[i * 3 + 1]; 217 | System.arraycopy(decodedData, 0, tmp, 0, i * 3); 218 | tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); 219 | return tmp; 220 | } else if (!isPad(d3) && isPad(d4)) { 221 | b3 = base64Alphabet[d3]; 222 | if ((b3 & 0x3) != 0)// last 2 bits should be zero 223 | { 224 | return null; 225 | } 226 | byte[] tmp = new byte[i * 3 + 2]; 227 | System.arraycopy(decodedData, 0, tmp, 0, i * 3); 228 | tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 229 | tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 230 | return tmp; 231 | } else { 232 | return null; 233 | } 234 | } else { // No PAD e.g 3cQl 235 | b3 = base64Alphabet[d3]; 236 | b4 = base64Alphabet[d4]; 237 | decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); 238 | decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); 239 | decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); 240 | 241 | } 242 | 243 | return decodedData; 244 | } 245 | 246 | /** 247 | * remove WhiteSpace from MIME containing encoded Base64 data. 248 | * 249 | * @param data 250 | * the byte array of base64 data (with WS) 251 | * @return the new length 252 | */ 253 | private static int removeWhiteSpace(char[] data) { 254 | if (data == null) { 255 | return 0; 256 | } 257 | 258 | // count characters that's not whitespace 259 | int newSize = 0; 260 | int len = data.length; 261 | for (int i = 0; i < len; i++) { 262 | if (!isWhiteSpace(data[i])) { 263 | data[newSize++] = data[i]; 264 | } 265 | } 266 | return newSize; 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/utils/WeChatPayUtils.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.utils; 2 | 3 | import android.text.TextUtils; 4 | 5 | import com.tencent.mm.opensdk.modelpay.PayReq; 6 | 7 | import java.security.MessageDigest; 8 | import java.util.Iterator; 9 | import java.util.Map; 10 | import java.util.Random; 11 | import java.util.Set; 12 | import java.util.TreeMap; 13 | 14 | /** 15 | * @author zhangqi 16 | * Date:2019/3/16 17 | * Description:微信支付工具类 18 | */ 19 | public class WeChatPayUtils { 20 | 21 | /** 22 | * @param appId 微信支付AppID 23 | * @param partnerId 微信支付商户号 24 | * @param prepayId 预支付码 25 | * @param packageValue 包名 26 | * @param nonceStr 随机字符串 27 | * @param timeStamp 时间戳 28 | * @param sign 签名 29 | * @param privateKey 私钥 30 | * @return 31 | */ 32 | public static PayReq getPayReq(String appId, String partnerId, String prepayId, 33 | String packageValue, String nonceStr, String timeStamp, String sign, String privateKey) { 34 | if (TextUtils.isEmpty(nonceStr)) { 35 | nonceStr = genNonceStr(); 36 | } 37 | if (TextUtils.isEmpty(timeStamp)) { 38 | timeStamp = getTimeStamp(); 39 | } 40 | 41 | if (TextUtils.isEmpty(sign)) { 42 | TreeMap treeMap = new TreeMap<>(); 43 | treeMap.put("appid", appId); 44 | treeMap.put("noncestr", nonceStr); 45 | treeMap.put("package", packageValue); 46 | treeMap.put("partnerid", partnerId); 47 | treeMap.put("prepayid", prepayId); 48 | treeMap.put("timestamp", timeStamp); 49 | sign = genSign(treeMap, privateKey); 50 | } 51 | 52 | PayReq payReq = new PayReq(); 53 | payReq.appId = appId; 54 | payReq.partnerId = partnerId; 55 | payReq.prepayId = prepayId; 56 | payReq.nonceStr = nonceStr; 57 | payReq.timeStamp = timeStamp; 58 | payReq.packageValue = packageValue; 59 | payReq.sign = sign; 60 | 61 | return payReq; 62 | } 63 | 64 | /** 65 | * 获取时间戳 66 | * 67 | * @return 68 | */ 69 | private static String getTimeStamp() { 70 | //System.currentTimeMillis() 获取毫秒 71 | return String.valueOf(System.currentTimeMillis() / 1000); 72 | } 73 | 74 | /** 75 | * 随机字符串,不长于32位 76 | * 77 | * @return 78 | */ 79 | private static String genNonceStr() { 80 | Random random = new Random(); 81 | return getMessageDigest(String.valueOf(random.nextInt(Integer.MAX_VALUE)).getBytes()); 82 | } 83 | 84 | /** 85 | * 获取签名 86 | * 87 | * @param treeMap 88 | * @return 89 | */ 90 | private static String genSign(TreeMap treeMap, String privateKey) { 91 | StringBuilder sb = new StringBuilder(); 92 | Set> set = treeMap.entrySet(); 93 | Iterator> iterator = set.iterator(); 94 | String key; 95 | while (iterator.hasNext()) { 96 | key = iterator.next().getKey(); 97 | sb.append(key); 98 | sb.append('='); 99 | sb.append(treeMap.get(key)); 100 | sb.append('&'); 101 | } 102 | sb.append("key="); 103 | sb.append(privateKey); 104 | String sign = getMessageDigest(sb.toString().getBytes()).toUpperCase(); 105 | return sign; 106 | } 107 | 108 | private static String getMessageDigest(byte[] buffer) { 109 | char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 110 | try { 111 | // 获取MD5转换器 112 | MessageDigest mdTemp = MessageDigest.getInstance("MD5"); 113 | mdTemp.update(buffer); 114 | byte[] md = mdTemp.digest(); 115 | int j = md.length; 116 | char str[] = new char[j * 2]; 117 | int k = 0; 118 | for (int i = 0; i < j; i++) { 119 | byte byte0 = md[i]; 120 | str[k++] = hexDigits[byte0 >>> 4 & 0xf]; 121 | str[k++] = hexDigits[byte0 & 0xf]; 122 | } 123 | //转成32位的字符串 124 | return new String(str); 125 | } catch (Exception e) { 126 | return null; 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/wechat/WechatPayAPI.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.wechat; 2 | 3 | /** 4 | * 微信支付API 5 | */ 6 | public class WechatPayAPI { 7 | 8 | public static final int WECHAT_PAY_SUCCESS = 0; 9 | public static final int WECHAT_PAY_FAIL = -1; 10 | public static final int WECHAT_PAY_CANCEL = -2; 11 | 12 | private WechatPayReq mPayReq; 13 | 14 | /** 15 | * 获取微信支付API 16 | */ 17 | private static final Object mLock = new Object(); 18 | private static WechatPayAPI mInstance; 19 | 20 | public static WechatPayAPI getInstance() { 21 | if (mInstance == null) { 22 | synchronized (mLock) { 23 | if (mInstance == null) { 24 | mInstance = new WechatPayAPI(); 25 | } 26 | } 27 | } 28 | return mInstance; 29 | } 30 | 31 | /** 32 | * 发送微信支付请求 33 | * 34 | * @param wechatPayReq 35 | */ 36 | public void sendPayReq(WechatPayReq wechatPayReq) { 37 | if (this.mPayReq!=null){ 38 | this.mPayReq.release(); 39 | this.mPayReq = null; 40 | } 41 | this.mPayReq = wechatPayReq; 42 | wechatPayReq.send(); 43 | } 44 | 45 | /** 46 | * 获取PayReq 47 | * @return 48 | */ 49 | public WechatPayReq getPayReq() { 50 | return mPayReq; 51 | } 52 | 53 | public void release() { 54 | if (this.mPayReq != null) { 55 | this.mPayReq.release(); 56 | this.mPayReq = null; 57 | mInstance = null; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/wechat/WechatPayCallbackActivity.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.wechat; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | 7 | import com.tencent.mm.opensdk.constants.ConstantsAPI; 8 | import com.tencent.mm.opensdk.modelbase.BaseReq; 9 | import com.tencent.mm.opensdk.modelbase.BaseResp; 10 | import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler; 11 | import com.yi2580.easypay.R; 12 | 13 | /** 14 | * @author zhangqi 15 | * Date:2019/3/8 16 | * Description:微信支付回调Activity 17 | */ 18 | public class WechatPayCallbackActivity extends Activity implements IWXAPIEventHandler { 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_wechat_pay_callback); 24 | 25 | if (WechatPayAPI.getInstance().getPayReq() != null 26 | && WechatPayAPI.getInstance().getPayReq().getWXApi() != null) { 27 | WechatPayAPI.getInstance().getPayReq().getWXApi().handleIntent(getIntent(), this); 28 | } else { 29 | finish(); 30 | } 31 | } 32 | 33 | @Override 34 | protected void onNewIntent(Intent intent) { 35 | super.onNewIntent(intent); 36 | setIntent(intent); 37 | if (WechatPayAPI.getInstance().getPayReq() != null 38 | && WechatPayAPI.getInstance().getPayReq().getWXApi() != null) { 39 | WechatPayAPI.getInstance().getPayReq().getWXApi().handleIntent(intent, this); 40 | } 41 | } 42 | 43 | @Override 44 | public void onReq(BaseReq baseReq) { 45 | 46 | } 47 | 48 | @Override 49 | public void onResp(BaseResp baseResp) { 50 | if(baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { 51 | if(WechatPayAPI.getInstance().getPayReq() != null) { 52 | WechatPayAPI.getInstance().getPayReq().onResp(baseResp); 53 | finish(); 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/wechat/WechatPayReq.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.wechat; 2 | 3 | import android.app.Activity; 4 | import android.text.TextUtils; 5 | 6 | import com.tencent.mm.opensdk.modelbase.BaseResp; 7 | import com.tencent.mm.opensdk.modelpay.PayReq; 8 | import com.tencent.mm.opensdk.modelpay.PayResp; 9 | import com.tencent.mm.opensdk.openapi.IWXAPI; 10 | import com.tencent.mm.opensdk.openapi.WXAPIFactory; 11 | import com.yi2580.easypay.EasyPay; 12 | import com.yi2580.easypay.utils.WeChatPayUtils; 13 | 14 | 15 | /** 16 | * 微信支付请求 17 | * 微信APP支付文档 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1 18 | * 19 | * @author Administrator 20 | */ 21 | public class WechatPayReq { 22 | 23 | private static final String TAG = WechatPayReq.class.getSimpleName(); 24 | 25 | private WechatPayResultCallBack mCallback; 26 | 27 | private Activity mActivity; 28 | 29 | private PayReq mPayReq; 30 | 31 | private IWXAPI mWXApi; 32 | 33 | public WechatPayReq() { 34 | super(); 35 | } 36 | 37 | 38 | /** 39 | * 发送微信支付请求 40 | */ 41 | public void send() { 42 | if (mPayReq == null) { 43 | return; 44 | } 45 | mWXApi.sendReq(mPayReq); 46 | } 47 | 48 | /** 49 | * 支付回调响应 50 | * 51 | * @param baseResp 52 | */ 53 | public void onResp(BaseResp baseResp) { 54 | if (mCallback == null) { 55 | return; 56 | } 57 | 58 | if (baseResp.errCode == WechatPayAPI.WECHAT_PAY_SUCCESS) { 59 | //成功 60 | String prepayId = null; 61 | String extData = null; 62 | if (baseResp instanceof PayResp) { 63 | PayResp payResp = (PayResp) baseResp; 64 | prepayId = payResp.prepayId; 65 | extData = payResp.extData; 66 | } 67 | mCallback.onSuccess(prepayId, extData); 68 | } else if (baseResp.errCode == WechatPayAPI.WECHAT_PAY_CANCEL) { 69 | //取消 70 | mCallback.onCancel(); 71 | } else { 72 | //错误 73 | mCallback.onError(baseResp.errCode, baseResp.errStr); 74 | } 75 | mCallback = null; 76 | } 77 | 78 | 79 | public WechatPayReq setWechatPayResultCallBack(WechatPayResultCallBack wechatPayListener) { 80 | this.mCallback = wechatPayListener; 81 | return this; 82 | } 83 | 84 | public IWXAPI getWXApi() { 85 | return mWXApi; 86 | } 87 | 88 | public static class Builder { 89 | //上下文 90 | private Activity activity; 91 | //微信支付AppID 92 | private String appId = EasyPay.WECHAT_PAY_APP_ID; 93 | //微信支付商户号 94 | private String partnerId = EasyPay.WECHAT_PAY_PARTNER_ID; 95 | //预支付码(重要) 96 | private String prepayId; 97 | //"Sign=WXPay" 98 | private String packageValue = "Sign=WXPay"; 99 | private String nonceStr; 100 | //时间戳 101 | private String timeStamp; 102 | //签名 103 | private String sign; 104 | //API密钥,在商户平台设置 105 | private String privateKey; 106 | 107 | public Builder() { 108 | super(); 109 | } 110 | 111 | public Builder with(Activity activity) { 112 | this.activity = activity; 113 | return this; 114 | } 115 | 116 | /** 117 | * 设置微信支付AppID 118 | * 119 | * @param appId 120 | * @return 121 | */ 122 | public Builder setAppId(String appId) { 123 | this.appId = appId; 124 | return this; 125 | } 126 | 127 | /** 128 | * 微信支付商户号 129 | * 130 | * @param partnerId 131 | * @return 132 | */ 133 | public Builder setPartnerId(String partnerId) { 134 | this.partnerId = partnerId; 135 | return this; 136 | } 137 | 138 | /** 139 | * 设置预支付码(重要) 140 | * 141 | * @param prepayId 142 | * @return 143 | */ 144 | public Builder setPrepayId(String prepayId) { 145 | this.prepayId = prepayId; 146 | return this; 147 | } 148 | 149 | 150 | /** 151 | * 设置 152 | * 153 | * @param packageValue 154 | * @return 155 | */ 156 | public Builder setPackageValue(String packageValue) { 157 | this.packageValue = packageValue; 158 | return this; 159 | } 160 | 161 | 162 | /** 163 | * 设置 164 | * 165 | * @param nonceStr 166 | * @return 167 | */ 168 | public Builder setNonceStr(String nonceStr) { 169 | this.nonceStr = nonceStr; 170 | return this; 171 | } 172 | 173 | /** 174 | * 设置时间戳 175 | * 176 | * @param timeStamp 177 | * @return 178 | */ 179 | public Builder setTimeStamp(String timeStamp) { 180 | this.timeStamp = timeStamp; 181 | return this; 182 | } 183 | 184 | /** 185 | * 设置签名 186 | * 187 | * @param sign 188 | * @return 189 | */ 190 | public Builder setSign(String sign) { 191 | this.sign = sign; 192 | return this; 193 | } 194 | 195 | /** 196 | * 使用私钥客户端完成签名 197 | * 198 | * @param privateKey 199 | * @return 200 | */ 201 | public Builder setPrivateKey(String privateKey) { 202 | this.privateKey = privateKey; 203 | return this; 204 | } 205 | 206 | public WechatPayReq create() { 207 | //如果签名和私钥同时为空,抛出异常 208 | if (TextUtils.isEmpty(sign) && TextUtils.isEmpty(privateKey)) 209 | throw new NullPointerException("sign is null"); 210 | 211 | PayReq payReq = WeChatPayUtils.getPayReq(appId, partnerId, prepayId, packageValue, nonceStr, timeStamp, sign, privateKey); 212 | 213 | WechatPayReq wechatPayReq = new WechatPayReq(); 214 | 215 | wechatPayReq.mActivity = this.activity; 216 | wechatPayReq.mPayReq = payReq; 217 | 218 | //微信核心API 219 | wechatPayReq.mWXApi = WXAPIFactory.createWXAPI(this.activity, null); 220 | wechatPayReq.mWXApi.registerApp(this.appId); 221 | return wechatPayReq; 222 | } 223 | 224 | } 225 | 226 | protected void release() { 227 | this.mWXApi = null; 228 | this.mPayReq = null; 229 | this.mCallback = null; 230 | this.mActivity = null; 231 | } 232 | 233 | } 234 | -------------------------------------------------------------------------------- /paylibrary/src/main/java/com/yi2580/easypay/wechat/WechatPayResultCallBack.java: -------------------------------------------------------------------------------- 1 | package com.yi2580.easypay.wechat; 2 | 3 | /** 4 | * @author zhangqi 5 | * Date:2019/3/8 6 | * Description:微信支付结果回调 7 | */ 8 | public interface WechatPayResultCallBack { 9 | /** 10 | * 支付成功 11 | */ 12 | void onSuccess(String prepayId, String extData); 13 | 14 | /** 15 | * 支付失败 16 | */ 17 | void onError(int errorCode, String message); 18 | 19 | /** 20 | * 支付取消 21 | */ 22 | void onCancel(); 23 | } 24 | -------------------------------------------------------------------------------- /paylibrary/src/main/res/layout/activity_wechat_pay_callback.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /paylibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | PayLibrary 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':paylibrary' 2 | --------------------------------------------------------------------------------