Android微信刷卡支付开发

1 篇文章 0 订阅
1 篇文章 0 订阅

微信开发不容易,在于各个环节的理解和疏通。

支付类型分为几种,每种的业务也不相同,所以就困扰了很多人,我也抱怨过资料的混乱,没办法慢慢梳理。

摸索过后才知道我需要的是刷卡支付业务。公司的Android终端集成扫描功能,不需要调用微信的扫描。

 

1、什么是刷卡支付

 

刷卡支付场景就像超市收银台扫你微信上的二维码,收银台主动发起扫码请求,

扫码完成后扣取你的费用。

流程如下图

 

2、需要做什么准备

 

1:企业公众平台注册,获取AppID和Key

 AppID路径:公众账号->开发->基本配置->开发者ID(AppID)

 Key路径:公众账号->开发->基本配置->开发者密码(AppSecret)

 

2:商户账号注册,获取Mch_ID和证书

 Mch_ID路径:商户平台->账号中心->账户设置->商家信息->微信支付商户号

 证书路径:商户平台->账号中心->账户设置->API安全->下载证书

 

3:从github下载JAVA SDK,截止2018-01-29用的是v0.0.4版本,和微信官网的资源v0.0.3不同。

版本不同之处,v0.0.4用的是java.net.HttpURLConnection,v0.0.3使用的是apache接口。

apache版本多,兼容性不好,所以不用apache,化繁为简。

 

3、开发

 

本质是Http协议。推荐使用Android Studio开发工具,SDK上的例子使用的是Android Sutdio,可以避免很多麻烦

 1:添加依赖库com.github.wxpay:WXPay-SDK-Java:0.0.4

 

	dependencies {
    		compile 'com.github.wxpay:WXPay-SDK-Java:0.0.4'
	}

	allprojects {
    		 repositories {
        	jcenter()
        	maven { url 'https://jitpack.io' }
    	}
	}

 2:申请支付接口,需要实现一个接口。这个接口用于填充AppId,MchID,Key和证书。

WXConfig config = new WXConfig();
mPay = new WXPay(config);

 

 WXConfig实现

 

 具体信息还请诸君自己填写,下面我用的是假的。

 

public class WXConfig implements WXPayConfig{

    private byte[] certData;

    public WXConfig() throws Exception {
        String certPath = "/sdcard/cert/apiclient_cert.p12";
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public String getAppID() {
        return "wx12333ed86ce5d123";
    }

    public String getMchID() {
        return "1301235123";
    }

    public String getKey() {
        return "beijing12312345devmpinctrl12345";
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}

 

3 :支付

参照微信API提交刷卡支付,几个关键字是必填的。授权码指的是扫描设备扫描到用户的QR码。

费用是按分计算。

API链接:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1

microPay要记得超时时间的设置,过短会抛出异常:java.net.SocketTimeoutException

        Map<String, String> data = new HashMap<String, String>();
        System.out.println("pay bill ....................................");
        data = new HashMap<String, String>();
        data.put("body", "zpd_test");
        data.put("out_trade_no", trade_no);
        data.put("auth_code", auth_code);
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        Map<String, String> resp = mPay.microPay(data, 3000, 3000);

 

 4:解析返回字段

还是参考API,前5次扫描是免密,用户不需要输入密码。返回SUCC状态。

超过5次后,需要用户输入密码,此时范围USERPAYING的错误状态,要做下一步查询

API链接:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2

 

 5:查询

参考查询API,查看支付状态,订单号是必填的关键字。trade_state为SUCCSS为支付成功,

其余的参考API上说明。

API链接:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_2

        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", trade_no);
        Map<String, String> resp = mPay.orderQuery(data,1000, 1000);
        String s =getResult(resp);

 测试截图:左边扫描终端 ,右边用户

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值