电商收付通系列⑦,合单下单之小程序支付

2021-02-03 10:43:19 浏览数 (1)

1、介绍

在我接这个接口的时候,官方并没有明确给出合单支付支持小程序支付,凭借一腔热血去尝试了一下可以成功,prepay_id就是调用JS合单支付获取的。现在再看文档,已经明确列出来了“小程序调起支付”的字眼。所以大家可以放心的接入小程序合单支付哈。支付场景较多,系列文章只介绍小程序合单支付,APP合单支付、JS合单支付依瓢画葫芦,都一样哈。要注意,合单中同一个二级商户只允许有一笔子订单。订单如果需要进行分账等,需要在合单中指定需要进行分账(profit_sharing为true)。

文档地址:

https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/combine/chapter3_2.shtml

2、创建参数实体

按文档创建参数实体类。

参数实体JsapiParam:

代码语言:javascript复制
@Data
public class JsapiParam {
    private String combine_appid;
    private String combine_mchid;
    private String combine_out_trade_no;
    private SceneInfo scene_info;
    private List<SubOrder> sub_orders;
    private CombinePayerInfo combine_payer_info;
    private String time_start;
    private String time_expire;
    private String notify_url;
    private String limit_pay;
}

参数实体SceneInfo:

代码语言:javascript复制
@Data
public class SceneInfo {

    private String device_id;
    private String payer_client_ip;
}

参数实体SubOrder:

代码语言:javascript复制
@Data
public class SubOrder {

    private String mchid;
    private String attach;
    private Amount amount;
    private String out_trade_no;
    private String sub_mchid;
    private String detail;
    private Boolean profit_sharing;
    private String description;
    private SettleInfo settle_info;

}

参数实体CombinePayerInfo:

代码语言:javascript复制
@Data
public class CombinePayerInfo {

    private String openid;
}

参数实体Amount:

代码语言:javascript复制
@Data
public class Amount {

    private Integer total_amount;
    private String currency;
}

参数实体SettleInfo:

代码语言:javascript复制
@Data
public class SettleInfo {

    private Boolean profit_sharing;
    private Integer subsidy_amount;
}

3、生成预支付交易会话标识prepay_id

代码语言:javascript复制
public static JSONObject jsapi(JsapiParam param) {

    String requestParam = JSON.toJSONString(param);

    logger.info("jsapi param:{}",requestParam);

    HttpResponse response = HttpUtils.v3HttpExecute("POST", TransactionsUrl.JSAPI,
            "自己系统的应用名称","服务商商户号","商户API证书序列号","平台证书序列号",requestParam,"商户API证书位置.pem",null);
    JSONObject body = JSONObject.parseObject(response.body());

    logger.info("jsapi heads result:{}",response.headers());
  
    logger.info("jsapi body result:{}",body);
  
    return body;
}
代码语言:javascript复制

public static void main(String[] args) {
  JsapiParam jsapiParam = new JsapiParam();
  jsapiParam.setCombine_appid("服务商appid");
  jsapiParam.setCombine_mchid("服务商商户号");
  jsapiParam.setCombine_out_trade_no("out_trade_no1_" System.currentTimeMillis()   "");

  SceneInfo sceneInfo = new SceneInfo();
  sceneInfo.setDevice_id("POS1:12");
  sceneInfo.setPayer_client_ip("14.17.22.32");

  jsapiParam.setScene_info(sceneInfo);

  SubOrder subOrder = new SubOrder();
  subOrder.setMchid("服务商商户号");
  subOrder.setAttach("123&456&789");
  Amount amount = new Amount();
  amount.setTotal_amount(1);
  amount.setCurrency("CNY");
  subOrder.setAmount(amount);
  subOrder.setOut_trade_no("out_trade_no2_" System.currentTimeMillis()   "");
  subOrder.setSub_mchid("二级商户商户号");
  subOrder.setDetail("商品详细描述" subOrder.getSub_mchid());
  subOrder.setProfit_sharing(true);
  subOrder.setDescription("商品" subOrder.getSub_mchid());

  List<SubOrder> subOrders = new ArrayList<>();
  subOrders.add(subOrder);

  jsapiParam.setSub_orders(subOrders);

  CombinePayerInfo combinePayerInfo = new CombinePayerInfo();
  combinePayerInfo.setOpenid("服务商appid对应下的openid");
  jsapiParam.setCombine_payer_info(combinePayerInfo);
  String a = DateFormatUtils.format(new Date(),"yyyy-MM-dd'T'HH:mm:ssZZ");
  System.out.println("a=====" a);
  jsapiParam.setTime_start(a);
  jsapiParam.setNotify_url("https://yourapp.com/notify");
  
  jsapi(jsapiParam);
}

获取的结果

4、计算签名值

签名规则:

https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pay/combine/chapter3_8.shtml

代码语言:javascript复制
public static String paySign(String mchAppid,String timestamp,String nonceStr,String packageParam,String mchPrivateKeyPath) {

  try {

      //商户私钥
      String mchPrivateKey = CertificateUtils.getPrivateKey(mchPrivateKeyPath);
      logger.info("timestamp:{},nonceStr:{}",timestamp,nonceStr);

      //拼签名串
      StringBuilder sb = paySignMessage(mchAppid,timestamp,nonceStr,packageParam);

      logger.info("paySign original string:{}",sb.toString());

      //计算签名
      String paySign = new String(Base64.encodeBase64(v3signRSA(sb.toString(),mchPrivateKey)));

      logger.info("paySign result:{}",paySign);

      return paySign;

  } catch (Exception e) {
      logger.error("authorization Exception result:{}",e);
      e.printStackTrace();
      return null;
  }
}

5、小程序端测试

将参加签名timeStamp、nonceStr、package以及签名结果paySign传给小程序端,paySign默认为RSA,即可在小程序端唤起支付

例如:

代码语言:javascript复制
wx.requestPayment(
{
"timeStamp": "1414561699",
"nonceStr": "5K8264ILTKCH16CQ2502SI8ZNMTM67VS",
"package": "prepay_id=wx201410272009395522657a690389285100",
"signType": "RSA",
"paySign": "oR9d8PuhnIc YZ8cBHFCwfgpaK9gd7vaRvkYD7rthRAZ/X QBhcCYL21N7cHCTUxbQ EAt6Uy lwSN22f5YZvI45MLko8Pfso0jm46v5hqcVwrk6uddkGuT Cdvu4WBqDzaDjnNa5UK3GfE1Wfl2gHxIIY5lLdUgWFts17D4WuolLLkiFZV JSHMvH7eaLdT9N5GBovBwu5yYKUR7skR8Fu LozcSqQixnlEZUfyE55feLOQTUYzLmR9pNtPbPsu6WVhbNHMS3Ss2 AehHvz n64GDmXxbX  IOBvm2olHu3PsOUGRwhudhVf7UcGcunXt8cqNjKNqZLhLw4jq/xDg==",
})

6、结果

上一篇:电商收付通系列⑥,商户进件之查询申请状态

0 人点赞