├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── demo.keystore ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── chaychan │ │ └── demo │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── chaychan │ │ │ └── demo │ │ │ ├── MainActivity.kt │ │ │ └── WebViewActivity.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── progress_bas_states.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_web_view.xml │ │ ├── menu │ │ └── menu_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 │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── chaychan │ └── demo │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img ├── pay-demo.png └── pay.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | /.idea 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 主要内容 2 | 3 |   关于企业资质开发者申请的微信支付,如何调起微信支付的,官方文档已经很详细,且百度上也有很多相关资料,这里不再介绍。本篇主要讲的是个人资质的支付如何在自己的App中调起微信支付。 4 | 5 | ### 演示 6 | 7 | ![image](https://github.com/chaychan/WechatH5PayDemo/blob/master/img/pay.gif) 8 | 9 | 10 | ### 下载体验 11 | 12 | ![image](https://github.com/chaychan/WechatH5PayDemo/blob/master/img/pay-demo.png) 13 | 14 | 15 | ### 实现方式 16 | 17 | 博客: 18 | 19 | [https://juejin.im/post/6867350169147572237](https://juejin.im/post/6867350169147572237) 20 | 21 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | buildToolsVersion "28.0.3" 8 | 9 | defaultConfig { 10 | applicationId "com.chaychan.demo" 11 | minSdkVersion 21 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | signingConfigs { 20 | appSign { 21 | keyAlias KEY_ALIAS 22 | keyPassword KEY_PASSWORD 23 | storeFile file(KEY_FILE_PATH) 24 | storePassword KEY_STORE_PASSWORD 25 | } 26 | } 27 | 28 | buildTypes { 29 | debug { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 32 | signingConfig signingConfigs.appSign 33 | } 34 | release { 35 | minifyEnabled false 36 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 37 | signingConfig signingConfigs.appSign 38 | } 39 | } 40 | compileOptions { 41 | sourceCompatibility JavaVersion.VERSION_1_8 42 | targetCompatibility JavaVersion.VERSION_1_8 43 | } 44 | kotlinOptions { 45 | jvmTarget = '1.8' 46 | } 47 | } 48 | 49 | dependencies { 50 | implementation fileTree(dir: "libs", include: ["*.jar"]) 51 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 52 | implementation 'androidx.core:core-ktx:1.3.1' 53 | implementation 'androidx.appcompat:appcompat:1.1.0' 54 | implementation 'com.google.android.material:material:1.1.0' 55 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 56 | implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0' 57 | implementation 'androidx.navigation:navigation-ui-ktx:2.3.0' 58 | testImplementation 'junit:junit:4.12' 59 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 60 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 61 | 62 | } -------------------------------------------------------------------------------- /app/demo.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chaychan/WechatH5PayDemo/03d81d3a94dcd7ca1e307c64b650698a6fce0519/app/demo.keystore -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /app/src/androidTest/java/com/chaychan/demo/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.chaychan.demo 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.chaychan.demo", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/chaychan/demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.chaychan.demo 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.appcompat.app.AppCompatActivity 6 | import kotlinx.android.synthetic.main.activity_main.* 7 | 8 | class MainActivity : AppCompatActivity() { 9 | 10 | override fun onCreate(savedInstanceState: Bundle?) { 11 | super.onCreate(savedInstanceState) 12 | setContentView(R.layout.activity_main) 13 | setSupportActionBar(findViewById(R.id.toolbar)) 14 | 15 | btnPay.setOnClickListener { 16 | startActivity(Intent(this,WebViewActivity::class.java)) 17 | } 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /app/src/main/java/com/chaychan/demo/WebViewActivity.kt: -------------------------------------------------------------------------------- 1 | package com.chaychan.demo 2 | 3 | import android.content.Context 4 | import android.content.Intent 5 | import android.net.Uri 6 | import android.net.http.SslError 7 | import android.os.Bundle 8 | import android.text.TextUtils 9 | import android.view.View 10 | import android.webkit.* 11 | import android.widget.Toast 12 | import androidx.appcompat.app.AppCompatActivity 13 | import kotlinx.android.synthetic.main.activity_web_view.* 14 | 15 | class WebViewActivity : AppCompatActivity() { 16 | 17 | companion object{ 18 | const val BASE_URL = "https://www.yungouos.com/" 19 | const val DEMO_URL = "$BASE_URL#/demo" 20 | } 21 | 22 | private var isError = false 23 | 24 | override fun onCreate(savedInstanceState: Bundle?) { 25 | super.onCreate(savedInstanceState) 26 | setContentView(R.layout.activity_web_view) 27 | setSupportActionBar(findViewById(R.id.toolbar)) 28 | 29 | initWebView() 30 | webView.loadUrl(DEMO_URL) 31 | } 32 | 33 | private fun initWebView() { 34 | val settings = webView.settings 35 | settings.setJavaScriptEnabled(true) 36 | settings.javaScriptCanOpenWindowsAutomatically = true 37 | settings.setSupportZoom(true) 38 | settings.setSupportMultipleWindows(true) 39 | settings.setAppCacheEnabled(true) 40 | settings.domStorageEnabled = true 41 | 42 | webView.webChromeClient = object:WebChromeClient(){ 43 | override fun onReceivedTitle(view: WebView, title: String) { 44 | toolbar.title = title 45 | } 46 | 47 | override fun onProgressChanged(p0: WebView?, progress: Int) { 48 | if (progress == 100) { 49 | pbLoading.visibility = View.GONE 50 | } else { 51 | if (pbLoading.visibility == View.GONE) 52 | pbLoading.visibility = View.VISIBLE 53 | 54 | pbLoading.progress = progress 55 | } 56 | } 57 | } 58 | 59 | webView.webViewClient = object : WebViewClient() { 60 | override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean { 61 | if (url.isNullOrEmpty()) { 62 | return false 63 | } 64 | 65 | if (url.contains("https://wx.tenpay.com")) { 66 | //是支付的url 添加Referer 67 | val extraHeaders = HashMap() 68 | extraHeaders.put("Referer", BASE_URL) 69 | view?.loadUrl(url, extraHeaders) 70 | return true 71 | } 72 | 73 | 74 | try { 75 | if (url.startsWith("weixin://")) { 76 | if (url.startsWith("weixin://") && !hadInstalledWechat()){ 77 | //没有安装微信 则不跳转 78 | return false 79 | } 80 | 81 | //唤起微信 82 | val intent = Intent() 83 | intent.action = Intent.ACTION_VIEW 84 | intent.data = Uri.parse(url) 85 | startActivity(intent) 86 | return true 87 | } 88 | } catch (e: Exception) { 89 | return false 90 | } 91 | 92 | return super.shouldOverrideUrlLoading(view,url) 93 | } 94 | 95 | override fun onPageFinished(view: WebView, url: String) { 96 | super.onPageFinished(view, url) 97 | 98 | if (isError) { 99 | isError = false 100 | onLoadError() 101 | } else { 102 | onLoadPageFinished() 103 | } 104 | } 105 | 106 | override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) { 107 | handler?.proceed() 108 | } 109 | 110 | override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) { 111 | super.onReceivedError(view, request, error) 112 | isError = true 113 | } 114 | } 115 | } 116 | 117 | private fun onLoadPageFinished() { 118 | //加载完成 119 | } 120 | 121 | private fun onLoadError() { 122 | Toast.makeText(this,"加载失败,请重新进入重试", Toast.LENGTH_LONG) 123 | } 124 | 125 | fun hadInstalledWechat(): Boolean { 126 | val packageManager = packageManager // 获取packagemanager 127 | val pInfo = packageManager.getInstalledPackages(0) // 获取所有已安装程序的包信息 128 | if (pInfo != null) { 129 | for (i in pInfo.indices) { 130 | val pn = pInfo[i].packageName 131 | if (pn == "com.tencent.mm") { 132 | return true 133 | } 134 | } 135 | } 136 | return false 137 | } 138 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /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/drawable/progress_bas_states.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 22 | 23 | 24 | 25 |