先来看下演示效果
想利用yungouos平台的sdk做微信H5支付,但是没有查到在AS开发的Demo,只好自己琢磨了。由于本人也是第一次做微信支付,期间遇到非常多的问题,也查阅了很多的资料,总算将步骤理清楚了!!!写下这篇文章为了记录一下,也为后来人有个参考。
最主要的思路还是yungouos官方的一个Demo:
uniapp开发APP接入个人微信支付、支付宝接口实战演练(附源码)
在build.gradle导入两种类型的jar包
dependencies模块
//yungouos支付接口
implementation ('com.yungouos.pay:yungouos-pay-sdk:2.0.9')
//异步
implementation 'io.reactivex.rxjava2:rxjava:2.1.16'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
然后在android模块还需注明引用 java 1.8
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
activity_main.xml部分代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="500px"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="payTest"
android:text="立即支付"
android:textSize="36sp" />
LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RelativeLayout>
LinearLayout>
然后在做微信支付之前,还需请求网络权限
AndroidManifest.xml 添加
<uses-permission android:name="android.permission.INTERNET" />
然后就是两个Java类了
MainActivity 类
import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.yungouos.pay.entity.PayOrder;
import com.yungouos.pay.order.SystemOrder;
import com.yungouos.pay.wxpay.WxPay;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class MainActivity extends BaseActivity {
private static final String TAG = "MainActivity";
private static final int ACTION_REQUEST_PERMISSIONS = 0x001;
private static String[] NEEDED_PERMISSIONS = new String[]{
Manifest.permission.INTERNET
};
private String mch_id = "1602333609";//商户号,yungouos平台申请
private String key = "D29ADE73DC084C5EB434302014687FAF";//商户支付密匙
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private String outTradeNo;//订单号唯一
public void payTest(View view){
//开头随机7位 + 时间戳
outTradeNo = "1"+new DecimalFormat("000000").format(new Random().nextInt(100000)) +
System.currentTimeMillis() + "";
if (checkPermissions(NEEDED_PERMISSIONS)){
orderPayment();
}else{
ActivityCompat.requestPermissions(this, NEEDED_PERMISSIONS, ACTION_REQUEST_PERMISSIONS);
}
}
public WebView webView;
private Boolean isWxPay = false;
public void orderPayment(){
webView = findViewById(R.id.webView);
webView.setVisibility(View.INVISIBLE);//隐藏界面显示
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) {
String h5payResult = WxPay.H5Pay(outTradeNo, "0.01", mch_id, "扫码测试支付",
null, null,"http://www.yungouos.com", null, null, null, key);
emitter.onNext(h5payResult);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String h5payResult) {
Log.e(TAG, "微信下单成功: "+h5payResult );//微信支付收银台的中间页面
Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Referer", "http://www.yungouos.com");//如修改了商户号需要将这里修改成申请H5时提交的授权域名
webView.loadUrl(h5payResult, extraHeaders);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);//支持javascript
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); //设置 缓存模式
webSettings.setUseWideViewPort(true);//扩大比例的缩放
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//自适应屏幕
webSettings.setLoadWithOverviewMode(true);
webView.requestFocus();//触摸焦点起作用
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);//取消滚动条
webView.setWebViewClient(new android.webkit.WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//weixin://wap/pay?prepayid%3Dwx12163851787018726df1c8385fdf7c0000&package=4149489681&noncestr=1620808732&sign=de598dc49f2d999958ad58c13bf0c5e1
Log.e(TAG, "重定向的支付链接:"+url );
if (url.startsWith("weixin://wap/pay?") || url.startsWith("http://weixin/wap/pay")){
Log.e(TAG, "调起微信支付" );
isWxPay = true;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} else {
Log.e(TAG, "shouldOverrideUrlLoading: 22222222222" );
Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("Referer", "http://wxpay.wxutil.com");
view.loadUrl(url, extraHeaders);
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.e(TAG, url);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
super.onReceivedSslError(view, handler, error);
}
});
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
@Override
protected void onResume() {
super.onResume();
if (isWxPay) {
Log.e(TAG, "onResume: 支付调起成功返回页面,不知道付款成功与否");
isWxPay = false;
/**
* 查询订单状态
*/
Observable.create(new ObservableOnSubscribe<PayOrder>() {
@Override
public void subscribe(ObservableEmitter<PayOrder> emitter) {
PayOrder payOrder = SystemOrder.getOrderInfoByOutTradeNo(outTradeNo, mch_id, key);
emitter.onNext(payOrder);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<PayOrder>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(PayOrder payOrder) {
Log.e(TAG, "onNext: 查询系统订单返回结果:" + payOrder.toString());
if (payOrder.getPayStatus() == 1){
Log.e(TAG, "onNext: 成功" );
showToast("支付成功");
}else{
Log.e(TAG, "onNext: 失败" );
showToast("支付失败");
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
@Override
void afterRequestPermission(int requestCode, boolean isAllGranted) {
if (requestCode == ACTION_REQUEST_PERMISSIONS) {
if (isAllGranted) {
orderPayment();
} else {
showToast("权限未允许!");
}
}
}
}
BaseActivity 类
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.Toast;
public abstract class BaseActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}
/**
* 权限检查
*
* @param neededPermissions 需要的权限
* @return 是否全部被允许
*/
protected boolean checkPermissions(String[] neededPermissions) {
if (neededPermissions == null || neededPermissions.length == 0) {
return true;
}
boolean allGranted = true;
for (String neededPermission : neededPermissions) {
allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
}
return allGranted;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isAllGranted = true;
for (int grantResult : grantResults) {
isAllGranted &= (grantResult == PackageManager.PERMISSION_GRANTED);
}
afterRequestPermission(requestCode, isAllGranted);
}
/**
* 请求权限的回调
*
* @param requestCode 请求码
* @param isAllGranted 是否全部被同意
*/
abstract void afterRequestPermission(int requestCode, boolean isAllGranted);
protected void showToast(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
protected void showLongToast(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
教程至此app内微信H5支付完美跑通。那个商户号是yungouos官方给出的演示账号,如果是自己需要做支付业务,可以到 yungouos平台 注册并签约一个微信商户,可签约个人的,个人签约商户类型必须为渠道商户才能做H5支付,如果是公司做支付,直接有微信商户号,可以自行接入自有商户。
个人做H5支付还需要注意拥有自己的域名,并将这个域名指向一个app下载引导页,提交给客服审核并绑定
extraHeaders.put("Referer", "商户申请H5时提交的授权域名");
webView.loadUrl(h5payResult, extraHeaders);
等客服那边绑定好以后修改这个地方
如果是公司商户号则需要自己到微信商户平台提交资料审核
ps:记录一下
在导入yungouos的SDK时学习到了一种方式,介绍一下 Maven存储库 这个网站
可以在里面直接搜索到绝大部分类型的jar包,引入项目非常的方便
可以是jar包下载放入libs下,也可以直接通过builder的方式
ps: 在最开始时我引用的是2.0.10这个版本,但是里面有个fastJson版本是1.2.75,这个版本的fastJson好像存在个bug我也不知道怎么回事,对请求回调的json做类型转换时有个异常报错,我解决不了,所以降低成2.0.9版本,该版本里面引用的fastJson是 1.2.66 就没有问题
最后附上Demo链接:
积分下载:https://download.csdn.net/download/qq_41618587/18878904
白嫖下载:https://github.com/qaling/PayTest
感谢支持!