微信和支付宝的被动扫码支付

需要下载微信和支付宝的sdk,从官网下载就行

微信
1.调起支付

public static String micropay(WechatAccount config,String auth_code,String body,String outTradeNo,String money){

        WXPay wxPay = new WXPay(config);

        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("appid", config.getAppID());
        parameters.put("mch_id", config.getMchID());

        parameters.put("nonce_str", WXPayUtil.generateNonceStr());
        if (StringUtils.isNotBlank(config.getSub_mchID())) {
            parameters.put("sub_mch_id", config.getSub_mchID());
        }
        parameters.put("sign_type", WXPayConstants.MD5);
        parameters.put("body", body);//购买支付信息
        //生成订单号
        parameters.put("out_trade_no", outTradeNo);//订单号

        parameters.put("total_fee", money);// 总金额单位为分
        parameters.put("spbill_create_ip", "127.0.0.1");//getIpAddr(request)
        parameters.put("auth_code", auth_code);//微信授权码
        String sign = null;
        try {
            sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        parameters.put("sign", sign);

        String result = null;
        try {
            result = wxPay.requestWithoutCert(WXPayConstants.MICROPAY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

2.查询是否支付成功

public static String queryOrder(WechatAccount config,String outTradeNo){
        WXPay wxPay = new WXPay(config);

        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("appid", config.getAppID());
        parameters.put("mch_id", config.getMchID());

        parameters.put("nonce_str", WXPayUtil.generateNonceStr());
        if (StringUtils.isNotEmpty(config.getSub_mchID())) {
            parameters.put("sub_mch_id", config.getSub_mchID());
        }
        parameters.put("sign_type", WXPayConstants.MD5);
        parameters.put("out_trade_no", outTradeNo);//购买支付信息
        String sign = null;
        try {
            sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        parameters.put("sign", sign);

        String result = null;
        try {
            result = wxPay.requestWithoutCert(WXPayConstants.ORDERQUERY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

3退费
退费需要证书,跟其他类似,因为接入的其他的项目的退费接口,所以没有加。官网开发文档上有

4查询退费是否成功

public static String queryRefund(WechatAccount config,String outTradeNo){
    WXPay wxPay = new WXPay(config);

    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("appid", config.getAppID());
    parameters.put("mch_id", config.getMchID());

    parameters.put("nonce_str", WXPayUtil.generateNonceStr());
    if (StringUtils.isNotEmpty(config.getSub_mchID())) {
        parameters.put("sub_mch_id", config.getSub_mchID());
    }
    parameters.put("out_trade_no", outTradeNo);
    String sign = null;
    try {
        sign = WXPayUtil.generateSignature(parameters, config.getKey(), SignType.MD5);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    parameters.put("sign", sign);

    String result = null;
    try {
        result = wxPay.requestWithoutCert(WXPayConstants.REFUNDQUERY_URL,parameters,config.getHttpConnectTimeoutMs(),config.getHttpReadTimeoutMs());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

微信实体类

package com.jinyi.medicine.modules.pay.model;

import java.io.InputStream;

import com.github.wxpay.sdk.WXPayConfig;

public class WechatAccount implements WXPayConfig{
    //账户类型
    private String accountType;

    private String appID;
    private String mchID;
    private String sub_mchID;
    private String key;
    private InputStream certStream;
    private int httpConnectTimeoutMs = 10000;
    private int httpReadTimeoutMs = 10000;
    public String getAccountType() {
        return accountType;
    }
    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }
    public String getAppID() {
        return appID;
    }
    public void setAppID(String appID) {
        this.appID = appID;
    }
    public String getMchID() {
        return mchID;
    }
    public void setMchID(String mchID) {
        this.mchID = mchID;
    }
    public String getSub_mchID() {
        return sub_mchID;
    }
    public void setSub_mchID(String sub_mchID) {
        this.sub_mchID = sub_mchID;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public InputStream getCertStream() {
        return certStream;
    }
    public void setCertStream(InputStream certStream) {
        this.certStream = certStream;
    }
    public int getHttpConnectTimeoutMs() {
        return httpConnectTimeoutMs;
    }
    public void setHttpConnectTimeoutMs(int httpConnectTimeoutMs) {
        this.httpConnectTimeoutMs = httpConnectTimeoutMs;
    }
    public int getHttpReadTimeoutMs() {
        return httpReadTimeoutMs;
    }
    public void setHttpReadTimeoutMs(int httpReadTimeoutMs) {
        this.httpReadTimeoutMs = httpReadTimeoutMs;
    }


}


支付宝
1.调起支付

    public static String aliPay(AlipayAccount alipayAccount,String authCode,String body,String outTradeNo,String money ){

        String appId = alipayAccount.getAppId();
        String privateKey = alipayAccount.getPrivateKey();
        String aliPublicKey = alipayAccount.getAliPublicKey();
        AlipayTradePayRequest request = new AlipayTradePayRequest(); //创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\""+outTradeNo+"\"," +
                "\"scene\":\"bar_code\"," +
                "\"auth_code\":\""+authCode+"\"," +
                "\"subject\":\""+body+"\"," +
                "\"store_id\":\"jinyi\"," +
                "\"timeout_express\":\"2m\"," +
                "\"total_amount\":\""+money+"\"" +
                "}"); //设置业务参数
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
            AlipayTradePayResponse response = alipayClient.execute(request);
            if (response!=null) {
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
            try {
                AlipayTradePayResponse response = alipayClient.execute(request);
                if (response!=null ) {
                    return response.getBody();
                }
            } catch (AlipayApiException e2) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        return null;
    }

2.查询是否支付成功

    public static String queryAliOrder(AlipayAccount alipayAccount ,String outTradeNo){
        String appId = alipayAccount.getAppId();
        String privateKey = alipayAccount.getPrivateKey();
        String aliPublicKey = alipayAccount.getAliPublicKey();


        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\""+outTradeNo+"\"," +
                "\"trade_no\":\"\"" +
                "}");
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                return response.getBody();
            } 
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
            AlipayTradeQueryResponse response = null;
            try {
                response = alipayClient.execute(request);
            } catch (AlipayApiException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(response.isSuccess()){
                return response.getBody();
            } 
        }
        return null;
    }

3退费

public static String refundFee(AlipayAccount alipayAccount ,String outTradeNo,String money){
        String appId = alipayAccount.getAppId();
        String privateKey = alipayAccount.getPrivateKey();
        String aliPublicKey = alipayAccount.getAliPublicKey();

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent("{" +
            "\"out_trade_no\":\""+outTradeNo+"\"," +
            "\"trade_no\":\"\"," +
            "\"refund_amount\":"+money+"," +
            "\"refund_reason\":\"退款\"," +
            "\"out_request_no\":\""+outTradeNo+"\"," +
            "\"operator_id\":\"OP001\"," +
            "\"store_id\":\"jinyi001\"," +
            "\"terminal_id\":\"jinyi001\"" +
            "  }");
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                return response.getBody();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            logger.info("调用该支付宝加密方式报错,开始执行RSA2==="+outTradeNo);
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
            try {
                AlipayTradeRefundResponse response = alipayClient.execute(request);
                if (response.isSuccess()) {
                    return response.getBody();
                }
            } catch (AlipayApiException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                logger.info("调用该支付宝加密方式RSA2报错,返回null==="+outTradeNo);
            }
        }
        return null;
    }

4.查询是否退费成功

    public static String queryRefund(AlipayAccount alipayAccount ,String outTradeNo ){
        String appId = alipayAccount.getAppId();
        String privateKey = alipayAccount.getPrivateKey();
        String aliPublicKey = alipayAccount.getAliPublicKey();

        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        request.setBizContent("{" +
                "\"trade_no\":\"\"," +
                "\"out_trade_no\":\""+outTradeNo+"\"," +
                "\"out_request_no\":\""+outTradeNo+"\"" +
                "}");
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA"); //获得初始化的AlipayClient
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                return response.getBody();
            } 
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            logger.info("查询订单退费状态报错,改用RSA2重新执行:"+outTradeNo);
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", aliPublicKey, "RSA2"); //获得初始化的AlipayClient
            try {
                AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
                if(response.isSuccess()){
                    return response.getBody();
                } 
            } catch (AlipayApiException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        return null;
    }

支付宝实体类

package com.jinyi.medicine.modules.pay.model;

public class AlipayAccount {
    private Long id;
    //hospital_id + "_" accountType 生成的唯一键
    private String uniqueKey;
    //开发者公钥
    private String publicKey;
    //支付宝公钥--下载支付宝对账单要用
    private String aliPublicKey;
    //开发者私钥
    private String privateKey;
    //appId
    private String appId;
    //合作伙伴身份pid
    private String partnerId;
    //apiKey
    private String apiKey;
    //退款私钥
    private String refundKey;
    //卖家的email
    private String sellerId;
    //账户类型
    private String accountType;

    private String encryption;//加密方式

    public String getEncryption() {
        return encryption;
    }
    public void setEncryption(String encryption) {
        this.encryption = encryption;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUniqueKey() {
        return uniqueKey;
    }
    public void setUniqueKey(String uniqueKey) {
        this.uniqueKey = uniqueKey;
    }
    public String getPublicKey() {
        return publicKey;
    }
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }
    public String getAliPublicKey() {
        return aliPublicKey;
    }
    public void setAliPublicKey(String aliPublicKey) {
        this.aliPublicKey = aliPublicKey;
    }
    public String getPrivateKey() {
        return privateKey;
    }
    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }
    public String getAppId() {
        return appId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
    public String getPartnerId() {
        return partnerId;
    }
    public void setPartnerId(String partnerId) {
        this.partnerId = partnerId;
    }
    public String getApiKey() {
        return apiKey;
    }
    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }
    public String getRefundKey() {
        return refundKey;
    }
    public void setRefundKey(String refundKey) {
        this.refundKey = refundKey;
    }
    public String getSellerId() {
        return sellerId;
    }
    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }
    public String getAccountType() {
        return accountType;
    }
    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 微信小程序是微信官方提供的一种可以在微信内运行的应用程序平台。Onenet是中国移动推出的物联网云平台,它提供了一系列的服务,包括设备管理、数据存储、数据分析等功能。 在微信小程序中,集成了Onenet的支付功能,用户可以通过微信小程序中的二维码描功能,直接描Onenet提供的支付二维码来进行支付。具体的流程如下: 1. 用户打开微信小程序,在小程序中找到需要进行支付的功能入口。 2. 小程序调用相机权限,启动二维码描功能。 3. 用户将手机相机对准Onenet提供的支付二维码,等待相机描成功。 4. 描成功后,微信小程序会解析二维码的内容,包括支付金额、商户信息等。 5. 微信小程序将解析出的支付信息发送到Onenet支付接口进行验证。 6. Onenet验证支付信息,并返回支付结果给微信小程序。 7. 微信小程序根据支付结果进行相应的处理,如提示支付成功或失败。 8. 用户在微信小程序中可以查看支付记录,以及需要等待商户发货或服务。 使用微信小程序进行Onenet支付的好处是,用户无需离开微信,即可完成支付。同时,由于在微信环境下进行支付,用户的支付信息会得到更好的保护和安全性。 总之,微信小程序 Onenet 支付为用户提供了一种便捷、安全的支付方式,提升了用户在小程序中的使用体验。 ### 回答2: 微信小程序 onenet 支付是指在微信小程序中集成了onenet支付功能,用户可以通过描二维码进行支付操作。首先,用户需要打开微信小程序,并进入相应的页面。在页面中,用户可以看到一个二维码,可以使用微信功能进行描。描后,小程序会跳转到相应的支付页面,用户可以选择使用微信支付进行支付。在支付页面中,用户可以输入支付金额和其他支付信息,然后确认支付操作。一旦支付成功,小程序会显示支付成功的提示,并返回到原来的页面。如果支付失败,小程序会给出相应的失败提示,并提供重新支付的选项。微信小程序 onenet 支付的好处是,用户可以在微信小程序中完成支付操作,无需再跳转到其他页面或应用。同时,微信支付的安全性和便捷性也能够保证用户的支付体验。所以,微信小程序 onenet 支付成为了许多商家和用户选择的支付方式之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值