2024年小程序微信支付v3版java代码,支付与退款(1),刚从阿里、头条面试回来

总结

总的来说,面试是有套路的,一面基础,二面架构,三面个人。

最后,小编这里收集整理了一些资料,其中包括面试题(含答案)、书籍、视频等。希望也能帮助想进大厂的朋友

三面蚂蚁金服成功拿到offer后,他说他累了

三面蚂蚁金服成功拿到offer后,他说他累了

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

    order.put("amount",amount);

    JSONObject payer = new JSONObject();

    payer.put("openid",userOpenid);

    order.put("payer",payer);

//放在服务器上的v3证书

    PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(ResourceUtils.getFile("/www/zs/apiclient_key.pem")));

    AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(

            new WechatPay2Credentials(ConstantPropertiesUtil.PARTNER,new PrivateKeySigner(ConstantPropertiesUtil.NUM,merchantPrivateKey)),

            ConstantPropertiesUtil.MY.getBytes(StandardCharsets.UTF_8));



    WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()

            .withMerchant(ConstantPropertiesUtil.PARTNER, ConstantPropertiesUtil.NUM, merchantPrivateKey)

            .withValidator(new WechatPay2Validator(verifier));

    HttpClient httpClient = builder.build();

    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");

    httpPost.addHeader("Accept","application/json");

    httpPost.addHeader("Content-type","application/json;charset=utf-8");

    httpPost.setEntity(new StringEntity(order.toJSONString(),"UTF-8"));

    HttpResponse response = httpClient.execute(httpPost);

    String body = EntityUtils.toString(response.getEntity());

    JSONObject jsonObject = JSONObject.parseObject(body);

    if(jsonObject.containsKey("code")) {

        System.out.println(jsonObject.getString("message"));

        return null;

    }

    final String prepay_id = jsonObject.getString("prepay_id");



    final String timeStamp = System.currentTimeMillis()+"";



    final String nonceStr = RandomStringGenerator.getRandomStringByLength(32);



    StringBuffer stringBuffer = new StringBuffer();

    stringBuffer.append(ConstantPropertiesUtil.WX_OPEN_APP_ID+"\n");

    stringBuffer.append(timeStamp+"\n");

    stringBuffer.append(nonceStr+"\n");

    stringBuffer.append("prepay_id="+prepay_id+"\n");



        Signature signature = Signature.getInstance("SHA256withRSA");

        signature.initSign(merchantPrivateKey);

        signature.update(stringBuffer.toString().getBytes(StandardCharsets.UTF_8));

    byte[] signBytes = signature.sign();

    String paySign = Base64.encodeBytes(signBytes);



    //处理自己业务逻辑

    corderService.saveCorder(user,companyClass,str_no);



    JSONObject params = new JSONObject();

    params.put("appId",ConstantPropertiesUtil.WX_OPEN_APP_ID);

    params.put("timeStamp",timeStamp);

    params.put("nonceStr",nonceStr);

    params.put("package","prepay_id="+prepay_id);

    params.put("signType","RSA");

    params.put("paySign",paySign);



    return R.ok().data("weixinMap",params);

}

/**

 * 买课支付通知(api)

 */

@RequestMapping(value = "payNotice", method = RequestMethod.POST)

public R payNotice(

        HttpServletRequest request,

        HttpServletResponse response){

    System.out.println("支付回调执行");

    try {

        String reqParams = StreamUtil.read(request.getInputStream());

        logger.info("-------支付结果:"+reqParams);

        JSONObject json = JSONObject.parseObject(reqParams);

        if(json.getString("event_type").equals("TRANSACTION.SUCCESS")){

            logger.info("-------支付成功");



        }

        String ciphertext = json.getJSONObject("resource").getString("ciphertext");

        final String associated_data = json.getJSONObject("resource").getString("associated_data");

        final String nonce = json.getJSONObject("resource").getString("nonce");

        AesUtil aesUtil = new AesUtil(ConstantPropertiesUtil.MY.getBytes(StandardCharsets.UTF_8));

        ciphertext = aesUtil.decryptToString(associated_data.getBytes(), nonce.getBytes(), ciphertext);

        logger.info(JSONObject.parseObject(ciphertext).getString("out_trade_no"));

        //商户单号

        String out_trade_no =  JSONObject.parseObject(ciphertext).getString("out_trade_no");

        //交易单号

        String transaction_id = JSONObject.parseObject(ciphertext).getString("transaction_id");

        //金额

        String total =JSONObject.parseObject(ciphertext).getString("total");

        String openid =JSONObject.parseObject(ciphertext).getString("openid");

        System.out.println("更新数据库");

            UpdateWrapper<Corder> wrapper = new UpdateWrapper<>();

            wrapper.eq("orderid",out_trade_no).set("status",1).set("paytype",2);

        boolean b = corderService.update(wrapper);

        System.out.println(b+"=============");





    } catch (Exception e) {

        e.printStackTrace();

        logger.error(e.toString());

    }

    return R.ok();

}



> 由于服务器上的jre对v3秘钥进行加密解密的长度超过128,会报异常需要我们自己下载jrc将其中的jar覆盖掉原有的。



退款

--



@RequestMapping(value=“/refund”)

public void refund(

        String out_trade_no,

        String reason){

    System.out.println("退款接口执行");

    Integer totalFee = 1;

    Integer total = 1;

    Orderr orderr = orderrService.getById(out_trade_no);

    if (!StringUtils.isEmpty(orderr)){

        total = orderr.getPayment();

        totalFee = orderr.getPayment();

    }else {

        Corder corder = corderService.getCorderByNo(out_trade_no);

        totalFee = corder.getPayment();

        total = corder.getPayment();

    }

    String out_refund_no = new DateTime().toString("yyyyMMddHHmmssSSS");

    try {



        JSONObject order = new JSONObject();

        order.put("out_trade_no", out_trade_no);//商户订单号

        order.put("out_refund_no", out_refund_no);//商户退款单号

        order.put("reason", reason);//退款原因

        order.put("notify_url","https://www.dsxssd.com/api/dsxs/wx/refundNotice/");//退款通知



        JSONObject amount = new JSONObject();

        amount.put("refund", (long)(totalFee*100));//退款金额

        amount.put("currency", "CNY");

        amount.put("total", (long)(total*100));//原订单金额

        order.put("amount", amount);



        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(ResourceUtils.getFile("/www/zs/apiclient_key.pem")));



        // 使用定时更新的签名验证器,不需要传入证书

        ScheduledUpdateCertificatesVerifier verifier = new ScheduledUpdateCertificatesVerifier(

                new WechatPay2Credentials(ConstantPropertiesUtil.PARTNER, new PrivateKeySigner( ConstantPropertiesUtil.NUM, merchantPrivateKey)),

                ConstantPropertiesUtil.MY.getBytes(StandardCharsets.UTF_8));

        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()

                .withMerchant(ConstantPropertiesUtil.PARTNER, ConstantPropertiesUtil.NUM, merchantPrivateKey)

                .withValidator(new WechatPay2Validator(verifier));

        // ... 接下来,你仍然可以通过builder设置各种参数,来配置你的HttpClient



        // 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签,并进行证书自动更新

        HttpClient httpClient = builder.build();

        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");

        httpPost.addHeader("Accept", "application/json");

        httpPost.addHeader("Content-type","application/json; charset=utf-8");

        httpPost.setEntity(new StringEntity(order.toJSONString(), "UTF-8"));

        // 后面跟使用Apache HttpClient一样



        HttpResponse response = httpClient.execute(httpPost);

        String bodyAsString = EntityUtils.toString(response.getEntity());



        JSONObject bodyAsJSON = JSONObject.parseObject(bodyAsString);



        logger.info(bodyAsJSON.toJSONString());



        final String status = bodyAsJSON.getString("status");

        if(status.equals("SUCCESS")){

Kafka进阶篇知识点

image

Kafka高级篇知识点

image

44个Kafka知识点(基础+进阶+高级)解析如下

image

由于篇幅有限,小编已将上面介绍的**《Kafka源码解析与实战》、Kafka面试专题解析、复习学习必备44个Kafka知识点(基础+进阶+高级)都整理成册,全部都是PDF文档**

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

析如下**

[外链图片转存中…(img-69d8vi21-1715062399314)]

由于篇幅有限,小编已将上面介绍的**《Kafka源码解析与实战》、Kafka面试专题解析、复习学习必备44个Kafka知识点(基础+进阶+高级)都整理成册,全部都是PDF文档**

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值