[微信开发] - 微信支付 JSAPI 形式_微信

 

 

微信官方的JSAPI文档

微信官方的JSAPI支付SDK与DEMO下载

查看JSAPI的API可以从这里看

下载了支付DEMO其实有些地方不对的,比如如果做沙盒测试的时候,需要使用getsignkey,而官方的demo中没有这个.

看了看sf.gg的一个文章,之后把获取getsignkey的api请求加了进去.

现在的 WXPayConstants :

package com.wechat.demo.wxpay;

import org.apache.http.client.HttpClient;

/**
 * 微信支付常量类
 */
public class WXPayConstants {

    public enum SignType {
        MD5, HMACSHA256
    }

    public static final String DOMAIN_API = "api.mch.weixin.qq.com";
    public static final String DOMAIN_API2 = "api2.mch.weixin.qq.com";
    public static final String DOMAIN_APIHK = "apihk.mch.weixin.qq.com";
    public static final String DOMAIN_APIUS = "apius.mch.weixin.qq.com";


    public static final String FAIL     = "FAIL";
    public static final String SUCCESS  = "SUCCESS";
    public static final String HMACSHA256 = "HMAC-SHA256";
    public static final String MD5 = "MD5";

    public static final String FIELD_SIGN = "sign";
    public static final String FIELD_SIGN_TYPE = "sign_type";

    public static final String WXPAYSDK_VERSION = "WXPaySDK/3.0.9";
    public static final String USER_AGENT = WXPAYSDK_VERSION +
            " (" + System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version") +
            ") Java/" + System.getProperty("java.version") + " HttpClient/" + HttpClient.class.getPackage().getImplementationVersion();

    //提交付款码支付 https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
    public static final String MICROPAY_URL_SUFFIX     = "/pay/micropay";
    //统一下单 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
    public static final String UNIFIEDORDER_URL_SUFFIX = "/pay/unifiedorder";
    //查询订单 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_2
    public static final String ORDERQUERY_URL_SUFFIX   = "/pay/orderquery";
    //撤销订单 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_11
    public static final String REVERSE_URL_SUFFIX      = "/secapi/pay/reverse";
    //关闭订单 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_3
    public static final String CLOSEORDER_URL_SUFFIX   = "/pay/closeorder";
    //申请退款 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4
    public static final String REFUND_URL_SUFFIX       = "/secapi/pay/refund";
    //查询退款 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_5
    public static final String REFUNDQUERY_URL_SUFFIX  = "/pay/refundquery";
    //下载对账单 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_6
    public static final String DOWNLOADBILL_URL_SUFFIX = "/pay/downloadbill";
    //交易保障 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_8&index=9
    public static final String REPORT_URL_SUFFIX       = "/payitil/report";
    //转换短链接 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_9&index=10
    public static final String SHORTURL_URL_SUFFIX     = "/tools/shorturl";
    //授权码查询openid https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9
    public static final String AUTHCODETOOPENID_URL_SUFFIX = "/tools/authcodetoopenid";

    // sandbox 沙盒测试
    public static final String SANDBOX_GETSIGNKEY_URL_SUFFIX   = "/sandboxnew/pay/getsignkey";
    public static final String SANDBOX_MICROPAY_URL_SUFFIX     = "/sandboxnew/pay/micropay";
    public static final String SANDBOX_UNIFIEDORDER_URL_SUFFIX = "/sandboxnew/pay/unifiedorder";
    public static final String SANDBOX_ORDERQUERY_URL_SUFFIX   = "/sandboxnew/pay/orderquery";
    public static final String SANDBOX_REVERSE_URL_SUFFIX      = "/sandboxnew/secapi/pay/reverse";
    public static final String SANDBOX_CLOSEORDER_URL_SUFFIX   = "/sandboxnew/pay/closeorder";
    public static final String SANDBOX_REFUND_URL_SUFFIX       = "/sandboxnew/secapi/pay/refund";
    public static final String SANDBOX_REFUNDQUERY_URL_SUFFIX  = "/sandboxnew/pay/refundquery";
    public static final String SANDBOX_DOWNLOADBILL_URL_SUFFIX = "/sandboxnew/pay/downloadbill";
    public static final String SANDBOX_REPORT_URL_SUFFIX       = "/sandboxnew/payitil/report";
    public static final String SANDBOX_SHORTURL_URL_SUFFIX     = "/sandboxnew/tools/shorturl";
    public static final String SANDBOX_AUTHCODETOOPENID_URL_SUFFIX = "/sandboxnew/tools/authcodetoopenid";

}

请注意上面红色的代码是请求沙盒测试需要的key的请求路径.

之后把请求的方法写到了原来的 WXPayUtil 中:

/**
     * 获取sandbox测试需要的signkey
     * @param config
     * @param wxPay
     * @return
     * @throws Exception
     */
    public static String retrieveSandboxSignKey(IWXPayConfig config, WXPay wxPay) throws Exception {
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("mch_id", config.getMchID());
            params.put("nonce_str", WXPayUtil.generateNonceStr());
            params.put("sign", generateSignatrue(params, config.getKey()));
            String strXML = wxPay.requestWithoutCert(WXPayConstants.SANDBOX_GETSIGNKEY_URL_SUFFIX, params, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs());
            if (StringUtils.isBlank(strXML)) {
                return null;
            }
            Map<String, String> result = WXPayUtil.xmlToMap(strXML);
            getLogger().info("retrieveSandboxSignKey:" + result);
            if ("SUCCESS".equals(result.get("return_code"))) {
                return result.get("sandbox_signkey");
            }
            return null;
        }catch(Exception e){
            getLogger().info("获取Sandbox_signkey异常",e);
            return null;
        }

    }

在这之后就可以根据DEMO中的README去设置一下了.

这里还有官方提供的测试用例:

测试用例的作用是测试支付的传参与返回数据是否正确.

 

测试了官方支付DEMO后,可以再根据官方的测试用例文档进行相应地处理.

由于现有公司使用的微信平台对接一套php系统,所以暂时没有找到更稳妥的在线测试.

只能进行模拟的测试+后期业务逻辑编写了.

调起支付(需要对接开通支付的微信服务号),现调不起支付也不要紧,只需要按照调起支付

后续编写逻辑代码即可.